mirror of
https://github.com/privacy-scaling-explorations/emp-wasm.git
synced 2026-01-10 10:37:54 -05:00
Draft new jslib.cpp
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
#include <cstring>
|
||||
|
||||
#include "emp-tool/io/i_raw_io.h"
|
||||
#include "emp-ag2pc/2pc.h"
|
||||
#include "emp-agmpc/mpc.h"
|
||||
|
||||
void run_impl(int party);
|
||||
void run_impl(int party, int nP);
|
||||
|
||||
// Implement send_js function to send data from C++ to JavaScript
|
||||
EM_JS(void, send_js, (const void* data, size_t len), {
|
||||
EM_JS(void, send_js, (int party2, char channel_label, const void* data, size_t len), {
|
||||
if (!Module.emp?.io?.send) {
|
||||
throw new Error("Module.emp.io.send is not defined in JavaScript.");
|
||||
}
|
||||
@@ -18,18 +18,18 @@ EM_JS(void, send_js, (const void* data, size_t len), {
|
||||
// Copy data from WebAssembly memory to a JavaScript Uint8Array
|
||||
const dataArray = HEAPU8.slice(data, data + len);
|
||||
|
||||
Module.emp.io.send(dataArray);
|
||||
Module.emp.io.send(party2 - 1, channel_label, dataArray);
|
||||
});
|
||||
|
||||
// Implement recv_js function to receive data from JavaScript to C++
|
||||
EM_ASYNC_JS(void, recv_js, (void* data, size_t len), {
|
||||
EM_ASYNC_JS(void, recv_js, (int party2, char channel_label, void* data, size_t len), {
|
||||
if (!Module.emp?.io?.recv) {
|
||||
reject(new Error("Module.emp.io.recv is not defined in JavaScript."));
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for data from JavaScript
|
||||
const dataArray = await Module.emp.io.recv(arguments[1]);
|
||||
const dataArray = await Module.emp.io.recv(party2 - 1, channel_label, arguments[1]);
|
||||
|
||||
// Copy data from JavaScript Uint8Array to WebAssembly memory
|
||||
HEAPU8.set(dataArray, data);
|
||||
@@ -37,12 +37,23 @@ EM_ASYNC_JS(void, recv_js, (void* data, size_t len), {
|
||||
|
||||
class RawIOJS : public IRawIO {
|
||||
public:
|
||||
int party2;
|
||||
char channel_label;
|
||||
|
||||
RawIOJS(
|
||||
int party2,
|
||||
char channel_label
|
||||
):
|
||||
party2(party2),
|
||||
channel_label(channel_label)
|
||||
{}
|
||||
|
||||
void send(const void* data, size_t len) override {
|
||||
send_js(data, len);
|
||||
send_js(party2, channel_label, data, len);
|
||||
}
|
||||
|
||||
void recv(void* data, size_t len) override {
|
||||
recv_js(data, len);
|
||||
recv_js(party2, channel_label, data, len);
|
||||
}
|
||||
|
||||
void flush() override {
|
||||
@@ -50,6 +61,53 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class MultiIOJS : public IMultiIO {
|
||||
public:
|
||||
int mParty;
|
||||
int nP;
|
||||
|
||||
std::vector<emp::IOChannel> a_channels;
|
||||
std::vector<emp::IOChannel> b_channels;
|
||||
|
||||
MultiIOJS(int party, int nP) : mParty(party), nP(nP) {
|
||||
for (int i = 1; i <= nP; i++) {
|
||||
a_channels.emplace_back(std::make_shared<RawIOJS>(i, 'a'));
|
||||
b_channels.emplace_back(std::make_shared<RawIOJS>(i, 'b'));
|
||||
}
|
||||
}
|
||||
|
||||
int size() override {
|
||||
return nP;
|
||||
}
|
||||
|
||||
int party() override {
|
||||
return mParty;
|
||||
}
|
||||
|
||||
emp::IOChannel& a_channel(int party2) override {
|
||||
assert(party2 != 0);
|
||||
assert(party2 != party());
|
||||
|
||||
return a_channels[party2];
|
||||
}
|
||||
|
||||
emp::IOChannel& b_channel(int party2) override {
|
||||
assert(party2 != 0);
|
||||
assert(party2 != party());
|
||||
|
||||
return b_channels[party2];
|
||||
}
|
||||
|
||||
void flush(int idx) override {
|
||||
assert(idx != 0);
|
||||
|
||||
if (party() < idx)
|
||||
a_channels[idx].flush();
|
||||
else
|
||||
b_channels[idx].flush();
|
||||
}
|
||||
};
|
||||
|
||||
EM_JS(char*, get_circuit_raw, (int* lengthPtr), {
|
||||
if (!Module.emp?.circuit) {
|
||||
throw new Error("Module.emp.circuit is not defined in JavaScript.");
|
||||
@@ -113,6 +171,14 @@ 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.");
|
||||
}
|
||||
|
||||
return Module.emp.inputBitsStart;
|
||||
});
|
||||
|
||||
EM_JS(void, handle_output_bits_raw, (uint8_t* outputBits, int length), {
|
||||
if (!Module.emp?.handleOutput) {
|
||||
throw new Error("Module.emp.handleOutput is not defined in JavaScript.");
|
||||
@@ -139,8 +205,8 @@ void handle_output_bits(const std::vector<bool>& output_bits) {
|
||||
|
||||
extern "C" {
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void run(int party) {
|
||||
run_impl(party);
|
||||
void run(int party, int size) {
|
||||
run_impl(party + 1, size);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
@@ -154,18 +220,41 @@ extern "C" {
|
||||
}
|
||||
}
|
||||
|
||||
void run_impl(int party) {
|
||||
auto io = emp::IOChannel(std::make_shared<RawIOJS>());
|
||||
void run_impl(int party, int nP) {
|
||||
std::shared_ptr<IMultiIO> io = std::make_shared<MultiIOJS>(party, nP);
|
||||
auto circuit = get_circuit();
|
||||
|
||||
auto twopc = emp::C2PC(io, party, &circuit);
|
||||
auto mpc = CMPC(io, &circuit);
|
||||
|
||||
twopc.function_independent();
|
||||
twopc.function_dependent();
|
||||
mpc.function_independent();
|
||||
mpc.function_dependent();
|
||||
|
||||
std::vector<bool> input_bits = get_input_bits();
|
||||
int input_bits_start = get_input_bits_start();
|
||||
|
||||
FlexIn input(nP, circuit.n1 + circuit.n2, party);
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
std::vector<bool> output_bits = twopc.online(input_bits, true);
|
||||
handle_output_bits(output_bits);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user