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

Feat/gkr testing #740

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: usable placeholder hints for bn254
  • Loading branch information
Tabaie committed Jun 30, 2023
commit 2505c13033fc5446db6308f4a2426c3c95fc223d
6 changes: 6 additions & 0 deletions constraint/solver/hint_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func RegisterNamedHint(hintFn Hint, key HintID) {
registry[key] = hintFn
}

func RemoveNamedHint(key HintID) {
registryM.Lock()
defer registryM.Unlock()
delete(registry, key)
}

// GetRegisteredHints returns all registered hint functions.
func GetRegisteredHints() []Hint {
registryM.RLock()
Expand Down
6 changes: 6 additions & 0 deletions std/gkr/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func (s Solution) Export(v frontend.Variable) []frontend.Variable {
}

// Verify encodes the verification circuitry for the GKR circuit
// hashName denotes the random oracle used to make GKR non-interactive (Fiat-Shamir)
// initialChallenges are initially fed to the Fiat-Shamir hash. If there is many of them, use api.(Committer).Commit to
// consolidate them into a few values
func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable) error {
var (
err error
Expand Down Expand Up @@ -206,6 +209,9 @@ func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable
return err
}

solver.RegisterNamedHint(SolveHintPlaceholderGenerator(s.toStore.SolveHintID, s.toStore), s.toStore.SolveHintID)
solver.RegisterNamedHint(ProveHintPlaceholderGenerator(hashName, s.toStore.SolveHintID, s.toStore.ProveHintID), s.toStore.ProveHintID)

return s.parentApi.Compiler().SetGkrInfo(s.toStore)
}

Expand Down
82 changes: 82 additions & 0 deletions std/gkr/placeholder_hints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package gkr

import (
"errors"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/constraint"
bls12_377 "github.com/consensys/gnark/constraint/bls12-377"
bls12_381 "github.com/consensys/gnark/constraint/bls12-381"
bls24_315 "github.com/consensys/gnark/constraint/bls24-315"
bls24_317 "github.com/consensys/gnark/constraint/bls24-317"
bn254 "github.com/consensys/gnark/constraint/bn254"
bw6_633 "github.com/consensys/gnark/constraint/bw6-633"
bw6_761 "github.com/consensys/gnark/constraint/bw6-761"

"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/internal/utils"
"math/big"
)

// TODO @Tabaie Autogen this and possibly move to another package

var placeholderGkrSolvingData map[solver.HintID]any

func SolveHintPlaceholderGenerator(hintId solver.HintID, gkrInfo constraint.GkrInfo) func(*big.Int, []*big.Int, []*big.Int) error {
return func(mod *big.Int, in []*big.Int, out []*big.Int) (err error) {
solver.RemoveNamedHint(hintId)

curve := utils.FieldToCurve(mod)
switch curve {
case ecc.BLS12_377:
var data bls12_377.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bls12_377.GkrSolveHint(gkrInfo, &data)(mod, in, out)
case ecc.BLS12_381:
var data bls12_381.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bls12_381.GkrSolveHint(gkrInfo, &data)(mod, in, out)
case ecc.BLS24_315:
var data bls24_315.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bls24_315.GkrSolveHint(gkrInfo, &data)(mod, in, out)
case ecc.BLS24_317:
var data bls24_317.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bls24_317.GkrSolveHint(gkrInfo, &data)(mod, in, out)
case ecc.BN254:
var data bn254.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bn254.GkrSolveHint(gkrInfo, &data)(mod, in, out)
case ecc.BW6_633:
var data bw6_633.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bw6_633.GkrSolveHint(gkrInfo, &data)(mod, in, out)
case ecc.BW6_761:
var data bw6_761.GkrSolvingData
placeholderGkrSolvingData[hintId] = &data
err = bw6_761.GkrSolveHint(gkrInfo, &data)(mod, in, out)
default:
err = errors.New("unsupported curve")
}

return err
}
}

func ProveHintPlaceholderGenerator(hashName string, solveHintId, proveHintId solver.HintID) func(*big.Int, []*big.Int, []*big.Int) error {
return func(mod *big.Int, in []*big.Int, out []*big.Int) (err error) {
solver.RemoveNamedHint(proveHintId)

curve := utils.FieldToCurve(mod)
switch curve {
case ecc.BN254:
err = bn254.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bn254.GkrSolvingData))(mod, in, out)
default:
err = errors.New("unsupported curve")
}

delete(placeholderGkrSolvingData, solveHintId)

return err
}
}