github.com/iden3/go-circom-witnesscalc@v0.2.1-0.20230314155733-dd1f248a91b6/helpers.go (about)

     1  package witnesscalc
     2  
     3  import (
     4  	"io/ioutil"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/iden3/go-wasm3"
     9  
    10  	log "github.com/sirupsen/logrus"
    11  )
    12  
    13  func CalculateWitnessBinWASM(wasmBytes []byte, inputs map[string]interface{}) ([]*big.Int, error) {
    14  	runtime := wasm3.NewRuntime(&wasm3.Config{
    15  		Environment: wasm3.NewEnvironment(),
    16  		StackSize:   64 * 1024,
    17  	})
    18  	defer runtime.Destroy()
    19  
    20  	module, err := runtime.ParseModule(wasmBytes)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	module, err = runtime.LoadModule(module)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	witnessCalculator, err := NewWitnessCalculator(runtime, module)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	start := time.Now()
    35  	witness, err := witnessCalculator.CalculateWitness(inputs, true)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	log.WithField("elapsed", time.Now().Sub(start)).Debug("Witness calculated")
    40  
    41  	return witness, err
    42  }
    43  
    44  func CalculateWitness(wasmFilePath string, inputs map[string]interface{}) ([]*big.Int, error) {
    45  	wasmBytes, err := ioutil.ReadFile(wasmFilePath)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return CalculateWitnessBinWASM(wasmBytes, inputs)
    50  }