Skip to content

Commit

Permalink
ptauId calculation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mllwchrry committed Nov 6, 2024
1 parent 8ad4219 commit 6231b41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/core/compile/CompilationProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import { HardhatZKitError } from "../../errors";
import { CIRCUIT_ARTIFACT_VERSION, NODE_MODULES } from "../../constants";
import { Reporter } from "../../reporter";

import { getGroth16ConstraintsNumber } from "../../utils/constraints-utils";
import { getNormalizedFullPath, renameFilesRecursively, readDirRecursively } from "../../utils/path-utils";
import {
getNormalizedFullPath,
renameFilesRecursively,
readDirRecursively,
getR1CSConstraintsNumber,
terminateCurve,
} from "../../utils";

import { ZKitConfig } from "../../types/zkit-config";
import { ArtifactsFileType, CircuitArtifact, ICircuitArtifacts } from "../../types/artifacts/circuit-artifacts";
Expand Down Expand Up @@ -104,9 +109,11 @@ export class CompilationProcessor {

for (const info of compilationInfoArr) {
const r1csFilePath = getNormalizedFullPath(info.tempArtifactsPath, `${info.circuitName}.r1cs`);
info.constraintsNumber = await getGroth16ConstraintsNumber(r1csFilePath);
info.constraintsNumber = await getR1CSConstraintsNumber(r1csFilePath);
}

await terminateCurve();

await this._moveFromTempDirToArtifacts(compilationInfoArr);

await this._emitArtifacts(compilationInfoArr);
Expand Down
2 changes: 1 addition & 1 deletion src/core/setup/SetupProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class SetupProcessor {

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

const ptauId = Math.max(Math.ceil(Math.log2(maxConstraintsNumber)), 8);
const ptauId = Math.max(Math.ceil(Math.log2(maxConstraintsNumber - 1)) + 1, 8);

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

Expand Down
19 changes: 12 additions & 7 deletions src/utils/constraints-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as binfileutils from "@iden3/binfileutils";

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

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

Expand All @@ -19,10 +19,13 @@ export async function getPlonkConstraintsNumber(r1csFilePath: string, Fr: any):

const join = (lc1: LinearCombination, k: bigint, lc2: LinearCombination) => {
const res = { ...lc1 };

Object.keys(lc2).forEach((s) => {
res[s] = res[s] ? Fr.add(res[s], Fr.mul(k, lc2[s])) : lc2[s];
});

normalize(res);

return res;
};

Expand All @@ -45,12 +48,14 @@ export async function getPlonkConstraintsNumber(r1csFilePath: string, Fr: any):
let n = 0;

Object.keys(lc).forEach((key) => {
if (lc[key] !== 0n) {
if (key === "0") {
k = Fr.add(k, lc[key]);
} else {
n++;
}
if (lc[key] === 0n) {
return;
}

if (key === "0") {
k = Fr.add(k, lc[key]);
} else {
n++;
}
});

Expand Down

0 comments on commit 6231b41

Please sign in to comment.