From 03827d4358348682b1f65ffc8ce73bc8e2d09b7f Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Wed, 18 Nov 2020 11:31:21 +0800 Subject: [PATCH] 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])