witnesscalc

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2022 License: GPL-3.0 Imports: 14 Imported by: 2

README

go-circom-witnesscalc

GoDoc

Witness Calculator in go, calling WASM generated from circom.

Example

package main

import (
	"encoding/json"
	"io/ioutil"

	wasm3 "github.com/iden3/go-wasm3"
	"github.com/stretchr/testify/assert"
)

func Test(t *testing.T) {
	wasmFilename :=   "test_files/mycircuit.wasm"
	inputsFilename := "test_files/mycircuit-input2.json"

	runtime := wasm3.NewRuntime(&wasm3.Config{
		Environment: wasm3.NewEnvironment(),
		StackSize:   64 * 1024,
	})
        defer runtime.Destroy()

	wasmBytes, err := ioutil.ReadFile(wasmFilename)
	require.Nil(t, err)

	module, err := runtime.ParseModule(wasmBytes)
	require.Nil(t, err)

	module, err = runtime.LoadModule(module)
	require.Nil(t, err)

	inputsBytes, err := ioutil.ReadFile(inputsFilename)
	require.Nil(t, err)

	inputs, err := ParseInputs(inputsBytes)
	require.Nil(t, err)

	witnessCalculator, err := NewWitnessCalculator(runtime, module)
	require.Nil(t, err)

	w, err := witnessCalculator.CalculateWitness(inputs, false)
	require.Nil(t, err)

	wJSON, err := json.Marshal(WitnessJSON(w))
	require.Nil(t, err)
        fmt.Print(string(wJSON))
}

License

GPLv3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateWitness

func CalculateWitness(wasmFilePath string, inputs map[string]interface{}) ([]*big.Int, error)

func CalculateWitnessBinWASM

func CalculateWitnessBinWASM(wasmBytes []byte, inputs map[string]interface{}) ([]*big.Int, error)

func ParseInputs

func ParseInputs(inputsJSON []byte) (map[string]interface{}, error)

ParseInputs parses WitnessCalc inputs from JSON that consist of a map of types which contain a recursive combination of: numbers, base-10 encoded numbers in string format, arrays.

Types

type Circom2WitnessCalculator added in v0.2.0

type Circom2WitnessCalculator struct {
	// contains filtered or unexported fields
}

Circom2WitnessCalculator is the object that allows performing witness calculation from signal inputs using the WitnessCalc WASM module.

func NewCircom2WitnessCalculator added in v0.2.0

func NewCircom2WitnessCalculator(wasmBytes []byte, sanityCheck bool) (*Circom2WitnessCalculator, error)

NewCircom2WitnessCalculator creates a new WitnessCalculator from the WitnessCalc loaded WASM module in the runtime.

func (*Circom2WitnessCalculator) CalculateBinWitness added in v0.2.0

func (wc *Circom2WitnessCalculator) CalculateBinWitness(inputs map[string]interface{}, sanityCheck bool) ([]byte, error)

CalculateBinWitness calculates the witness in binary given the inputs.

func (*Circom2WitnessCalculator) CalculateWTNSBin added in v0.2.0

func (wc *Circom2WitnessCalculator) CalculateWTNSBin(inputs map[string]interface{}, sanityCheck bool) ([]byte, error)

CalculateWTNSBin calculates the witness in binary given the inputs.

func (*Circom2WitnessCalculator) CalculateWitness added in v0.2.0

func (wc *Circom2WitnessCalculator) CalculateWitness(inputs map[string]interface{}, sanityCheck bool) ([]*big.Int, error)

CalculateWitness calculates the witness given the inputs.

type WitnessCalculator

type WitnessCalculator struct {
	// contains filtered or unexported fields
}

WitnessCalculator is the object that allows performing witness calculation from signal inputs using the WitnessCalc WASM module.

func NewWitnessCalculator

func NewWitnessCalculator(runtime *wasm3.Runtime, module *wasm3.Module) (*WitnessCalculator, error)

NewWitnessCalculator creates a new WitnessCalculator from the WitnessCalc loaded WASM module in the runtime.

func (*WitnessCalculator) CalculateBinWitness

func (wc *WitnessCalculator) CalculateBinWitness(inputs map[string]interface{}, sanityCheck bool) ([]byte, error)

CalculateWitness calculates the witness in binary given the inputs.

func (*WitnessCalculator) CalculateWitness

func (wc *WitnessCalculator) CalculateWitness(inputs map[string]interface{}, sanityCheck bool) ([]*big.Int, error)

CalculateWitness calculates the witness given the inputs.

type WitnessJSON

type WitnessJSON []*big.Int

WitnessJSON is a wrapper type to Marshal the Witness in JSON format

func (WitnessJSON) MarshalJSON

func (w WitnessJSON) MarshalJSON() ([]byte, error)

MarshalJSON marshals the WitnessJSON where each value is encoded in base 10 as a string in an array.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL