Skip to content

Commit 1b98eef

Browse files
committed
fixed ptauId calculation for different proving systems
1 parent 6231b41 commit 1b98eef

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/core/setup/SetupProcessor.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import { HardhatZKitError } from "../../errors";
1111
import { BN128_CURVE_NAME, PTAU_FILE_REG_EXP } from "../../constants";
1212
import { Reporter } from "../../reporter";
1313
import { PtauDownloader } from "../utils/PtauDownloader";
14-
import { terminateCurve } from "../../utils/utils";
15-
import { getNormalizedFullPath } from "../../utils/path-utils";
16-
import { getPlonkConstraintsNumber } from "../../utils/constraints-utils";
14+
15+
import {
16+
terminateCurve,
17+
getNormalizedFullPath,
18+
getGroth16ConstraintsNumber,
19+
getPlonkConstraintsNumber,
20+
} from "../../utils";
1721

1822
import { ICircuitArtifacts } from "../../types/artifacts/circuit-artifacts";
1923
import { CircuitSetupInfo, SetupContributionSettings } from "../../types/core";
@@ -221,15 +225,15 @@ export class SetupProcessor {
221225
circuitSetupInfoArr.map(async (setupInfo: CircuitSetupInfo) => {
222226
return usePlonk
223227
? getPlonkConstraintsNumber(setupInfo.r1csSourcePath, curve.Fr)
224-
: setupInfo.circuitArtifact.baseCircuitInfo.constraintsNumber;
228+
: getGroth16ConstraintsNumber(setupInfo.r1csSourcePath);
225229
}),
226230
);
227231

228232
await curve.terminate();
229233

230234
const maxConstraintsNumber = Math.max(...circuitsConstraintsNumber);
231235

232-
const ptauId = Math.max(Math.ceil(Math.log2(maxConstraintsNumber - 1)) + 1, 8);
236+
const ptauId = Math.max(Math.floor(Math.log2(maxConstraintsNumber - (usePlonk ? 1 : 0))) + 1, 8);
233237

234238
let entries: fsExtra.Dirent[] = [];
235239

src/utils/constraints-utils.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import * as snarkjs from "snarkjs";
2-
// @ts-expect-error: No type definitions available for the "r1csfile" package
3-
import * as r1csfile from "r1csfile";
4-
// @ts-expect-error: No type definitions available for the "@iden3/binfileutils" package
5-
import * as binfileutils from "@iden3/binfileutils";
62

73
import { LinearCombination, R1CSConstraint } from "../../src/types/utils";
84

95
export async function getR1CSConstraintsNumber(r1csFilePath: string): Promise<number> {
106
return (await snarkjs.r1cs.info(r1csFilePath)).nConstraints;
117
}
128

9+
export async function getGroth16ConstraintsNumber(r1csFilePath: string): Promise<number> {
10+
const r1cs = await snarkjs.r1cs.info(r1csFilePath);
11+
12+
return r1cs.nConstraints + r1cs.nPubInputs + r1cs.nOutputs;
13+
}
14+
1315
export async function getPlonkConstraintsNumber(r1csFilePath: string, Fr: any): Promise<number> {
1416
const normalize = (lc: LinearCombination) => {
1517
Object.keys(lc).forEach((key) => {
@@ -79,20 +81,11 @@ export async function getPlonkConstraintsNumber(r1csFilePath: string, Fr: any):
7981
}
8082
};
8183

82-
const { fd: fdR1cs, sections: sectionsR1cs } = await binfileutils.readBinFile(
83-
r1csFilePath,
84-
"r1cs",
85-
1,
86-
1 << 22,
87-
1 << 24,
88-
);
89-
const r1cs = await r1csfile.readR1csFd(fdR1cs, sectionsR1cs, { loadConstraints: true, loadCustomGates: true });
84+
const r1cs = await snarkjs.r1cs.info(r1csFilePath);
9085

9186
let plonkConstraintsCount = r1cs.nOutputs + r1cs.nPubInputs;
9287

9388
r1cs.constraints.forEach((constraint: R1CSConstraint) => process(...constraint));
9489

95-
await fdR1cs.fd.close();
96-
9790
return plonkConstraintsCount;
9891
}

0 commit comments

Comments
 (0)