-
Notifications
You must be signed in to change notification settings - Fork 493
feat: GKR Add Instance #1504
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
base: master
Are you sure you want to change the base?
feat: GKR Add Instance #1504
Changes from 31 commits
609d47d
79d4cbf
82e33e5
ba06e5d
700c152
30f5633
44d6b4c
f5f726c
644fd6a
2001e83
d3ca3c1
6474482
0c0b6b0
478d53d
6c83740
d6e382f
a8f30a4
8ac5f9f
5bb879d
7f9a0f1
355277c
cfdb9d3
12788f3
9a0bf0e
618beff
22b77f2
250a35b
1586760
e593d90
9df619a
8c88c52
f4d7aa8
499ff52
4ae9324
4765d61
b3d1af8
31a0a59
db56157
b4544d6
bdd01fd
c170695
70802ea
4ca453c
f2b1122
b9c01be
893ba91
a13ac27
8947d9f
4a09fd1
929d732
905d7c5
b7107e4
67257dc
b1d61f1
9971328
99cf4a8
5242279
af81887
c523fd2
36784d6
0ae0d21
82f2c9e
26bbef6
7e1c10f
a3d316e
61302f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "runtime" | ||
| "sync" | ||
|
|
||
| "github.com/consensys/gnark" | ||
| "github.com/consensys/gnark-crypto/ecc" | ||
|
|
||
| bls12377 "github.com/consensys/gnark/internal/gkr/bls12-377" | ||
|
|
@@ -99,31 +100,62 @@ func WithCurves(curves ...ecc.ID) registerOption { | |
| // - name is a human-readable name for the gate. | ||
| // - f is the polynomial function defining the gate. | ||
| // - nbIn is the number of inputs to the gate. | ||
| func Register(f gkr.GateFunction, nbIn int, options ...registerOption) error { | ||
| s := registerSettings{degree: -1, solvableVar: -1, name: GetDefaultGateName(f), curves: []ecc.ID{ecc.BN254}} | ||
| // | ||
| // If the gate is already registered, it will return false and no error. | ||
| func Register(f gkr.GateFunction, nbIn int, options ...registerOption) (registered bool, err error) { | ||
Tabaie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| s := registerSettings{degree: -1, solvableVar: -1, name: GetDefaultGateName(f)} | ||
| for _, option := range options { | ||
| option(&s) | ||
| } | ||
|
|
||
| for _, curve := range s.curves { | ||
| curvesForTesting := s.curves | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For gate degree testing - do we need an option at all? Cannot we just take some "good" modulus and test over that field. For me it makes a bit confusing that the user can choose the curve, but then needs to know the relation between the gate degree and modulus.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it may be best to pick a random 144 bit or so modulus and just test over that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I think it is a good idea to test over random fields, there actually is a good reason to want to define a gate function over a certain curve only. A good example are hash round functions, which contain hard-coded constants, which for performance reasons it makes sense to keep as concrete types such as |
||
| allowedCurves := s.curves | ||
| if len(curvesForTesting) == 0 { | ||
| // no restriction on curves, but only test on BN254 | ||
| curvesForTesting = []ecc.ID{ecc.BN254} | ||
| allowedCurves = gnark.Curves() | ||
| } | ||
|
|
||
| gatesLock.Lock() | ||
| defer gatesLock.Unlock() | ||
|
|
||
| if g, ok := gates[s.name]; ok { | ||
| // gate already registered | ||
| if g.NbIn() != nbIn { | ||
| return false, fmt.Errorf("gate \"%s\" already registered with a different number of inputs (%d != %d)", s.name, g.NbIn(), nbIn) | ||
| } | ||
|
|
||
| for _, curve := range curvesForTesting { | ||
| gateVer, err := NewGateVerifier(curve) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if !gateVer.equal(f, g.Evaluate, nbIn) { | ||
| return false, fmt.Errorf("mismatch with already registered gate \"%s\" (degree %d) over curve %s", s.name, g.Degree(), curve) | ||
| } | ||
| } | ||
|
|
||
| return false, nil // gate already registered | ||
| } | ||
|
|
||
| for _, curve := range curvesForTesting { | ||
| gateVer, err := NewGateVerifier(curve) | ||
| if err != nil { | ||
| return err | ||
| return false, err | ||
| } | ||
|
|
||
| if s.degree == -1 { // find a degree | ||
| if s.noDegreeVerification { | ||
| panic("invalid settings") | ||
| } | ||
| const maxAutoDegreeBound = 32 | ||
| var err error | ||
| if s.degree, err = gateVer.findDegree(f, maxAutoDegreeBound, nbIn); err != nil { | ||
| return fmt.Errorf("for gate %s: %v", s.name, err) | ||
| return false, fmt.Errorf("for gate \"%s\": %v", s.name, err) | ||
| } | ||
| } else { | ||
| if !s.noDegreeVerification { // check that the given degree is correct | ||
| if err = gateVer.verifyDegree(f, s.degree, nbIn); err != nil { | ||
| return fmt.Errorf("for gate %s: %v", s.name, err) | ||
| return false, fmt.Errorf("for gate \"%s\": %v", s.name, err) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -135,16 +167,14 @@ func Register(f gkr.GateFunction, nbIn int, options ...registerOption) error { | |
| } else { | ||
| // solvable variable given | ||
| if !s.noSolvableVarVerification && !gateVer.isVarSolvable(f, s.solvableVar, nbIn) { | ||
| return fmt.Errorf("cannot verify the solvability of variable %d in gate %s", s.solvableVar, s.name) | ||
| return false, fmt.Errorf("cannot verify the solvability of variable %d in gate \"%s\"", s.solvableVar, s.name) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| gatesLock.Lock() | ||
| defer gatesLock.Unlock() | ||
| gates[s.name] = gkrtypes.NewGate(f, nbIn, s.degree, s.solvableVar) | ||
| return nil | ||
| gates[s.name] = gkrtypes.NewGate(f, nbIn, s.degree, s.solvableVar, allowedCurves) | ||
| return true, nil | ||
| } | ||
|
|
||
| func Get(name gkr.GateName) *gkrtypes.Gate { | ||
Tabaie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -160,6 +190,7 @@ type gateVerifier struct { | |
| isAdditive func(f gkr.GateFunction, i int, nbIn int) bool | ||
| findDegree func(f gkr.GateFunction, max, nbIn int) (int, error) | ||
| verifyDegree func(f gkr.GateFunction, claimedDegree, nbIn int) error | ||
| equal func(f1, f2 gkr.GateFunction, nbIn int) bool | ||
| } | ||
|
|
||
| func NewGateVerifier(curve ecc.ID) (*gateVerifier, error) { | ||
Tabaie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
@@ -172,30 +203,37 @@ func NewGateVerifier(curve ecc.ID) (*gateVerifier, error) { | |
| o.isAdditive = bls12377.IsGateFunctionAdditive | ||
| o.findDegree = bls12377.FindGateFunctionDegree | ||
| o.verifyDegree = bls12377.VerifyGateFunctionDegree | ||
| o.equal = bls12377.EqualGateFunction | ||
| case ecc.BLS12_381: | ||
| o.isAdditive = bls12381.IsGateFunctionAdditive | ||
| o.findDegree = bls12381.FindGateFunctionDegree | ||
| o.verifyDegree = bls12381.VerifyGateFunctionDegree | ||
| o.equal = bls12381.EqualGateFunction | ||
| case ecc.BLS24_315: | ||
| o.isAdditive = bls24315.IsGateFunctionAdditive | ||
| o.findDegree = bls24315.FindGateFunctionDegree | ||
| o.verifyDegree = bls24315.VerifyGateFunctionDegree | ||
| o.equal = bls24315.EqualGateFunction | ||
| case ecc.BLS24_317: | ||
| o.isAdditive = bls24317.IsGateFunctionAdditive | ||
| o.findDegree = bls24317.FindGateFunctionDegree | ||
| o.verifyDegree = bls24317.VerifyGateFunctionDegree | ||
| o.equal = bls24317.EqualGateFunction | ||
| case ecc.BN254: | ||
| o.isAdditive = bn254.IsGateFunctionAdditive | ||
| o.findDegree = bn254.FindGateFunctionDegree | ||
| o.verifyDegree = bn254.VerifyGateFunctionDegree | ||
| o.equal = bn254.EqualGateFunction | ||
| case ecc.BW6_633: | ||
| o.isAdditive = bw6633.IsGateFunctionAdditive | ||
| o.findDegree = bw6633.FindGateFunctionDegree | ||
| o.verifyDegree = bw6633.VerifyGateFunctionDegree | ||
| o.equal = bw6633.EqualGateFunction | ||
| case ecc.BW6_761: | ||
| o.isAdditive = bw6761.IsGateFunctionAdditive | ||
| o.findDegree = bw6761.FindGateFunctionDegree | ||
| o.verifyDegree = bw6761.VerifyGateFunctionDegree | ||
| o.equal = bw6761.EqualGateFunction | ||
| default: | ||
| err = fmt.Errorf("unsupported curve %s", curve) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.