go-blindsecp256k1/blindsecp256k1_test.go
blindsecp256k1_test.goRaw
1package blindsecp256k1
2
3import (
4 "math/big"
5 "testing"
6
7 "github.com/ethereum/go-ethereum/crypto"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10)
11
12func TestFlow(t *testing.T) {
13 // signer: create new signer key pair
14 sk := NewPrivateKey()
15 signerPubK := sk.Public()
16
17 // signer: when user requests new R parameter to blind a new msg,
18 // create new signerR (public) with its secret k
19 k, signerR := NewRequestParameters()
20
21 // user: blinds the msg using signer's R
22 msg := new(big.Int).SetBytes([]byte("test"))
23 msgBlinded, userSecretData, err := Blind(msg, signerR)
24 require.Nil(t, err)
25
26 // signer: signs the blinded message using its private key & secret k
27 sBlind, err := sk.BlindSign(msgBlinded, k)
28 require.Nil(t, err)
29
30 // user: unblinds the blinded signature
31 sig := Unblind(sBlind, userSecretData)
32 sigB := sig.Bytes()
33 sig2, err := NewSignatureFromBytes(sigB)
34 assert.Nil(t, err)
35 assert.Equal(t, sig, sig2)
36
37 // signature can be verified with signer PublicKey
38 verified := Verify(msg, sig, signerPubK)
39 assert.True(t, verified)
40}
41
42func TestHashMOddBytes(t *testing.T) {
43 // This test is made with same values than
44 // https://github.com/arnaucube/blindsecp256k1-js to ensure
45 // compatibility
46 mStr := "3024162961766929396601888431330224482373544644288322432261208139289299439809"
47 m, ok := new(big.Int).SetString(mStr, 10)
48 require.True(t, ok)
49 mBytes := m.Bytes()
50
51 hBytes := crypto.Keccak256(mBytes[3:])
52 h := new(big.Int).SetBytes(hBytes)
53 assert.Equal(t,
54 "57523339312508913023232057765773019244858443678197951618720342803494056599369",
55 h.String())
56
57 hBytes = crypto.Keccak256(append(mBytes, []byte{0x12, 0x34}...))
58 h = new(big.Int).SetBytes(hBytes)
59 assert.Equal(t,
60 "9697834584560956691445940439424778243200861871421750951058436814122640359156",
61 h.String())
62}
63
64// func newBigIntWithBitLen(n int) *big.Int {
65// b := make([]byte, n/8)
66// for i := 0; i < len(b); i++ {
67// b[i] = 255
68// }
69// bi := new(big.Int).SetBytes(b[:])
70// return bi
71// }
72//
73// func TestMinBigIntBytesLen(t *testing.T) {
74// k := big.NewInt(1)
75// sk := PrivateKey(*k)
76//
77// mBlinded := newBigIntWithBitLen(MinBigIntBytesLen)
78// require.Equal(t, MinBigIntBytesLen, mBlinded.BitLen())
79// _, err := sk.BlindSign(mBlinded, k)
80// assert.Nil(t, err)
81//
82// mBlinded = new(big.Int).Div(mBlinded, big.NewInt(2))
83// require.Equal(t, MinBigIntBytesLen-1, mBlinded.BitLen())
84// _, err = sk.BlindSign(mBlinded, k)
85// assert.Equal(t, "mBlinded too small", err.Error())
86// }