-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from ConsenSys/feat/emulated/subgroup-check
Feat: emulated subgroup check
- Loading branch information
Showing
19 changed files
with
1,145 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package sw_bls12381 | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/consensys/gnark-crypto/ecc" | ||
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381" | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/test" | ||
) | ||
|
||
type addG2Circuit struct { | ||
In1, In2 G2Affine | ||
Res G2Affine | ||
} | ||
|
||
func (c *addG2Circuit) Define(api frontend.API) error { | ||
g2 := NewG2(api) | ||
res := g2.add(&c.In1, &c.In2) | ||
g2.AssertIsEqual(res, &c.Res) | ||
return nil | ||
} | ||
|
||
func TestAddG2TestSolve(t *testing.T) { | ||
assert := test.NewAssert(t) | ||
_, in1 := randomG1G2Affines(assert) | ||
_, in2 := randomG1G2Affines(assert) | ||
var res bls12381.G2Affine | ||
res.Add(&in1, &in2) | ||
witness := addG2Circuit{ | ||
In1: NewG2Affine(in1), | ||
In2: NewG2Affine(in2), | ||
Res: NewG2Affine(res), | ||
} | ||
err := test.IsSolved(&addG2Circuit{}, &witness, ecc.BN254.ScalarField()) | ||
assert.NoError(err) | ||
} | ||
|
||
type doubleG2Circuit struct { | ||
In1 G2Affine | ||
Res G2Affine | ||
} | ||
|
||
func (c *doubleG2Circuit) Define(api frontend.API) error { | ||
g2 := NewG2(api) | ||
res := g2.double(&c.In1) | ||
g2.AssertIsEqual(res, &c.Res) | ||
return nil | ||
} | ||
|
||
func TestDoubleG2TestSolve(t *testing.T) { | ||
assert := test.NewAssert(t) | ||
_, in1 := randomG1G2Affines(assert) | ||
var res bls12381.G2Affine | ||
var in1Jac, resJac bls12381.G2Jac | ||
in1Jac.FromAffine(&in1) | ||
resJac.Double(&in1Jac) | ||
res.FromJacobian(&resJac) | ||
witness := doubleG2Circuit{ | ||
In1: NewG2Affine(in1), | ||
Res: NewG2Affine(res), | ||
} | ||
err := test.IsSolved(&doubleG2Circuit{}, &witness, ecc.BN254.ScalarField()) | ||
assert.NoError(err) | ||
} | ||
|
||
type doubleAndAddG2Circuit struct { | ||
In1, In2 G2Affine | ||
Res G2Affine | ||
} | ||
|
||
func (c *doubleAndAddG2Circuit) Define(api frontend.API) error { | ||
g2 := NewG2(api) | ||
res := g2.doubleAndAdd(&c.In1, &c.In2) | ||
g2.AssertIsEqual(res, &c.Res) | ||
return nil | ||
} | ||
|
||
func TestDoubleAndAddG2TestSolve(t *testing.T) { | ||
assert := test.NewAssert(t) | ||
_, in1 := randomG1G2Affines(assert) | ||
_, in2 := randomG1G2Affines(assert) | ||
var res bls12381.G2Affine | ||
res.Double(&in1). | ||
Add(&res, &in2) | ||
witness := doubleAndAddG2Circuit{ | ||
In1: NewG2Affine(in1), | ||
In2: NewG2Affine(in2), | ||
Res: NewG2Affine(res), | ||
} | ||
err := test.IsSolved(&doubleAndAddG2Circuit{}, &witness, ecc.BN254.ScalarField()) | ||
assert.NoError(err) | ||
} | ||
|
||
type scalarMulG2BySeedCircuit struct { | ||
In1 G2Affine | ||
Res G2Affine | ||
} | ||
|
||
func (c *scalarMulG2BySeedCircuit) Define(api frontend.API) error { | ||
g2 := NewG2(api) | ||
res := g2.scalarMulBySeed(&c.In1) | ||
g2.AssertIsEqual(res, &c.Res) | ||
return nil | ||
} | ||
|
||
func TestScalarMulG2BySeedTestSolve(t *testing.T) { | ||
assert := test.NewAssert(t) | ||
_, in1 := randomG1G2Affines(assert) | ||
var res bls12381.G2Affine | ||
x0, _ := new(big.Int).SetString("15132376222941642752", 10) | ||
res.ScalarMultiplication(&in1, x0).Neg(&res) | ||
witness := scalarMulG2BySeedCircuit{ | ||
In1: NewG2Affine(in1), | ||
Res: NewG2Affine(res), | ||
} | ||
err := test.IsSolved(&scalarMulG2BySeedCircuit{}, &witness, ecc.BN254.ScalarField()) | ||
assert.NoError(err) | ||
} |
Oops, something went wrong.