Skip to content

Commit ab44419

Browse files
Harsh Bajpaibajpai244
authored andcommitted
feat(ENG-653): add method getTotatlConstraints to allow get total constraints of the circuit
1 parent ac5310c commit ab44419

File tree

7 files changed

+40
-27
lines changed

7 files changed

+40
-27
lines changed

src/circuit.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {CircuitConfig, Networks, Witness, ZK_PROOF} from "./types";
77
import {genGrothZKey, genPlonkZKey, genVerificationKey} from "./utils/zKey";
88
import {genGroth16Proof, genPlonkProof, verifyGroth16Proof, verifyPlonkProof} from "./utils/proof";
99
import * as fs from "fs";
10+
import {getTotalConstraints} from "./utils/r1cs";
1011

1112
export class Circuit {
1213
private _circuitConfig: CircuitConfig;
@@ -46,6 +47,14 @@ export class Circuit {
4647
return this._wasmTester.checkConstraints(w)
4748
}
4849

50+
async getTotalConstraints(): Promise<number>{
51+
if(!fs.existsSync(this._circuitConfig.r1csPath)){
52+
await this.compile()
53+
}
54+
55+
return getTotalConstraints(this._circuitConfig.r1csPath)
56+
}
57+
4958
private _genZKey() {
5059
log.info("generating zKey, ckt:%s, pTau:%s, zKey:%s",
5160
this._circuitConfig.cktName,

src/configParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export class ConfigParser {
144144
wasmPath: path.join(cktOutputDir, `${cktName}_js`, `${cktName}.wasm`),
145145
zKeyPath: path.join(cktOutputDir, "circuit_final.zkey"), // DO NOT change the names
146146
vKeyPath: path.join(cktOutputDir, "verification_key.json"), // DO NOT change the names
147+
r1csPath: path.join(cktOutputDir, `${cktName}.r1cs`),
147148
compileOptions: {
148149
include: [],
149150
snarkType: c.proofType ? c.proofType : "groth16",

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export type CircuitConfig = {
6464
wasmPath: string;
6565
zKeyPath: string;
6666
vKeyPath: string;
67-
compileOptions: CompileOptions
67+
compileOptions: CompileOptions;
68+
r1csPath:string
6869
}
6970

7071
export type CompileOptions = {
@@ -78,3 +79,7 @@ export type CompileOptions = {
7879
O: number;
7980
verbose: boolean;
8081
}
82+
83+
export type R1CSInfo = {
84+
85+
}

src/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * as cfg from '../configParser'
2-
export * as proof from './proof'
2+
export * as proof from './proof'
3+
export * as r1cs from "./r1cs"

src/utils/r1cs.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {r1cs} = require("snarkjs")
2+
const {info} = r1cs
3+
4+
5+
// TODO: provide typescript type for the return type
6+
const getR1CSInfo = async(r1csFilePath:string) => {
7+
return await info(r1csFilePath)
8+
}
9+
10+
export const getTotalConstraints = async (r1csFilePath: string): Promise<number> => {
11+
const {nConstraints} = await getR1CSInfo(r1csFilePath)
12+
return nConstraints
13+
}

tests/circuit.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ describe("Circuit test", () => {
121121
expect(verificationRes).toBe(true);
122122
}, 30000);
123123

124+
it("should calculate total constraints in a circuit", async () => {
125+
const c = new CircomJS(testConfigPath);
126+
const n1 =await c.getCircuit("mul").getTotalConstraints()
127+
const n1000 =await c.getCircuit("circ1000constraints").getTotalConstraints()
128+
129+
expect(n1).toEqual(1)
130+
expect(n1000).toEqual(1001)
131+
})
132+
124133
afterAll(() => {
125134
fs.rmSync(testConfigPath);
126135
});

tests/data/a1506726-6a14-4828-97de-7fb851c5fafe.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)