go-blindsecp256k1/blindsecp256k1_test.go
blindsecp256k1_test.goRaw
1package blindsecp256k1
2
3import (
4 "encoding/hex"
5 "math/big"
6 "testing"
7
8 "github.com/ethereum/go-ethereum/crypto"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
12
13func TestFlow(t *testing.T) {
14 // signer: create new signer key pair
15 sk := NewPrivateKey()
16 signerPubK := sk.Public()
17
18 // signer: when user requests new R parameter to blind a new msg,
19 // create new signerR (public) with its secret k
20 k, signerR := NewRequestParameters()
21
22 // user: blinds the msg using signer's R
23 msg := new(big.Int).SetBytes([]byte("test"))
24 msgBlinded, userSecretData, err := Blind(msg, signerR)
25 require.Nil(t, err)
26
27 // signer: signs the blinded message using its private key & secret k
28 sBlind, err := sk.BlindSign(msgBlinded, k)
29 require.Nil(t, err)
30
31 // user: unblinds the blinded signature
32 sig := Unblind(sBlind, userSecretData)
33 sigB := sig.Bytes()
34 sig2, err := NewSignatureFromBytes(sigB)
35 assert.Nil(t, err)
36 assert.Equal(t, sig, sig2)
37
38 // signature can be verified with signer PublicKey
39 verified := Verify(msg, sig, signerPubK)
40 assert.True(t, verified)
41}
42
43func TestHashMOddBytes(t *testing.T) {
44 // This test is made with same values than
45 // https://github.com/arnaucube/blindsecp256k1-js to ensure
46 // compatibility
47 mStr := "3024162961766929396601888431330224482373544644288322432261208139289299439809"
48 m, ok := new(big.Int).SetString(mStr, 10)
49 require.True(t, ok)
50 mBytes := m.Bytes()
51
52 hBytes := crypto.Keccak256(mBytes[3:])
53 h := new(big.Int).SetBytes(hBytes)
54 assert.Equal(t,
55 "57523339312508913023232057765773019244858443678197951618720342803494056599369",
56 h.String())
57
58 hBytes = crypto.Keccak256(append(mBytes, []byte{0x12, 0x34}...))
59 h = new(big.Int).SetBytes(hBytes)
60 assert.Equal(t,
61 "9697834584560956691445940439424778243200861871421750951058436814122640359156",
62 h.String())
63}
64
65// func newBigIntWithBitLen(n int) *big.Int {
66// b := make([]byte, n/8)
67// for i := 0; i < len(b); i++ {
68// b[i] = 255
69// }
70// bi := new(big.Int).SetBytes(b[:])
71// return bi
72// }
73//
74// func TestMinBigIntBytesLen(t *testing.T) {
75// k := big.NewInt(1)
76// sk := PrivateKey(*k)
77//
78// mBlinded := newBigIntWithBitLen(MinBigIntBytesLen)
79// require.Equal(t, MinBigIntBytesLen, mBlinded.BitLen())
80// _, err := sk.BlindSign(mBlinded, k)
81// assert.Nil(t, err)
82//
83// mBlinded = new(big.Int).Div(mBlinded, big.NewInt(2))
84// require.Equal(t, MinBigIntBytesLen-1, mBlinded.BitLen())
85// _, err = sk.BlindSign(mBlinded, k)
86// assert.Equal(t, "mBlinded too small", err.Error())
87// }
88
89func TestPointCompressDecompress(t *testing.T) {
90 p := G
91 b := p.Compress()
92 assert.Equal(t,
93 "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800",
94 hex.EncodeToString(b[:]))
95 p2, err := DecompressPoint(b)
96 require.Nil(t, err)
97 assert.Equal(t, p, p2)
98
99 for i := 2; i < 1000; i++ {
100 p := G.Mul(big.NewInt(int64(i)))
101 b := p.Compress()
102 assert.Equal(t, 33, len(b))
103
104 p2, err := DecompressPoint(b)
105 require.Nil(t, err)
106 assert.Equal(t, p, p2)
107 }
108}
109
110func BenchmarkCompressDecompress(b *testing.B) {
111 const n = 256
112 var points [n]*Point
113 var compPoints [n][33]byte
114
115 for i := 0; i < n; i++ {
116 points[i] = G.Mul(big.NewInt(int64(i)))
117 }
118 for i := 0; i < n; i++ {
119 compPoints[i] = points[i].Compress()
120 }
121
122 b.Run("Compress", func(b *testing.B) {
123 for i := 0; i < b.N; i++ {
124 _ = points[i%n].Compress()
125 }
126 })
127 b.Run("DecompressPoint", func(b *testing.B) {
128 for i := 0; i < b.N; i++ {
129 _, _ = DecompressPoint(compPoints[i%n])
130 }
131 })
132}