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 TestSignatureCompressDecompress(t *testing.T) {
111 f := G
112 sig := &Signature{
113 S: big.NewInt(1),
114 F: f,
115 }
116 b := sig.Compress()
117 assert.Equal(t,
118 "01000000000000000000000000000000000000000000000000000000000000007"+
119 "9be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800",
120 hex.EncodeToString(b[:]))
121 sig2, err := DecompressSignature(b)
122 require.Nil(t, err)
123 assert.Equal(t, sig, sig2)
124
125 f = G
126 sig = &Signature{
127 S: Q,
128 F: f,
129 }
130 b = sig.Compress()
131 assert.Equal(t,
132 "0cffffbfffffffffffffffffffffffffffffffffffffffffffffffffffffff3f7"+
133 "9be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800",
134 hex.EncodeToString(b[:]))
135 sig2, err = DecompressSignature(b)
136 require.Nil(t, err)
137 require.Equal(t, sig, sig2)
138
139 for i := 2; i < 10; i++ {
140 s := new(big.Int).Mod(new(big.Int).Mul(Q, big.NewInt(int64(i))), P)
141 f := G.Mul(big.NewInt(int64(i)))
142 sig := &Signature{
143 S: s,
144 F: f,
145 }
146 b := sig.Compress()
147 assert.Equal(t, 65, len(b))
148
149 sig2, err := DecompressSignature(b)
150 require.Nil(t, err)
151 assert.Equal(t, sig, sig2)
152 }
153}
154
155func BenchmarkCompressDecompress(b *testing.B) {
156 const n = 256
157 var points [n]*Point
158 var compPoints [n][33]byte
159
160 for i := 0; i < n; i++ {
161 points[i] = G.Mul(big.NewInt(int64(i)))
162 }
163 for i := 0; i < n; i++ {
164 compPoints[i] = points[i].Compress()
165 }
166
167 b.Run("Compress", func(b *testing.B) {
168 for i := 0; i < b.N; i++ {
169 _ = points[i%n].Compress()
170 }
171 })
172 b.Run("DecompressPoint", func(b *testing.B) {
173 for i := 0; i < b.N; i++ {
174 _, _ = DecompressPoint(compPoints[i%n])
175 }
176 })
177}