Skip to content

Commit 327139d

Browse files
authored
refactor: simpler native proof decoding (#16)
1 parent b2d6741 commit 327139d

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

packages/contracts/sdk/mpc/MpcNetworkService.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { omit } from "lodash";
55
import fs from "node:fs";
66
import os from "node:os";
77
import path from "node:path";
8-
import { z } from "zod";
9-
import { promiseWithResolvers } from "../utils.js";
8+
import { decodeNativeHonkProof, promiseWithResolvers } from "../utils.js";
109
import { inWorkingDir, makeRunCommand, splitInput } from "./utils.js";
1110

1211
export class MpcProverService {
@@ -143,27 +142,11 @@ async function proveAsParty(params: {
143142
`./run-party.sh ${workingDir} ${circuitPath} ${params.partyIndex}`,
144143
);
145144

146-
const publicInputs = z
147-
.string()
148-
.array()
149-
.parse(
150-
JSON.parse(
151-
fs.readFileSync(path.join(workingDir, "public_input.json"), "utf-8"),
152-
),
153-
);
154-
const proofData = Uint8Array.from(
145+
const { proof, publicInputs } = decodeNativeHonkProof(
155146
fs.readFileSync(
156147
path.join(workingDir, `proof.${params.partyIndex}.proof`),
157148
),
158149
);
159-
// arcane magic
160-
const proof = ethers.getBytes(
161-
ethers.concat([
162-
proofData.slice(0, 2),
163-
proofData.slice(6, 100),
164-
proofData.slice(100 + publicInputs.length * 32),
165-
]),
166-
);
167150

168151
// pre-verify proof
169152
const backend = new UltraHonkBackend(params.circuit.bytecode, {
@@ -172,16 +155,7 @@ async function proveAsParty(params: {
172155
let verified: boolean;
173156
try {
174157
verified = await backend.verifyProof(
175-
{
176-
// prepend length as 4 bytes
177-
proof: ethers.getBytes(
178-
ethers.concat([
179-
ethers.zeroPadValue(ethers.toBeArray(proof.length), 4),
180-
proof,
181-
]),
182-
),
183-
publicInputs,
184-
},
158+
{ proof, publicInputs },
185159
{ keccak: true },
186160
);
187161
} catch (e: any) {
@@ -196,8 +170,10 @@ async function proveAsParty(params: {
196170
throw new Error("mpc generated invalid proof: returned false");
197171
}
198172

199-
// console.log("proof native\n", JSON.stringify(Array.from(proof)));
200-
return { proof, publicInputs };
173+
return {
174+
proof: proof.slice(4), // remove length
175+
publicInputs,
176+
};
201177
});
202178
}
203179

packages/contracts/sdk/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Fr } from "@aztec/aztec.js";
2+
import { splitHonkProof } from "@aztec/bb.js";
23
import type { InputMap } from "@noir-lang/noir_js";
34
import { ethers } from "ethers";
45
import { assert } from "ts-essentials";
@@ -91,3 +92,22 @@ export function promiseWithResolvers<T>(): {
9192
});
9293
return ret;
9394
}
95+
96+
export function decodeNativeHonkProof(nativeProof: Uint8Array) {
97+
const { proof, publicInputs: publicInputsRaw } = splitHonkProof(nativeProof);
98+
const publicInputs = deflattenFields(publicInputsRaw);
99+
return { proof, publicInputs };
100+
}
101+
102+
// TODO: import from @aztec/bb.js when available
103+
function deflattenFields(flattenedFields: Uint8Array): string[] {
104+
const publicInputSize = 32;
105+
const chunkedFlattenedPublicInputs: Uint8Array[] = [];
106+
107+
for (let i = 0; i < flattenedFields.length; i += publicInputSize) {
108+
const publicInput = flattenedFields.slice(i, i + publicInputSize);
109+
chunkedFlattenedPublicInputs.push(publicInput);
110+
}
111+
112+
return chunkedFlattenedPublicInputs.map((x) => ethers.hexlify(x));
113+
}

0 commit comments

Comments
 (0)