Skip to content

Commit

Permalink
feat: use batch invert
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasPiellard committed Jan 13, 2025
1 parent f734e9d commit 08bfbbd
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions backend/plonk/bn254/prove.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2020-2025 Consensys Software Inc.
// Licensed under the Apache License, Version 2.0. See the LICENSE file for details.

// Code generated by gnark DO NOT EDIT

package plonk

import (
Expand Down Expand Up @@ -172,6 +170,7 @@ type instance struct {
blindedZ []fr.Element // blindedZ is the blinded version of Z
quotientShardsRandomizers [2]fr.Element // random elements for blinding the shards of the quotient

precomputedDenominators []fr.Element // stores the denominators of the Lagrange polynomials
linearizedPolynomial []fr.Element
linearizedPolynomialDigest kzg.Digest

Expand Down Expand Up @@ -376,12 +375,11 @@ func (s *instance) completeQk() error {
}

// evacomputeLagrangeOneOnCosetluate computes 1/n (x**n-1)/(x-1) on coset*ωⁱ
func (s *instance) computeLagrangeOneOnCoset(cosetExpMinusOne, x fr.Element) fr.Element {
func (s *instance) computeLagrangeOneOnCoset(cosetExpMinusOne fr.Element, index int) fr.Element {
var res, one fr.Element
one.SetOne()
res.Mul(&cosetExpMinusOne, &s.domain0.CardinalityInv)
one.Sub(&x, &one)
res.Div(&res, &one)
res.Mul(&cosetExpMinusOne, &s.domain0.CardinalityInv).
Mul(&res, &s.precomputedDenominators[index])
return res
}

Expand Down Expand Up @@ -823,7 +821,7 @@ func (s *instance) computeNumerator() (*iop.Polynomial, error) {
one.SetOne()
bn := big.NewInt(int64(n))

orderingConstraintAndRatioLocalConstrait := func(index int, u ...fr.Element) fr.Element {
orderingConstraint := func(index int, u ...fr.Element) fr.Element {

gamma := s.gamma

Expand All @@ -850,9 +848,8 @@ func (s *instance) computeNumerator() (*iop.Polynomial, error) {

localConstraint := func(index int, u ...fr.Element) fr.Element {
// local constraint
var res, lone, cosetOmegai fr.Element
cosetOmegai.Mul(&coset, &twiddles0[index])
lone = s.computeLagrangeOneOnCoset(cosetExponentiatedToNMinusOne, cosetOmegai)
var res, lone fr.Element
lone = s.computeLagrangeOneOnCoset(cosetExponentiatedToNMinusOne, index)
res.SetOne()
res.Sub(&u[id_Z], &res).Mul(&res, &lone)

Expand Down Expand Up @@ -899,7 +896,7 @@ func (s *instance) computeNumerator() (*iop.Polynomial, error) {
u[id_ZS].Add(&u[id_ZS], &y)

a := gateConstraint(u...)
b := orderingConstraintAndRatioLocalConstrait(index, u...)
b := orderingConstraint(index, u...)
c := localConstraint(index, u...)
c.Mul(&c, &s.alpha).Add(&c, &b).Mul(&c, &s.alpha).Add(&c, &a)
return c
Expand All @@ -916,12 +913,21 @@ func (s *instance) computeNumerator() (*iop.Polynomial, error) {
m := uint64(s.domain1.Cardinality)
mm := uint64(64 - bits.TrailingZeros64(m))

s.precomputedDenominators = make([]fr.Element, s.domain0.Cardinality)

for i := 0; i < rho; i++ {

coset.Mul(&coset, &shifters[i])
cosetExponentiatedToNMinusOne.Exp(coset, bn).
Sub(&cosetExponentiatedToNMinusOne, &one)

for j := 0; j < int(s.domain0.Cardinality); j++ {
s.precomputedDenominators[j].
Mul(&coset, &twiddles0[j]).
Sub(&s.precomputedDenominators[j], &one)
}
s.precomputedDenominators = fr.BatchInvert(s.precomputedDenominators)

// bl <- bl *( (s*ωⁱ)ⁿ-1 )s
for _, q := range s.bp {
cq := q.Coefficients()
Expand Down Expand Up @@ -973,6 +979,7 @@ func (s *instance) computeNumerator() (*iop.Polynomial, error) {
})

wgBuf.Wait()

if _, err := iop.Evaluate(
allConstraints,
buf,
Expand Down

0 comments on commit 08bfbbd

Please sign in to comment.