forked from cobinhood/moneroutil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathringsignature.go
184 lines (173 loc) · 4.69 KB
/
ringsignature.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
package moneroutil
import (
"fmt"
"io"
"math/rand"
)
type RingSignatureElement struct {
c *Key
r *Key
}
type RingSignature []*RingSignatureElement
func (r *RingSignatureElement) Serialize() (result []byte) {
result = make([]byte, 2*KeyLength)
copy(result[:KeyLength], r.c[:])
copy(result[KeyLength:2*KeyLength], r.r[:])
return
}
func (r *RingSignature) Serialize() (result []byte) {
result = make([]byte, len(*r)*KeyLength*2)
for i := 0; i < len(*r); i++ {
copy(result[i*KeyLength*2:(i+1)*KeyLength*2], (*r)[i].Serialize())
}
return
}
func NewRingSignatureElement() (r *RingSignatureElement) {
r = &RingSignatureElement{
c: new(Key),
r: new(Key),
}
return
}
func ParseSignature(buf io.Reader) (result *RingSignatureElement, err error) {
rse := NewRingSignatureElement()
c := make([]byte, KeyLength)
n, err := buf.Read(c)
if err != nil {
return
}
if n != KeyLength {
err = fmt.Errorf("Not enough bytes for signature c")
return
}
copy(rse.c[:], c)
r := make([]byte, KeyLength)
n, err = buf.Read(r)
if err != nil {
return
}
if n != KeyLength {
err = fmt.Errorf("Not enough bytes for signature r")
return
}
copy(rse.r[:], r)
result = rse
return
}
func ParseSignatures(mixinLengths []int, buf io.Reader) (signatures []RingSignature, err error) {
// mixinLengths is the number of mixins at each input position
sigs := make([]RingSignature, len(mixinLengths), len(mixinLengths))
for i, nMixin := range mixinLengths {
sigs[i] = make([]*RingSignatureElement, nMixin, nMixin)
for j := 0; j < nMixin; j++ {
sigs[i][j], err = ParseSignature(buf)
if err != nil {
return
}
}
}
signatures = sigs
return
}
func HashToScalar(data ...[]byte) (result *Key) {
result = new(Key)
*result = Key(Keccak256(data...))
ScReduce32(result)
return
}
func CreateSignature(prefixHash *Hash, mixins []Key, privKey *Key) (keyImage Key, pubKeys []Key, sig RingSignature) {
point := privKey.PubKey().HashToEC()
keyImagePoint := new(ProjectiveGroupElement)
GeScalarMult(keyImagePoint, privKey, point)
// convert key Image point from Projective to Extended
// in order to precompute
keyImagePoint.ToBytes(&keyImage)
keyImageGe := new(ExtendedGroupElement)
keyImageGe.FromBytes(&keyImage)
var keyImagePre [8]CachedGroupElement
GePrecompute(&keyImagePre, keyImageGe)
k := RandomScalar()
pubKeys = make([]Key, len(mixins)+1)
privIndex := rand.Intn(len(pubKeys))
pubKeys[privIndex] = *privKey.PubKey()
r := make([]*RingSignatureElement, len(pubKeys))
sum := new(Key)
toHash := prefixHash[:]
for i := 0; i < len(pubKeys); i++ {
tmpE := new(ExtendedGroupElement)
tmpP := new(ProjectiveGroupElement)
var tmpEBytes, tmpPBytes Key
if i == privIndex {
GeScalarMultBase(tmpE, k)
tmpE.ToBytes(&tmpEBytes)
toHash = append(toHash, tmpEBytes[:]...)
tmpE = privKey.PubKey().HashToEC()
GeScalarMult(tmpP, k, tmpE)
tmpP.ToBytes(&tmpPBytes)
toHash = append(toHash, tmpPBytes[:]...)
} else {
if i > privIndex {
pubKeys[i] = mixins[i-1]
} else {
pubKeys[i] = mixins[i]
}
r[i] = &RingSignatureElement{
c: RandomScalar(),
r: RandomScalar(),
}
tmpE.FromBytes(&pubKeys[i])
GeDoubleScalarMultVartime(tmpP, r[i].c, tmpE, r[i].r)
tmpP.ToBytes(&tmpPBytes)
toHash = append(toHash, tmpPBytes[:]...)
tmpE = pubKeys[i].HashToEC()
GeDoubleScalarMultPrecompVartime(tmpP, r[i].r, tmpE, r[i].c, &keyImagePre)
tmpP.ToBytes(&tmpPBytes)
toHash = append(toHash, tmpPBytes[:]...)
ScAdd(sum, sum, r[i].c)
}
}
h := HashToScalar(toHash)
r[privIndex] = NewRingSignatureElement()
ScSub(r[privIndex].c, h, sum)
ScMulSub(r[privIndex].r, r[privIndex].c, privKey, k)
sig = r
return
}
func VerifySignature(prefixHash *Hash, keyImage *Key, pubKeys []Key, ringSignature RingSignature) (result bool) {
keyImageGe := new(ExtendedGroupElement)
if !keyImageGe.FromBytes(keyImage) {
result = false
return
}
var keyImagePre [8]CachedGroupElement
GePrecompute(&keyImagePre, keyImageGe)
toHash := prefixHash[:]
tmpS, sum := new(Key), new(Key)
for i, pubKey := range pubKeys {
rse := ringSignature[i]
if !ScValid(rse.c) || !ScValid(rse.r) {
result = false
return
}
tmpE := new(ExtendedGroupElement)
tmpP := new(ProjectiveGroupElement)
if !tmpE.FromBytes(&pubKey) {
result = false
return
}
var tmpPBytes, tmpEBytes Key
GeDoubleScalarMultVartime(tmpP, rse.c, tmpE, rse.r)
tmpP.ToBytes(&tmpPBytes)
toHash = append(toHash, tmpPBytes[:]...)
tmpE = pubKey.HashToEC()
tmpE.ToBytes(&tmpEBytes)
GeDoubleScalarMultPrecompVartime(tmpP, rse.r, tmpE, rse.c, &keyImagePre)
tmpP.ToBytes(&tmpPBytes)
toHash = append(toHash, tmpPBytes[:]...)
ScAdd(sum, sum, rse.c)
}
tmpS = HashToScalar(toHash)
ScSub(sum, tmpS, sum)
result = ScIsZero(sum)
return
}