Skip to content

Commit

Permalink
add hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Liuhaai committed Dec 19, 2024
1 parent dc5954a commit f8afece
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions gnark-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions gnark-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions gnark-server/prover/groth16.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ 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 {
circuit constraint.ConstraintSystem
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))
Expand Down
15 changes: 10 additions & 5 deletions gnark-server/prover/groth16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions gnark-server/prover/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f8afece

Please sign in to comment.