From f8afece5916da0e14afbef08261d73739a545ea7 Mon Sep 17 00:00:00 2001 From: Liuhaai Date: Thu, 19 Dec 2024 10:15:10 -0800 Subject: [PATCH] add hints --- gnark-server/go.mod | 1 + gnark-server/go.sum | 2 ++ gnark-server/prover/groth16.go | 5 +++++ gnark-server/prover/groth16_test.go | 15 ++++++++++----- gnark-server/prover/manager_test.go | 5 +++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/gnark-server/go.mod b/gnark-server/go.mod index c559793..2008769 100644 --- a/gnark-server/go.mod +++ b/gnark-server/go.mod @@ -27,6 +27,7 @@ require ( github.com/rs/zerolog v1.33.0 // indirect github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.27.0 // indirect + golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect diff --git a/gnark-server/go.sum b/gnark-server/go.sum index ad5fe14..632b9f2 100644 --- a/gnark-server/go.sum +++ b/gnark-server/go.sum @@ -56,6 +56,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA= +golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= diff --git a/gnark-server/prover/groth16.go b/gnark-server/prover/groth16.go index 09f9a5a..b1e7c73 100644 --- a/gnark-server/prover/groth16.go +++ b/gnark-server/prover/groth16.go @@ -9,6 +9,7 @@ import ( "github.com/consensys/gnark/backend/groth16" "github.com/consensys/gnark/backend/witness" "github.com/consensys/gnark/constraint" + "github.com/consensys/gnark/std" ) type Groth16Prover struct { @@ -16,6 +17,10 @@ type Groth16Prover struct { provingKey groth16.ProvingKey } +func init() { + std.RegisterHints() +} + func (p *Groth16Prover) LoadCircuit(b []byte) error { cs := groth16.NewCS(ecc.BN254) _, err := cs.ReadFrom(bytes.NewReader(b)) diff --git a/gnark-server/prover/groth16_test.go b/gnark-server/prover/groth16_test.go index 445d6d5..12161e7 100644 --- a/gnark-server/prover/groth16_test.go +++ b/gnark-server/prover/groth16_test.go @@ -11,17 +11,22 @@ import ( "github.com/consensys/gnark/backend/witness" "github.com/consensys/gnark/frontend" "github.com/consensys/gnark/frontend/cs/r1cs" + "github.com/consensys/gnark/std/math/uints" "github.com/iotexproject/w3bstream-vm/gnark-server/prover" "github.com/stretchr/testify/require" ) type addCircuit struct { - X frontend.Variable - Y frontend.Variable `gnark:",public"` + X uints.U32 + Y uints.U32 `gnark:",public"` } func (circuit *addCircuit) Define(api frontend.API) error { - api.AssertIsEqual(circuit.Y, api.Add(circuit.X, circuit.X)) + uapi, err := uints.New[uints.U32](api) + if err != nil { + return err + } + uapi.AssertEq(circuit.X, circuit.Y) return nil } @@ -37,8 +42,8 @@ func setupaddCircuit(t *testing.T) ([]byte, []byte, witness.Witness, groth16.Ver // Create witness assignment := &addCircuit{ - X: 42, - Y: 84, + X: uints.NewU32(0x12345678), + Y: uints.NewU32(0x12345678), } witness, err := frontend.NewWitness(assignment, ecc.BN254.ScalarField()) require.NoError(t, err) diff --git a/gnark-server/prover/manager_test.go b/gnark-server/prover/manager_test.go index 3c85035..1490e10 100644 --- a/gnark-server/prover/manager_test.go +++ b/gnark-server/prover/manager_test.go @@ -6,6 +6,7 @@ import ( "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/backend/witness" "github.com/consensys/gnark/frontend" + "github.com/consensys/gnark/std/math/uints" "github.com/iotexproject/w3bstream-vm/gnark-server/prover" "github.com/stretchr/testify/require" ) @@ -55,8 +56,8 @@ func TestProverManager(t *testing.T) { // Create an invalid witness invalidAssignment := &addCircuit{ - X: 42, - Y: 100, // This should be 84 (2*42) + X: uints.NewU32(0x12345678), + Y: uints.NewU32(0x1234567), } invalidWitness, err := frontend.NewWitness(invalidAssignment, ecc.BN254.ScalarField()) require.NoError(t, err)