/
scalar.go
294 lines (248 loc) · 5.52 KB
/
scalar.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
package privacy
import (
"crypto/subtle"
"encoding/hex"
"errors"
"fmt"
C25519 "github.com/incognitochain/go-incognito-sdk/privacy/curve25519"
"math/big"
"sort"
)
type Scalar struct {
key C25519.Key
}
func (sc Scalar) GetKey() C25519.Key {
return sc.key
}
func (sc Scalar) String() string {
return fmt.Sprintf("%x", sc.key[:])
}
func (sc Scalar) MarshalText() []byte {
return []byte(fmt.Sprintf("%x", sc.key[:]))
}
func (sc *Scalar) UnmarshalText(data []byte) (*Scalar, error) {
if sc == nil {
sc = new(Scalar)
}
byteSlice, _ := hex.DecodeString(string(data))
if len(byteSlice) != Ed25519KeySize {
return nil, errors.New("Incorrect key size")
}
copy(sc.key[:], byteSlice)
return sc, nil
}
func (sc Scalar) ToBytes() [Ed25519KeySize]byte {
return sc.key.ToBytes()
}
func (sc Scalar) ToBytesS() []byte {
slice := sc.key.ToBytes()
return slice[:]
}
func (sc *Scalar) FromBytes(b [Ed25519KeySize]byte) *Scalar {
if sc == nil {
sc = new(Scalar)
}
sc.key.FromBytes(b)
//if !C25519.ScValid(&sc.key) {
// panic("Invalid Scalar Value")
//}
return sc
}
func (sc *Scalar) FromBytesS(b []byte) *Scalar {
//if len(b) != Ed25519KeySize {
// panic("Invalid Ed25519 Key Size")
//}
if sc == nil {
sc = new(Scalar)
}
var array [Ed25519KeySize]byte
copy(array[:], b)
sc.key.FromBytes(array)
//if !C25519.ScValid(&sc.key) {
// panic("Invalid Scalar Value")
//}
return sc
}
func (sc *Scalar) SetKey(a *C25519.Key) (*Scalar, error) {
if sc == nil {
sc = new(Scalar)
}
sc.key = *a
if sc.ScalarValid() == false {
return nil, errors.New("Invalid key value")
}
return sc, nil
}
func (sc *Scalar) Set(a *Scalar) *Scalar {
if sc == nil {
sc = new(Scalar)
}
sc.key = a.key
return sc
}
func RandomScalar() *Scalar {
sc := new(Scalar)
key := C25519.RandomScalar()
sc.key = *key
return sc
}
func HashToScalar(data []byte) *Scalar {
key := C25519.HashToScalar(data)
sc, error := new(Scalar).SetKey(key)
if error != nil {
return nil
}
return sc
}
func (sc *Scalar) FromUint64(i uint64) *Scalar {
if sc == nil {
sc = new(Scalar)
}
sc.SetKey(d2h(i))
return sc
}
func (sc *Scalar) ToUint64() uint64 {
if sc == nil {
return 0
}
keyBN := new(big.Int).SetBytes(sc.ToBytesS())
return keyBN.Uint64()
}
func (sc *Scalar) Add(a, b *Scalar) *Scalar {
if sc == nil {
sc = new(Scalar)
}
var res C25519.Key
C25519.ScAdd(&res, &a.key, &b.key)
sc.key = res
return sc
}
func (sc *Scalar) Sub(a, b *Scalar) *Scalar {
if sc == nil {
sc = new(Scalar)
}
var res C25519.Key
C25519.ScSub(&res, &a.key, &b.key)
sc.key = res
return sc
}
func (sc *Scalar) Mul(a, b *Scalar) *Scalar {
if sc == nil {
sc = new(Scalar)
}
var res C25519.Key
C25519.ScMul(&res, &a.key, &b.key)
sc.key = res
return sc
}
// a*b + c % l
func (sc *Scalar) MulAdd(a, b, c *Scalar) *Scalar {
if sc == nil {
sc = new(Scalar)
}
var res C25519.Key
C25519.ScMulAdd(&res, &a.key, &b.key, &c.key)
sc.key = res
return sc
}
func (sc *Scalar) Exp(a *Scalar, v uint64) *Scalar {
if sc == nil {
sc = new(Scalar)
}
var res C25519.Key
C25519.ScMul(&res, &a.key, &a.key)
for i := 0; i < int(v)-2; i++ {
C25519.ScMul(&res, &res, &a.key)
}
sc.key = res
return sc
}
func (sc *Scalar) ScalarValid() bool {
if sc == nil {
return false
}
return C25519.ScValid(&sc.key)
}
func (sc *Scalar) IsOne() bool {
s := sc.key
return ((int(s[0]|s[1]|s[2]|s[3]|s[4]|s[5]|s[6]|s[7]|s[8]|
s[9]|s[10]|s[11]|s[12]|s[13]|s[14]|s[15]|s[16]|s[17]|
s[18]|s[19]|s[20]|s[21]|s[22]|s[23]|s[24]|s[25]|s[26]|
s[27]|s[28]|s[29]|s[30]|s[31])-1)>>8)+1 == 1
}
func IsScalarEqual(sc1, sc2 *Scalar) bool {
tmpa := sc1.ToBytesS()
tmpb := sc2.ToBytesS()
return subtle.ConstantTimeCompare(tmpa, tmpb) == 1
}
func Compare(sca, scb *Scalar) int {
tmpa := sca.ToBytesS()
tmpb := scb.ToBytesS()
for i := Ed25519KeySize - 1; i >= 0; i-- {
if uint64(tmpa[i]) > uint64(tmpb[i]) {
return 1
}
if uint64(tmpa[i]) < uint64(tmpb[i]) {
return -1
}
}
return 0
}
func (sc *Scalar) IsZero() bool {
if sc == nil {
return false
}
return C25519.ScIsZero(&sc.key)
}
func CheckDuplicateScalarArray(arr []*Scalar) bool {
sort.Slice(arr, func(i, j int) bool {
return Compare(arr[i], arr[j]) == -1
})
for i := 0; i < len(arr)-1; i++ {
if IsScalarEqual(arr[i], arr[i+1]) == true {
return true
}
}
return false
}
func (sc *Scalar) Invert(a *Scalar) *Scalar {
if sc == nil {
sc = new(Scalar)
}
var inverse_result C25519.Key
x := a.key
reversex := Reverse(x)
bigX := new(big.Int).SetBytes(reversex[:])
reverseL := Reverse(C25519.CurveOrder()) // as speed improvements it can be made constant
bigL := new(big.Int).SetBytes(reverseL[:])
var inverse big.Int
inverse.ModInverse(bigX, bigL)
inverse_bytes := inverse.Bytes()
if len(inverse_bytes) > Ed25519KeySize {
panic("Inverse cannot be more than Ed25519KeySize bytes in this domain")
}
for i, j := 0, len(inverse_bytes)-1; i < j; i, j = i+1, j-1 {
inverse_bytes[i], inverse_bytes[j] = inverse_bytes[j], inverse_bytes[i]
}
copy(inverse_result[:], inverse_bytes[:]) // copy the bytes as they should be
sc.key = inverse_result
return sc
}
func Reverse(x C25519.Key) (result C25519.Key) {
result = x
// A key is in little-endian, but the big package wants the bytes in
// big-endian, so Reverse them.
blen := len(x) // its hardcoded 32 bytes, so why do len but lets do it
for i := 0; i < blen/2; i++ {
result[i], result[blen-1-i] = result[blen-1-i], result[i]
}
return
}
func d2h(val uint64) *C25519.Key {
key := new(C25519.Key)
for i := 0; val > 0; i++ {
key[i] = byte(val & 0xFF)
val /= 256
}
return key
}