diff --git a/src/core/compile/CompilationProcessor.ts b/src/core/compile/CompilationProcessor.ts index 45f4729..f596053 100644 --- a/src/core/compile/CompilationProcessor.ts +++ b/src/core/compile/CompilationProcessor.ts @@ -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"; @@ -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); diff --git a/src/core/setup/SetupProcessor.ts b/src/core/setup/SetupProcessor.ts index c4da17d..f8a3358 100644 --- a/src/core/setup/SetupProcessor.ts +++ b/src/core/setup/SetupProcessor.ts @@ -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[] = []; diff --git a/src/utils/constraints-utils.ts b/src/utils/constraints-utils.ts index 57906cc..fecf8af 100644 --- a/src/utils/constraints-utils.ts +++ b/src/utils/constraints-utils.ts @@ -6,7 +6,7 @@ import * as binfileutils from "@iden3/binfileutils"; import { LinearCombination, R1CSConstraint } from "../../src/types/utils"; -export async function getGroth16ConstraintsNumber(r1csFilePath: string): Promise { +export async function getR1CSConstraintsNumber(r1csFilePath: string): Promise { return (await snarkjs.r1cs.info(r1csFilePath)).nConstraints; } @@ -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; }; @@ -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++; } });