This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ring.go
228 lines (179 loc) · 5.32 KB
/
ring.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
// Copyright (c) 2017 Clearmatics Technologies Ltd
// SPDX-License-Identifier: LGPL-3.0+
package main
import (
"crypto/sha256"
"encoding/json"
"math/big"
)
// A Ring is a number of public/private key pairs
type Ring struct {
PubKeys []CurvePoint `json:"pubkeys"`
PrivKeys []*big.Int `json:"privkeys"`
}
// MarshalJSON converts a Ring to a JSON representation
func (r *Ring) MarshalJSON() ([]byte, error) {
pks := make([]*hexBig, len(r.PrivKeys))
for i, v := range r.PrivKeys {
pks[i] = (*hexBig)(v)
}
return json.Marshal(&struct {
PubKeys []CurvePoint `json:"pubkeys"`
PrivKeys []*hexBig `json:"privkeys"`
}{
PubKeys: r.PubKeys,
PrivKeys: pks,
})
}
// UnmarshalJSON converts a JSON representation to a Ring struct
func (r *Ring) UnmarshalJSON(data []byte) error {
var aux struct {
PubKeys []CurvePoint `json:"pubkeys"`
PrivKeys []*hexBig `json:"privkeys"`
}
err := json.Unmarshal(data, &aux)
if err != nil {
return err
}
pks := make([]*big.Int, len(aux.PrivKeys))
for i, v := range aux.PrivKeys {
pks[i] = (*big.Int)(v)
}
r.PrivKeys = pks[:]
r.PubKeys = aux.PubKeys
return nil
}
func convert(data []byte) *big.Int {
z := new(big.Int)
z.SetBytes(data)
return z
}
var curveB = new(big.Int).SetInt64(3)
// PublicKeysHashed returns the hashed public key for the given Ring
func (r Ring) PublicKeysHashed() [sha256.Size]byte {
var out [sha256.Size]byte
for i := 0; i < len(r.PubKeys); i++ {
out = sha256.Sum256(append(out[:], r.PubKeys[i].Marshal()...))
}
return out
}
// Generate creates public and private keypairs for a ring with the size of n
func (r *Ring) Generate(n int) error {
for i := 0; i < n; i++ {
public, private, err := generateKeyPair()
if err != nil {
return err
}
r.PrivKeys = append(r.PrivKeys, private)
r.PubKeys = append(r.PubKeys, *public)
}
return nil
}
// PubKeyIndex returns the index of a public key
func (r *Ring) PubKeyIndex(pk CurvePoint) int {
for i, pub := range r.PubKeys {
if pub == pk {
return i
}
}
return -1
}
// Signature generates a signature
func (r *Ring) Signature(pk *big.Int, message []byte, signer int) (*RingSignature, error) {
N := CurvePoint{}.Order()
// Message is a 256 bit token which uniquely identifies the Ring and the public keys
// of all of its participants
var messageHash [32]byte
copy(messageHash[:], message)
hashp := NewCurvePointFromHash(messageHash)
// Calculate Tau
pk.Mod(pk, N)
hashSP := hashp.ScalarMult(pk)
// hashout = H(hash.X, tau)
hashAcc := sha256.Sum256(append(hashp.Marshal()[:32], hashSP.Marshal()...))
n := len(r.PubKeys)
var ctlist []*big.Int //This has to be 2n so here we have n = 4 so 2n = 8 :)
var a, b CurvePoint
var ri *big.Int
csum := big.NewInt(0)
for j := 0; j < n; j++ {
if j != signer {
cj := CurvePoint{}.RandomN()
tj := CurvePoint{}.RandomN()
a = r.PubKeys[j].ParameterPointAdd(tj, cj)
b = hashp.HashPointAdd(hashSP, tj, cj)
ctlist = append(ctlist, cj)
ctlist = append(ctlist, tj)
csum.Add(csum, cj)
}
if j == signer {
dummy := big.NewInt(0)
ctlist = append(ctlist, dummy)
ctlist = append(ctlist, dummy)
ri = CurvePoint{}.RandomN()
a = CurvePoint{}.ScalarBaseMult(ri)
b = hashp.ScalarMult(ri)
}
hashAcc = sha256.Sum256(append(hashAcc[:], append(a.Marshal(), b.Marshal()...)...))
}
hashb := new(big.Int).SetBytes(hashAcc[:])
hashb.Mod(hashb, N)
csum.Mod(csum, N)
c := new(big.Int).Sub(hashb, csum)
c.Mod(c, N)
cx := new(big.Int).Mul(c, pk)
cx.Mod(cx, N)
ti := new(big.Int).Sub(ri, cx)
ti.Mod(ti, N)
ctlist[2*signer] = c
ctlist[2*signer+1] = ti
return &RingSignature{hashSP, ctlist}, nil
}
// Signatures generates a signature given a message
func (r *Ring) Signatures(message []byte) ([]RingSignature, error) {
var signaturesArr []RingSignature
for i, privKey := range r.PrivKeys {
pub := r.PubKeys[i]
signerNumber := r.PubKeyIndex(pub)
signature, err := r.Signature(privKey, message, signerNumber)
if err != nil {
return nil, err
}
signaturesArr = append(signaturesArr, *signature)
}
return signaturesArr, nil
}
// VerifySignature verifys a signature given a message
func (r *Ring) VerifySignature(message []byte, sigma RingSignature) bool {
// ring verification
// assumes R = pk1, pk2, ..., pkn
// sigma = H(m||R)^x_i, c1, t1, ..., cn, tn = taux, tauy, c1, t1, ..., cn, tn
tau := sigma.Tau
ctlist := sigma.Ctlist
n := len(r.PubKeys)
N := CurvePoint{}.Order() //group.N
var messageHash [32]byte
copy(messageHash[:], message)
hashp := NewCurvePointFromHash(messageHash)
hashAcc := sha256.Sum256(append(hashp.Marshal()[:32], tau.Marshal()...))
csum := big.NewInt(0)
for j := 0; j < n; j++ {
cj := ctlist[2*j]
tj := ctlist[2*j+1]
cj.Mod(cj, N)
tj.Mod(tj, N)
yc := r.PubKeys[j].ScalarMult(cj) // y^c = g^(xc)
gt := CurvePoint{}.ScalarBaseMult(tj) // g^t + y^c
gt = gt.Add(yc)
tauc := tau.ScalarMult(cj) //H(m||R)^(xc)
H := hashp.ScalarMult(tj) //H(m||R)^t
H = H.Add(tauc) // fieldJacobianToBigAffine `normalizes' values before returning so yes - normalize uses fast reduction using specialised form of secp256k1's prime! :D
hashAcc = sha256.Sum256(append(hashAcc[:], append(gt.Marshal(), H.Marshal()...)...))
csum.Add(csum, cj)
csum.Mod(csum, N)
}
hashout := new(big.Int).SetBytes(hashAcc[:])
hashout.Mod(hashout, N)
csum.Mod(csum, N)
return csum.Cmp(hashout) == 0
}