-
Notifications
You must be signed in to change notification settings - Fork 368
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: emulated subgroup check #629
Conversation
The current plan is for the zk-evm arithmetization to perform preliminary checks (is on curve for C1 and C2) but not subgroup checks (for G2) in order to reduce the number of junk calls to this circuit. |
Yes! this is captured in the last commit 0e0958c. |
Follows https://github.com/ConsenSys/zkevm-spec/issues/50 and fixes #634 |
Suggested edit: diff --git a/std/algebra/emulated/sw_bn254/hints.go b/std/algebra/emulated/sw_bn254/hints.go
index b1674afd..9f240981 100644
--- a/std/algebra/emulated/sw_bn254/hints.go
+++ b/std/algebra/emulated/sw_bn254/hints.go
@@ -9,7 +9,11 @@ import (
)
func init() {
- solver.RegisterHint(subgroupG2Hint)
+ solver.RegisterHint(GetHints()...)
+}
+
+func GetHints() []solver.Hint {
+ return []solver.Hint{subgroupG2Hint}
}
func subgroupG2Hint(nativeMod *big.Int, nativeInputs, nativeOutputs []*big.Int) error {
diff --git a/std/hints.go b/std/hints.go
index 2d044830..5d54fb10 100644
--- a/std/hints.go
+++ b/std/hints.go
@@ -4,6 +4,8 @@ import (
"sync"
"github.com/consensys/gnark/constraint/solver"
+ "github.com/consensys/gnark/std/algebra/emulated/sw_bls12381"
+ "github.com/consensys/gnark/std/algebra/emulated/sw_bn254"
"github.com/consensys/gnark/std/algebra/native/sw_bls12377"
"github.com/consensys/gnark/std/algebra/native/sw_bls24315"
"github.com/consensys/gnark/std/evmprecompiles"
@@ -37,4 +39,6 @@ func registerHints() {
solver.RegisterHint(emulated.GetHints()...)
solver.RegisterHint(rangecheck.CountHint, rangecheck.DecomposeHint)
solver.RegisterHint(evmprecompiles.GetHints()...)
+ solver.RegisterHint(sw_bn254.GetHints()...)
+ solver.RegisterHint(sw_bls12381.GetHints()...)
}
|
This is to ensure that if the compiler and prover are in different processes then in the prover process we do |
Suggested edit: diff --git a/std/algebra/emulated/sw_bls12381/doc_test.go b/std/algebra/emulated/sw_bls12381/doc_test.go
index ae87b729..a1ce0f5c 100644
--- a/std/algebra/emulated/sw_bls12381/doc_test.go
+++ b/std/algebra/emulated/sw_bls12381/doc_test.go
@@ -23,6 +23,10 @@ func (c *PairCircuit) Define(api frontend.API) error {
if err != nil {
return fmt.Errorf("new pairing: %w", err)
}
+ // Pair method does not check that the points are in the proper groups.
+ pairing.AssertIsOnG1(&c.InG1)
+ pairing.AssertIsOnG2(&c.InG2)
+ // Compute the pairing
res, err := pairing.Pair([]*sw_bls12381.G1Affine{&c.InG1}, []*sw_bls12381.G2Affine{&c.InG2})
if err != nil {
return fmt.Errorf("pair: %w", err)
diff --git a/std/algebra/emulated/sw_bn254/doc_test.go b/std/algebra/emulated/sw_bn254/doc_test.go
index db095a02..7d8ef6a6 100644
--- a/std/algebra/emulated/sw_bn254/doc_test.go
+++ b/std/algebra/emulated/sw_bn254/doc_test.go
@@ -23,6 +23,10 @@ func (c *PairCircuit) Define(api frontend.API) error {
if err != nil {
return fmt.Errorf("new pairing: %w", err)
}
+ // Pair method does not check that the points are in the proper groups.
+ pairing.AssertIsOnG1(&c.InG1)
+ pairing.AssertIsOnG2(&c.InG2)
+ // Compute the pairing
res, err := pairing.Pair([]*sw_bn254.G1Affine{&c.InG1}, []*sw_bn254.G2Affine{&c.InG2})
if err != nil {
return fmt.Errorf("pair: %w", err)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the G1 group checks are sufficient. Imo if the prover is malicious and replaces the hints with identity functions then the in-circuit assertions hold but the points may still be outside the group?
It is also possible I have misunderstood. Otherwise I think is perfect, but I would only wrap AssertIsOnCurve
of sw_emulated
instead of reimplementing in this package, but I can do it myself.
I assumed that the hint is a private function and a gnark user cannot change it, but I see your point.
Go for the suggested edit and I'll think about the subgroup membership hints. |
We now even have made it easier with a solver option for overriding hints: https://github.com/ConsenSys/gnark/blob/develop/constraint/solver/options.go#L39-L44 |
@ivokub Ok I did the BN254 G2 membership directly in-circuit (without hints). I optimised quite a bit the (fixed) scalar mul. A full pairing with both G1 and G2 membership:
Will do BLS12-381 later when you review the structure of the code? |
I think it looks good! Imo considering that the subgroup check is optional the performance of the check is great. |
Did the BLS12-381 too but did not optimize the fixed scalar Mul in G1. We don't use the Edit: An addition chain using the public method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Only fixed one typo.
Edit: and merge develop. No should be good to merge!
Adds the methods:
AssertIsOnCurve
tosw_emulated
,AssertIsOnG1
andAssertIsOnG2
tosw_bn254
andsw_bl12381
with hints.This PR needs Consensys/gnark-crypto#376.
TODO:
AssertIsOnCurve
part.