Skip to content

Commit

Permalink
fixed ptauId calculation for different proving systems
Browse files Browse the repository at this point in the history
  • Loading branch information
mllwchrry committed Nov 7, 2024
1 parent 6231b41 commit 1b98eef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
14 changes: 9 additions & 5 deletions src/core/setup/SetupProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import { HardhatZKitError } from "../../errors";
import { BN128_CURVE_NAME, PTAU_FILE_REG_EXP } from "../../constants";
import { Reporter } from "../../reporter";
import { PtauDownloader } from "../utils/PtauDownloader";
import { terminateCurve } from "../../utils/utils";
import { getNormalizedFullPath } from "../../utils/path-utils";
import { getPlonkConstraintsNumber } from "../../utils/constraints-utils";

import {
terminateCurve,
getNormalizedFullPath,
getGroth16ConstraintsNumber,
getPlonkConstraintsNumber,
} from "../../utils";

import { ICircuitArtifacts } from "../../types/artifacts/circuit-artifacts";
import { CircuitSetupInfo, SetupContributionSettings } from "../../types/core";
Expand Down Expand Up @@ -221,15 +225,15 @@ export class SetupProcessor {
circuitSetupInfoArr.map(async (setupInfo: CircuitSetupInfo) => {
return usePlonk
? getPlonkConstraintsNumber(setupInfo.r1csSourcePath, curve.Fr)
: setupInfo.circuitArtifact.baseCircuitInfo.constraintsNumber;
: getGroth16ConstraintsNumber(setupInfo.r1csSourcePath);
}),
);

await curve.terminate();

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

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

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

Expand Down
21 changes: 7 additions & 14 deletions src/utils/constraints-utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import * as snarkjs from "snarkjs";
// @ts-expect-error: No type definitions available for the "r1csfile" package
import * as r1csfile from "r1csfile";
// @ts-expect-error: No type definitions available for the "@iden3/binfileutils" package
import * as binfileutils from "@iden3/binfileutils";

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

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

export async function getGroth16ConstraintsNumber(r1csFilePath: string): Promise<number> {
const r1cs = await snarkjs.r1cs.info(r1csFilePath);

return r1cs.nConstraints + r1cs.nPubInputs + r1cs.nOutputs;
}

export async function getPlonkConstraintsNumber(r1csFilePath: string, Fr: any): Promise<number> {
const normalize = (lc: LinearCombination) => {
Object.keys(lc).forEach((key) => {
Expand Down Expand Up @@ -79,20 +81,11 @@ export async function getPlonkConstraintsNumber(r1csFilePath: string, Fr: any):
}
};

const { fd: fdR1cs, sections: sectionsR1cs } = await binfileutils.readBinFile(
r1csFilePath,
"r1cs",
1,
1 << 22,
1 << 24,
);
const r1cs = await r1csfile.readR1csFd(fdR1cs, sectionsR1cs, { loadConstraints: true, loadCustomGates: true });
const r1cs = await snarkjs.r1cs.info(r1csFilePath);

let plonkConstraintsCount = r1cs.nOutputs + r1cs.nPubInputs;

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

await fdR1cs.fd.close();

return plonkConstraintsCount;
}

0 comments on commit 1b98eef

Please sign in to comment.