forked from drbh/zkproofs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p256.go
252 lines (227 loc) · 5.08 KB
/
p256.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
/*
Encapsulates secp256k1 elliptic curve.
*/
package zkproofs
import (
"math/big"
"github.com/MizukiSonoko/zkproofs/go-ethereum/crypto/secp256k1"
"github.com/MizukiSonoko/zkproofs/go-ethereum/byteconversion"
"errors"
"bytes"
"strconv"
"crypto/sha256"
)
var (
CURVE = secp256k1.S256()
GX = CURVE.Gx
GY = CURVE.Gy
)
/*
Elliptic Curve Point struct.
*/
type p256 struct {
X, Y *big.Int
}
/*
IsZero returns true if and only if the elliptic curve point is the point at infinity.
*/
func (p *p256) IsZero() bool {
c1 := (p.X == nil || p.Y == nil)
if !c1 {
z := new(big.Int).SetInt64(0)
return p.X.Cmp(z)==0 && p.Y.Cmp(z)==0
}
return true
}
/*
Neg returns the inverse of the given elliptic curve point.
*/
func (p *p256) Neg(a *p256) (*p256) {
// (X, Y) -> (X, X + Y)
if (a.IsZero()) {
return p.SetInfinity()
}
one := new(big.Int).SetInt64(1)
mone := new(big.Int).Sub(CURVE.N, one)
p.ScalarMult(p, mone)
return p
}
/*
Input points must be distinct
*/
func (p *p256) Add(a,b *p256) (*p256) {
if (a.IsZero()) {
p.X = b.X
p.Y = b.Y
return p
} else if (b.IsZero()) {
p.X = b.X
p.Y = b.Y
return p
}
resx, resy := CURVE.Add(a.X, a.Y, b.X, b.Y)
p.X = resx
p.Y = resy
return p
}
/*
Double returns 2*P, where P is the given elliptic curve point.
*/
func (p *p256) Double(a *p256) (*p256) {
if (a.IsZero()) {
return p.SetInfinity()
}
resx, resy := CURVE.Double(a.X, a.Y)
p.X = resx
p.Y = resy
return p
}
/*
ScalarMul encapsulates the scalar Multiplication Algorithm from secp256k1.
*/
func (p *p256) ScalarMult(a *p256, n *big.Int) (*p256) {
if (a.IsZero()) {
return p.SetInfinity()
}
cmp := n.Cmp(big.NewInt(0))
if cmp == 0 {
return p.SetInfinity()
}
n = Mod(n, CURVE.N)
bn := n.Bytes()
resx, resy := CURVE.ScalarMult(a.X, a.Y, bn)
p.X = resx
p.Y = resy
return p
}
/*
ScalarBaseMult returns the Scalar Multiplication by the base generator.
*/
func (p *p256) ScalarBaseMult(n *big.Int) (*p256) {
cmp := n.Cmp(big.NewInt(0))
if cmp == 0 {
return p.SetInfinity()
}
n = Mod(n, CURVE.N)
bn := n.Bytes()
resx, resy := CURVE.ScalarBaseMult(bn)
p.X = resx
p.Y = resy
return p
}
/*
Multiply actually is reponsible for the addition of elliptic curve points.
The name here is to maintain compatibility with bn256 interface.
This algorithm verifies if the given elliptic curve points are equal, in which case it
returns the result of Double function, otherwise it returns the result of Add function.
*/
func (p *p256) Multiply(a,b *p256) (*p256) {
if (a.IsZero()) {
p.X = b.X
p.Y = b.Y
return p
} else if (b.IsZero()) {
p.X = a.X
p.Y = a.Y
return p
}
if (a.X.Cmp(b.X)==0 && a.Y.Cmp(b.Y)==0) {
resx, resy := CURVE.Double(a.X, a.Y)
p.X = resx
p.Y = resy
return p
}
resx, resy := CURVE.Add(a.X, a.Y, b.X, b.Y)
p.X = resx
p.Y = resy
return p
}
/*
SetInfinity sets the given elliptic curve point to the point at infinity.
*/
func (p *p256) SetInfinity() (*p256) {
p.X = nil
p.Y = nil
return p
}
/*
String returns the readable representation of the given elliptic curve point, i.e.
the tuple formed by X and Y coordinates.
*/
func (p *p256) String() string {
return "p256(" + p.X.String() + "," + p.Y.String() + ")"
}
/*
MapToGroup is a hash function that returns a valid elliptic curve point given as
input a string. It is also known as hash-to-point and is used to obtain a generator
that has no discrete logarithm known relation, thus addressing the concept of
NUMS (nothing up my sleeve).
This implementation is based on the paper:
Short signatures from the Weil pairing
Boneh, Lynn and Shacham
Journal of Cryptology, September 2004, Volume 17, Issue 4, pp 297–319
*/
func MapToGroup(m string) (*p256, error) {
var (
i int
buffer bytes.Buffer
)
i = 0
for i < 256 {
buffer.Reset()
buffer.WriteString(strconv.Itoa(i))
buffer.WriteString(m)
x, _ := HashToInt(buffer)
x = Mod(x, CURVE.P)
fx, _ := F(x)
fx = Mod(fx, CURVE.P)
y := fx.ModSqrt(fx, CURVE.P)
if (y != nil) {
p := &p256{X:x, Y:y}
if (p.IsOnCurve() && !p.IsZero()) {
return p, nil
}
}
i = i + 1
}
return nil, errors.New("Failed to Hash-to-point.")
}
/*
F receives a big integer x as input and return x^3 + 7 mod ORDER.
*/
func F(x *big.Int) (*big.Int, error) {
// Compute x^2
x3p7 := Multiply(x, x)
x3p7 = Mod(x3p7, CURVE.P)
// Compute x^3
x3p7 = Multiply(x3p7, x)
x3p7 = Mod(x3p7, CURVE.P)
// Compute X^3 + 7
x3p7 = Add(x3p7, new(big.Int).SetInt64(7))
x3p7 = Mod(x3p7, CURVE.P)
return x3p7, nil
}
/*
Hash is responsible for the computing a Zp element given the input string.
*/
func HashToInt(b bytes.Buffer) (*big.Int, error) {
digest := sha256.New()
digest.Write(b.Bytes())
output := digest.Sum(nil)
tmp := output[0: len(output)]
return byteconversion.FromByteArray(tmp)
}
/*
IsOnCurve returns TRUE if and only if p has coordinates X and Y that satisfy the
Elliptic Curve equation: y^2 = x^3 + 7.
*/
func (p *p256) IsOnCurve() bool {
// y² = x³ + 7
y2 := new(big.Int).Mul(p.Y, p.Y)
y2.Mod(y2, CURVE.P)
x3 := new(big.Int).Mul(p.X, p.X)
x3.Mul(x3, p.X)
x3.Add(x3, new(big.Int).SetInt64(7))
x3.Mod(x3, CURVE.P)
return x3.Cmp(y2) == 0
}