Browse Source

add wasm snark proof verification function (Pinocchio)

pull/14/head
arnaucube 5 years ago
parent
commit
417cc92a97
8 changed files with 113 additions and 13 deletions
  1. +1
    -1
      README.md
  2. BIN
      go-snark-cli
  3. +1
    -9
      wasm/README.md
  4. +55
    -2
      wasm/go-snark-wasm-wrapper.go
  5. BIN
      wasm/go-snark.wasm
  6. +1
    -0
      wasm/index.html
  7. +13
    -1
      wasm/index.js
  8. +42
    -0
      wasm/utils/utils.go

+ 1
- 1
README.md

@ -41,7 +41,7 @@ Improvements from the minimal implementation:
- [x] Groth16
- [ ] multiple optimizations
- [x] wasm proof generation
- [ ] wasm proof verification
- [x] wasm proof verification
## WASM usage
Ongoing experimentation with go-snark compiled to wasm: https://github.com/arnaucube/go-snark/tree/master/wasm

BIN
go-snark-cli


+ 1
- 9
wasm/README.md

@ -12,15 +12,7 @@ Add the file `wasm_exec.js` in the directory:
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
```
Call the library from javascript:
```js
let r = generateProofs(
JSON.stringify(circuit),
JSON.stringify(setup),
JSON.stringify(px),
JSON.stringify(inputs),
);
```
To see the usage from javascript, check `index.js` file.
Run the http server that allows to load the `.wasm` file:
```

+ 55
- 2
wasm/go-snark-wasm-wrapper.go

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"math/big"
"syscall/js"
"github.com/arnaucube/go-snark"
@ -11,15 +12,14 @@ import (
func main() {
c := make(chan struct{}, 0)
println("WASM Go Initialized")
// register functions
registerCallbacks()
<-c
}
func registerCallbacks() {
js.Global().Set("generateProofs", js.FuncOf(generateProofs))
js.Global().Set("verifyProofs", js.FuncOf(verifyProofs))
}
func generateProofs(this js.Value, i []js.Value) interface{} {
@ -89,3 +89,56 @@ func generateProofs(this js.Value, i []js.Value) interface{} {
println("proofJson", string(proofJson))
return js.ValueOf(string(proofJson))
}
func verifyProofs(this js.Value, i []js.Value) interface{} {
var circuitStr utils.CircuitString
err := json.Unmarshal([]byte(i[0].String()), &circuitStr)
if err != nil {
println(i[0].String())
println("error parsing circuit from stringified json")
}
circuit, err := utils.CircuitFromString(circuitStr)
if err != nil {
println("error " + err.Error())
}
var setupStr utils.SetupString
println(i[1].String())
err = json.Unmarshal([]byte(i[1].String()), &setupStr)
if err != nil {
println("error parsing setup from stringified json")
}
setup, err := utils.SetupFromString(setupStr)
if err != nil {
println("error " + err.Error())
}
var proofStr utils.ProofString
err = json.Unmarshal([]byte(i[2].String()), &proofStr)
if err != nil {
println(i[0].String())
println("error parsing proof from stringified json")
}
proof, err := utils.ProofFromString(proofStr)
if err != nil {
println("error " + err.Error())
}
var publicInputs []*big.Int
err = json.Unmarshal([]byte(i[3].String()), &publicInputs)
if err != nil {
println(i[0].String())
println("error parsing publicInputs from stringified json")
}
verified := snark.VerifyProof(circuit, setup, proof, publicInputs, false)
if err != nil {
println("error verifiyng proof", err)
}
verifiedJson, err := json.Marshal(verified)
if err != nil {
println("error marshal verified to json", err)
}
println("verifiedJson", string(verifiedJson))
return js.ValueOf(string(verifiedJson))
}

BIN
wasm/go-snark.wasm


+ 1
- 0
wasm/index.html

@ -14,6 +14,7 @@
<button onClick="callGenerateProof();">Generate Proof</button>
<textarea id="proofResult" rows="15" style="width: 80%;">
</textarea>
<button onClick="callVerifyProof();">Verify Proof</button>
<script src="index.js"></script>
</body>
</html>

+ 13
- 1
wasm/index.js
File diff suppressed because it is too large
View File


+ 42
- 0
wasm/utils/utils.go

@ -169,6 +169,14 @@ func SetupToString(setup snark.Setup) SetupString {
s.Pk.Bp = Array3BigIntToString(setup.Pk.Bp)
s.Pk.Cp = Array3BigIntToString(setup.Pk.Cp)
s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
s.Vk.Vka = BigInt32ToString(setup.Vk.Vka)
s.Vk.Vkb = BigInt3ToString(setup.Vk.Vkb)
s.Vk.Vkc = BigInt32ToString(setup.Vk.Vkc)
s.Vk.IC = Array3BigIntToString(setup.Vk.IC)
s.Vk.G1Kbg = BigInt3ToString(setup.Vk.G1Kbg)
s.Vk.G2Kbg = BigInt32ToString(setup.Vk.G2Kbg)
s.Vk.G2Kg = BigInt32ToString(setup.Vk.G2Kg)
s.Vk.Vkz = BigInt32ToString(setup.Vk.Vkz)
return s
}
func SetupFromString(s SetupString) (snark.Setup, error) {
@ -214,6 +222,40 @@ func SetupFromString(s SetupString) (snark.Setup, error) {
if err != nil {
return o, err
}
o.Vk.Vka, err = String32ToBigInt(s.Vk.Vka)
if err != nil {
return o, err
}
o.Vk.Vkb, err = String3ToBigInt(s.Vk.Vkb)
if err != nil {
return o, err
}
o.Vk.Vkc, err = String32ToBigInt(s.Vk.Vkc)
if err != nil {
return o, err
}
o.Vk.IC, err = Array3StringToBigInt(s.Vk.IC)
if err != nil {
return o, err
}
o.Vk.G1Kbg, err = String3ToBigInt(s.Vk.G1Kbg)
if err != nil {
return o, err
}
o.Vk.G2Kbg, err = String32ToBigInt(s.Vk.G2Kbg)
if err != nil {
return o, err
}
o.Vk.G2Kg, err = String32ToBigInt(s.Vk.G2Kg)
if err != nil {
return o, err
}
o.Vk.Vkz, err = String32ToBigInt(s.Vk.Vkz)
if err != nil {
return o, err
}
return o, nil
}

Loading…
Cancel
Save