Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Pectra BLS12-381 elliptic curve precompiles #1447

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d179d71
feat(pectra/precompiles): BLS12_G1ADD and BLS12_PAIRING_CHECK
yelhousni Mar 13, 2025
5c58de3
feat(pectra/precompiles): BLS12_G2ADD
yelhousni Mar 13, 2025
90ad595
perf(pectra/precompiles): include G2 membership in BLS12_PAIR
yelhousni Mar 13, 2025
b8f9004
test: update stats
yelhousni Mar 14, 2025
e748c1f
feat: add G2 memebrship to BLS12_G2ADD
yelhousni Mar 14, 2025
e5886ea
feat(pectra/precompiles): BLS12_G1MSM
yelhousni Mar 14, 2025
13f23c3
refactor: BLS12 precompiles to include G1/2 membership checks
yelhousni Mar 17, 2025
225d6f6
perf(pectra): optimize MSM G1
yelhousni Mar 17, 2025
ee8f46a
feat: constants isogeny ok
ThomasPiellard Mar 18, 2025
f8eef37
feat(pectra): add BLS12_G2MSM
yelhousni Mar 18, 2025
7ea03d5
refactor: clean code
yelhousni Mar 18, 2025
b9e729e
feat: mul by z ok
ThomasPiellard Mar 18, 2025
c9f6df4
style(pectra): rename methods
yelhousni Mar 18, 2025
0cd0f62
feat: hint ok
ThomasPiellard Mar 19, 2025
efe3928
Merge branch 'pectra/precompiles' of github.com:Consensys/gnark into …
ThomasPiellard Mar 19, 2025
61d3d5f
feat: move map-to-g1 to swbls12381 package
ThomasPiellard Mar 20, 2025
f215ee1
feat: main function ok
ThomasPiellard Mar 20, 2025
c2dcd18
feat: isogeny ok
ThomasPiellard Mar 20, 2025
f1566ea
feat: fixed api
ThomasPiellard Mar 21, 2025
8a072b8
fix: missing addAssign clear cofactor
ThomasPiellard Mar 21, 2025
0001898
fix: Clear cofactor ok
ThomasPiellard Mar 21, 2025
16ba968
feat: correct values, but test fails
ThomasPiellard Mar 23, 2025
28fed0b
bug: nonnative limb checking fails
ThomasPiellard Mar 24, 2025
d1bdd70
clean: removed some dereferencing
ThomasPiellard Mar 24, 2025
1e61775
perf(pectra): optimize G2 scalar mul with GLV
yelhousni Mar 24, 2025
5f36147
fix(pectra): make linter happy
yelhousni Mar 24, 2025
b32c12d
Merge branch 'master' into pectra/precompiles
yelhousni Mar 24, 2025
581ddb4
fix(pectra): make linter happier
yelhousni Mar 24, 2025
97aeba5
fix(pectra): make linter much happier
yelhousni Mar 24, 2025
506a359
fix: avoid dereferencing non-native values
ivokub Mar 25, 2025
fb37104
feat: hint is constrained
ThomasPiellard Mar 25, 2025
abe3cbb
feat: added full precompile
ThomasPiellard Mar 25, 2025
a7ef40d
feat: corrected name
ThomasPiellard Mar 25, 2025
c36c352
fix: replace OR with XOR
ThomasPiellard Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/stats/latest_stats.csv
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ pairing_bls12377,bls24_315,plonk,0,0
pairing_bls12377,bls24_317,plonk,0,0
pairing_bls12377,bw6_761,plonk,51280,51280
pairing_bls12377,bw6_633,plonk,0,0
pairing_bls12381,bn254,groth16,947528,1567714
pairing_bls12381,bn254,groth16,949444,1570802
pairing_bls12381,bls12_377,groth16,0,0
pairing_bls12381,bls12_381,groth16,0,0
pairing_bls12381,bls24_315,groth16,0,0
pairing_bls12381,bls24_317,groth16,0,0
pairing_bls12381,bw6_761,groth16,0,0
pairing_bls12381,bw6_633,groth16,0,0
pairing_bls12381,bn254,plonk,3642638,3233378
pairing_bls12381,bn254,plonk,3649555,3239775
pairing_bls12381,bls12_377,plonk,0,0
pairing_bls12381,bls12_381,plonk,0,0
pairing_bls12381,bls24_315,plonk,0,0
Expand Down
8 changes: 8 additions & 0 deletions std/algebra/emulated/fields_bls12381/e2.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ func (e Ext2) AssertIsEqual(x, y *E2) {
e.fp.AssertIsEqual(&x.A1, &y.A1)
}

func (e Ext2) IsEqual(x, y *E2) frontend.Variable {
xDiff := e.fp.Sub(&x.A0, &y.A0)
yDiff := e.fp.Sub(&x.A1, &y.A1)
xIsZero := e.fp.IsZero(xDiff)
yIsZero := e.fp.IsZero(yDiff)
return e.api.And(xIsZero, yIsZero)
}

func FromE2(y *bls12381.E2) E2 {
return E2{
A0: emulated.ValueOf[emulated.BLS12381Fp](y.A0),
Expand Down
60 changes: 60 additions & 0 deletions std/algebra/emulated/sw_bls12381/g1.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewG1Affine(v bls12381.G1Affine) G1Affine {
}

type G1 struct {
api frontend.API
curveF *emulated.Field[BaseField]
w *emulated.Element[BaseField]
}
Expand All @@ -39,11 +40,21 @@ func NewG1(api frontend.API) (*G1, error) {
}
w := emulated.ValueOf[BaseField]("4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436")
return &G1{
api: api,
curveF: ba,
w: &w,
}, nil
}

func (g1 G1) neg(p *G1Affine) *G1Affine {
xr := &p.X
yr := g1.curveF.Neg(&p.Y)
return &G1Affine{
X: *xr,
Y: *yr,
}
}

func (g1 *G1) phi(q *G1Affine) *G1Affine {
x := g1.curveF.Mul(&q.X, g1.w)

Expand Down Expand Up @@ -157,6 +168,55 @@ func (g1 *G1) scalarMulBySeedSquare(q *G1Affine) *G1Affine {
return z
}

func (g1 *G1) computeCurveEquation(P *G1Affine) (left, right *baseEl) {
// Curve: Y² == X³ + aX + b, where a=0 and b=4
// (X,Y) ∈ {Y² == X³ + aX + b} U (0,0)

// if P=(0,0) we assign b=0 otherwise 4, and continue
selector := g1.api.And(g1.curveF.IsZero(&P.X), g1.curveF.IsZero(&P.Y))
four := emulated.ValueOf[BaseField]("4")
b := g1.curveF.Select(selector, g1.curveF.Zero(), &four)

left = g1.curveF.Mul(&P.Y, &P.Y)
right = g1.curveF.Mul(&P.X, &P.X)
right = g1.curveF.Mul(right, &P.X)
right = g1.curveF.Add(right, b)
return left, right
}

func (g1 *G1) AssertIsOnCurve(P *G1Affine) {
left, right := g1.computeCurveEquation(P)
g1.curveF.AssertIsEqual(left, right)
}

func (g1 *G1) AssertIsOnG1(P *G1Affine) {
// 1- Check P is on the curve
g1.AssertIsOnCurve(P)

// 2- Check P has the right subgroup order
// [x²]ϕ(P)
phiP := g1.phi(P)
_P := g1.scalarMulBySeedSquare(phiP)
_P = g1.neg(_P)

// [r]Q == 0 <==> P = -[x²]ϕ(P)
g1.AssertIsEqual(_P, P)
}

// AssertIsEqual asserts that p and q are the same point.
func (g1 *G1) AssertIsEqual(p, q *G1Affine) {
g1.curveF.AssertIsEqual(&p.X, &q.X)
g1.curveF.AssertIsEqual(&p.Y, &q.Y)
}

func (g1 *G1) IsEqual(p, q *G1Affine) frontend.Variable {
xDiff := g1.curveF.Sub(&p.X, &q.X)
yDiff := g1.curveF.Sub(&p.Y, &q.Y)
xIsZero := g1.curveF.IsZero(xDiff)
yIsZero := g1.curveF.IsZero(yDiff)
return g1.api.And(xIsZero, yIsZero)
}

// NewScalar allocates a witness from the native scalar and returns it.
func NewScalar(v fr_bls12381.Element) Scalar {
return emulated.ValueOf[ScalarField](v)
Expand Down
Loading
Loading