Skip to content

Commit

Permalink
added more primes, calldata export todos (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant authored Feb 5, 2024
1 parent 93c7cf9 commit 4a1e998
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// https://raw.githubusercontent.com/PKief/vscode-material-icon-theme/main/images/fileIcons.png
"material-icon-theme.files.associations": {
"*.circom": "Verilog",
"*.wtns": "Authors"
"*.wtns": "Authors",
"*.zkey": "Lock",
"*.r1cs": "Diff",
"*.sym": "Edge"
}
}
4 changes: 3 additions & 1 deletion circomkit.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"version": "2.1.4",
"verbose": true
"verbose": true,
"prime": "bn128",
"protocol": "groth16"
}
6 changes: 6 additions & 0 deletions circuits.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
"file": "multiplier",
"template": "Multiplier",
"params": [3]
},
"someArrays_2_3": {
"file": "someArrays",
"template": "SomeArrays",
"params": [2, 3],
"pubs": ["in1D", "in2D"]
}
}
6 changes: 6 additions & 0 deletions circuits/main/someArrays_2_3.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// auto-generated by circomkit
pragma circom 2.1.4;

include "../someArrays.circom";

component main {public[in1D, in2D]} = SomeArrays(2, 3);
20 changes: 20 additions & 0 deletions circuits/someArrays.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pragma circom 2.0.0;

template SomeArrays(N, M) {
signal input in;
signal input in1D[N];
signal input in2D[N][M];

in === 1;
for (var i = 1; i < N; i++) {
in1D[i-1] + 1 === in1D[i];
}

for (var i = 0; i < N; i++) {
for (var j = 1; j < M; j++) {
in2D[i][j-1] + 1 === in2D[i][j];
}
}

log(1, N, N + M);
}
9 changes: 9 additions & 0 deletions inputs/someArrays_2_3/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"in": 1,
"in1D": [2, 3],
"in2D": [
[4, 5],
[6, 7],
[8, 9]
]
}
22 changes: 12 additions & 10 deletions src/circomkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
} from './types/';
import {WitnessTester, ProofTester} from './testers/';
import {prettyStringify, primeToName} from './utils';
import {defaultConfig, colors, CURVES, PROTOCOLS} from './utils/config';
import {defaultConfig, colors, PRIMES, PROTOCOLS} from './utils/config';

/**
* Circomkit is an opinionated wrapper around many SnarkJS functions.
Expand Down Expand Up @@ -59,7 +59,7 @@ export class Circomkit {
this.snarkjsLogger = this.config.verbose ? this.logger : undefined;

// sanity checks
if (!CURVES.includes(this.config.prime)) {
if (!PRIMES.includes(this.config.prime)) {
throw new Error('Invalid prime in configuration.');
}
if (!PROTOCOLS.includes(this.config.protocol)) {
Expand Down Expand Up @@ -297,14 +297,16 @@ export class Circomkit {
if (this.config.protocol === 'fflonk') {
throw new Error('Exporting calldata is not supported for fflonk yet.');
}
const [pubs, proof] = (
await Promise.all(
(['pubs', 'proof'] as const)
.map(type => this.pathWithInput(circuit, input, type))
.map(path => readFile(path, 'utf-8'))
)
).map(content => JSON.parse(content));
return await snarkjs[this.config.protocol].exportSolidityCallData(proof, pubs);

const pubs: snarkjs.PublicSignals = JSON.parse(await readFile(this.pathWithInput(circuit, input, 'pubs'), 'utf-8'));
const proof: snarkjs.Groth16Proof & snarkjs.PlonkProof & snarkjs.FflonkProof = JSON.parse(
await readFile(this.pathWithInput(circuit, input, 'proof'), 'utf-8')
);
// TODO: we can write this ourselves by simply parsing the proof object, now that we know its type!
// this way, we may be able to fix the calldata issue as well for fflonk
const res = await snarkjs[this.config.protocol].exportSolidityCallData(proof, pubs);
// prettyCalldata(this.config.protocol, res);
return res;
}

/** Instantiate the `main` component.
Expand Down
14 changes: 12 additions & 2 deletions src/types/circomkit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type {LogLevelDesc} from 'loglevel';

/**
* Primes supported by Circom, as described for the `-p` option.
* @see https://github.com/iden3/circom/blob/master/program_structure/src/utils/constants.rs
*/
export type CircomkitPrimes = 'bn128' | 'bls12381' | 'goldilocks' | 'grumpkin' | 'pallas' | 'vesta' | 'secq256r1';

export type CircomkitProtocol = 'groth16' | 'plonk' | 'fflonk';

export type CircomkitConfig = {
/** Protocol to be used. */
protocol: 'groth16' | 'plonk' | 'fflonk';
protocol: CircomkitProtocol;
/** Underlying prime field. */
prime: 'bn128' | 'bls12381' | 'goldilocks';
prime: CircomkitPrimes;
/** Circuit configurations path. */
circuits: string;
/** Directory to read circuits from. */
Expand Down Expand Up @@ -39,6 +47,8 @@ export type CircomkitConfig = {
logLevel: LogLevelDesc;
/** Whether to generate the C witness calculator. */
cWitness: boolean;
/** Whether to print Solidity copy-pasteable calldata. */
prettyCalldata: false;
};

/** Shorthand notations for which path to build in Circomkit. These paths require a circuit name. */
Expand Down
14 changes: 14 additions & 0 deletions src/utils/calldata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import {CircomkitProtocol} from '../types';

// /**
// * Prettifies the calldata.
// *
// * Since calldata always has the public signals at the end, and no matter the dimensions of those signals
// * they always get flattened to a 1D array, we can find the start of public signals at the last `[`.
// *
// * @param protocol protocol
// * @param calldata exported calldata string
// */
// export function prettyCalldata(protocol: CircomkitProtocol, calldata: string) {
// // TODO: implement
// }
4 changes: 3 additions & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {LogLevelNames} from 'loglevel';
import type {CircomkitConfig} from '../types/';

export const PROTOCOLS = ['groth16', 'plonk', 'fflonk'] as const;
export const CURVES = ['bn128', 'bls12381', 'goldilocks'] as const;
export const PRIMES = ['bn128', 'bls12381', 'goldilocks', 'grumpkin', 'pallas', 'vesta', 'secq256r1'] as const;

/** Default configurations */
export const defaultConfig: Readonly<CircomkitConfig> = Object.seal({
Expand All @@ -24,6 +24,8 @@ export const defaultConfig: Readonly<CircomkitConfig> = Object.seal({
// groth16 phase-2 settings
groth16numContributions: 1,
groth16askForEntropy: false,
// solidity & calldata
prettyCalldata: false,
// logger
logLevel: 'INFO',
verbose: true,
Expand Down
10 changes: 9 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ export const primes: Record<CircomkitConfig['prime'], bigint> = {
bn128: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
bls12381: 52435875175126190479447740508185965837690552500527637822603658699938581184513n,
goldilocks: 18446744069414584321n,
};
grumpkin: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,
pallas: 28948022309329048855892746252171976963363056481941560715954676764349967630337n,
vesta: 28948022309329048855892746252171976963363056481941647379679742748393362948097n,
secq256r1: 115792089210356248762697446949407573530086143415290314195533631308867097853951n,
} as const;

/** A mapping from prime (decimals) to prime name as supported by Circom's `-p` option. */
export const primeToName: Record<`${bigint}`, CircomkitConfig['prime']> = {
'21888242871839275222246405745257275088548364400416034343698204186575808495617': 'bn128',
'52435875175126190479447740508185965837690552500527637822603658699938581184513': 'bls12381',
'18446744069414584321': 'goldilocks',
'21888242871839275222246405745257275088696311157297823662689037894645226208583': 'grumpkin',
'28948022309329048855892746252171976963363056481941560715954676764349967630337': 'pallas',
'28948022309329048855892746252171976963363056481941647379679742748393362948097': 'vesta',
'115792089210356248762697446949407573530086143415290314195533631308867097853951': 'secq256r1',
} as const;

/** JSON Stringify with a prettier format. */
Expand Down

0 comments on commit 4a1e998

Please sign in to comment.