Connect io properly, connect exceptions, remove debugging output

This commit is contained in:
Andrew Morris
2025-01-30 13:30:14 +11:00
parent 604d69b53f
commit 162a1f8940
16 changed files with 703 additions and 536 deletions

View File

@@ -171,12 +171,22 @@ std::vector<bool> get_input_bits() {
return input_bits;
}
EM_JS(int, get_input_bits_start, (), {
if (!Module.emp?.inputBitsStart) {
throw new Error("Module.emp.inputBitsStart is not defined in JavaScript.");
EM_JS(size_t, get_input_bits_per_party, (int i), {
if (!Module.emp?.inputBitsPerParty) {
throw new Error("Module.emp.inputBitsPerParty is not defined in JavaScript.");
}
return Module.emp.inputBitsStart;
if (i >= Module.emp.inputBitsPerParty.length) {
throw new Error("Index out of bounds for Module.emp.inputBitsPerParty.");
}
const res = Module.emp.inputBitsPerParty[i];
if (res < 0) {
throw new Error("Negative value for Module.emp.inputBitsPerParty.");
}
return res;
});
EM_JS(void, handle_output_bits_raw, (uint8_t* outputBits, int length), {
@@ -191,6 +201,14 @@ EM_JS(void, handle_output_bits_raw, (uint8_t* outputBits, int length), {
Module.emp.handleOutput(outputBitsArray);
});
EM_JS(void, handle_error, (const char* message), {
if (!Module.emp?.handleError) {
throw new Error("Module.emp.handleError is not defined in JavaScript.");
}
Module.emp.handleError(new Error(UTF8ToString(message)));
});
void handle_output_bits(const std::vector<bool>& output_bits) {
uint8_t* output_bits_raw = new uint8_t[output_bits.size()];
@@ -221,49 +239,58 @@ extern "C" {
}
void run_impl(int party, int nP) {
std::cout << "x0" << std::endl;
std::shared_ptr<IMultiIO> io = std::make_shared<MultiIOJS>(party, nP);
try {
std::shared_ptr<IMultiIO> io = std::make_shared<MultiIOJS>(party, nP);
auto circuit = get_circuit();
auto mpc = CMPC(io, &circuit);
std::cout << "x0.5" << std::endl;
auto circuit = get_circuit();
std::cout << "x0.7" << std::endl;
mpc.function_independent();
mpc.function_dependent();
auto mpc = CMPC(io, &circuit);
std::cout << "x1" << std::endl;
std::vector<bool> input_bits = get_input_bits();
mpc.function_independent();
std::cout << "x2" << std::endl;
mpc.function_dependent();
std::cout << "x3" << std::endl;
FlexIn input(nP, circuit.n1 + circuit.n2, party);
std::vector<bool> input_bits = get_input_bits();
int input_bits_start = get_input_bits_start();
int bit_pos = 0;
for (int p = 0; p < nP; p++) {
size_t input_count = get_input_bits_per_party(p);
FlexIn input(nP, circuit.n1 + circuit.n2, party);
if (p + 1 == party) {
assert(input_count == input_bits.size());
}
// TODO: Assign party for all input bits
for (size_t i = 0; i < input_bits.size(); i++) {
size_t x = i + input_bits_start;
input.assign_party(x, party);
input.assign_plaintext_bit(x, input_bits[i]);
for (size_t i = 0; i < input_count; i++) {
input.assign_party(bit_pos, p + 1);
if (p + 1 == party) {
input.assign_plaintext_bit(bit_pos, input_bits[i]);
}
bit_pos++;
}
}
assert(bit_pos == circuit.n1 + circuit.n2);
FlexOut output(nP, circuit.n3, party);
for (int i = 0; i < circuit.n3; i++) {
// All parties receive the output.
output.assign_party(i, 0);
}
mpc.online(&input, &output);
std::vector<bool> output_bits;
for (int i = 0; i < circuit.n3; i++) {
output_bits.push_back(output.get_plaintext_bit(i));
}
handle_output_bits(output_bits);
} catch (const std::exception& e) {
handle_error(e.what());
}
FlexOut output(nP, circuit.n3, party);
for (int i = 0; i < circuit.n3; i++) {
// All parties receive the output.
output.assign_party(i, 0);
}
mpc.online(&input, &output);
std::vector<bool> output_bits;
for (int i = 0; i < circuit.n3; i++) {
output_bits.push_back(output.get_plaintext_bit(i));
}
handle_output_bits(output_bits);
}
int main() {

67
programs/test_m2pc.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include <emp-tool/emp-tool.h>
#include "emp-agmpc/emp-agmpc.h"
using namespace std;
using namespace emp;
const string circuit_file_location = "circuits/adder_32bit.txt";;
int main(int argc, char** argv) {
int port, party;
parse_party_and_port(argv, &party, &port);
const static int nP = 2;
std::shared_ptr<IMultiIO> io = std::make_shared<NetIOMP>(nP, party, port);
BristolFormat cf(circuit_file_location.c_str());
CMPC* mpc = new CMPC(io, &cf);
cout <<"Setup:\t"<<party<<"\n";
mpc->function_independent();
cout <<"FUNC_IND:\t"<<party<<"\n";
mpc->function_dependent();
cout <<"FUNC_DEP:\t"<<party<<"\n";
// The split of input into n1 and n2 is meaningless here,
// what matters is that there are n1+n2 input bits.
FlexIn input(nP, cf.n1 + cf.n2, party);
for (int i = 0; i < cf.n1 + cf.n2; i++) {
if (i < 32) {
input.assign_party(i, 1);
if (party == 1) {
// 3
input.assign_plaintext_bit(i, i == 0 || i == 1);
}
} else {
input.assign_party(i, 2);
if (party == 2) {
// 5
input.assign_plaintext_bit(i, i == 32 || i == 34);
}
}
}
FlexOut output(nP, cf.n3, party);
for (int i = 0; i < cf.n3; i++) {
// All parties receive the output.
output.assign_party(i, 0);
}
mpc->online(&input, &output);
uint64_t band2 = count_multi_io(*io);
cout <<"bandwidth\t"<<party<<"\t"<<band2<<endl;
cout <<"ONLINE:\t"<<party<<"\n";
string res = "";
for(int i = 0; i < cf.n3; ++i)
res += (output.get_plaintext_bit(i)?"1":"0");
cout << hex_to_binary(string(out3))<<endl;
cout << res<<endl;
delete mpc;
return 0;
}

View File

@@ -3,14 +3,14 @@
using namespace std;
using namespace emp;
const string circuit_file_location = "circuits/sha-1.txt";;
const string circuit_file_location = "circuits/adder_32bit.txt";;
static char out3[] = "92b404e556588ced6c1acd4ebf053f6809f73a93";//bafbc2c87c33322603f38e06c3e0f79c1f1b1475";
int main(int argc, char** argv) {
int port, party;
parse_party_and_port(argv, &party, &port);
const static int nP = 4;
const static int nP = 2;
std::shared_ptr<IMultiIO> io = std::make_shared<NetIOMP>(nP, party, port);
BristolFormat cf(circuit_file_location.c_str());
@@ -28,31 +28,31 @@ int main(int argc, char** argv) {
FlexIn input(nP, cf.n1 + cf.n2, party);
for (int i = 0; i < cf.n1 + cf.n2; i++) {
if (i < 100) {
if (i < 32) {
input.assign_party(i, 1);
if (party == 1) {
input.assign_plaintext_bit(i, false);
input.assign_plaintext_bit(i, i == 0 || i == 1);
}
} else if (i < 200) {
} else {
input.assign_party(i, 2);
if (party == 2) {
input.assign_plaintext_bit(i, false);
input.assign_plaintext_bit(i, i == 32 || i == 34);
}
} else if (i < 300) {
input.assign_party(i, 3);
} //else if (i < 300) {
// input.assign_party(i, 3);
if (party == 3) {
input.assign_plaintext_bit(i, false);
}
} else {
input.assign_party(i, 4);
// if (party == 3) {
// input.assign_plaintext_bit(i, false);
// }
// } else {
// input.assign_party(i, 4);
if (party == 4) {
input.assign_plaintext_bit(i, false);
}
}
// if (party == 4) {
// input.assign_plaintext_bit(i, false);
// }
// }
}
FlexOut output(nP, cf.n3, party);
@@ -72,7 +72,7 @@ int main(int argc, char** argv) {
res += (output.get_plaintext_bit(i)?"1":"0");
cout << hex_to_binary(string(out3))<<endl;
cout << res<<endl;
cout << (res == hex_to_binary(string(out3))? "GOOD!":"BAD!")<<endl<<flush;
// cout << (res == hex_to_binary(string(out3))? "GOOD!":"BAD!")<<endl<<flush;
delete mpc;
return 0;

View File

@@ -110,7 +110,7 @@ async function shell(cmd: string, args: string[], cwd: string): Promise<void> {
async function fixEmscriptenCode(gitRoot: string) {
await replaceInFile(
join(gitRoot, 'build/jslib.js'),
'var fs=require("fs")',
['var fs=require("fs")', "var fs = require('fs')"],
// This doesn't really affect behavior, but it fixes a nextjs issue where
// it analyzes the require statically and fails even when the code works as
// a whole.
@@ -118,17 +118,23 @@ async function fixEmscriptenCode(gitRoot: string) {
);
}
async function replaceInFile(path: string, search: string, replace: string) {
async function replaceInFile(path: string, searches: string[], replace: string) {
const content = await fs.readFile(path, 'utf-8');
const parts = content.split(search);
if (parts.length === 1) {
throw new Error(`Search string not found in file: ${search}`);
for (const search of searches) {
const parts = content.split(search);
if (parts.length === 1) {
continue;
}
const updatedContent = parts.join(replace);
await fs.writeFile(path, updatedContent, 'utf-8');
return;
}
const updatedContent = parts.join(replace);
await fs.writeFile(path, updatedContent, 'utf-8');
throw new Error(`Search strings not found in file: ${JSON.stringify(searches)}`)
}
build().catch(console.error);

19
scripts/build_m2pc.cpp Normal file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
set -euo pipefail
# Note: -D__debug does not seem to impact performance, and is good for testing
clang++ \
-O3 \
-std=c++17 \
-D__debug \
programs/test_m2pc.cpp \
-I src/cpp \
-I $(brew --prefix mbedtls)/include \
-L $(brew --prefix mbedtls)/lib \
-lmbedtls \
-lmbedcrypto \
-lmbedx509 \
-o build/m2pc
echo "Build successful, use ./scripts/m2pc_test.sh to run the program."

View File

@@ -1,5 +1,7 @@
#!/bin/bash
ARG1=$1
set -euo pipefail
# Variables
@@ -13,9 +15,20 @@ fi
mkdir -p build
CONDITIONAL_OPTS=""
if [ "$ARG1" == "" ]; then
CONDITIONAL_OPTS="-O3"
elif [ "$ARG1" == "debug" ]; then
CONDITIONAL_OPTS="-g -D__debug"
else
echo "Invalid argument"
exit 1
fi
# Emscripten build
em++ programs/jslib.cpp -sASYNCIFY -o build/jslib.js \
-O3 \
$CONDITIONAL_OPTS \
-Wall \
-Wextra \
-pedantic \
@@ -29,8 +42,10 @@ em++ programs/jslib.cpp -sASYNCIFY -o build/jslib.js \
-lembind \
-s SINGLE_FILE=1 \
-s ENVIRONMENT='web,worker,node' \
-sNO_DISABLE_EXCEPTION_CATCHING \
-sASSERTIONS=1 \
-sSTACK_SIZE=8388608 \
-sASYNCIFY_STACK_SIZE=16384 \
-sEXPORTED_FUNCTIONS=['_js_malloc','_main'] \
-sEXPORTED_RUNTIME_METHODS=['HEAPU8','setValue'] \
-s MODULARIZE=1 \

27
scripts/m2pc_test.sh Normal file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
set -euo pipefail
# Define the programs to run
PROGRAM_A="./build/m2pc 1 8005"
PROGRAM_B="./build/m2pc 2 8005"
# Run 3 instances of the program in the background and print output as it comes
$PROGRAM_A 2>&1 | sed 's/^/A: /' &
PID1=$!
$PROGRAM_B 2>&1 | sed 's/^/B: /' &
PID2=$!
# Function to abort everything if a process fails
abort() {
echo "Aborting..."
kill $PID1 $PID2 2>/dev/null
wait $PID1 $PID2 2>/dev/null
exit 1
}
# Wait for all processes to complete, abort if any fail
wait $PID1 || abort
wait $PID2 || abort
echo "Finished"

View File

@@ -105,8 +105,7 @@ block sampleRandom(int nP, IMultiIO& io, PRG * prg, int party) {
}
bool cheat = checkCheat(res2);
if(cheat) {
cout <<"cheat in sampleRandom\n"<<flush;
exit(0);
throw std::runtime_error("cheat in sampleRandom");
}
for(int i = 2; i <= nP; ++i)
S[1] = S[1] ^ S[i];

View File

@@ -371,7 +371,8 @@ class CMPC { public:
mask_input[cf->gates[4*i+2]] = mask_input[cf->gates[4*i+2]] != false;
else if(cmpBlock(&H.at(1), &t0, 1))
mask_input[cf->gates[4*i+2]] = mask_input[cf->gates[4*i+2]] != true;
else {cout <<ands <<"no match GT!"<<endl<<flush;
else {
throw std::runtime_error("no match GT!");
}
}
ands++;

View File

@@ -17,7 +17,7 @@ namespace emp {
template<typename T>
void inline delete_array_null(T * ptr);
inline void error(const char * s, int line = 0, const char * file = nullptr);
inline void error(const char * s);
template<class... Ts>
void run_function(void *function, const Ts&... args);

View File

@@ -19,12 +19,8 @@ inline double time_from(const time_point<high_resolution_clock>& s) {
return std::chrono::duration_cast<std::chrono::microseconds>(high_resolution_clock::now() - s).count();
}
inline void error(const char * s, int line, const char * file) {
fprintf(stderr, s, "\n");
if(file != nullptr) {
fprintf(stderr, "at %d, %s\n", line, file);
}
exit(1);
inline void error(const char * s) {
throw std::runtime_error(s);
}
inline void parse_party_and_port(const char *const * arg, int * party, int * port) {

View File

@@ -4,7 +4,7 @@ type Module = {
emp?: {
circuit?: string;
input?: Uint8Array;
inputBitsStart?: number;
inputBitsPerParty?: number[];
io?: IO;
handleOutput?: (value: Uint8Array) => void;
};
@@ -30,7 +30,7 @@ async function secureMPC(
size: number,
circuit: string,
input: Uint8Array,
inputBitsStart: number,
inputBitsPerParty: number[],
io: IO,
): Promise<Uint8Array> {
const module = await createModule();
@@ -44,22 +44,23 @@ async function secureMPC(
const emp: {
circuit?: string;
input?: Uint8Array;
inputBitsStart?: number;
inputBitsPerParty?: number[];
io?: IO;
handleOutput?: (value: Uint8Array) => void
handleError?: (error: Error) => void;
} = {};
module.emp = emp;
emp.circuit = circuit;
emp.input = input;
emp.inputBitsStart = inputBitsStart;
emp.inputBitsPerParty = inputBitsPerParty;
emp.io = io;
const result = new Promise<Uint8Array>((resolve, reject) => {
try {
emp.handleOutput = resolve;
// TODO: emp.handleError
emp.handleError = reject;
module._run(party, size);
} catch (error) {
@@ -87,7 +88,7 @@ onmessage = async (event) => {
const message = event.data;
if (message.type === 'start') {
const { party, size, circuit, input, inputBitsStart } = message;
const { party, size, circuit, input, inputBitsPerParty } = message;
// Create a proxy IO object to communicate with the main thread
const io: IO = {
@@ -104,7 +105,7 @@ onmessage = async (event) => {
};
try {
const result = await secureMPC(party, size, circuit, input, inputBitsStart, io);
const result = await secureMPC(party, size, circuit, input, inputBitsPerParty, io);
postMessage({ type: 'result', result });
} catch (error) {
postMessage({ type: 'error', error: (error as Error).message });

View File

@@ -1,47 +1,53 @@
// import { DataConnection, Peer } from 'peerjs';
import { DataConnection, Peer } from 'peerjs';
// import BufferedIO from "./BufferedIO.js";
// import BufferQueue from "./BufferQueue.js";
// import secureMPC from "./secureMPC.js";
// import { IO } from "./types";
import BufferedIO from "./BufferedIO.js";
import BufferQueue from "./BufferQueue.js";
import secureMPC from "./secureMPC.js";
import { IO } from "./types";
// const windowAny = window as any;
// TODO: Uncomment other tests
// windowAny.secureMPC = secureMPC;
const windowAny = window as any;
// windowAny.internalDemo = async function(
// aliceInput: number,
// bobInput: number
// ): Promise<{ alice: number, bob: number }> {
// const aliceBq = new BufferQueue();
// const bobBq = new BufferQueue();
windowAny.secureMPC = secureMPC;
// const [aliceBits, bobBits] = await Promise.all([
// secure2PC(
// 'alice',
// add32BitCircuit,
// numberTo32Bits(aliceInput),
// {
// send: data => bobBq.push(data),
// recv: len => aliceBq.pop(len),
// },
// ),
// secure2PC(
// 'bob',
// add32BitCircuit,
// numberTo32Bits(bobInput),
// {
// send: data => aliceBq.push(data),
// recv: len => bobBq.pop(len),
// },
// ),
// ]);
windowAny.internalDemo = async function(
aliceInput: number,
bobInput: number
): Promise<{ alice: number, bob: number }> {
const aliceBq = { a: new BufferQueue(), b: new BufferQueue() };
const bobBq = { a: new BufferQueue(), b: new BufferQueue() };
// return {
// alice: numberFrom32Bits(aliceBits),
// bob: numberFrom32Bits(bobBits),
// };
// }
const [aliceBits, bobBits] = await Promise.all([
secureMPC(
0,
2,
add32BitCircuit,
numberTo32Bits(aliceInput),
[32, 32],
{
send: (party2, channel, data) => bobBq[channel].push(data),
recv: (party2, channel, len) => aliceBq[channel].pop(len),
},
),
secureMPC(
1,
2,
add32BitCircuit,
numberTo32Bits(bobInput),
[32, 32],
{
send: (party2, channel, data) => aliceBq[channel].push(data),
recv: (party2, channel, len) => bobBq[channel].pop(len),
},
),
]);
return {
alice: numberFrom32Bits(aliceBits),
bob: numberFrom32Bits(bobBits),
};
}
// windowAny.consoleDemo = async function(
@@ -193,37 +199,37 @@
// return io;
// }
// /**
// * Converts a number into its 32-bit binary representation.
// *
// * @param x - The number to convert.
// * @returns A 32-bit binary representation of the number in the form of a Uint8Array.
// */
// function numberTo32Bits(x: number): Uint8Array {
// const result = new Uint8Array(32);
/**
* Converts a number into its 32-bit binary representation.
*
* @param x - The number to convert.
* @returns A 32-bit binary representation of the number in the form of a Uint8Array.
*/
function numberTo32Bits(x: number): Uint8Array {
const result = new Uint8Array(32);
// for (let i = 0; i < 32; i++) {
// result[i] = (x >>> i) & 1;
// }
for (let i = 0; i < 32; i++) {
result[i] = (x >>> i) & 1;
}
// return result;
// }
return result;
}
// /**
// * Converts a 32-bit binary representation back into a number.
// *
// * @param arr - A 32-bit binary array.
// * @returns The number represented by the 32-bit array.
// */
// function numberFrom32Bits(arr: Uint8Array): number {
// let result = 0;
/**
* Converts a 32-bit binary representation back into a number.
*
* @param arr - A 32-bit binary array.
* @returns The number represented by the 32-bit array.
*/
function numberFrom32Bits(arr: Uint8Array): number {
let result = 0;
// for (let i = 0; i < 32; i++) {
// result |= arr[i] << i;
// }
for (let i = 0; i < 32; i++) {
result |= arr[i] << i;
}
// return result;
// }
return result;
}
// /**
// * Creates an I/O interface for secure communication using a BufferQueue.
@@ -317,382 +323,382 @@
// return decodedArray;
// }
// const add32BitCircuit = `375 439
// 32 32 33
const add32BitCircuit = `375 439
32 32 33
// 2 1 0 32 406 XOR
// 2 1 5 37 373 AND
// 2 1 4 36 336 AND
// 2 1 10 42 340 AND
// 2 1 14 46 366 AND
// 2 1 24 56 341 AND
// 2 1 8 40 342 AND
// 2 1 1 33 343 AND
// 2 1 7 39 348 AND
// 2 1 28 60 349 AND
// 2 1 19 51 350 AND
// 2 1 2 34 351 AND
// 2 1 30 62 364 AND
// 2 1 13 45 352 AND
// 2 1 18 50 353 AND
// 2 1 11 43 355 AND
// 2 1 3 35 356 AND
// 2 1 16 48 359 AND
// 2 1 31 63 357 AND
// 2 1 27 59 358 AND
// 2 1 15 47 360 AND
// 2 1 17 49 361 AND
// 2 1 9 41 363 AND
// 2 1 32 0 278 AND
// 2 1 29 61 362 AND
// 2 1 6 38 365 AND
// 2 1 25 57 354 AND
// 2 1 20 52 367 AND
// 2 1 22 54 331 AND
// 2 1 21 53 371 AND
// 2 1 12 44 372 AND
// 2 1 23 55 339 AND
// 2 1 26 58 368 AND
// 1 1 56 398 INV
// 1 1 3 314 INV
// 1 1 40 346 INV
// 1 1 62 378 INV
// 1 1 6 389 INV
// 1 1 28 401 INV
// 1 1 10 377 INV
// 1 1 13 391 INV
// 1 1 27 335 INV
// 1 1 7 387 INV
// 1 1 24 399 INV
// 1 1 54 327 INV
// 1 1 36 315 INV
// 1 1 52 332 INV
// 1 1 50 380 INV
// 1 1 57 404 INV
// 1 1 31 323 INV
// 1 1 55 317 INV
// 1 1 18 381 INV
// 1 1 60 400 INV
// 1 1 5 322 INV
// 1 1 14 395 INV
// 1 1 47 402 INV
// 1 1 8 347 INV
// 1 1 19 385 INV
// 1 1 53 374 INV
// 1 1 29 330 INV
// 1 1 1 382 INV
// 1 1 34 344 INV
// 1 1 20 333 INV
// 1 1 37 321 INV
// 1 1 45 390 INV
// 1 1 11 338 INV
// 1 1 42 376 INV
// 1 1 12 370 INV
// 1 1 38 388 INV
// 1 1 23 318 INV
// 1 1 41 392 INV
// 1 1 61 329 INV
// 1 1 15 403 INV
// 1 1 48 396 INV
// 1 1 26 320 INV
// 1 1 43 337 INV
// 1 1 59 334 INV
// 1 1 9 393 INV
// 1 1 58 319 INV
// 1 1 17 326 INV
// 1 1 44 369 INV
// 1 1 21 375 INV
// 1 1 49 325 INV
// 1 1 16 397 INV
// 1 1 25 405 INV
// 1 1 51 384 INV
// 1 1 4 316 INV
// 1 1 2 345 INV
// 1 1 39 386 INV
// 1 1 46 394 INV
// 1 1 35 313 INV
// 1 1 22 328 INV
// 1 1 63 324 INV
// 1 1 33 383 INV
// 1 1 30 379 INV
// 2 1 313 314 282 AND
// 2 1 315 316 283 AND
// 2 1 317 318 284 AND
// 2 1 319 320 299 AND
// 2 1 321 322 285 AND
// 2 1 323 324 286 AND
// 2 1 325 326 288 AND
// 2 1 327 328 289 AND
// 2 1 329 330 290 AND
// 1 1 331 130 INV
// 2 1 332 333 287 AND
// 2 1 334 335 292 AND
// 1 1 336 256 INV
// 2 1 337 338 293 AND
// 1 1 339 123 INV
// 1 1 340 214 INV
// 1 1 341 116 INV
// 1 1 342 228 INV
// 1 1 343 276 INV
// 2 1 344 345 310 AND
// 2 1 346 347 300 AND
// 1 1 348 235 INV
// 1 1 349 88 INV
// 1 1 350 151 INV
// 1 1 351 270 INV
// 1 1 352 193 INV
// 1 1 353 158 INV
// 1 1 354 109 INV
// 1 1 355 207 INV
// 1 1 356 263 INV
// 1 1 357 66 INV
// 1 1 358 95 INV
// 1 1 359 172 INV
// 1 1 360 179 INV
// 1 1 361 165 INV
// 1 1 362 81 INV
// 1 1 363 221 INV
// 1 1 364 74 INV
// 1 1 365 242 INV
// 1 1 366 186 INV
// 1 1 367 144 INV
// 1 1 368 102 INV
// 2 1 369 370 301 AND
// 1 1 371 137 INV
// 1 1 372 200 INV
// 1 1 373 249 INV
// 2 1 374 375 298 AND
// 2 1 376 377 296 AND
// 2 1 378 379 291 AND
// 2 1 380 381 297 AND
// 2 1 382 383 306 AND
// 2 1 384 385 294 AND
// 2 1 386 387 295 AND
// 2 1 388 389 302 AND
// 2 1 390 391 303 AND
// 2 1 392 393 304 AND
// 2 1 394 395 305 AND
// 2 1 396 397 307 AND
// 2 1 398 399 308 AND
// 2 1 400 401 309 AND
// 2 1 402 403 311 AND
// 2 1 404 405 312 AND
// 1 1 282 266 INV
// 1 1 283 259 INV
// 1 1 284 126 INV
// 1 1 285 252 INV
// 1 1 286 69 INV
// 1 1 287 147 INV
// 1 1 288 168 INV
// 1 1 289 133 INV
// 1 1 290 84 INV
// 1 1 291 77 INV
// 1 1 292 98 INV
// 1 1 293 210 INV
// 1 1 294 154 INV
// 1 1 295 238 INV
// 1 1 296 217 INV
// 1 1 297 161 INV
// 1 1 298 140 INV
// 1 1 299 105 INV
// 1 1 300 231 INV
// 1 1 301 203 INV
// 1 1 302 245 INV
// 1 1 303 196 INV
// 1 1 304 224 INV
// 1 1 305 189 INV
// 1 1 306 281 INV
// 1 1 307 175 INV
// 1 1 308 119 INV
// 1 1 309 91 INV
// 1 1 310 273 INV
// 1 1 311 182 INV
// 1 1 312 112 INV
// 2 1 281 276 277 AND
// 2 1 69 66 279 AND
// 2 1 281 278 280 AND
// 2 1 277 278 407 XOR
// 1 1 279 71 INV
// 1 1 280 275 INV
// 2 1 275 276 274 AND
// 1 1 274 271 INV
// 2 1 2 271 268 XOR
// 2 1 271 273 272 AND
// 2 1 34 268 408 XOR
// 1 1 272 269 INV
// 2 1 269 270 267 AND
// 1 1 267 264 INV
// 2 1 3 264 261 XOR
// 2 1 264 266 265 AND
// 2 1 35 261 409 XOR
// 1 1 265 262 INV
// 2 1 262 263 260 AND
// 1 1 260 257 INV
// 2 1 4 257 253 XOR
// 2 1 257 259 258 AND
// 2 1 36 253 410 XOR
// 1 1 258 255 INV
// 2 1 255 256 254 AND
// 1 1 254 250 INV
// 2 1 5 250 247 XOR
// 2 1 250 252 251 AND
// 2 1 37 247 411 XOR
// 1 1 251 248 INV
// 2 1 248 249 246 AND
// 1 1 246 243 INV
// 2 1 6 243 239 XOR
// 2 1 243 245 244 AND
// 2 1 38 239 412 XOR
// 1 1 244 241 INV
// 2 1 241 242 240 AND
// 1 1 240 236 INV
// 2 1 7 236 233 XOR
// 2 1 236 238 237 AND
// 2 1 39 233 413 XOR
// 1 1 237 234 INV
// 2 1 234 235 232 AND
// 1 1 232 229 INV
// 2 1 8 229 226 XOR
// 2 1 229 231 230 AND
// 2 1 40 226 414 XOR
// 1 1 230 227 INV
// 2 1 227 228 225 AND
// 1 1 225 222 INV
// 2 1 9 222 219 XOR
// 2 1 222 224 223 AND
// 2 1 41 219 415 XOR
// 1 1 223 220 INV
// 2 1 220 221 218 AND
// 1 1 218 215 INV
// 2 1 10 215 212 XOR
// 2 1 215 217 216 AND
// 2 1 42 212 416 XOR
// 1 1 216 213 INV
// 2 1 213 214 211 AND
// 1 1 211 208 INV
// 2 1 11 208 205 XOR
// 2 1 208 210 209 AND
// 2 1 43 205 417 XOR
// 1 1 209 206 INV
// 2 1 206 207 204 AND
// 1 1 204 201 INV
// 2 1 12 201 198 XOR
// 2 1 201 203 202 AND
// 2 1 44 198 418 XOR
// 1 1 202 199 INV
// 2 1 199 200 197 AND
// 1 1 197 195 INV
// 2 1 13 195 190 XOR
// 2 1 195 196 194 AND
// 2 1 45 190 419 XOR
// 1 1 194 192 INV
// 2 1 192 193 191 AND
// 1 1 191 187 INV
// 2 1 14 187 183 XOR
// 2 1 187 189 188 AND
// 2 1 46 183 420 XOR
// 1 1 188 185 INV
// 2 1 185 186 184 AND
// 1 1 184 180 INV
// 2 1 15 180 177 XOR
// 2 1 180 182 181 AND
// 2 1 47 177 421 XOR
// 1 1 181 178 INV
// 2 1 178 179 176 AND
// 1 1 176 173 INV
// 2 1 48 173 170 XOR
// 2 1 173 175 174 AND
// 2 1 16 170 422 XOR
// 1 1 174 171 INV
// 2 1 171 172 169 AND
// 1 1 169 166 INV
// 2 1 17 166 163 XOR
// 2 1 166 168 167 AND
// 2 1 49 163 423 XOR
// 1 1 167 164 INV
// 2 1 164 165 162 AND
// 1 1 162 159 INV
// 2 1 18 159 156 XOR
// 2 1 159 161 160 AND
// 2 1 50 156 424 XOR
// 1 1 160 157 INV
// 2 1 157 158 155 AND
// 1 1 155 152 INV
// 2 1 19 152 149 XOR
// 2 1 152 154 153 AND
// 2 1 51 149 425 XOR
// 1 1 153 150 INV
// 2 1 150 151 148 AND
// 1 1 148 145 INV
// 2 1 20 145 141 XOR
// 2 1 145 147 146 AND
// 2 1 52 141 426 XOR
// 1 1 146 143 INV
// 2 1 143 144 142 AND
// 1 1 142 138 INV
// 2 1 53 138 135 XOR
// 2 1 138 140 139 AND
// 2 1 21 135 427 XOR
// 1 1 139 136 INV
// 2 1 136 137 134 AND
// 1 1 134 132 INV
// 2 1 22 132 127 XOR
// 2 1 132 133 131 AND
// 2 1 54 127 428 XOR
// 1 1 131 129 INV
// 2 1 129 130 128 AND
// 1 1 128 124 INV
// 2 1 23 124 121 XOR
// 2 1 124 126 125 AND
// 2 1 55 121 429 XOR
// 1 1 125 122 INV
// 2 1 122 123 120 AND
// 1 1 120 117 INV
// 2 1 24 117 114 XOR
// 2 1 117 119 118 AND
// 2 1 56 114 430 XOR
// 1 1 118 115 INV
// 2 1 115 116 113 AND
// 1 1 113 110 INV
// 2 1 25 110 107 XOR
// 2 1 110 112 111 AND
// 2 1 57 107 431 XOR
// 1 1 111 108 INV
// 2 1 108 109 106 AND
// 1 1 106 103 INV
// 2 1 26 103 100 XOR
// 2 1 103 105 104 AND
// 2 1 58 100 432 XOR
// 1 1 104 101 INV
// 2 1 101 102 99 AND
// 1 1 99 96 INV
// 2 1 59 96 93 XOR
// 2 1 96 98 97 AND
// 2 1 27 93 433 XOR
// 1 1 97 94 INV
// 2 1 94 95 92 AND
// 1 1 92 89 INV
// 2 1 28 89 86 XOR
// 2 1 89 91 90 AND
// 2 1 60 86 434 XOR
// 1 1 90 87 INV
// 2 1 87 88 85 AND
// 1 1 85 83 INV
// 2 1 61 83 79 XOR
// 2 1 83 84 82 AND
// 2 1 29 79 435 XOR
// 1 1 82 80 INV
// 2 1 80 81 78 AND
// 1 1 78 76 INV
// 2 1 30 76 72 XOR
// 2 1 76 77 75 AND
// 2 1 62 72 436 XOR
// 1 1 75 73 INV
// 2 1 73 74 70 AND
// 2 1 70 71 437 XOR
// 1 1 70 68 INV
// 2 1 68 69 67 AND
// 1 1 67 65 INV
// 2 1 65 66 64 AND
// 1 1 64 438 INV
// `;
2 1 0 32 406 XOR
2 1 5 37 373 AND
2 1 4 36 336 AND
2 1 10 42 340 AND
2 1 14 46 366 AND
2 1 24 56 341 AND
2 1 8 40 342 AND
2 1 1 33 343 AND
2 1 7 39 348 AND
2 1 28 60 349 AND
2 1 19 51 350 AND
2 1 2 34 351 AND
2 1 30 62 364 AND
2 1 13 45 352 AND
2 1 18 50 353 AND
2 1 11 43 355 AND
2 1 3 35 356 AND
2 1 16 48 359 AND
2 1 31 63 357 AND
2 1 27 59 358 AND
2 1 15 47 360 AND
2 1 17 49 361 AND
2 1 9 41 363 AND
2 1 32 0 278 AND
2 1 29 61 362 AND
2 1 6 38 365 AND
2 1 25 57 354 AND
2 1 20 52 367 AND
2 1 22 54 331 AND
2 1 21 53 371 AND
2 1 12 44 372 AND
2 1 23 55 339 AND
2 1 26 58 368 AND
1 1 56 398 INV
1 1 3 314 INV
1 1 40 346 INV
1 1 62 378 INV
1 1 6 389 INV
1 1 28 401 INV
1 1 10 377 INV
1 1 13 391 INV
1 1 27 335 INV
1 1 7 387 INV
1 1 24 399 INV
1 1 54 327 INV
1 1 36 315 INV
1 1 52 332 INV
1 1 50 380 INV
1 1 57 404 INV
1 1 31 323 INV
1 1 55 317 INV
1 1 18 381 INV
1 1 60 400 INV
1 1 5 322 INV
1 1 14 395 INV
1 1 47 402 INV
1 1 8 347 INV
1 1 19 385 INV
1 1 53 374 INV
1 1 29 330 INV
1 1 1 382 INV
1 1 34 344 INV
1 1 20 333 INV
1 1 37 321 INV
1 1 45 390 INV
1 1 11 338 INV
1 1 42 376 INV
1 1 12 370 INV
1 1 38 388 INV
1 1 23 318 INV
1 1 41 392 INV
1 1 61 329 INV
1 1 15 403 INV
1 1 48 396 INV
1 1 26 320 INV
1 1 43 337 INV
1 1 59 334 INV
1 1 9 393 INV
1 1 58 319 INV
1 1 17 326 INV
1 1 44 369 INV
1 1 21 375 INV
1 1 49 325 INV
1 1 16 397 INV
1 1 25 405 INV
1 1 51 384 INV
1 1 4 316 INV
1 1 2 345 INV
1 1 39 386 INV
1 1 46 394 INV
1 1 35 313 INV
1 1 22 328 INV
1 1 63 324 INV
1 1 33 383 INV
1 1 30 379 INV
2 1 313 314 282 AND
2 1 315 316 283 AND
2 1 317 318 284 AND
2 1 319 320 299 AND
2 1 321 322 285 AND
2 1 323 324 286 AND
2 1 325 326 288 AND
2 1 327 328 289 AND
2 1 329 330 290 AND
1 1 331 130 INV
2 1 332 333 287 AND
2 1 334 335 292 AND
1 1 336 256 INV
2 1 337 338 293 AND
1 1 339 123 INV
1 1 340 214 INV
1 1 341 116 INV
1 1 342 228 INV
1 1 343 276 INV
2 1 344 345 310 AND
2 1 346 347 300 AND
1 1 348 235 INV
1 1 349 88 INV
1 1 350 151 INV
1 1 351 270 INV
1 1 352 193 INV
1 1 353 158 INV
1 1 354 109 INV
1 1 355 207 INV
1 1 356 263 INV
1 1 357 66 INV
1 1 358 95 INV
1 1 359 172 INV
1 1 360 179 INV
1 1 361 165 INV
1 1 362 81 INV
1 1 363 221 INV
1 1 364 74 INV
1 1 365 242 INV
1 1 366 186 INV
1 1 367 144 INV
1 1 368 102 INV
2 1 369 370 301 AND
1 1 371 137 INV
1 1 372 200 INV
1 1 373 249 INV
2 1 374 375 298 AND
2 1 376 377 296 AND
2 1 378 379 291 AND
2 1 380 381 297 AND
2 1 382 383 306 AND
2 1 384 385 294 AND
2 1 386 387 295 AND
2 1 388 389 302 AND
2 1 390 391 303 AND
2 1 392 393 304 AND
2 1 394 395 305 AND
2 1 396 397 307 AND
2 1 398 399 308 AND
2 1 400 401 309 AND
2 1 402 403 311 AND
2 1 404 405 312 AND
1 1 282 266 INV
1 1 283 259 INV
1 1 284 126 INV
1 1 285 252 INV
1 1 286 69 INV
1 1 287 147 INV
1 1 288 168 INV
1 1 289 133 INV
1 1 290 84 INV
1 1 291 77 INV
1 1 292 98 INV
1 1 293 210 INV
1 1 294 154 INV
1 1 295 238 INV
1 1 296 217 INV
1 1 297 161 INV
1 1 298 140 INV
1 1 299 105 INV
1 1 300 231 INV
1 1 301 203 INV
1 1 302 245 INV
1 1 303 196 INV
1 1 304 224 INV
1 1 305 189 INV
1 1 306 281 INV
1 1 307 175 INV
1 1 308 119 INV
1 1 309 91 INV
1 1 310 273 INV
1 1 311 182 INV
1 1 312 112 INV
2 1 281 276 277 AND
2 1 69 66 279 AND
2 1 281 278 280 AND
2 1 277 278 407 XOR
1 1 279 71 INV
1 1 280 275 INV
2 1 275 276 274 AND
1 1 274 271 INV
2 1 2 271 268 XOR
2 1 271 273 272 AND
2 1 34 268 408 XOR
1 1 272 269 INV
2 1 269 270 267 AND
1 1 267 264 INV
2 1 3 264 261 XOR
2 1 264 266 265 AND
2 1 35 261 409 XOR
1 1 265 262 INV
2 1 262 263 260 AND
1 1 260 257 INV
2 1 4 257 253 XOR
2 1 257 259 258 AND
2 1 36 253 410 XOR
1 1 258 255 INV
2 1 255 256 254 AND
1 1 254 250 INV
2 1 5 250 247 XOR
2 1 250 252 251 AND
2 1 37 247 411 XOR
1 1 251 248 INV
2 1 248 249 246 AND
1 1 246 243 INV
2 1 6 243 239 XOR
2 1 243 245 244 AND
2 1 38 239 412 XOR
1 1 244 241 INV
2 1 241 242 240 AND
1 1 240 236 INV
2 1 7 236 233 XOR
2 1 236 238 237 AND
2 1 39 233 413 XOR
1 1 237 234 INV
2 1 234 235 232 AND
1 1 232 229 INV
2 1 8 229 226 XOR
2 1 229 231 230 AND
2 1 40 226 414 XOR
1 1 230 227 INV
2 1 227 228 225 AND
1 1 225 222 INV
2 1 9 222 219 XOR
2 1 222 224 223 AND
2 1 41 219 415 XOR
1 1 223 220 INV
2 1 220 221 218 AND
1 1 218 215 INV
2 1 10 215 212 XOR
2 1 215 217 216 AND
2 1 42 212 416 XOR
1 1 216 213 INV
2 1 213 214 211 AND
1 1 211 208 INV
2 1 11 208 205 XOR
2 1 208 210 209 AND
2 1 43 205 417 XOR
1 1 209 206 INV
2 1 206 207 204 AND
1 1 204 201 INV
2 1 12 201 198 XOR
2 1 201 203 202 AND
2 1 44 198 418 XOR
1 1 202 199 INV
2 1 199 200 197 AND
1 1 197 195 INV
2 1 13 195 190 XOR
2 1 195 196 194 AND
2 1 45 190 419 XOR
1 1 194 192 INV
2 1 192 193 191 AND
1 1 191 187 INV
2 1 14 187 183 XOR
2 1 187 189 188 AND
2 1 46 183 420 XOR
1 1 188 185 INV
2 1 185 186 184 AND
1 1 184 180 INV
2 1 15 180 177 XOR
2 1 180 182 181 AND
2 1 47 177 421 XOR
1 1 181 178 INV
2 1 178 179 176 AND
1 1 176 173 INV
2 1 48 173 170 XOR
2 1 173 175 174 AND
2 1 16 170 422 XOR
1 1 174 171 INV
2 1 171 172 169 AND
1 1 169 166 INV
2 1 17 166 163 XOR
2 1 166 168 167 AND
2 1 49 163 423 XOR
1 1 167 164 INV
2 1 164 165 162 AND
1 1 162 159 INV
2 1 18 159 156 XOR
2 1 159 161 160 AND
2 1 50 156 424 XOR
1 1 160 157 INV
2 1 157 158 155 AND
1 1 155 152 INV
2 1 19 152 149 XOR
2 1 152 154 153 AND
2 1 51 149 425 XOR
1 1 153 150 INV
2 1 150 151 148 AND
1 1 148 145 INV
2 1 20 145 141 XOR
2 1 145 147 146 AND
2 1 52 141 426 XOR
1 1 146 143 INV
2 1 143 144 142 AND
1 1 142 138 INV
2 1 53 138 135 XOR
2 1 138 140 139 AND
2 1 21 135 427 XOR
1 1 139 136 INV
2 1 136 137 134 AND
1 1 134 132 INV
2 1 22 132 127 XOR
2 1 132 133 131 AND
2 1 54 127 428 XOR
1 1 131 129 INV
2 1 129 130 128 AND
1 1 128 124 INV
2 1 23 124 121 XOR
2 1 124 126 125 AND
2 1 55 121 429 XOR
1 1 125 122 INV
2 1 122 123 120 AND
1 1 120 117 INV
2 1 24 117 114 XOR
2 1 117 119 118 AND
2 1 56 114 430 XOR
1 1 118 115 INV
2 1 115 116 113 AND
1 1 113 110 INV
2 1 25 110 107 XOR
2 1 110 112 111 AND
2 1 57 107 431 XOR
1 1 111 108 INV
2 1 108 109 106 AND
1 1 106 103 INV
2 1 26 103 100 XOR
2 1 103 105 104 AND
2 1 58 100 432 XOR
1 1 104 101 INV
2 1 101 102 99 AND
1 1 99 96 INV
2 1 59 96 93 XOR
2 1 96 98 97 AND
2 1 27 93 433 XOR
1 1 97 94 INV
2 1 94 95 92 AND
1 1 92 89 INV
2 1 28 89 86 XOR
2 1 89 91 90 AND
2 1 60 86 434 XOR
1 1 90 87 INV
2 1 87 88 85 AND
1 1 85 83 INV
2 1 61 83 79 XOR
2 1 83 84 82 AND
2 1 29 79 435 XOR
1 1 82 80 INV
2 1 80 81 78 AND
1 1 78 76 INV
2 1 30 76 72 XOR
2 1 76 77 75 AND
2 1 62 72 436 XOR
1 1 75 73 INV
2 1 73 74 70 AND
2 1 70 71 437 XOR
1 1 70 68 INV
2 1 68 69 67 AND
1 1 67 65 INV
2 1 65 66 64 AND
1 1 64 438 INV
`;

View File

@@ -14,7 +14,7 @@ export default async function nodeSecure2PC(
size: number,
circuit: string,
input: Uint8Array,
inputBitsStart: number,
inputBitsPerParty: number[],
io: IO,
): Promise<Uint8Array> {
if (typeof process === 'undefined' || typeof process.versions === 'undefined' || !process.versions.node) {
@@ -26,22 +26,23 @@ export default async function nodeSecure2PC(
const emp: {
circuit?: string;
input?: Uint8Array;
inputBitsStart?: number;
inputBitsPerParty?: number[];
io?: IO;
handleOutput?: (value: Uint8Array) => void
handleOutput?: (value: Uint8Array) => void;
handleError?: (error: Error) => void;
} = {};
module.emp = emp;
emp.circuit = circuit;
emp.input = input;
emp.inputBitsStart = inputBitsStart;
emp.inputBitsPerParty = inputBitsPerParty;
emp.io = io;
const result = await new Promise<Uint8Array>((resolve, reject) => {
try {
emp.handleOutput = resolve;
// TODO: emp.handleError
emp.handleError = reject;
module._run(party, size);
} catch (error) {

View File

@@ -10,11 +10,11 @@ export default function secureMPC(
size: number,
circuit: string,
input: Uint8Array,
inputBitsStart: number,
inputBitsPerParty: number[],
io: IO,
): Promise<Uint8Array> {
if (typeof Worker === 'undefined') {
return nodeSecure2PC(party, size, circuit, input, inputBitsStart, io);
return nodeSecure2PC(party, size, circuit, input, inputBitsPerParty, io);
}
const ev = new EventEmitter<{ cleanup(): void }>();
@@ -32,7 +32,7 @@ export default function secureMPC(
size,
circuit,
input,
inputBitsStart,
inputBitsPerParty,
});
worker.onmessage = async (event) => {

View File

@@ -21,7 +21,7 @@ async function internalDemo(
2,
add32BitCircuit,
numberTo32Bits(aliceInput),
0,
[32, 32],
{
send: (party2, channel, data) => {
expect(party2).to.equal(1);
@@ -38,7 +38,7 @@ async function internalDemo(
2,
add32BitCircuit,
numberTo32Bits(bobInput),
32,
[32, 32],
{
send: (party2, channel, data) => {
expect(party2).to.equal(0);
@@ -52,6 +52,8 @@ async function internalDemo(
),
]);
console.log({ aliceBits, bobBits });
return {
alice: numberFrom32Bits(aliceBits),
bob: numberFrom32Bits(bobBits),