-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfrrepr.go
249 lines (217 loc) · 4.71 KB
/
frrepr.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 bls
import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"math/bits"
)
// FRRepr represents a uint256.
type FRRepr [4]uint64
// IsOdd checks if the FRRepr is odd.
func (f FRRepr) IsOdd() bool {
return f[0]&1 == 1
}
// IsEven checks if the FRRepr is even.
func (f FRRepr) IsEven() bool {
return f[0]&1 == 0
}
// IsZero checks if the FRRepr is zero.
func (f FRRepr) IsZero() bool {
for _, f := range f {
if f != 0 {
return false
}
}
return true
}
// NewFRRepr creates a new number given a uint64.
func NewFRRepr(n uint64) *FRRepr {
return &FRRepr{n, 0, 0, 0}
}
// Rsh shifts the FRRepr right by a certain number of bits.
func (f *FRRepr) Rsh(n uint) {
if n >= 64*4 {
out := NewFRRepr(0)
*f = *out
return
}
for n >= 64 {
t := uint64(0)
for i := 3; i >= 0; i-- {
t, f[i] = f[i], t
}
n -= 64
}
if n > 0 {
t := uint64(0)
for i := 3; i >= 0; i-- {
t2 := f[i] << (64 - n)
f[i] >>= n
f[i] |= t
t = t2
}
}
}
// Div2 divides the FRRepr by 2.
func (f *FRRepr) Div2() {
t := uint64(0)
for i := 3; i >= 0; i-- {
t2 := f[i] << 63
f[i] >>= 1
f[i] |= t
t = t2
}
}
// Mul2 multiplies the FRRepr by 2.
func (f *FRRepr) Mul2() {
last := uint64(0)
for i := 0; i < 4; i++ {
tmp := f[i] >> 63
f[i] <<= 1
f[i] |= last
last = tmp
}
}
// Lsh shifts the FRRepr left by a certain number of bits.
func (f *FRRepr) Lsh(n uint) {
if n >= 64*4 {
out := NewFRRepr(0)
*f = *out
return
}
for n >= 64 {
t := uint64(0)
for i := 0; i < 4; i++ {
t, f[i] = f[i], t
}
n -= 64
}
if n > 0 {
t := uint64(0)
for i := 0; i < 4; i++ {
t2 := f[i] >> (64 - n)
f[i] <<= n
f[i] |= t
t = t2
}
}
}
// AddNoCarry adds two FRReprs to another and does not handle
// carry.
func (f *FRRepr) AddNoCarry(g *FRRepr) {
carry := uint64(0)
for i := 0; i < 4; i++ {
f[i], carry = AddWithCarry(f[i], g[i], carry)
}
}
// SubNoBorrow subtracts two FRReprs from another and does not handle
// borrow.
func (f *FRRepr) SubNoBorrow(g *FRRepr) {
borrow := uint64(0)
for i := 0; i < 4; i++ {
f[i], borrow = SubWithBorrow(f[i], g[i], borrow)
}
}
// Equals checks if two FRRepr's are equal.
func (f *FRRepr) Equals(g *FRRepr) bool {
return f[0] == g[0] && f[1] == g[1] && f[2] == g[2] && f[3] == g[3]
}
// Cmp compares two FRRepr's
func (f *FRRepr) Cmp(g *FRRepr) int {
for i := 3; i >= 0; i-- {
if f[i] > g[i] {
return 1
} else if f[i] < g[i] {
return -1
}
}
return 0
}
// Copy copies a FRRepr to a new instance and returns it.
func (f *FRRepr) Copy() *FRRepr {
var newBytes [4]uint64
copy(newBytes[:], f[:])
newf := FRRepr(newBytes)
return &newf
}
// ToString converts the FRRepr to a string.
func (f FRRepr) String() string {
return fmt.Sprintf("%016x%016x%016x%016x", f[3], f[2], f[1], f[0])
}
// BitLen counts the number of bits the number is.
func (f FRRepr) BitLen() uint {
ret := uint(4 * 64)
for i := 3; i >= 0; i-- {
leading := uint(bits.LeadingZeros64(f[i]))
ret -= leading
if leading != 64 {
break
}
}
return ret
}
// FRReprFromBytes gets a new FRRepr from big-endian bytes.
func FRReprFromBytes(b [32]byte) *FRRepr {
m0 := binary.BigEndian.Uint64(b[0:8])
m1 := binary.BigEndian.Uint64(b[8:16])
m2 := binary.BigEndian.Uint64(b[16:24])
m3 := binary.BigEndian.Uint64(b[24:32])
return &FRRepr{m3, m2, m1, m0}
}
// Bytes gets the bytes used for an FRRepr.
func (f FRRepr) Bytes() [32]byte {
var out [32]byte
binary.BigEndian.PutUint64(out[0:8], f[3])
binary.BigEndian.PutUint64(out[8:16], f[2])
binary.BigEndian.PutUint64(out[16:24], f[1])
binary.BigEndian.PutUint64(out[24:32], f[0])
return out
}
// Bit checks if a bit is set (little-endian)
func (f FRRepr) Bit(n uint) bool {
return f[n/64]&(1<<(n%64)) != 0
}
// FRReprFromString creates a FRRepr from a string.
func FRReprFromString(s string, b uint) (*FRRepr, error) {
out, valid := new(big.Int).SetString(s, int(b))
if !valid {
return nil, errors.New("FRRepr not valid")
}
return FRReprFromBigInt(out)
}
// ToBig gets the big.Int representation of the FRRepr.
func (f FRRepr) ToBig() *big.Int {
out := big.NewInt(0)
for i := 3; i >= 0; i-- {
out.Add(out, new(big.Int).SetUint64(f[i]))
if i != 0 {
out.Lsh(out, 64)
}
}
return out
}
// FRReprFromBigInt create a FRRepr from a big.Int.
func FRReprFromBigInt(n *big.Int) (*FRRepr, error) {
if n.BitLen() > 256 || n.Sign() == -1 {
return nil, errors.New("invalid input string")
}
out := new(big.Int).Set(n)
newf := NewFRRepr(0)
i := 0
for out.Cmp(bigIntZero) != 0 {
o := new(big.Int).And(out, oneLsh64MinusOne)
newf[i] = o.Uint64()
i++
out.Rsh(out, 64)
}
return newf, nil
}
// ToFQ converts an FRRepr to an FQ.
func (f *FRRepr) ToFQ() FQRepr {
newf := NewFQRepr(f[0])
newf[1] = f[1]
newf[2] = f[2]
newf[3] = f[3]
return newf
}