schnorr_wasm.mx raw

   1  //:build wasm
   2  
   3  package schnorr
   4  
   5  import (
   6  	"crypto/secp256k1"
   7  	"crypto/sha256"
   8  )
   9  
  10  func PubKeyFromSecKey(seckey []byte) (out []byte, ok2 bool) {
  11  	var sk [32]byte
  12  	copy(sk[:], seckey)
  13  	pk, ok := secp256k1.PubKeyFromSecKey(sk)
  14  	if !ok {
  15  		return nil, false
  16  	}
  17  	return pk[:], true
  18  }
  19  
  20  func SignSchnorr(seckey, msg, auxRand []byte) (out []byte, ok2 bool) {
  21  	var sk, m, aux [32]byte
  22  	copy(sk[:], seckey)
  23  	copy(m[:], msg)
  24  	copy(aux[:], auxRand)
  25  	sig, ok := secp256k1.SignSchnorr(sk, m, aux)
  26  	if !ok {
  27  		return nil, false
  28  	}
  29  	return sig[:], true
  30  }
  31  
  32  func VerifySchnorr(pubkey, msg, sig []byte) (ok bool) {
  33  	var pk, m [32]byte
  34  	var s [64]byte
  35  	copy(pk[:], pubkey)
  36  	copy(m[:], msg)
  37  	copy(s[:], sig)
  38  	return secp256k1.VerifySchnorr(pk, m, s)
  39  }
  40  
  41  func ECDH(seckey, pubkey []byte) (out []byte, ok2 bool) {
  42  	var sk, pk [32]byte
  43  	copy(sk[:], seckey)
  44  	copy(pk[:], pubkey)
  45  	shared, ok := secp256k1.ECDH(sk, pk)
  46  	if !ok {
  47  		return nil, false
  48  	}
  49  	return shared[:], true
  50  }
  51  
  52  func SHA256Sum(data []byte) (buf []byte) {
  53  	h := sha256.Sum(data)
  54  	return h[:]
  55  }
  56  
  57  func ScalarAddModN(a, b []byte) (out []byte, ok2 bool) {
  58  	var fa, fb [32]byte
  59  	copy(fa[:], a)
  60  	copy(fb[:], b)
  61  	r, ok := secp256k1.ScalarAddModN(fa, fb)
  62  	if !ok {
  63  		return nil, false
  64  	}
  65  	return r[:], true
  66  }
  67  
  68  func CompressedPubKey(seckey []byte) (out []byte, ok2 bool) {
  69  	var sk [32]byte
  70  	copy(sk[:], seckey)
  71  	cpk, ok := secp256k1.CompressedPubKey(sk)
  72  	if !ok {
  73  		return nil, false
  74  	}
  75  	return cpk[:], true
  76  }
  77