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..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 { @@ -68,7 +69,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") @@ -112,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).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/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 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])