Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmisar committed Feb 6, 2025
1 parent 0cab5e7 commit dc09b4e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 110 deletions.
26 changes: 5 additions & 21 deletions packages/contracts/sdk/LobService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assert, type AsyncOrSync } from "ts-essentials";
import { type PoolERC20 } from "../typechain-types";
import { NoteInputStruct } from "../typechain-types/contracts/PoolERC20";
import { MpcProverService, type Side } from "./mpc/MpcNetworkService.js";
import { splitInput, splitInput2 } from "./mpc/utils.js";
import { splitInput } from "./mpc/utils.js";
import { type ITreesService } from "./RemoteTreesService.js";
import {
CompleteWaAddress,
Expand All @@ -13,6 +13,7 @@ import {
type NoirAndBackend,
type PoolErc20Service,
} from "./RollupService.js";
import { prove } from "./utils.js";

export class LobService {
constructor(
Expand Down Expand Up @@ -69,19 +70,15 @@ export class LobService {
randomness: buyerRandomness,
};

const inputPublic = {
const input = {
tree_roots: await this.trees.getTreeRoots(),
};
const input0 = {
seller_secret_key: params.sellerSecretKey,
seller_note: await this.poolErc20.toNoteConsumptionInputs(
params.sellerSecretKey,
params.sellerNote,
),
seller_order,
seller_randomness: sellerRandomness,
};
const input1 = {
buyer_secret_key: params.buyerSecretKey,
buyer_note: await this.poolErc20.toNoteConsumptionInputs(
params.buyerSecretKey,
Expand All @@ -90,19 +87,7 @@ export class LobService {
buyer_order,
buyer_randomness: buyerRandomness,
};
const inputs0Shared = await splitInput(swapCircuit.circuit, {
// merge public inputs into first input because it does not matter how public inputs are passed
...input0,
...inputPublic,
});
const inputs1Shared = await splitInput(swapCircuit.circuit, input1);
// const { proof } = await prove("swap", swapCircuit, input);
const { proof } = await this.mpcProver.prove({
circuit: swapCircuit.circuit,
inputs0Shared,
inputs1Shared,
numPublicInputs: 8,
});
const { proof } = await prove("swap", swapCircuit, input);
const noteInputs: [
NoteInputStruct,
NoteInputStruct,
Expand Down Expand Up @@ -176,7 +161,7 @@ export class LobService {
tree_roots: await this.trees.getTreeRoots(),
}
: undefined;
const inputsShared = await splitInput2(swapCircuit.circuit, {
const inputsShared = await splitInput(swapCircuit.circuit, {
// merge public inputs into first input because it does not matter how public inputs are passed
...input,
...inputPublic,
Expand All @@ -194,7 +179,6 @@ export class LobService {
});
}),
);
console.log("got proofs", proofs.length);
assert(uniq(proofs).length === 1, "proofs mismatch");
const proof = proofs[0]!;
return {
Expand Down
34 changes: 4 additions & 30 deletions packages/contracts/sdk/mpc/MpcNetworkService.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { CompiledCircuit } from "@noir-lang/noir_js";
import { ethers } from "ethers";
import { omit, range } from "lodash";
import { omit } from "lodash";
import fs from "node:fs";
import path from "node:path";
import { assert } from "ts-essentials";
import { z } from "zod";
import { promiseWithResolvers } from "../utils.js";
import { inWorkingDir, makeRunCommand } from "./utils.js";

export type OrderId = string & { __brand: "OrderId" };
export type PartyIndex = 0 | 1 | 2;
/**
* Deterministically determined based on the tokens being swapped
*/
export type Side = "seller" | "buyer";

type Order = {
Expand Down Expand Up @@ -97,7 +99,6 @@ export class MpcProverService {
input1Shared: inputsShared[1],
numPublicInputs: params.numPublicInputs,
});
console.log("got proof", orderId, params.partyIndex, proof.length);
const proofHex = ethers.hexlify(proof);
order.result.resolve(proofHex);
otherOrder.result.resolve(proofHex);
Expand Down Expand Up @@ -166,31 +167,4 @@ export class MpcProverService {
return { proof, publicInputs };
});
}

async prove(params: {
circuit: CompiledCircuit;
inputs0Shared: string[];
inputs1Shared: string[];
// TODO: infer number of public inputs
numPublicInputs: number;
}) {
assert(
params.inputs0Shared.length === params.inputs1Shared.length,
"inputs length mismatch",
);
const proofs = await Promise.all(
range(params.inputs0Shared.length).map(async (i) => {
const input0Shared = params.inputs0Shared[i]!;
const input1Shared = params.inputs1Shared[i]!;
return await this.proveAsParty({
partyIndex: i,
circuit: params.circuit,
input0Shared,
input1Shared,
numPublicInputs: params.numPublicInputs,
});
}),
);
return proofs[0]!;
}
}
47 changes: 0 additions & 47 deletions packages/contracts/sdk/mpc/run.sh

This file was deleted.

16 changes: 4 additions & 12 deletions packages/contracts/sdk/mpc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import path from "node:path";
import toml from "smol-toml";
import type { PartyIndex } from "./MpcNetworkService.js";

/**
* @deprecated use {@link splitInput2} instead
*/
export async function splitInput(circuit: CompiledCircuit, input: InputMap) {
return await inWorkingDir(async (workingDir) => {
const proverPath = path.join(workingDir, "ProverX.toml");
Expand All @@ -21,18 +18,13 @@ export async function splitInput(circuit: CompiledCircuit, input: InputMap) {
const x = Uint8Array.from(fs.readFileSync(`${proverPath}.${i}.shared`));
return ethers.hexlify(x);
});
return shared;
return Array.from(shared.entries()).map(([partyIndex, inputShared]) => ({
partyIndex: partyIndex as PartyIndex,
inputShared,
}));
});
}

export async function splitInput2(circuit: CompiledCircuit, input: InputMap) {
const shared = await splitInput(circuit, input);
return Array.from(shared.entries()).map(([partyIndex, inputShared]) => ({
partyIndex: partyIndex as PartyIndex,
inputShared,
}));
}

export async function inWorkingDir<T>(f: (workingDir: string) => Promise<T>) {
const id = crypto.randomUUID();
const workingDir = path.join(__dirname, "work-dirs", id);
Expand Down

0 comments on commit dc09b4e

Please # to comment.