Skip to content

Commit a8eb033

Browse files
authored
perf(emulated): small perf on doubleAndAdd (#1386)
1 parent 8b4dc2e commit a8eb033

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

internal/stats/latest_stats.csv

+3-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ scalar_mul_G1_bn254,bls24_315,groth16,0,0
216216
scalar_mul_G1_bn254,bls24_317,groth16,0,0
217217
scalar_mul_G1_bn254,bw6_761,groth16,0,0
218218
scalar_mul_G1_bn254,bw6_633,groth16,0,0
219-
scalar_mul_G1_bn254,bn254,plonk,220594,207103
219+
scalar_mul_G1_bn254,bn254,plonk,220030,206539
220220
scalar_mul_G1_bn254,bls12_377,plonk,0,0
221221
scalar_mul_G1_bn254,bls12_381,plonk,0,0
222222
scalar_mul_G1_bn254,bls24_315,plonk,0,0
@@ -230,7 +230,7 @@ scalar_mul_P256,bls24_315,groth16,0,0
230230
scalar_mul_P256,bls24_317,groth16,0,0
231231
scalar_mul_P256,bw6_761,groth16,0,0
232232
scalar_mul_P256,bw6_633,groth16,0,0
233-
scalar_mul_P256,bn254,plonk,294014,274427
233+
scalar_mul_P256,bn254,plonk,293762,274175
234234
scalar_mul_P256,bls12_377,plonk,0,0
235235
scalar_mul_P256,bls12_381,plonk,0,0
236236
scalar_mul_P256,bls24_315,plonk,0,0
@@ -244,7 +244,7 @@ scalar_mul_secp256k1,bls24_315,groth16,0,0
244244
scalar_mul_secp256k1,bls24_317,groth16,0,0
245245
scalar_mul_secp256k1,bw6_761,groth16,0,0
246246
scalar_mul_secp256k1,bw6_633,groth16,0,0
247-
scalar_mul_secp256k1,bn254,plonk,223354,209690
247+
scalar_mul_secp256k1,bn254,plonk,222782,209118
248248
scalar_mul_secp256k1,bls12_377,plonk,0,0
249249
scalar_mul_secp256k1,bls12_381,plonk,0,0
250250
scalar_mul_secp256k1,bls24_315,plonk,0,0

std/algebra/emulated/sw_emulated/point.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ func (c *Curve[B, S]) doubleAndAdd(p, q *AffinePoint[B]) *AffinePoint[B] {
376376
mone := c.baseApi.NewElement(-1)
377377
// compute λ1 = (q.y-p.y)/(q.x-p.x)
378378
yqyp := c.baseApi.Sub(&q.Y, &p.Y)
379-
xqxp := c.baseApi.Sub(&q.X, &p.X)
379+
xpn := c.baseApi.Neg(&p.X)
380+
xqxp := c.baseApi.Add(&q.X, xpn)
380381
λ1 := c.baseApi.Div(yqyp, xqxp)
381382

382383
// compute x2 = λ1²-p.x-q.x
@@ -386,15 +387,15 @@ func (c *Curve[B, S]) doubleAndAdd(p, q *AffinePoint[B]) *AffinePoint[B] {
386387

387388
// compute -λ2 = λ1+2*p.y/(x2-p.x)
388389
ypyp := c.baseApi.MulConst(&p.Y, big.NewInt(2))
389-
x2xp := c.baseApi.Sub(x2, &p.X)
390+
x2xp := c.baseApi.Add(x2, xpn)
390391
λ2 := c.baseApi.Div(ypyp, x2xp)
391392
λ2 = c.baseApi.Add(λ1, λ2)
392393

393394
// compute x3 = (-λ2)²-p.x-x2
394395
x3 := c.baseApi.Eval([][]*emulated.Element[B]{{λ2, λ2}, {mone, &p.X}, {mone, x2}}, []int{1, 1, 1})
395396

396397
// compute y3 = -λ2*(x3 - p.x)-p.y
397-
y3 := c.baseApi.Eval([][]*emulated.Element[B]{{λ2, c.baseApi.Sub(x3, &p.X)}, {mone, &p.Y}}, []int{1, 1})
398+
y3 := c.baseApi.Eval([][]*emulated.Element[B]{{λ2, c.baseApi.Add(x3, xpn)}, {mone, &p.Y}}, []int{1, 1})
398399

399400
return &AffinePoint[B]{
400401
X: *c.baseApi.Reduce(x3),

0 commit comments

Comments
 (0)