Skip to content

Commit

Permalink
perf(emulated): small perf on doubleAndAdd (Consensys#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
yelhousni authored Jan 8, 2025
1 parent 8b4dc2e commit a8eb033
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions internal/stats/latest_stats.csv
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ scalar_mul_G1_bn254,bls24_315,groth16,0,0
scalar_mul_G1_bn254,bls24_317,groth16,0,0
scalar_mul_G1_bn254,bw6_761,groth16,0,0
scalar_mul_G1_bn254,bw6_633,groth16,0,0
scalar_mul_G1_bn254,bn254,plonk,220594,207103
scalar_mul_G1_bn254,bn254,plonk,220030,206539
scalar_mul_G1_bn254,bls12_377,plonk,0,0
scalar_mul_G1_bn254,bls12_381,plonk,0,0
scalar_mul_G1_bn254,bls24_315,plonk,0,0
Expand All @@ -230,7 +230,7 @@ scalar_mul_P256,bls24_315,groth16,0,0
scalar_mul_P256,bls24_317,groth16,0,0
scalar_mul_P256,bw6_761,groth16,0,0
scalar_mul_P256,bw6_633,groth16,0,0
scalar_mul_P256,bn254,plonk,294014,274427
scalar_mul_P256,bn254,plonk,293762,274175
scalar_mul_P256,bls12_377,plonk,0,0
scalar_mul_P256,bls12_381,plonk,0,0
scalar_mul_P256,bls24_315,plonk,0,0
Expand All @@ -244,7 +244,7 @@ scalar_mul_secp256k1,bls24_315,groth16,0,0
scalar_mul_secp256k1,bls24_317,groth16,0,0
scalar_mul_secp256k1,bw6_761,groth16,0,0
scalar_mul_secp256k1,bw6_633,groth16,0,0
scalar_mul_secp256k1,bn254,plonk,223354,209690
scalar_mul_secp256k1,bn254,plonk,222782,209118
scalar_mul_secp256k1,bls12_377,plonk,0,0
scalar_mul_secp256k1,bls12_381,plonk,0,0
scalar_mul_secp256k1,bls24_315,plonk,0,0
Expand Down
7 changes: 4 additions & 3 deletions std/algebra/emulated/sw_emulated/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ func (c *Curve[B, S]) doubleAndAdd(p, q *AffinePoint[B]) *AffinePoint[B] {
mone := c.baseApi.NewElement(-1)
// compute λ1 = (q.y-p.y)/(q.x-p.x)
yqyp := c.baseApi.Sub(&q.Y, &p.Y)
xqxp := c.baseApi.Sub(&q.X, &p.X)
xpn := c.baseApi.Neg(&p.X)
xqxp := c.baseApi.Add(&q.X, xpn)
λ1 := c.baseApi.Div(yqyp, xqxp)

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

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

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

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

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

0 comments on commit a8eb033

Please sign in to comment.