Skip to content

Commit

Permalink
dbladdgkr passes
Browse files Browse the repository at this point in the history
  • Loading branch information
amit0365 committed Jul 9, 2024
1 parent b34071a commit 47bad48
Show file tree
Hide file tree
Showing 9 changed files with 653 additions and 227 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/dl v0.0.0-20240621154342-20a4bcbb3ee2 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/dl v0.0.0-20240621154342-20a4bcbb3ee2 h1:pV/1u+ib3c3Lhedg7EeTXMmyo7pKi7xHFH++3qlpxV8=
golang.org/dl v0.0.0-20240621154342-20a4bcbb3ee2/go.mod h1:fwQ+hlTD8I6TIzOGkQqxQNfE2xqR+y7SzGaDkksVFkw=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
Expand Down
152 changes: 76 additions & 76 deletions std/math/emulated/field_mul.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,99 +115,99 @@ func (mc *mulCheck[T]) cleanEvaluations() {
// mulMod returns a*b mod r. In practice it computes the result using a hint and
// defers the actual multiplication check.
func (f *Field[T]) mulMod(a, b *Element[T], _ uint, p *Element[T]) *Element[T] {
//return f.mulModProfiling(a, b, p, true)
f.enforceWidthConditional(a)
f.enforceWidthConditional(b)
f.enforceWidthConditional(p)
k, r, c, err := f.callMulHint(a, b, true, p)
if err != nil {
panic(err)
}
mc := mulCheck[T]{
f: f,
a: a,
b: b,
c: c,
k: k,
r: r,
p: p,
}
f.mulChecks = append(f.mulChecks, mc)
return r
return f.mulModProfiling(a, b, p, true)
// f.enforceWidthConditional(a)
// f.enforceWidthConditional(b)
// f.enforceWidthConditional(p)
// k, r, c, err := f.callMulHint(a, b, true, p)
// if err != nil {
// panic(err)
// }
// mc := mulCheck[T]{
// f: f,
// a: a,
// b: b,
// c: c,
// k: k,
// r: r,
// p: p,
// }
// f.mulChecks = append(f.mulChecks, mc)
// return r
}

// checkZero creates multiplication check a * 1 = 0 + k*p.
func (f *Field[T]) checkZero(a *Element[T], p *Element[T]) {
//f.mulModProfiling(a, f.shortOne(), p, false)
f.mulModProfiling(a, f.shortOne(), p, false)
// the method works similarly to mulMod, but we know that we are multiplying
// by one and expected result should be zero.
// f.enforceWidthConditional(a)
// f.enforceWidthConditional(p)
// b := f.shortOne()
// k, r, c, err := f.callMulHint(a, b, false, p)
// if err != nil {
// panic(err)
// }
// mc := mulCheck[T]{
// f: f,
// a: a,
// b: b, // one on single limb to speed up the polynomial evaluation
// c: c,
// k: k,
// r: r, // expected to be zero on zero limbs.
// p: p,
// }
// f.mulChecks = append(f.mulChecks, mc)
}

func (f *Field[T]) mulModProfiling(a, b *Element[T], p *Element[T], isMulMod bool) *Element[T] {
f.enforceWidthConditional(a)
f.enforceWidthConditional(p)
b := f.shortOne()
k, r, c, err := f.callMulHint(a, b, false, p)
f.enforceWidthConditional(b)
k, r, c, err := f.callMulHint(a, b, isMulMod, p)
if err != nil {
panic(err)
}
mc := mulCheck[T]{
f: f,
a: a,
b: b, // one on single limb to speed up the polynomial evaluation
b: b,
c: c,
k: k,
r: r, // expected to be zero on zero limbs.
p: p,
r: r,
}
f.mulChecks = append(f.mulChecks, mc)
}

// func (f *Field[T]) mulModProfiling(a, b *Element[T], p *Element[T], isMulMod bool) *Element[T] {
// f.enforceWidthConditional(a)
// f.enforceWidthConditional(b)
// k, r, c, err := f.callMulHint(a, b, isMulMod, p)
// if err != nil {
// panic(err)
// }
// mc := mulCheck[T]{
// f: f,
// a: a,
// b: b,
// c: c,
// k: k,
// r: r,
// }
// var toCommit []frontend.Variable
// toCommit = append(toCommit, mc.a.Limbs...)
// toCommit = append(toCommit, mc.b.Limbs...)
// toCommit = append(toCommit, mc.r.Limbs...)
// toCommit = append(toCommit, mc.k.Limbs...)
// toCommit = append(toCommit, mc.c.Limbs...)
// multicommit.WithCommitment(f.api, func(api frontend.API, commitment frontend.Variable) error {
// // we do nothing. We just want to ensure that we count the commitments
// return nil
// }, toCommit...)
// // XXX: or use something variable to count the commitments and constraints properly. Maybe can use 123 from hint?
// commitment := 123
var toCommit []frontend.Variable
toCommit = append(toCommit, mc.a.Limbs...)
toCommit = append(toCommit, mc.b.Limbs...)
toCommit = append(toCommit, mc.r.Limbs...)
toCommit = append(toCommit, mc.k.Limbs...)
toCommit = append(toCommit, mc.c.Limbs...)
multicommit.WithCommitment(f.api, func(api frontend.API, commitment frontend.Variable) error {
// we do nothing. We just want to ensure that we count the commitments
return nil
}, toCommit...)
// XXX: or use something variable to count the commitments and constraints properly. Maybe can use 123 from hint?
commitment := 123

// // for efficiency, we compute all powers of the challenge as slice at.
// coefsLen := max(len(mc.a.Limbs), len(mc.b.Limbs),
// len(mc.c.Limbs), len(mc.k.Limbs))
// at := make([]frontend.Variable, coefsLen)
// at[0] = commitment
// for i := 1; i < len(at); i++ {
// at[i] = f.api.Mul(at[i-1], commitment)
// }
// mc.evalRound1(at)
// mc.evalRound2(at)
// // evaluate p(X) at challenge
// pval := f.evalWithChallenge(f.Modulus(), at)
// // compute (2^t-X) at challenge
// coef := big.NewInt(1)
// coef.Lsh(coef, f.fParams.BitsPerLimb())
// ccoef := f.api.Sub(coef, commitment)
// // verify all mulchecks
// mc.check(f.api, pval.evaluation, ccoef)
// return r
// }
// for efficiency, we compute all powers of the challenge as slice at.
coefsLen := max(len(mc.a.Limbs), len(mc.b.Limbs),
len(mc.c.Limbs), len(mc.k.Limbs))
at := make([]frontend.Variable, coefsLen)
at[0] = commitment
for i := 1; i < len(at); i++ {
at[i] = f.api.Mul(at[i-1], commitment)
}
mc.evalRound1(at)
mc.evalRound2(at)
// evaluate p(X) at challenge
pval := f.evalWithChallenge(f.Modulus(), at)
// compute (2^t-X) at challenge
coef := big.NewInt(1)
coef.Lsh(coef, f.fParams.BitsPerLimb())
ccoef := f.api.Sub(coef, commitment)
// verify all mulchecks
mc.check(f.api, pval.evaluation, ccoef)
return r
}

// evalWithChallenge represents element a as a polynomial a(X) and evaluates at
// at[0]. For efficiency, we use already evaluated powers of at[0] given by at.
Expand Down
18 changes: 15 additions & 3 deletions std/recursion/gkr/gkr_nonnative.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gkr
package gkrnonative

import (
"fmt"
Expand Down Expand Up @@ -778,12 +778,11 @@ func Prove(current *big.Int, target *big.Int, c Circuit, assignment WireAssignme

claim := claims.getClaim(be, wire)
var finalEvalProofLen int
finalEvalProof := proof[i].FinalEvalProof

if wire.noProof() { // input wires with one claim only
proof[i] = sumcheck.NativeProof{
RoundPolyEvaluations: []sumcheck.NativePolynomial{},
FinalEvalProof: finalEvalProof,
FinalEvalProof: sumcheck.NativeDeferredEvalProof([]big.Int{}),
}
} else {
proof[i], err = sumcheck.Prove(
Expand All @@ -793,6 +792,19 @@ func Prove(current *big.Int, target *big.Int, c Circuit, assignment WireAssignme
return proof, err
}

finalEvalProof := proof[i].FinalEvalProof
switch finalEvalProof := finalEvalProof.(type) {
case nil:
finalEvalProofCasted := sumcheck.NativeDeferredEvalProof([]big.Int{})
proof[i].FinalEvalProof = finalEvalProofCasted
case []big.Int:
finalEvalProofLen = len(finalEvalProof)
finalEvalProofCasted := sumcheck.NativeDeferredEvalProof(finalEvalProof)
proof[i].FinalEvalProof = finalEvalProofCasted
default:
return nil, fmt.Errorf("finalEvalProof is not of type DeferredEvalProof")
}

baseChallenge = make([]*big.Int, finalEvalProofLen)
for i := 0; i < finalEvalProofLen; i++ {
baseChallenge[i] = &finalEvalProof.([]big.Int)[i]
Expand Down
Loading

0 comments on commit 47bad48

Please sign in to comment.