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

gkr_nonnative intial review #1162

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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?
amit0365 marked this conversation as resolved.
Show resolved Hide resolved
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