-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new inverse method of PackScalarToVar (UnpackVarToScalar) and test
- Loading branch information
1 parent
8d5b0ca
commit 0ab4d0b
Showing
2 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/rand" | ||
"testing" | ||
|
||
"github.com/consensys/gnark-crypto/ecc" | ||
"github.com/consensys/gnark/backend" | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/std/algebra/native/sw_bls12377" | ||
"github.com/consensys/gnark/std/math/emulated" | ||
"github.com/consensys/gnark/std/recursion/groth16" | ||
"github.com/consensys/gnark/test" | ||
) | ||
|
||
type testPackUnpackCircuit struct { | ||
Input emulated.Element[sw_bls12377.ScalarField] | ||
} | ||
|
||
func (c *testPackUnpackCircuit) Define(api frontend.API) error { | ||
// pack the emulated element to a variable | ||
packed, err := PackScalarToVar[sw_bls12377.ScalarField](api, &c.Input) | ||
if err != nil { | ||
return err | ||
} | ||
// unpack the variable to an emulated element | ||
unpacked, err := UnpackVarToScalar[sw_bls12377.ScalarField](api, packed) | ||
if err != nil { | ||
return err | ||
} | ||
// compare the limbs of the input and the unpacked element | ||
for i, limb := range c.Input.Limbs { | ||
api.AssertIsEqual(limb, unpacked.Limbs[i]) | ||
} | ||
return nil | ||
} | ||
|
||
func TestUnpackPackVar(t *testing.T) { | ||
assert := test.NewAssert(t) | ||
// generate a random big number | ||
r, err := rand.Int(rand.Reader, ecc.BLS12_377.ScalarField()) | ||
assert.NoError(err) | ||
|
||
assert.SolvingSucceeded(&testPackUnpackCircuit{}, &testPackUnpackCircuit{Input: emulated.ValueOf[sw_bls12377.ScalarField](r)}, | ||
test.WithCurves(ecc.BW6_761), test.WithBackends(backend.GROTH16), | ||
test.WithProverOpts(groth16.GetNativeProverOptions(ecc.BN254.ScalarField(), ecc.BW6_761.ScalarField()))) | ||
} |