Skip to content

Commit

Permalink
Merge pull request #11 from iden3/feature/wasmer-fork
Browse files Browse the repository at this point in the history
Switch to our wasmer-go fork
  • Loading branch information
OBrezhniev authored Apr 28, 2023
2 parents f049cc2 + 008855d commit bb27e9f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 47 deletions.
57 changes: 40 additions & 17 deletions prover/rapidsnark.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,48 @@ func Groth16ProverRaw(zkey []byte,

proofBufSize := bufferSize
publicBufSize := bufferSize
var proofBuffer = make([]byte, proofBufSize)
var publicBuffer = make([]byte, publicBufSize)

const errorBufSize = 4096
var errorMessage [errorBufSize]byte
errorMessage := make([]byte, errorBufSize)

zkeyPointer := C.CBytes(zkey)
wtnsPointer := C.CBytes(witness)

errorMessagePointer := C.CString(string(errorMessage))

defer func() {
C.free(zkeyPointer)
C.free(wtnsPointer)
C.free(unsafe.Pointer(errorMessagePointer))
}()

for {
var proofBuffer = make([]byte, proofBufSize)
var publicBuffer = make([]byte, publicBufSize)

//proofBufferPointer := unsafe.Pointer(&proofBuffer[0])
proofBufferPointer := C.CString(string(proofBuffer))
proofBufSizePointer := unsafe.Pointer(&proofBufSize)
publicBufferPointer := C.CString(string(publicBuffer))
publicBufSizePointer := unsafe.Pointer(&publicBufSize)

defer func() {
C.free(unsafe.Pointer(proofBufferPointer))
C.free(unsafe.Pointer(publicBufferPointer))
}()

r := C.groth16_prover(
unsafe.Pointer(&zkey[0]), C.ulong(len(zkey)),
unsafe.Pointer(&witness[0]), C.ulong(len(witness)),
(*C.char)(unsafe.Pointer(&proofBuffer[0])), (*C.ulong)(unsafe.Pointer(&proofBufSize)),
(*C.char)(unsafe.Pointer(&publicBuffer[0])), (*C.ulong)(unsafe.Pointer(&publicBufSize)),
(*C.char)(unsafe.Pointer(&errorMessage[0])), errorBufSize)
zkeyPointer, C.ulong(len(zkey)),
wtnsPointer, C.ulong(len(witness)),
proofBufferPointer, (*C.ulong)(proofBufSizePointer),
publicBufferPointer, (*C.ulong)(publicBufSizePointer),
errorMessagePointer, errorBufSize)

proofBuffer = []byte(C.GoString(proofBufferPointer))
publicBuffer = []byte(C.GoString(publicBufferPointer))
errorMessage = []byte(C.GoString(errorMessagePointer))

if r != 0 {
if r != 0 && r != 2 {
idx := bytes.IndexByte(errorMessage[:], 0)
if idx == -1 {
idx = len(errorMessage)
Expand All @@ -79,34 +106,30 @@ func Groth16ProverRaw(zkey []byte,
// if true, enlarge buffers and repeat.
repeat := false

idx := bytes.IndexByte(proofBuffer[:], 0)
if idx == -1 {
if len(proofBuffer) == 0 {
if proofBufSize >= MaxBufferSize {
return "", "", errors.New("proof is too large")
}
proofBufSize *= 2
if proofBufSize >= MaxBufferSize {
proofBufSize = MaxBufferSize
}
proofBuffer = make([]byte, proofBufSize)
repeat = true
} else {
proof = string(proofBuffer[:idx])
proof = string(proofBuffer)
}

idx = bytes.IndexByte(publicBuffer[:], 0)
if idx == -1 {
if len(publicBuffer) == 0 {
if publicBufSize >= MaxBufferSize {
return "", "", errors.New("public inputs is too large")
}
publicBufSize *= 2
if publicBufSize >= MaxBufferSize {
publicBufSize = MaxBufferSize
}
publicBuffer = make([]byte, publicBufSize)
repeat = true
} else {
publicInputs = string(publicBuffer[:idx])
publicInputs = string(publicBuffer)
}

if repeat {
Expand Down
28 changes: 13 additions & 15 deletions tests/rapidsnark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,49 @@ import (

"github.com/iden3/go-rapidsnark/prover"
"github.com/iden3/go-rapidsnark/verifier"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Groth16Prover(t *testing.T) {
var provingKey, verificationKey, witness []byte
var err error

provingKey, err = os.ReadFile("./testdata/circuit_final.zkey")
assert.Nil(t, err)
require.NoError(t, err)

witness, err = os.ReadFile("./testdata/witness.wtns")
assert.Nil(t, err)
require.NoError(t, err)

verificationKey, err = os.ReadFile("./testdata/verification_key.json")
assert.Nil(t, err)

assert.NoError(t, err)
require.NoError(t, err)

proof, err := prover.Groth16Prover(provingKey, witness)
assert.NoError(t, err)
require.NoError(t, err)

err = verifier.VerifyGroth16(*proof, verificationKey)
assert.NoError(t, err)
require.NoError(t, err)
}

func Benchmark(b *testing.B) {
var provingKey, verificationKey, witness []byte
var err error

provingKey, err = os.ReadFile("./testdata/circuit_final.zkey")
assert.Nil(b, err)
require.NoError(b, err)

witness, err = os.ReadFile("./testdata/witness.wtns")
assert.Nil(b, err)
require.NoError(b, err)

verificationKey, err = os.ReadFile("./testdata/verification_key.json")
assert.Nil(b, err)

assert.NoError(b, err)
require.NoError(b, err)

for i := 0; i < b.N; i++ {
proof, err := prover.Groth16Prover(provingKey, witness)
assert.NoError(b, err)
require.NoError(b, err)
require.NotEmpty(b, proof)
require.NotEmpty(b, verificationKey)

err = verifier.VerifyGroth16(*proof, verificationKey)
assert.NoError(b, err)
require.NoError(b, err)
}
}
23 changes: 18 additions & 5 deletions witness/circom2witnesscalc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"fmt"
"math/big"

"github.com/wasmerio/wasmer-go/wasmer"
"github.com/iden3/wasmer-go/wasmer"
)

// Circom2WitnessCalculator is the object that allows performing witness calculation
// from signal inputs using the WitnessCalc WASM module.
type Circom2WitnessCalculator struct {
engine *wasmer.Engine
module *wasmer.Module
instance *wasmer.Instance
store *wasmer.Store
sanityCheck bool
Expand Down Expand Up @@ -42,11 +44,16 @@ func NewCircom2WitnessCalculator(wasmBytes []byte, sanityCheck bool) (*Circom2Wi
wc := Circom2WitnessCalculator{}
wc.sanityCheck = sanityCheck

engine := wasmer.NewEngine()
wc.store = wasmer.NewStore(engine)
wc.engine = wasmer.NewEngine()
wc.store = wasmer.NewStore(wc.engine)

var err error

// Compiles the module
module, _ := wasmer.NewModule(wc.store, wasmBytes)
wc.module, err = wasmer.NewModule(wc.store, wasmBytes)
if err != nil {
return nil, err
}

limits, err := wasmer.NewLimits(2000, 100000)
if err != nil {
Expand All @@ -72,7 +79,7 @@ func NewCircom2WitnessCalculator(wasmBytes []byte, sanityCheck bool) (*Circom2Wi
"writeBufferMessage": wc.writeBufferMessageHandler(),
})

wc.instance, err = wasmer.NewInstance(module, importObject)
wc.instance, err = wasmer.NewInstance(wc.module, importObject)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -542,6 +549,12 @@ func (wc *Circom2WitnessCalculator) writeBufferMessageHandler() wasmer.IntoExter
return function
}

func (wc *Circom2WitnessCalculator) Close() {
wc.store.Close()
wc.instance.Close()
wc.module.Close()
}

func toArray32(s *big.Int, size int) ([]uint32, error) {
res := make([]uint32, size)
rem := s
Expand Down
8 changes: 4 additions & 4 deletions witness/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/iden3/go-rapidsnark/witness
go 1.18

require (
github.com/stretchr/testify v1.7.1
github.com/wasmerio/wasmer-go v1.0.4
github.com/iden3/wasmer-go v0.0.1
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 11 additions & 6 deletions witness/go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iden3/wasmer-go v0.0.1 h1:TZKh8Se8B/73PvWrcu+FTU9L1k5XYAmtFbioj7l0Uog=
github.com/iden3/wasmer-go v0.0.1/go.mod h1:ZnZBAO012M7o+Q1INXLRIxKQgEcH2FuwL0Iga8A4ufg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
github.com/wasmerio/wasmer-go v1.0.4/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit bb27e9f

Please sign in to comment.