-
Notifications
You must be signed in to change notification settings - Fork 11
/
ring_test.go
249 lines (205 loc) · 6.6 KB
/
ring_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package ring
import (
"crypto/rand"
"encoding/hex"
"math/big"
"testing"
"github.com/athanorlabs/go-dleq/types"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)
var (
testMsg = sha3.Sum256([]byte("helloworld"))
)
func createSigWithCurve(t *testing.T, curve types.Curve, size, idx int) *RingSig {
// instantiate private key
privkey := curve.NewRandomScalar()
// generate keyring
keyring, err := NewKeyRing(curve, size, privkey, idx)
require.NoError(t, err)
// sign message
sig, err := keyring.Sign(testMsg, privkey)
require.NoError(t, err)
return sig
}
func createSig(t *testing.T, size, idx int) *RingSig {
return createSigWithCurve(t, Secp256k1(), size, idx)
}
func TestSign_Loop_Ed25519(t *testing.T) {
maxSize := 100
curve := Ed25519()
for i := 2; i < maxSize; i++ {
idx, err := rand.Int(rand.Reader, big.NewInt(int64(i)))
require.NoError(t, err)
sig := createSigWithCurve(t, curve, i, int(idx.Int64()))
require.True(t, sig.Verify(testMsg))
}
}
func TestSign_Loop_Secp256k1(t *testing.T) {
maxSize := 100
for i := 2; i < maxSize; i++ {
idx, err := rand.Int(rand.Reader, big.NewInt(int64(i)))
require.NoError(t, err)
sig := createSig(t, i, int(idx.Int64()))
require.True(t, sig.Verify(testMsg))
}
}
func TestNewKeyRing(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
keyring, err := NewKeyRing(curve, 2, privkey, 0)
require.NoError(t, err)
require.NotNil(t, keyring)
require.Equal(t, 2, len(keyring.pubkeys))
}
func TestNewKeyRing3(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
keyring, err := NewKeyRing(curve, 3, privkey, 1)
require.NoError(t, err)
require.NotNil(t, keyring)
require.Equal(t, 3, len(keyring.pubkeys))
}
func TestNewKeyRing_IdxOutOfBounds(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
_, err := NewKeyRing(curve, 2, privkey, 3)
require.Error(t, err)
}
func TestGenKeyRing(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
s := 0
size := 3
// generate some pubkeys for the keyring
pubkeys := make([]types.Point, size)
for i := 0; i < size; i++ {
priv := curve.NewRandomScalar()
pubkeys[i] = curve.ScalarBaseMul(priv)
}
keyring, err := NewKeyRingFromPublicKeys(curve, pubkeys, privkey, s)
require.NoError(t, err)
require.NotNil(t, keyring)
require.Equal(t, size+1, keyring.Size())
require.True(t, keyring.pubkeys[s].Equals(curve.ScalarBaseMul(privkey)))
fixedkeys := make([]types.Point, size+1)
fixedkeys[0] = curve.ScalarBaseMul(privkey)
copy(fixedkeys[1:], pubkeys)
keyring, err = NewFixedKeyRingFromPublicKeys(curve, fixedkeys)
require.NoError(t, err)
require.NotNil(t, keyring)
for i := 0; i < size; i++ {
require.True(t, keyring.pubkeys[i].Equals(fixedkeys[i]))
}
}
func TestRing_Equals(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
keyring, err := NewKeyRing(curve, 10, privkey, 0)
require.NoError(t, err)
keyring2, err := NewKeyRing(curve, 10, privkey, 0)
require.NoError(t, err)
require.False(t, keyring.Equals(keyring2)) // NewKeyRing generates random pubkeys
keyring3, err := NewFixedKeyRingFromPublicKeys(curve, keyring.pubkeys)
require.NoError(t, err)
require.True(t, keyring.Equals(keyring3))
}
func TestSig_RingEquals(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
keyring, err := NewKeyRing(curve, 10, privkey, 0)
require.NoError(t, err)
keyring2, err := NewKeyRing(curve, 10, privkey, 0)
require.NoError(t, err)
sig, err := keyring.Sign(testMsg, privkey)
require.NoError(t, err)
sig2, err := keyring2.Sign(testMsg, privkey)
require.NoError(t, err)
require.False(t, sig.Ring().Equals(keyring2))
require.True(t, sig.Ring().Equals(keyring))
require.False(t, sig2.Ring().Equals(keyring))
require.True(t, sig2.Ring().Equals(keyring2))
}
func TestSign(t *testing.T) {
createSig(t, 9, 0)
}
func TestSignAgain(t *testing.T) {
createSig(t, 100, 17)
}
func TestVerify(t *testing.T) {
sig := createSig(t, 5, 4)
require.True(t, sig.Verify(testMsg))
}
func TestVerifyFalse(t *testing.T) {
curve := Secp256k1()
sig := createSig(t, 5, 2)
// alter signature
sig.c = curve.NewRandomScalar()
require.False(t, sig.Verify(testMsg))
}
func TestVerifyWrongMessage(t *testing.T) {
sig := createSig(t, 5, 1)
fakeMsg := sha3.Sum256([]byte("noot"))
require.False(t, sig.Verify(fakeMsg))
}
func TestLinkabilityTrue(t *testing.T) {
curve := Secp256k1()
privkey := curve.NewRandomScalar()
msg1 := "helloworld"
msgHash1 := sha3.Sum256([]byte(msg1))
keyring1, err := NewKeyRing(curve, 2, privkey, 0)
require.NoError(t, err)
sig1, err := keyring1.Sign(msgHash1, privkey)
require.NoError(t, err)
msg2 := "hello world"
msgHash2 := sha3.Sum256([]byte(msg2))
keyring2, err := NewKeyRing(curve, 2, privkey, 0)
require.NoError(t, err)
sig2, err := keyring2.Sign(msgHash2, privkey)
require.NoError(t, err)
require.True(t, Link(sig1, sig2))
}
func TestLinkabilityTrue_imageSmallSubgroup(t *testing.T) {
// test for the audit case where one image has a small subgroup
// point added to it in an attempt to break the linkability property.
// ensure that the two images can still be linked.
// point from https://monero.stackexchange.com/questions/8671/what-are-the-hex-representations-of-the-small-subgroup-curve-points-on-ed25519
subgroupPointBytes, err := hex.DecodeString("0100000000000000000000000000000000000000000000000000000000000000")
require.NoError(t, err)
subgroupPoint, err := Ed25519().DecodeToPoint(subgroupPointBytes)
require.NoError(t, err)
curve := Ed25519()
privkey := curve.NewRandomScalar()
msg1 := "helloworld"
msgHash1 := sha3.Sum256([]byte(msg1))
keyring1, err := NewKeyRing(curve, 2, privkey, 0)
require.NoError(t, err)
sig1, err := keyring1.Sign(msgHash1, privkey)
require.NoError(t, err)
msg2 := "hello world"
msgHash2 := sha3.Sum256([]byte(msg2))
keyring2, err := NewKeyRing(curve, 2, privkey, 0)
require.NoError(t, err)
sig2, err := keyring2.Sign(msgHash2, privkey)
require.NoError(t, err)
sig2.image = sig2.image.Add(subgroupPoint)
require.True(t, Link(sig1, sig2))
}
func TestLinkabilityFalse(t *testing.T) {
curve := Secp256k1()
privkey1 := curve.NewRandomScalar()
msg1 := "helloworld"
msgHash1 := sha3.Sum256([]byte(msg1))
keyring1, err := NewKeyRing(curve, 2, privkey1, 0)
require.NoError(t, err)
sig1, err := keyring1.Sign(msgHash1, privkey1)
require.NoError(t, err)
privkey2 := curve.NewRandomScalar()
msg2 := "hello world"
msgHash2 := sha3.Sum256([]byte(msg2))
keyring2, err := NewKeyRing(curve, 2, privkey2, 0)
require.NoError(t, err)
sig2, err := keyring2.Sign(msgHash2, privkey2)
require.NoError(t, err)
require.False(t, Link(sig1, sig2))
}