micro-zk-proofs
Version:
Create & verify zero-knowledge SNARK proofs in parallel, using noble cryptography
131 lines • 4.33 kB
TypeScript
/**
* The code is only used if you plan to run **legacy circom-js programs**. It is unused in WASM.
* Minimal witness program executor for circom programs, based on websnark/wasmsnark/snarkjs.
* Unsafe: it uses eval, better to be used inside worker threads.
* Depends on **monkey-patched BigInt** prototypes due to how circom programs are serialized.
* We only patch prototypes before execution. After finishing, patches are reverted.
* @module
*/
import * as P from 'micro-packed';
import { type CircuitInfo, type Constraint, type ProvingKey, type VerificationKey } from './index.ts';
import type { BlsCurvePair as BLSCurvePair } from '@noble/curves/abstract/bls.js';
import type { TRet } from '@noble/hashes/utils.js';
/**
* Builds a witness generator for a legacy circom-js circuit JSON.
* @param circJson - Circom circuit JSON artifact.
* @returns Function that executes the circuit and returns the witness.
* @example
* Build a witness runner from a circom JSON circuit artifact.
* ```ts
* import { generateWitness } from 'micro-zk-proofs/witness.js';
* // Addition circuit: witness output is one, a + b, b, a.
* const circuitJson = {
* nVars: 4,
* nInputs: 2,
* nOutputs: 1,
* nSignals: 4,
* templates: {
* Main: `function(ctx) {
* ctx.setSignal(
* "out",
* [],
* bigInt(ctx.getSignal("a", [])).add(bigInt(ctx.getSignal("b", []))).mod(__P__)
* );
* }`,
* },
* functions: {},
* components: [{ name: 'main', params: {}, template: 'Main', inputSignals: 2 }],
* signals: [
* { names: ['one'], triggerComponents: [] },
* { names: ['main.out'], triggerComponents: [] },
* { names: ['main.b'], triggerComponents: [0] },
* { names: ['main.a'], triggerComponents: [0] },
* ],
* signalName2Idx: { one: 0, 'main.out': 1, 'main.b': 2, 'main.a': 3 },
* };
* const witness = generateWitness(circuitJson)({ a: '33', b: '34' });
* // [1n, 67n, 34n, 33n]
* ```
*/
export declare function generateWitness(circJson: any): (input: any) => any;
/** Binary coder type for `.r1cs` files. */
export type R1CSType = P.CoderType<P.StructInput<{
magic: undefined;
version: number;
sections: P.Values<{
header: {
TAG: 'header';
data: P.StructInput<{
prime: any;
nWires: any;
nPubOut: any;
nPubIn: any;
nPrvIn: any;
nLables: any;
mConstraints: any;
}>;
};
constraint: {
TAG: 'constraint';
data: [Constraint, Constraint, Constraint][];
};
wire2label: {
TAG: 'wire2label';
data: bigint[];
};
customGatesList: {
TAG: 'customGatesList';
data: P.Bytes;
};
customGatesApplication: {
TAG: 'customGatesApplication';
data: P.Bytes;
};
}>[];
}>>;
/** Binary coder type for `.wtns` files. */
export type WTNSType = P.CoderType<P.StructInput<{
magic: undefined;
version: number;
sections: P.Values<{
header: {
TAG: 'header';
data: P.StructInput<{
prime: any;
size: any;
}>;
};
witness: {
TAG: 'witness';
data: bigint[];
};
}>[];
}>>;
type CodersOutput = {
R1CS: R1CSType;
binWitness: P.CoderType<bigint[]>;
WTNS: WTNSType;
getCircuitInfo: (bytes: Uint8Array) => CircuitInfo;
ZKeyRaw: P.CoderType<any>;
parseZKey: (bytes: Uint8Array) => {
json: any;
pkey: ProvingKey;
vkey: VerificationKey;
};
};
/**
* Binary coders and parsers for Circom2 artifacts.
* @param curve - Curve pair used for field sizing and point decoding.
* @returns R1CS, witness, and zkey coders plus parse helpers.
* @example
* Build the coders once, then use them to parse and encode Circom2 artifacts.
* ```ts
* const { bn254 } = await import('@noble/curves/bn254.js');
* const coders = getCoders(bn254);
* const bytes = coders.binWitness.encode([1n, 2n]);
* coders.binWitness.decode(bytes);
* ```
*/
export declare const getCoders: (curve: BLSCurvePair) => TRet<CodersOutput>;
export {};
//# sourceMappingURL=witness.d.ts.map