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

Batch sumchecks in a Wire bundle #1248

Draft
wants to merge 31 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b1a4c73
gkr_nonnative intial review
Jun 12, 2024
2825fdc
removed unused fns
Jun 12, 2024
94a6866
added some tests
Jun 13, 2024
6cbdc74
Debugged nil elements -m added verifier in claimsmaanager and popoula…
Jun 13, 2024
2b30342
Revert "Debugged nil elements -m added verifier in claimsmaanager and…
Jun 13, 2024
ac3b04e
git commit -m "Debugged nil elements" -m "Added verifier in ClaimsMan…
Jun 13, 2024
59c8554
Debugged nil elements
Jun 13, 2024
650d48a
removed unused fns
Jun 13, 2024
4717901
compiles now
Jun 13, 2024
04b5caf
fixed
Jun 13, 2024
9a6f78c
resolve ivo comments - prover done
TheDarkMatters Jun 17, 2024
a8917d9
fixed ivo comments
amit0365 Jun 18, 2024
5224fac
removed Fr naming with Emulated
amit0365 Jun 18, 2024
4720856
removed all Fr with Emulated
amit0365 Jun 18, 2024
218c27a
testing single add gate
amit0365 Jun 19, 2024
6670c2d
testgkrvector fails, debug
amit0365 Jun 21, 2024
043048b
arya review test
amit0365 Jun 21, 2024
0da71f7
fixed single_identity_gate_two_instances prove, renamed partialSumPol…
amit0365 Jun 28, 2024
93e787c
single identity gate passes testgkr
amit0365 Jun 29, 2024
b091b79
testgkr passes
amit0365 Jul 4, 2024
81335d7
commented print patch
amit0365 Jul 4, 2024
29c23c5
changes from gofmt
amit0365 Jul 4, 2024
3a72147
fixed lint
amit0365 Jul 4, 2024
b34071a
removed TestLogNbInstances
amit0365 Jul 4, 2024
47bad48
dbladdgkr passes
amit0365 Jul 9, 2024
33011f8
input layer doesn't work
amit0365 Jul 28, 2024
fe46739
manyInstances works
amit0365 Jul 29, 2024
14d12b4
passes with random inputs
amit0365 Aug 7, 2024
1cf2c2d
eq common in next and computeGJ
amit0365 Aug 8, 2024
de475cd
cleanup
amit0365 Aug 14, 2024
793cf77
cleanup
amit0365 Aug 15, 2024
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
36 changes: 0 additions & 36 deletions .gotestfmt/downloads.gotpl

This file was deleted.

42 changes: 0 additions & 42 deletions .gotestfmt/package.gotpl

This file was deleted.

56 changes: 56 additions & 0 deletions internal/parallel/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package parallel

import (
"runtime"
"sync"
)

// Execute process in parallel the work function
func Execute(nbIterations int, work func(int, int), maxCpus ...int) {

nbTasks := runtime.NumCPU()
if len(maxCpus) == 1 {
nbTasks = maxCpus[0]
if nbTasks < 1 {
nbTasks = 1
} else if nbTasks > 512 {
nbTasks = 512
}
}

if nbTasks == 1 {
// no go routines
work(0, nbIterations)
return
}

nbIterationsPerCpus := nbIterations / nbTasks

// more CPUs than tasks: a CPU will work on exactly one iteration
if nbIterationsPerCpus < 1 {
nbIterationsPerCpus = 1
nbTasks = nbIterations
}

var wg sync.WaitGroup

extraTasks := nbIterations - (nbTasks * nbIterationsPerCpus)
extraTasksOffset := 0

for i := 0; i < nbTasks; i++ {
wg.Add(1)
_start := i*nbIterationsPerCpus + extraTasksOffset
_end := _start + nbIterationsPerCpus
if extraTasks > 0 {
_end++
extraTasks--
extraTasksOffset++
}
go func() {
work(_start, _end)
wg.Done()
}()
}

wg.Wait()
}
47 changes: 47 additions & 0 deletions std/fiat-shamir/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package fiatshamir
import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/hash"
"github.com/consensys/gnark/std/math/emulated"
gohash "hash"
"math/big"
)

type Settings struct {
Expand All @@ -12,6 +15,20 @@ type Settings struct {
Hash hash.FieldHasher
}

type SettingsBigInt struct {
Transcript *Transcript
Prefix string
BaseChallenges []big.Int
Hash gohash.Hash
}

type SettingsEmulated[FR emulated.FieldParams] struct {
Transcript *Transcript
Prefix string
BaseChallenges []emulated.Element[FR]
Hash hash.FieldHasher
}

func WithTranscript(transcript *Transcript, prefix string, baseChallenges ...frontend.Variable) Settings {
return Settings{
Transcript: transcript,
Expand All @@ -20,9 +37,39 @@ func WithTranscript(transcript *Transcript, prefix string, baseChallenges ...fro
}
}

func WithTranscriptBigInt(transcript *Transcript, prefix string, baseChallenges ...big.Int) SettingsBigInt {
return SettingsBigInt{
Transcript: transcript,
Prefix: prefix,
BaseChallenges: baseChallenges,
}
}

func WithTranscriptFr[FR emulated.FieldParams](transcript *Transcript, prefix string, baseChallenges ...emulated.Element[FR]) SettingsEmulated[FR] {
return SettingsEmulated[FR]{
Transcript: transcript,
Prefix: prefix,
BaseChallenges: baseChallenges,
}
}

func WithHash(hash hash.FieldHasher, baseChallenges ...frontend.Variable) Settings {
return Settings{
BaseChallenges: baseChallenges,
Hash: hash,
}
}

func WithHashBigInt(hash gohash.Hash, baseChallenges ...big.Int) SettingsBigInt {
return SettingsBigInt{
BaseChallenges: baseChallenges,
Hash: hash,
}
}

func WithHashFr[FR emulated.FieldParams](hash hash.FieldHasher, baseChallenges ...emulated.Element[FR]) SettingsEmulated[FR] {
return SettingsEmulated[FR]{
BaseChallenges: baseChallenges,
Hash: hash,
}
}
17 changes: 9 additions & 8 deletions std/gkr/gkr.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ func Verify(api frontend.API, c Circuit, assignment WireAssignment, proof Proof,
claims := newClaimsManager(c, assignment)

var firstChallenge []frontend.Variable
// why no bind values here?
firstChallenge, err = getChallenges(o.transcript, getFirstChallengeNames(o.nbVars, o.transcriptPrefix))
if err != nil {
return err
Expand All @@ -327,7 +328,7 @@ func Verify(api frontend.API, c Circuit, assignment WireAssignment, proof Proof,
claim := claims.getLazyClaim(wire)
if wire.noProof() { // input wires with one claim only
// make sure the proof is empty
if len(finalEvalProof) != 0 || len(proofW.PartialSumPolys) != 0 {
if len(finalEvalProof) != 0 || len(proofW.RoundPolyEvaluations) != 0 {
return fmt.Errorf("no proof allowed for input wire with a single claim")
}

Expand Down Expand Up @@ -470,16 +471,16 @@ func (a WireAssignment) NumVars() int {
func (p Proof) Serialize() []frontend.Variable {
size := 0
for i := range p {
for j := range p[i].PartialSumPolys {
size += len(p[i].PartialSumPolys[j])
for j := range p[i].RoundPolyEvaluations {
size += len(p[i].RoundPolyEvaluations[j])
}
size += len(p[i].FinalEvalProof.([]frontend.Variable))
}

res := make([]frontend.Variable, 0, size)
for i := range p {
for j := range p[i].PartialSumPolys {
res = append(res, p[i].PartialSumPolys[j]...)
for j := range p[i].RoundPolyEvaluations {
res = append(res, p[i].RoundPolyEvaluations[j]...)
}
res = append(res, p[i].FinalEvalProof.([]frontend.Variable)...)
}
Expand Down Expand Up @@ -519,9 +520,9 @@ func DeserializeProof(sorted []*Wire, serializedProof []frontend.Variable) (Proo
reader := variablesReader(serializedProof)
for i, wI := range sorted {
if !wI.noProof() {
proof[i].PartialSumPolys = make([]polynomial.Polynomial, logNbInstances)
for j := range proof[i].PartialSumPolys {
proof[i].PartialSumPolys[j] = reader.nextN(wI.Gate.Degree() + 1)
proof[i].RoundPolyEvaluations = make([]polynomial.Polynomial, logNbInstances)
for j := range proof[i].RoundPolyEvaluations {
proof[i].RoundPolyEvaluations[j] = reader.nextN(wI.Gate.Degree() + 1)
}
}
proof[i].FinalEvalProof = reader.nextN(wI.nbUniqueInputs())
Expand Down
16 changes: 8 additions & 8 deletions std/gkr/gkr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gkr
import (
"encoding/json"
"fmt"
"math/big"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -165,8 +166,8 @@ type TestCase struct {
type TestCaseInfo struct {
Hash HashDescription `json:"hash"`
Circuit string `json:"circuit"`
Input [][]interface{} `json:"input"`
Output [][]interface{} `json:"output"`
Input [][]big.Int `json:"input"`
Output [][]big.Int `json:"output"`
Proof PrintableProof `json:"proof"`
}

Expand Down Expand Up @@ -275,8 +276,8 @@ func (g _select) Degree() int {
type PrintableProof []PrintableSumcheckProof

type PrintableSumcheckProof struct {
FinalEvalProof interface{} `json:"finalEvalProof"`
PartialSumPolys [][]interface{} `json:"partialSumPolys"`
FinalEvalProof interface{} `json:"finalEvalProof"`
RoundPolyEvaluations [][]interface{} `json:"roundPolyEvaluations"`
}

func unmarshalProof(printable PrintableProof) (proof Proof) {
Expand All @@ -294,9 +295,9 @@ func unmarshalProof(printable PrintableProof) (proof Proof) {
proof[i].FinalEvalProof = nil
}

proof[i].PartialSumPolys = make([]polynomial.Polynomial, len(printable[i].PartialSumPolys))
for k := range printable[i].PartialSumPolys {
proof[i].PartialSumPolys[k] = ToVariableSlice(printable[i].PartialSumPolys[k])
proof[i].RoundPolyEvaluations = make([]polynomial.Polynomial, len(printable[i].RoundPolyEvaluations))
for k := range printable[i].RoundPolyEvaluations {
proof[i].RoundPolyEvaluations[k] = ToVariableSlice(printable[i].RoundPolyEvaluations[k])
}
}
return
Expand Down Expand Up @@ -327,7 +328,6 @@ func TestLoadCircuit(t *testing.T) {
assert.Equal(t, []*Wire{}, c[0].Inputs)
assert.Equal(t, []*Wire{&c[0]}, c[1].Inputs)
assert.Equal(t, []*Wire{&c[1]}, c[2].Inputs)

}

func TestTopSortTrivial(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions std/gkr/test_vectors/single_identity_gate_two_instances.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"proof": [
{
"finalEvalProof": [],
"partialSumPolys": []
"roundPolyEvaluations": []
},
{
"finalEvalProof": [
5
],
"partialSumPolys": [
"roundPolyEvaluations": [
[
-3,
-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"proof": [
{
"finalEvalProof": [],
"partialSumPolys": [
"roundPolyEvaluations": [
[
0,
0
Expand All @@ -34,7 +34,7 @@
"finalEvalProof": [
1
],
"partialSumPolys": [
"roundPolyEvaluations": [
[
-3,
-16
Expand All @@ -45,7 +45,7 @@
"finalEvalProof": [
1
],
"partialSumPolys": [
"roundPolyEvaluations": [
[
-3,
-16
Expand Down
6 changes: 3 additions & 3 deletions std/gkr/test_vectors/single_input_two_outs_two_instances.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"proof": [
{
"finalEvalProof": [],
"partialSumPolys": [
"roundPolyEvaluations": [
[
0,
0
Expand All @@ -34,7 +34,7 @@
"finalEvalProof": [
0
],
"partialSumPolys": [
"roundPolyEvaluations": [
[
-4,
-36,
Expand All @@ -46,7 +46,7 @@
"finalEvalProof": [
0
],
"partialSumPolys": [
"roundPolyEvaluations": [
[
-2,
-12
Expand Down
Loading
Loading