forked from cobinhood/moneroutil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathringct.go
388 lines (353 loc) · 7.96 KB
/
ringct.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package moneroutil
import (
"fmt"
"io"
)
const (
RCTTypeNull = iota
RCTTypeFull
RCTTypeSimple
)
// Pedersen Commitment is generated from this struct
// C = aG + bH where a = mask and b = amount
// senderPk is the one-time public key for ECDH exchange
type ecdhTuple struct {
mask Key
amount Key
senderPk Key
}
// Range proof commitments
type Key64 [64]Key
// Borromean Signature
type BoroSig struct {
s0 Key64
s1 Key64
ee Key
}
// MLSAG (Multilayered Linkable Spontaneous Anonymous Group) Signature
type MlsagSig struct {
ss [][]Key
cc Key
ii []Key
}
// Range Signature
// Essentially data for a Borromean Signature
type RangeSig struct {
asig BoroSig
ci Key64
}
// Confidential Transaction Keys
type CtKey struct {
destination Key
mask Key
}
// Ring Confidential Signature parts that we have to keep
type RctSigBase struct {
sigType uint8
message Key
mixRing [][]CtKey
pseudoOuts []Key
ecdhInfo []ecdhTuple
outPk []CtKey
txFee uint64
}
// Ring Confidential Signature parts that we can just prune later
type RctSigPrunable struct {
rangeSigs []RangeSig
mlsagSigs []MlsagSig
}
// Ring Confidential Signature struct that can verify everything
type RctSig struct {
RctSigBase
RctSigPrunable
}
func (k *Key) ToExtended() (result *ExtendedGroupElement) {
result = new(ExtendedGroupElement)
result.FromBytes(k)
return
}
func identity() (result *Key) {
result = new(Key)
result[0] = 1
return
}
// convert a uint64 to a scalar
func d2h(val uint64) (result *Key) {
result = new(Key)
for i := 0; val > 0; i++ {
result[i] = byte(val & 0xFF)
val /= 256
}
return
}
// multiply a scalar by H (second curve point of Pedersen Commitment)
func ScalarMultH(scalar *Key) (result *Key) {
h := new(ExtendedGroupElement)
h.FromBytes(&H)
resultPoint := new(ProjectiveGroupElement)
GeScalarMult(resultPoint, scalar, h)
result = new(Key)
resultPoint.ToBytes(result)
return
}
// add two points together
func AddKeys(sum, k1, k2 *Key) {
a := k1.ToExtended()
b := new(CachedGroupElement)
k2.ToExtended().ToCached(b)
c := new(CompletedGroupElement)
geAdd(c, a, b)
tmp := new(ExtendedGroupElement)
c.ToExtended(tmp)
tmp.ToBytes(sum)
return
}
// compute a*G + b*B
func AddKeys2(result, a, b, B *Key) {
BPoint := B.ToExtended()
RPoint := new(ProjectiveGroupElement)
GeDoubleScalarMultVartime(RPoint, b, BPoint, a)
RPoint.ToBytes(result)
return
}
// subtract two points A - B
func SubKeys(diff, k1, k2 *Key) {
a := k1.ToExtended()
b := new(CachedGroupElement)
k2.ToExtended().ToCached(b)
c := new(CompletedGroupElement)
geSub(c, a, b)
tmp := new(ExtendedGroupElement)
c.ToExtended(tmp)
tmp.ToBytes(diff)
return
}
func (k *Key64) Serialize() (result []byte) {
for _, key := range k {
result = append(result, key[:]...)
}
return
}
func (b *BoroSig) Serialize() (result []byte) {
result = append(b.s0.Serialize(), b.s1.Serialize()...)
result = append(result, b.ee[:]...)
return
}
func (r *RangeSig) Serialize() (result []byte) {
result = append(r.asig.Serialize(), r.ci.Serialize()...)
return
}
func (m *MlsagSig) Serialize() (result []byte) {
for i := 0; i < len(m.ss); i++ {
for j := 0; j < len(m.ss[i]); j++ {
result = append(result, m.ss[i][j][:]...)
}
}
result = append(result, m.cc[:]...)
return
}
func (r *RctSigBase) SerializeBase() (result []byte) {
result = []byte{r.sigType}
// Null type returns right away
if r.sigType == RCTTypeNull {
return
}
result = append(result, Uint64ToBytes(r.txFee)...)
if r.sigType == RCTTypeSimple {
for _, input := range r.pseudoOuts {
result = append(result, input[:]...)
}
}
for _, ecdh := range r.ecdhInfo {
result = append(result, ecdh.mask[:]...)
result = append(result, ecdh.amount[:]...)
}
for _, ctKey := range r.outPk {
result = append(result, ctKey.mask[:]...)
}
return
}
func (r *RctSigBase) BaseHash() (result Hash) {
result = Keccak256(r.SerializeBase())
return
}
func (r *RctSig) SerializePrunable() (result []byte) {
if r.sigType == RCTTypeNull {
return
}
for _, rangeSig := range r.rangeSigs {
result = append(result, rangeSig.Serialize()...)
}
for _, mlsagSig := range r.mlsagSigs {
result = append(result, mlsagSig.Serialize()...)
}
return
}
func (r *RctSig) PrunableHash() (result Hash) {
if r.sigType == RCTTypeNull {
return
}
result = Keccak256(r.SerializePrunable())
return
}
func verBorromean(b *BoroSig, p1, p2 *Key64) bool {
var data []byte
tmp, tmp2 := new(Key), new(Key)
for i := 0; i < 64; i++ {
AddKeys2(tmp, &b.s0[i], &b.ee, &p1[i])
tmp3 := HashToScalar(tmp[:])
AddKeys2(tmp2, &b.s1[i], tmp3, &p2[i])
data = append(data, tmp2[:]...)
}
computed := HashToScalar(data)
return *computed == b.ee
}
func verRange(c *Key, as RangeSig) bool {
var CiH Key64
tmp := identity()
for i := 0; i < 64; i++ {
SubKeys(&CiH[i], &as.ci[i], &H2[i])
AddKeys(tmp, tmp, &as.ci[i])
}
if *c != *tmp {
return false
}
return verBorromean(&as.asig, &as.ci, &CiH)
}
// Verify a RCTTypeSimple RingCT Signature
func (r *RctSig) VerifyRctSimple() bool {
sumOutPks := identity()
for _, ctKey := range r.outPk {
AddKeys(sumOutPks, sumOutPks, &ctKey.mask)
}
txFeeKey := ScalarMultH(d2h(r.txFee))
AddKeys(sumOutPks, sumOutPks, txFeeKey)
sumPseudoOuts := identity()
for _, pseudoOut := range r.pseudoOuts {
AddKeys(sumPseudoOuts, sumPseudoOuts, &pseudoOut)
}
if *sumPseudoOuts != *sumOutPks {
return false
}
for i, ctKey := range r.outPk {
if !verRange(&ctKey.mask, r.rangeSigs[i]) {
return false
}
}
return true
}
func (r *RctSig) VerifyRctFull() bool {
for i, ctKey := range r.outPk {
if !verRange(&ctKey.mask, r.rangeSigs[i]) {
return false
}
}
return true
}
func ParseCtKey(buf io.Reader) (result CtKey, err error) {
if result.mask, err = ParseKey(buf); err != nil {
return
}
return
}
func ParseKey64(buf io.Reader) (result Key64, err error) {
for i := 0; i < 64; i++ {
if result[i], err = ParseKey(buf); err != nil {
return
}
}
return
}
func ParseBoroSig(buf io.Reader) (result BoroSig, err error) {
if result.s0, err = ParseKey64(buf); err != nil {
return
}
if result.s1, err = ParseKey64(buf); err != nil {
return
}
if result.ee, err = ParseKey(buf); err != nil {
return
}
return
}
func ParseRangeSig(buf io.Reader) (result RangeSig, err error) {
if result.asig, err = ParseBoroSig(buf); err != nil {
return
}
if result.ci, err = ParseKey64(buf); err != nil {
return
}
return
}
func ParseRingCtSignature(buf io.Reader, nInputs, nOutputs, nMixin int) (result *RctSig, err error) {
r := new(RctSig)
sigType := make([]byte, 1)
_, err = buf.Read(sigType)
if err != nil {
return
}
r.sigType = uint8(sigType[0])
if r.sigType == RCTTypeNull {
result = r
return
}
if r.sigType != RCTTypeFull || r.sigType != RCTTypeSimple {
err = fmt.Errorf("Bad sigType %d", r.sigType)
}
r.txFee, err = ReadVarInt(buf)
if err != nil {
return
}
var nMg, nSS int
if r.sigType == RCTTypeSimple {
nMg = nInputs
nSS = 2
r.pseudoOuts = make([]Key, nInputs)
for i := 0; i < nInputs; i++ {
if r.pseudoOuts[i], err = ParseKey(buf); err != nil {
return
}
}
} else {
nMg = 1
nSS = nInputs + 1
}
r.ecdhInfo = make([]ecdhTuple, nOutputs)
for i := 0; i < nOutputs; i++ {
if r.ecdhInfo[i].mask, err = ParseKey(buf); err != nil {
return
}
if r.ecdhInfo[i].amount, err = ParseKey(buf); err != nil {
return
}
}
r.outPk = make([]CtKey, nOutputs)
for i := 0; i < nOutputs; i++ {
if r.outPk[i], err = ParseCtKey(buf); err != nil {
return
}
}
r.rangeSigs = make([]RangeSig, nOutputs)
for i := 0; i < nOutputs; i++ {
if r.rangeSigs[i], err = ParseRangeSig(buf); err != nil {
return
}
}
r.mlsagSigs = make([]MlsagSig, nMg)
for i := 0; i < nMg; i++ {
r.mlsagSigs[i].ss = make([][]Key, nMixin+1)
for j := 0; j < nMixin+1; j++ {
r.mlsagSigs[i].ss[j] = make([]Key, nSS)
for k := 0; k < nSS; k++ {
if r.mlsagSigs[i].ss[j][k], err = ParseKey(buf); err != nil {
return
}
}
}
if r.mlsagSigs[i].cc, err = ParseKey(buf); err != nil {
return
}
}
result = r
return
}