From 00956616549240441f473e2e80cb3dfbb5ce93d7 Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Fri, 6 Nov 2020 11:45:22 +0800 Subject: [PATCH 1/4] fix: private key generation, schnorr verify --- keytools/secp256k1.go | 12 +++++------- schnorr/schnorr.go | 4 ++-- schnorr/schnorr_test.go | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/keytools/secp256k1.go b/keytools/secp256k1.go index d8c5c4f..a379a2d 100644 --- a/keytools/secp256k1.go +++ b/keytools/secp256k1.go @@ -18,7 +18,6 @@ package keytools import ( "crypto/rand" - "io" "math/big" "github.com/Zilliqa/gozilliqa-sdk/util" @@ -32,19 +31,18 @@ var ( type PrivateKey [32]byte func GeneratePrivateKey() (PrivateKey, error) { - pvk := [32]byte{} - + var bytes [32]byte for { - _, err := io.ReadFull(rand.Reader, pvk[:]) + privk, err := btcec.NewPrivateKey(Secp256k1) if err == nil { - pvkInt := new(big.Int).SetBytes(pvk[:]) + pvkInt := privk.D if pvkInt.Cmp(big.NewInt(0)) == 1 && pvkInt.Cmp(Secp256k1.N) == -1 { + privk.D.FillBytes(bytes[:]) break } } } - - return PrivateKey(pvk), nil + return bytes,nil } func GetPublicKeyFromPrivateKey(privateKey []byte, compress bool) []byte { diff --git a/schnorr/schnorr.go b/schnorr/schnorr.go index c56bc3d..6da7bbb 100644 --- a/schnorr/schnorr.go +++ b/schnorr/schnorr.go @@ -114,8 +114,8 @@ func Verify(publicKey []byte, msg []byte, r []byte, s []byte) bool { _r := util.Hash(Q, publicKey, msg) - rn := new(big.Int).SetBytes(r) - _rn := new(big.Int).SetBytes(_r) + rn := new(big.Int).Mod(new(big.Int).SetBytes(r),keytools.Secp256k1.N) + _rn := new(big.Int).Mod(new(big.Int).SetBytes(_r),keytools.Secp256k1.N) fmt.Printf("r = %s, _r = %s\n", hex.EncodeToString(r), hex.EncodeToString(_r)) return rn.Cmp(_rn) == 0 } diff --git a/schnorr/schnorr_test.go b/schnorr/schnorr_test.go index 815b8ce..11e5cae 100644 --- a/schnorr/schnorr_test.go +++ b/schnorr/schnorr_test.go @@ -78,7 +78,7 @@ func run_verify_test(t *testing.T) { panic("unmarshal failed") } - fmt.Printf("test data number = %d", len(data)) + fmt.Printf("test data number = %d\n", len(data)) n := 0 From c72c60b15390261068e847565678de21bb008f9a Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Fri, 6 Nov 2020 13:03:46 +0800 Subject: [PATCH 2/4] fix: sign function --- schnorr/schnorr.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/schnorr/schnorr.go b/schnorr/schnorr.go index 6da7bbb..f4d8d8c 100644 --- a/schnorr/schnorr.go +++ b/schnorr/schnorr.go @@ -68,7 +68,8 @@ func TrySign(privateKey []byte, publicKey []byte, message []byte, k []byte) ([]b //4. Compute s = k - r * prv // 4a. Compute r * prv _r := *r - s := new(big.Int).Mod(_r.Sub(bintK, _r.Mul(&_r, priKey)), keytools.Secp256k1.N) + s := new(big.Int).Mod(_r.Mul(&_r, priKey),keytools.Secp256k1.N) + s = new(big.Int).Mod(new(big.Int).Sub(bintK, s), keytools.Secp256k1.N) if s.Cmp(big.NewInt(0)) == 0 { return nil, nil, errors.New("invalid s") From 53947a695ae0289c0886bb8edb402216828b181e Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Fri, 6 Nov 2020 13:06:31 +0800 Subject: [PATCH 3/4] fix: verify --- schnorr/schnorr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schnorr/schnorr.go b/schnorr/schnorr.go index f4d8d8c..8496075 100644 --- a/schnorr/schnorr.go +++ b/schnorr/schnorr.go @@ -115,7 +115,7 @@ func Verify(publicKey []byte, msg []byte, r []byte, s []byte) bool { _r := util.Hash(Q, publicKey, msg) - rn := new(big.Int).Mod(new(big.Int).SetBytes(r),keytools.Secp256k1.N) + rn := new(big.Int).SetBytes(r) _rn := new(big.Int).Mod(new(big.Int).SetBytes(_r),keytools.Secp256k1.N) fmt.Printf("r = %s, _r = %s\n", hex.EncodeToString(r), hex.EncodeToString(_r)) return rn.Cmp(_rn) == 0 From 03827d4358348682b1f65ffc8ce73bc8e2d09b7f Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Wed, 18 Nov 2020 11:31:21 +0800 Subject: [PATCH 4/4] chore: move Hash function to package schnorr --- schnorr/schnorr.go | 13 +++++++++++-- util/util.go | 8 -------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/schnorr/schnorr.go b/schnorr/schnorr.go index 8496075..4dddd18 100644 --- a/schnorr/schnorr.go +++ b/schnorr/schnorr.go @@ -17,6 +17,7 @@ package go_schnorr import ( + "bytes" "encoding/hex" "errors" "fmt" @@ -58,7 +59,7 @@ func TrySign(privateKey []byte, publicKey []byte, message []byte, k []byte) ([]b // 3. Compute the challenge r = H(Q || pubKey || msg) // mod reduce r by the order of secp256k1, n - r := new(big.Int).SetBytes(util.Hash(Q, publicKey, message[:])) + r := new(big.Int).SetBytes(hash(Q, publicKey, message[:])) r = r.Mod(r, keytools.Secp256k1.N) if r.Cmp(bintZero) == 0 { @@ -113,10 +114,18 @@ func Verify(publicKey []byte, msg []byte, r []byte, s []byte) bool { Qx, Qy := keytools.Secp256k1.Add(rx, ry, lx, ly) Q := util.Compress(keytools.Secp256k1, Qx, Qy, true) - _r := util.Hash(Q, publicKey, msg) + _r := hash(Q, publicKey, msg) rn := new(big.Int).SetBytes(r) _rn := new(big.Int).Mod(new(big.Int).SetBytes(_r),keytools.Secp256k1.N) fmt.Printf("r = %s, _r = %s\n", hex.EncodeToString(r), hex.EncodeToString(_r)) return rn.Cmp(_rn) == 0 } + +func hash(Q []byte, pubKey []byte, msg []byte) []byte { + var buffer bytes.Buffer + buffer.Write(Q) + buffer.Write(pubKey[:33]) + buffer.Write(msg) + return util.Sha256(buffer.Bytes()) +} diff --git a/util/util.go b/util/util.go index 8ad334d..ff2112f 100644 --- a/util/util.go +++ b/util/util.go @@ -88,14 +88,6 @@ func bigIntToBytes(bi *big.Int) []byte { return b1[:] } -func Hash(Q []byte, pubKey []byte, msg []byte) []byte { - var buffer bytes.Buffer - buffer.Write(Q) - buffer.Write(pubKey[:33]) - buffer.Write(msg) - return Sha256(buffer.Bytes()) -} - func GenerateMac(derivedKey, cipherText, iv []byte) []byte { buffer := bytes.NewBuffer(nil) buffer.Write(derivedKey[16:32])