mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
use gnark API for Add
This commit is contained in:
parent
a65fe31842
commit
8eecf9fdad
2 changed files with 1 additions and 92 deletions
|
|
@ -20,25 +20,7 @@ type G1 struct {
|
||||||
|
|
||||||
// Add adds `a` and `b` together storing the result in `g`
|
// Add adds `a` and `b` together storing the result in `g`
|
||||||
func (g *G1) Add(a, b *G1) {
|
func (g *G1) Add(a, b *G1) {
|
||||||
// TODO(Decision to be made): There are three ways to
|
g.inner.Add(&a.inner, &b.inner)
|
||||||
// TODO do this addition. Each with different performance
|
|
||||||
// TODO: characteristics.
|
|
||||||
//
|
|
||||||
// Option 1: This just calls a method in gnark
|
|
||||||
// g.inner.Add(&a.inner, &b.inner)
|
|
||||||
|
|
||||||
// Option 2: This calls multiple methods in gnark
|
|
||||||
// but is faster.
|
|
||||||
//
|
|
||||||
// var res bn254.G1Jac
|
|
||||||
// res.FromAffine(&a.inner)
|
|
||||||
// res.AddMixed(&b.inner)
|
|
||||||
// g.inner.FromJacobian(&res)
|
|
||||||
|
|
||||||
// Option 3: This calls a method that I created that
|
|
||||||
// we can upstream to gnark.
|
|
||||||
// This should be the fastest, I can write the same for G2
|
|
||||||
g.addAffine(a, b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult computes the scalar multiplication between `a` and
|
// ScalarMult computes the scalar multiplication between `a` and
|
||||||
|
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
||||||
package bn256
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// This is just the addition formula
|
|
||||||
// but given we know that we do not need Jacobian
|
|
||||||
// coordinates, we use the naive implementation.
|
|
||||||
//
|
|
||||||
// Ideally, we push this into gnark
|
|
||||||
func (g *G1) addAffine(a_, b_ *G1) {
|
|
||||||
|
|
||||||
// Get the gnark specific points
|
|
||||||
var a = a_.inner
|
|
||||||
var b = b_.inner
|
|
||||||
|
|
||||||
// If a is 0, then return b
|
|
||||||
if a.IsInfinity() {
|
|
||||||
g.inner.Set(&b)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// If b is 0, then return a
|
|
||||||
if b.IsInfinity() {
|
|
||||||
g.inner.Set(&a)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a == -b, then return 0
|
|
||||||
g.inner.Neg(&b)
|
|
||||||
if a.Equal(&g.inner) {
|
|
||||||
g.inner.X.SetZero()
|
|
||||||
g.inner.Y.SetZero()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute lambda based on whether we
|
|
||||||
// are doing a point addition or a point doubling
|
|
||||||
//
|
|
||||||
// Check if points are equal
|
|
||||||
var pointsAreEqual = a.Equal(&b)
|
|
||||||
|
|
||||||
var denominator fp.Element
|
|
||||||
var lambda fp.Element
|
|
||||||
|
|
||||||
// If a == b, then we need to compute lambda for double
|
|
||||||
// else we need to compute lambda for addition
|
|
||||||
if pointsAreEqual {
|
|
||||||
// Compute numerator
|
|
||||||
lambda.Square(&a.X)
|
|
||||||
fp.MulBy3(&lambda)
|
|
||||||
|
|
||||||
denominator.Add(&a.Y, &a.Y)
|
|
||||||
} else {
|
|
||||||
// Compute numerator
|
|
||||||
lambda.Sub(&b.Y, &a.Y)
|
|
||||||
|
|
||||||
denominator.Sub(&b.X, &a.X)
|
|
||||||
}
|
|
||||||
denominator.Inverse(&denominator)
|
|
||||||
lambda.Mul(&lambda, &denominator)
|
|
||||||
|
|
||||||
// Compute x_3 as lambda^2 - a_x - b_x
|
|
||||||
g.inner.X.Square(&lambda)
|
|
||||||
g.inner.X.Sub(&g.inner.X, &a.X)
|
|
||||||
g.inner.X.Sub(&g.inner.X, &b.X)
|
|
||||||
|
|
||||||
// Compute y as lambda * (a_x - x_3) - a_y
|
|
||||||
g.inner.Y.Sub(&a.X, &g.inner.X)
|
|
||||||
g.inner.Y.Mul(&g.inner.Y, &lambda)
|
|
||||||
g.inner.Y.Sub(&g.inner.Y, &a.Y)
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue