Skip to content

Commit 887902e

Browse files
authored
crypto/cloudflare/bn256: fix in-place addition and unmarshalling (ethereum#23419)
1 parent d162142 commit 887902e

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

crypto/bn256/cloudflare/bn256_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ func TestTripartiteDiffieHellman(t *testing.T) {
9292
}
9393
}
9494

95+
func TestG2SelfAddition(t *testing.T) {
96+
s, _ := rand.Int(rand.Reader, Order)
97+
p := new(G2).ScalarBaseMult(s)
98+
99+
if !p.p.IsOnCurve() {
100+
t.Fatal("p isn't on curve")
101+
}
102+
m := p.Add(p, p).Marshal()
103+
if _, err := p.Unmarshal(m); err != nil {
104+
t.Fatalf("p.Add(p, p) ∉ G₂: %v", err)
105+
}
106+
}
107+
95108
func BenchmarkG1(b *testing.B) {
96109
x, _ := rand.Int(rand.Reader, Order)
97110
b.ResetTimer()

crypto/bn256/cloudflare/curve.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ func (c *curvePoint) Double(a *curvePoint) {
171171
gfpAdd(t, d, d)
172172
gfpSub(&c.x, f, t)
173173

174+
gfpMul(&c.z, &a.y, &a.z)
175+
gfpAdd(&c.z, &c.z, &c.z)
176+
174177
gfpAdd(t, C, C)
175178
gfpAdd(t2, t, t)
176179
gfpAdd(t, t2, t2)
177180
gfpSub(&c.y, d, &c.x)
178181
gfpMul(t2, e, &c.y)
179182
gfpSub(&c.y, t2, t)
180-
181-
gfpMul(t, &a.y, &a.z)
182-
gfpAdd(&c.z, t, t)
183183
}
184184

185185
func (c *curvePoint) Mul(a *curvePoint, scalar *big.Int) {

crypto/bn256/cloudflare/gfp.go

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func (e *gfP) Marshal(out []byte) {
6161
func (e *gfP) Unmarshal(in []byte) error {
6262
// Unmarshal the bytes into little endian form
6363
for w := uint(0); w < 4; w++ {
64+
e[3-w] = 0
6465
for b := uint(0); b < 8; b++ {
6566
e[3-w] += uint64(in[8*w+b]) << (56 - 8*b)
6667
}

crypto/bn256/cloudflare/twist.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ func (c *twistPoint) Double(a *twistPoint) {
150150
t.Add(d, d)
151151
c.x.Sub(f, t)
152152

153+
c.z.Mul(&a.y, &a.z)
154+
c.z.Add(&c.z, &c.z)
155+
153156
t.Add(C, C)
154157
t2.Add(t, t)
155158
t.Add(t2, t2)
156159
c.y.Sub(d, &c.x)
157160
t2.Mul(e, &c.y)
158161
c.y.Sub(t2, t)
159-
160-
t.Mul(&a.y, &a.z)
161-
c.z.Add(t, t)
162162
}
163163

164164
func (c *twistPoint) Mul(a *twistPoint, scalar *big.Int) {

0 commit comments

Comments
 (0)