Skip to content

Commit b84f5e8

Browse files
author
renlulu
authored
Merge pull request #77 from Zilliqa/develop
merge from develop
2 parents 8c647e6 + 53f9127 commit b84f5e8

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

keytools/secp256k1.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package keytools
1818

1919
import (
2020
"crypto/rand"
21-
"io"
2221
"math/big"
2322

2423
"github.com/Zilliqa/gozilliqa-sdk/util"
@@ -32,19 +31,18 @@ var (
3231
type PrivateKey [32]byte
3332

3433
func GeneratePrivateKey() (PrivateKey, error) {
35-
pvk := [32]byte{}
36-
34+
var bytes [32]byte
3735
for {
38-
_, err := io.ReadFull(rand.Reader, pvk[:])
36+
privk, err := btcec.NewPrivateKey(Secp256k1)
3937
if err == nil {
40-
pvkInt := new(big.Int).SetBytes(pvk[:])
38+
pvkInt := privk.D
4139
if pvkInt.Cmp(big.NewInt(0)) == 1 && pvkInt.Cmp(Secp256k1.N) == -1 {
40+
privk.D.FillBytes(bytes[:])
4241
break
4342
}
4443
}
4544
}
46-
47-
return PrivateKey(pvk), nil
45+
return bytes,nil
4846
}
4947

5048
func GetPublicKeyFromPrivateKey(privateKey []byte, compress bool) []byte {

schnorr/schnorr.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package go_schnorr
1818

1919
import (
20+
"bytes"
2021
"encoding/hex"
2122
"errors"
2223
"fmt"
@@ -58,7 +59,7 @@ func TrySign(privateKey []byte, publicKey []byte, message []byte, k []byte) ([]b
5859

5960
// 3. Compute the challenge r = H(Q || pubKey || msg)
6061
// mod reduce r by the order of secp256k1, n
61-
r := new(big.Int).SetBytes(util.Hash(Q, publicKey, message[:]))
62+
r := new(big.Int).SetBytes(hash(Q, publicKey, message[:]))
6263
r = r.Mod(r, keytools.Secp256k1.N)
6364

6465
if r.Cmp(bintZero) == 0 {
@@ -68,7 +69,8 @@ func TrySign(privateKey []byte, publicKey []byte, message []byte, k []byte) ([]b
6869
//4. Compute s = k - r * prv
6970
// 4a. Compute r * prv
7071
_r := *r
71-
s := new(big.Int).Mod(_r.Sub(bintK, _r.Mul(&_r, priKey)), keytools.Secp256k1.N)
72+
s := new(big.Int).Mod(_r.Mul(&_r, priKey),keytools.Secp256k1.N)
73+
s = new(big.Int).Mod(new(big.Int).Sub(bintK, s), keytools.Secp256k1.N)
7274

7375
if s.Cmp(big.NewInt(0)) == 0 {
7476
return nil, nil, errors.New("invalid s")
@@ -112,10 +114,18 @@ func Verify(publicKey []byte, msg []byte, r []byte, s []byte) bool {
112114
Qx, Qy := keytools.Secp256k1.Add(rx, ry, lx, ly)
113115
Q := util.Compress(keytools.Secp256k1, Qx, Qy, true)
114116

115-
_r := util.Hash(Q, publicKey, msg)
117+
_r := hash(Q, publicKey, msg)
116118

117119
rn := new(big.Int).SetBytes(r)
118-
_rn := new(big.Int).SetBytes(_r)
120+
_rn := new(big.Int).Mod(new(big.Int).SetBytes(_r),keytools.Secp256k1.N)
119121
fmt.Printf("r = %s, _r = %s\n", hex.EncodeToString(r), hex.EncodeToString(_r))
120122
return rn.Cmp(_rn) == 0
121123
}
124+
125+
func hash(Q []byte, pubKey []byte, msg []byte) []byte {
126+
var buffer bytes.Buffer
127+
buffer.Write(Q)
128+
buffer.Write(pubKey[:33])
129+
buffer.Write(msg)
130+
return util.Sha256(buffer.Bytes())
131+
}

schnorr/schnorr_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func run_verify_test(t *testing.T) {
7878
panic("unmarshal failed")
7979
}
8080

81-
fmt.Printf("test data number = %d", len(data))
81+
fmt.Printf("test data number = %d\n", len(data))
8282

8383
n := 0
8484

util/util.go

-8
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ func bigIntToBytes(bi *big.Int) []byte {
8888
return b1[:]
8989
}
9090

91-
func Hash(Q []byte, pubKey []byte, msg []byte) []byte {
92-
var buffer bytes.Buffer
93-
buffer.Write(Q)
94-
buffer.Write(pubKey[:33])
95-
buffer.Write(msg)
96-
return Sha256(buffer.Bytes())
97-
}
98-
9991
func GenerateMac(derivedKey, cipherText, iv []byte) []byte {
10092
buffer := bytes.NewBuffer(nil)
10193
buffer.Write(derivedKey[16:32])

0 commit comments

Comments
 (0)