/* Copyright 2019 0KIMS association. This file is part of websnark (Web Assembly zkSnark Prover). websnark is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. websnark is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with websnark. If not, see . */ const bigInt = require("big-integer"); const Circuit = require("snarkjs/src/circuit"); const bigInt2 = require("snarkjs/src/bigint"); const hexifyBigInts = require("../tools/stringifybigint").hexifyBigInts; const unhexifyBigInts = require("../tools/stringifybigint").unhexifyBigInts; const stringifyBigInts = require("../tools/stringifybigint").stringifyBigInts; const unstringifyBigInts = require("../tools/stringifybigint").unstringifyBigInts; const stringifyBigInts2 = require("snarkjs/src/stringifybigint").stringifyBigInts; const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBigInts; function bigInt2BytesLE(_a, len) { const b = Array(len); let v = bigInt(_a); for (let i=0; i toHex32(x)).join("") }; if (proof.publicSignals) { result.publicSignals = hexifyBigInts(unstringifyBigInts(proof.publicSignals)); } return result; } function genWitness(input, circuitJson) { const circuit = new Circuit(unstringifyBigInts2(circuitJson)); const witness = circuit.calculateWitness(unstringifyBigInts2(input)); const publicSignals = witness.slice(1, circuit.nPubInputs + circuit.nOutputs + 1); return {witness, publicSignals}; } async function genWitnessAndProve(groth16, input, circuitJson, provingKey) { const witnessData = genWitness(input, circuitJson); const witnessBin = convertWitness(witnessData.witness); const result = await groth16.proof(witnessBin, provingKey); result.publicSignals = stringifyBigInts2(witnessData.publicSignals); return result; } module.exports = {bigInt2BytesLE, bigInt2U32LE, toSolidityInput, genWitnessAndProve};