Skip to content

Commit

Permalink
Merge pull request #77 from Zilliqa/develop
Browse files Browse the repository at this point in the history
merge from develop
  • Loading branch information
renlulu authored Nov 19, 2020
2 parents 8c647e6 + 53f9127 commit b84f5e8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
12 changes: 5 additions & 7 deletions keytools/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package keytools

import (
"crypto/rand"
"io"
"math/big"

"github.com/Zilliqa/gozilliqa-sdk/util"
Expand All @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions schnorr/schnorr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package go_schnorr

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion schnorr/schnorr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 0 additions & 8 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit b84f5e8

Please sign in to comment.