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

chore: Use monomial form when computing the quotient #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
21 changes: 20 additions & 1 deletion internal/kzg/kzg_prove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@
import (
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/crate-crypto/go-eth-kzg/internal/domain"
"github.com/crate-crypto/go-eth-kzg/internal/poly"
)

func Open(domain *domain.Domain, polyCoeff []fr.Element, evaluationPoint fr.Element, ck *CommitKey, numGoRoutines int) (OpeningProof, error) {

Check failure on line 9 in internal/kzg/kzg_prove.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)

outputPoint := poly.PolyEval(polyCoeff, evaluationPoint)

quotient := poly.DividePolyByXminusA(polyCoeff, evaluationPoint)

comm, err := ck.Commit(quotient, 0)
if err != nil {
return OpeningProof{}, nil

Check failure on line 17 in internal/kzg/kzg_prove.go

View workflow job for this annotation

GitHub Actions / Lint

error is not nil (line 15) but it returns nil (nilerr)
}

Comment on lines +10 to +19
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not deleted the code, we no longer need but this is comparatively simpler than what we were doing before

return OpeningProof{
QuotientCommitment: *comm,
InputPoint: evaluationPoint,
ClaimedValue: outputPoint,
}, nil
}

// Open verifies that a polynomial f(x) when evaluated at a point `z` is equal to `f(z)`
//
// numGoRoutines is used to configure the amount of concurrency needed. Setting this
// value to a negative number or 0 will make it default to the number of CPUs.
//
// [compute_kzg_proof_impl]: https://github.com/ethereum/consensus-specs/blob/017a8495f7671f5fff2075a9bfc9238c1a0982f8/specs/deneb/polynomial-commitments.md#compute_kzg_proof_impl
func Open(domain *domain.Domain, p Polynomial, evaluationPoint fr.Element, ck *CommitKey, numGoRoutines int) (OpeningProof, error) {
func Open_(domain *domain.Domain, p Polynomial, evaluationPoint fr.Element, ck *CommitKey, numGoRoutines int) (OpeningProof, error) {
if len(p) == 0 || len(p) > len(ck.G1) {
return OpeningProof{}, ErrInvalidPolynomialSize
}
Expand Down
6 changes: 3 additions & 3 deletions internal/kzg/kzg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

func TestProofVerifySmoke(t *testing.T) {
domain := domain.NewDomain(4)
srs, _ := newLagrangeSRSInsecure(*domain, big.NewInt(1234))
srs, _ := newMonomialSRSInsecure(*domain, big.NewInt(1234))

// polynomial in lagrange form
// polynomial in monomial form
poly := Polynomial{fr.NewElement(2), fr.NewElement(3), fr.NewElement(4), fr.NewElement(5)}

comm, _ := srs.CommitKey.Commit(poly, 0)
Expand All @@ -29,7 +29,7 @@ func TestProofVerifySmoke(t *testing.T) {

func TestBatchVerifySmoke(t *testing.T) {
domain := domain.NewDomain(4)
srs, _ := newLagrangeSRSInsecure(*domain, big.NewInt(1234))
srs, _ := newMonomialSRSInsecure(*domain, big.NewInt(1234))

numProofs := 10
commitments := make([]Commitment, 0, numProofs)
Expand Down
11 changes: 9 additions & 2 deletions prove.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goethkzg

import (
"github.com/crate-crypto/go-eth-kzg/internal/domain"
"github.com/crate-crypto/go-eth-kzg/internal/kzg"
)

Expand Down Expand Up @@ -62,8 +63,11 @@ func (c *Context) ComputeBlobKZGProof(blob *Blob, blobCommitment KZGCommitment,
// 2. Compute Fiat-Shamir challenge
evaluationChallenge := computeChallenge(blob, blobCommitment)

domain.BitReverse(polynomial)
polyCoeff := c.domain.IfftFr(polynomial)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This additional IFFT costs 1ms which is not that much compared to the MSM


// 3. Create opening proof
openingProof, err := kzg.Open(c.domain, polynomial, evaluationChallenge, c.commitKeyLagrange, numGoRoutines)
openingProof, err := kzg.Open(c.domain, polyCoeff, evaluationChallenge, c.commitKeyMonomial, numGoRoutines)
if err != nil {
return KZGProof{}, err
}
Expand Down Expand Up @@ -95,8 +99,11 @@ func (c *Context) ComputeKZGProof(blob *Blob, inputPointBytes Scalar, numGoRouti
return KZGProof{}, [32]byte{}, err
}

domain.BitReverse(polynomial)
polyCoeff := c.domain.IfftFr(polynomial)

// 2. Create opening proof
openingProof, err := kzg.Open(c.domain, polynomial, inputPoint, c.commitKeyLagrange, numGoRoutines)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to get rid of commitKeyLagrange entirely, if we can stomach the 1ms ifft

openingProof, err := kzg.Open(c.domain, polyCoeff, inputPoint, c.commitKeyMonomial, numGoRoutines)
if err != nil {
return KZGProof{}, [32]byte{}, err
}
Expand Down
Loading