Skip to content

Commit

Permalink
feat test engine friendly GKR (#1253)
Browse files Browse the repository at this point in the history
Co-authored-by: Arya Tabaie <[email protected]>
  • Loading branch information
Tabaie and Tabaie authored Aug 21, 2024
1 parent 841a46e commit f969f1f
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 48 deletions.
24 changes: 24 additions & 0 deletions constraint/bls12-377/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions constraint/bls12-381/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions constraint/bls24-315/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions constraint/bls24-317/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions constraint/bn254/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions constraint/bw6-633/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions constraint/bw6-761/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/generator/backend/template/representations/gkr.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,28 @@ func GetHashBuilder(name string) (func() hash.Hash, error) {
}
return builder, nil
}


// For testing purposes
type ConstPseudoHash int

func (c ConstPseudoHash) Write(p []byte) (int, error) {
return len(p), nil
}

func (c ConstPseudoHash) Sum([]byte) []byte {
var x fr.Element
x.SetInt64(int64(c))
res := x.Bytes()
return res[:]
}

func (c ConstPseudoHash) Reset() {}

func (c ConstPseudoHash) Size() int {
return fr.Bytes
}

func (c ConstPseudoHash) BlockSize() int {
return fr.Bytes
}
72 changes: 38 additions & 34 deletions std/gkr/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package gkr

import (
"fmt"
bls12377 "github.com/consensys/gnark/constraint/bls12-377"
bls12381 "github.com/consensys/gnark/constraint/bls12-381"
bls24315 "github.com/consensys/gnark/constraint/bls24-315"
bls24317 "github.com/consensys/gnark/constraint/bls24-317"
bw6633 "github.com/consensys/gnark/constraint/bw6-633"
bw6761 "github.com/consensys/gnark/constraint/bw6-761"
"github.com/consensys/gnark/test"
"hash"
"math/rand"
"strconv"
Expand All @@ -10,7 +17,7 @@ import (

"github.com/consensys/gnark-crypto/kzg"
"github.com/consensys/gnark/backend/plonk"
bn254r1cs "github.com/consensys/gnark/constraint/bn254"
bn254 "github.com/consensys/gnark/constraint/bn254"
"github.com/stretchr/testify/require"

"github.com/consensys/gnark-crypto/ecc"
Expand Down Expand Up @@ -74,8 +81,8 @@ func TestDoubleNoDependencyCircuit(t *testing.T) {
assignment := doubleNoDependencyCircuit{X: xValues}
circuit := doubleNoDependencyCircuit{X: make([]frontend.Variable, len(xValues)), hashName: hashName}

testGroth16(t, &circuit, &assignment)
testPlonk(t, &circuit, &assignment)
test.NewAssert(t).CheckCircuit(&circuit, test.WithValidAssignment(&assignment))

}
}
}
Expand Down Expand Up @@ -427,7 +434,7 @@ func testPlonk(t *testing.T, circuit, assignment frontend.Circuit) {
}

func registerMiMC() {
bn254r1cs.RegisterHashBuilder("mimc", func() hash.Hash {
bn254.RegisterHashBuilder("mimc", func() hash.Hash {
return bn254MiMC.NewMiMC()
})
stdHash.Register("mimc", func(api frontend.API) (stdHash.FieldHasher, error) {
Expand All @@ -438,11 +445,31 @@ func registerMiMC() {

func registerConstant(c int) {
name := strconv.Itoa(c)
bn254r1cs.RegisterHashBuilder(name, func() hash.Hash {
return constHashBn254(c)
bls12377.RegisterHashBuilder(name, func() hash.Hash {
return bls12377.ConstPseudoHash(c)
})

bls12381.RegisterHashBuilder(name, func() hash.Hash {
return bls12381.ConstPseudoHash(c)
})
bls24315.RegisterHashBuilder(name, func() hash.Hash {
return bls24315.ConstPseudoHash(c)
})
bls24317.RegisterHashBuilder(name, func() hash.Hash {
return bls24317.ConstPseudoHash(c)
})
bn254.RegisterHashBuilder(name, func() hash.Hash {
return bn254.ConstPseudoHash(c)
})
bw6633.RegisterHashBuilder(name, func() hash.Hash {
return bw6633.ConstPseudoHash(c)
})
bw6761.RegisterHashBuilder(name, func() hash.Hash {
return bw6761.ConstPseudoHash(c)
})

stdHash.Register(name, func(frontend.API) (stdHash.FieldHasher, error) {
return constHash(c), nil
return constPseudoHash(c), nil
})
}

Expand All @@ -458,38 +485,15 @@ func registerMiMCGate() {
gkr.Gates["mimc"] = mimcCipherGate{}
}

type constHashBn254 int // TODO @Tabaie move to gnark-crypto

func (c constHashBn254) Write(p []byte) (int, error) {
return len(p), nil
}

func (c constHashBn254) Sum([]byte) []byte {
var x fr.Element
x.SetInt64(int64(c))
res := x.Bytes()
return res[:]
}

func (c constHashBn254) Reset() {}

func (c constHashBn254) Size() int {
return fr.Bytes
}

func (c constHashBn254) BlockSize() int {
return fr.Bytes
}

type constHash int
type constPseudoHash int

func (c constHash) Sum() frontend.Variable {
func (c constPseudoHash) Sum() frontend.Variable {
return int(c)
}

func (c constHash) Write(...frontend.Variable) {}
func (c constPseudoHash) Write(...frontend.Variable) {}

func (c constHash) Reset() {}
func (c constPseudoHash) Reset() {}

var mimcFrTotalCalls = 0

Expand Down
19 changes: 6 additions & 13 deletions std/gkr/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gkr

import (
"fmt"
"math/big"
"math/bits"

"github.com/consensys/gnark/constraint"
Expand Down Expand Up @@ -124,8 +123,9 @@ func (api *API) Solve(parentApi frontend.API) (Solution, error) {
}
}

outsSerialized, err := parentApi.Compiler().NewHint(SolveHintPlaceholder, solveHintNOut, ins...)
api.toStore.SolveHintID = solver.GetHintID(SolveHintPlaceholder)
solveHintPlaceholder := SolveHintPlaceholder(api.toStore)
outsSerialized, err := parentApi.Compiler().NewHint(solveHintPlaceholder, solveHintNOut, ins...)
api.toStore.SolveHintID = solver.GetHintID(solveHintPlaceholder)
if err != nil {
return Solution{}, err
}
Expand Down Expand Up @@ -176,11 +176,12 @@ func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable
}
copy(hintIns[1:], initialChallenges)

proveHintPlaceholder := ProveHintPlaceholder(hashName)
if proofSerialized, err = s.parentApi.Compiler().NewHint(
ProveHintPlaceholder, ProofSize(forSnark.circuit, logNbInstances), hintIns...); err != nil {
proveHintPlaceholder, ProofSize(forSnark.circuit, logNbInstances), hintIns...); err != nil {
return err
}
s.toStore.ProveHintID = solver.GetHintID(ProveHintPlaceholder)
s.toStore.ProveHintID = solver.GetHintID(proveHintPlaceholder)

forSnarkSorted := algo_utils.MapRange(0, len(s.toStore.Circuit), slicePtrAt(forSnark.circuit))

Expand All @@ -202,14 +203,6 @@ func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable
return s.parentApi.Compiler().SetGkrInfo(s.toStore)
}

func SolveHintPlaceholder(*big.Int, []*big.Int, []*big.Int) error { // TODO @Tabaie Add implementation for testing
return fmt.Errorf("placeholder - not meant to be called")
}

func ProveHintPlaceholder(*big.Int, []*big.Int, []*big.Int) error { // TODO @Tabaie Add implementation for testing
return fmt.Errorf("placeholder - not meant to be called")
}

func slicePtrAt[T any](slice []T) func(int) *T {
return func(i int) *T {
return &slice[i]
Expand Down
Loading

0 comments on commit f969f1f

Please sign in to comment.