wip hooking things up

This commit is contained in:
Andrew Morris
2025-01-29 17:43:30 +11:00
parent 7f4f3d2ff9
commit 922a84045a
4 changed files with 32 additions and 55 deletions

View File

@@ -4,10 +4,11 @@ type Module = {
emp?: {
circuit?: string;
input?: Uint8Array;
inputBitsStart?: number;
io?: IO;
handleOutput?: (value: Uint8Array) => void;
};
_run(party: number): void;
_run(party: number, size: number): void;
onRuntimeInitialized: () => void;
};
@@ -25,9 +26,11 @@ declare const createModule: () => Promise<Module>
* @returns A promise resolving with the output of the circuit (a 32-bit binary array).
*/
async function secure2PC(
party: 'alice' | 'bob',
party: number,
size: number,
circuit: string,
input: Uint8Array,
inputBitsStart: number,
io: IO,
): Promise<Uint8Array> {
const module = await createModule();
@@ -41,6 +44,7 @@ async function secure2PC(
const emp: {
circuit?: string;
input?: Uint8Array;
inputBitsStart?: number;
io?: IO;
handleOutput?: (value: Uint8Array) => void
} = {};
@@ -49,6 +53,7 @@ async function secure2PC(
emp.circuit = circuit;
emp.input = input;
emp.inputBitsStart = inputBitsStart;
emp.io = io;
const result = new Promise<Uint8Array>((resolve, reject) => {
@@ -56,7 +61,7 @@ async function secure2PC(
emp.handleOutput = resolve;
// TODO: emp.handleError
module._run(partyToIndex(party));
module._run(party, size);
} catch (error) {
reject(error);
}
@@ -69,25 +74,6 @@ async function secure2PC(
}
}
/**
* Maps a party ('alice' or 'bob') to an index number.
*
* @param party - The party ('alice' or 'bob').
* @returns 1 for 'alice', 2 for 'bob'.
* @throws Will throw an error if the party is invalid.
*/
function partyToIndex(party: 'alice' | 'bob'): number {
if (party === 'alice') {
return 1;
}
if (party === 'bob') {
return 2;
}
throw new Error(`Invalid party ${party} (must be 'alice' or 'bob')`);
}
let requestId = 0;
const pendingRequests: {
@@ -101,24 +87,24 @@ onmessage = async (event) => {
const message = event.data;
if (message.type === 'start') {
const { party, circuit, input } = message;
const { party, size, circuit, input, inputBitsStart } = message;
// Create a proxy IO object to communicate with the main thread
const io: IO = {
send: (data) => {
postMessage({ type: 'io_send', data });
send: (party2, channel, data) => {
postMessage({ type: 'io_send', party2, channel, data });
},
recv: (len) => {
recv: (party2, channel, len) => {
return new Promise((resolve, reject) => {
const id = requestId++;
pendingRequests[id] = { resolve, reject };
postMessage({ type: 'io_recv', len, id });
postMessage({ type: 'io_recv', party2, channel, len, id });
});
},
};
try {
const result = await secure2PC(party, circuit, input, io);
const result = await secure2PC(party, size, circuit, input, inputBitsStart, io);
postMessage({ type: 'result', result });
} catch (error) {
postMessage({ type: 'error', error: (error as Error).message });

View File

@@ -10,9 +10,11 @@ import type { IO } from "./types";
* @returns A promise resolving with the output of the circuit (a 32-bit binary array).
*/
export default async function nodeSecure2PC(
party: 'alice' | 'bob',
party: number,
size: number,
circuit: string,
input: Uint8Array,
inputBitsStart: number,
io: IO,
): Promise<Uint8Array> {
if (typeof process === 'undefined' || typeof process.versions === 'undefined' || !process.versions.node) {
@@ -24,6 +26,7 @@ export default async function nodeSecure2PC(
const emp: {
circuit?: string;
input?: Uint8Array;
inputBitsStart?: number;
io?: IO;
handleOutput?: (value: Uint8Array) => void
} = {};
@@ -32,6 +35,7 @@ export default async function nodeSecure2PC(
emp.circuit = circuit;
emp.input = input;
emp.inputBitsStart = inputBitsStart;
emp.io = io;
const result = await new Promise<Uint8Array>((resolve, reject) => {
@@ -39,7 +43,7 @@ export default async function nodeSecure2PC(
emp.handleOutput = resolve;
// TODO: emp.handleError
module._run(partyToIndex(party));
module._run(party, size);
} catch (error) {
reject(error);
}
@@ -47,22 +51,3 @@ export default async function nodeSecure2PC(
return result;
}
/**
* Maps a party ('alice' or 'bob') to an index number.
*
* @param party - The party ('alice' or 'bob').
* @returns 1 for 'alice', 2 for 'bob'.
* @throws Will throw an error if the party is invalid.
*/
function partyToIndex(party: 'alice' | 'bob'): number {
if (party === 'alice') {
return 1;
}
if (party === 'bob') {
return 2;
}
throw new Error(`Invalid party ${party} (must be 'alice' or 'bob')`);
}

View File

@@ -6,13 +6,15 @@ import nodeSecure2PC from "./nodeSecure2PC.js";
export type Secure2PC = typeof secure2PC;
export default function secure2PC(
party: 'alice' | 'bob',
party: number,
size: number,
circuit: string,
input: Uint8Array,
inputBitsStart: number,
io: IO,
): Promise<Uint8Array> {
if (typeof Worker === 'undefined') {
return nodeSecure2PC(party, circuit, input, io);
return nodeSecure2PC(party, size, circuit, input, inputBitsStart, io);
}
const ev = new EventEmitter<{ cleanup(): void }>();
@@ -27,8 +29,10 @@ export default function secure2PC(
worker.postMessage({
type: 'start',
party,
size,
circuit,
input,
inputBitsStart,
});
worker.onmessage = async (event) => {
@@ -36,11 +40,13 @@ export default function secure2PC(
if (message.type === 'io_send') {
// Forward the send request to the main thread's io.send
io.send(message.data);
const { party2, channel, data } = message;
io.send(party2, channel, data);
} else if (message.type === 'io_recv') {
const { party2, channel, len } = message;
// Handle the recv request from the worker
try {
const data = await io.recv(message.len);
const data = await io.recv(party2, channel, len);
worker.postMessage({ type: 'io_recv_response', id: message.id, data });
} catch (error) {
worker.postMessage({

View File

@@ -1,6 +1,6 @@
export type IO = {
send: (data: Uint8Array) => void;
recv: (len: number) => Promise<Uint8Array>;
send: (party2: number, channel: 'a' | 'b', data: Uint8Array) => void;
recv: (party2: number, channel: 'a' | 'b', len: number) => Promise<Uint8Array>;
on?: (event: 'error', listener: (error: Error) => void) => void;
off?: (event: 'error', listener: (error: Error) => void) => void;
close?: () => void;