Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mpc/src/ffi/c_bindings/network/fake_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ pub extern "C" fn node_receiver_recv_sync(
}
}
}

#[no_mangle]
pub extern "C" fn free_fake_network_receivers(receivers: *mut FakeNetworkReceiversOpaque) {
if !receivers.is_null() {
unsafe {
// Reconstruct the Box and drop it to free memory
let receivers = receivers as *mut FakeNetworkReceivers;
Box::from_raw(receivers);
}
}
}
51 changes: 51 additions & 0 deletions mpc/src/ffi/c_bindings/share/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,47 @@ pub struct NonRobustShareSlice {
pub len: usize,
}

// free the memory of a Shamirshare
#[no_mangle]
pub extern "C" fn free_shamir_share(share: ShamirShare) {
if !share.share.is_null() {
unsafe {
let _ = Box::from_raw(share.share as *mut GenericField);
}
}
}

// free the memory of a RobustShare
#[no_mangle]
pub extern "C" fn free_robust_share(share: RobustShare) {
if !share.share.is_null() {
unsafe {
let _ = Box::from_raw(share.share as *mut GenericField);
}
}
}

// free the memory of a NonRobustShare
#[no_mangle]
pub extern "C" fn free_non_robust_share(share: NonRobustShare) {
if !share.share.is_null() {
unsafe {
let _ = Box::from_raw(share.share as *mut GenericField);
}
}
}

// free the memory of a ShamirshareSlice
#[no_mangle]
pub extern "C" fn free_shamir_share_slice(slice: ShamirShareSlice) {
if !slice.pointer.is_null() {
for i in 0..slice.len {
let share = unsafe { &*(slice.pointer.add(i)) };
assert!(!share.share.is_null());
unsafe {
let _ = Box::from_raw(share.share as *mut GenericField);
}
}
unsafe {
let _ = Vec::from_raw_parts(slice.pointer, slice.len, slice.len);
}
Expand All @@ -126,6 +163,13 @@ pub extern "C" fn free_shamir_share_slice(slice: ShamirShareSlice) {
#[no_mangle]
pub extern "C" fn free_robust_share_slice(slice: RobustShareSlice) {
if !slice.pointer.is_null() {
for i in 0..slice.len {
let share = unsafe { &*(slice.pointer.add(i)) };
assert!(!share.share.is_null());
unsafe {
let _ = Box::from_raw(share.share as *mut GenericField);
}
}
unsafe {
let _ = Vec::from_raw_parts(slice.pointer, slice.len, slice.len);
}
Expand All @@ -136,6 +180,13 @@ pub extern "C" fn free_robust_share_slice(slice: RobustShareSlice) {
#[no_mangle]
pub extern "C" fn free_non_robust_share_slice(slice: NonRobustShareSlice) {
if !slice.pointer.is_null() {
for i in 0..slice.len {
let share = unsafe { &*(slice.pointer.add(i)) };
assert!(!share.share.is_null());
unsafe {
let _ = Box::from_raw(share.share as *mut GenericField);
}
}
unsafe {
let _ = Vec::from_raw_parts(slice.pointer, slice.len, slice.len);
}
Expand Down
115 changes: 74 additions & 41 deletions mpc/src/ffi/honey_badger_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
#include <stdint.h>
#include <stdlib.h>

#define F2_8_MODULUS 283

typedef enum ProtocolType {
None = 0,
Randousha = 1,
Ransha = 2,
Input = 3,
Rbc = 4,
Triple = 5,
BatchRecon = 6,
Dousha = 7,
Mul = 8,
PRandInt = 9,
PRandBit = 10,
RandBit = 11,
FpMul = 12,
Trunc = 13,
FpDivConst = 14,
} ProtocolType;

typedef enum FieldKind {
Bls12_381Fr,
} FieldKind;
Expand All @@ -26,6 +46,13 @@ typedef enum HoneyBadgerErrorCode {
HoneyBadgerJoinError,
HoneyBadgerChannelClosed,
HoneyBadgerOutputNotReady,
HoneyBadgerRandBitError,
HoneyBadgerPRandError,
HoneyBadgerFPMulError,
HoneyBadgerTruncPrError,
HoneyBadgerFPDivConstError,
HoneyBadgerTypesError,
HoneyBadgerAlreadyReservedError,
} HoneyBadgerErrorCode;

typedef enum NetworkErrorCode {
Expand All @@ -41,34 +68,6 @@ typedef enum NetworkErrorCode {
ClientNotFound,
} NetworkErrorCode;

typedef enum ProtocolType {
None = 0,
Randousha = 1,
Ransha = 2,
Input = 3,
Rbc = 4,
Triple = 5,
BatchRecon = 6,
Dousha = 7,
Mul = 8,
} ProtocolType;

typedef enum RbcErrorCode {
RbcSuccess,
RbcInvalidThreshold,
RbcSessionEnded,
RbcUnknownMsgType,
RbcSendFailed,
RbcInternal,
RbcNetworkSendError,
RbcNetworkTimeout,
RbcNetworkPartyNotFound,
RbcNetworkClientNotFound,
RbcSerializationError,
RbcShardError,
RbcSessionNotFound,
} RbcErrorCode;

typedef enum RbcMessageType {
BrachaInit,
BrachaEcho,
Expand All @@ -87,6 +86,22 @@ typedef enum RbcMessageType {
AcsUnknown,
} RbcMessageType;

typedef enum RbcErrorCode {
RbcSuccess,
RbcInvalidThreshold,
RbcSessionEnded,
RbcUnknownMsgType,
RbcSendFailed,
RbcInternal,
RbcNetworkSendError,
RbcNetworkTimeout,
RbcNetworkPartyNotFound,
RbcNetworkClientNotFound,
RbcSerializationError,
RbcShardError,
RbcSessionNotFound,
} RbcErrorCode;

typedef enum ShareErrorCode {
ShareSuccess,
InsufficientShares,
Expand All @@ -99,6 +114,11 @@ typedef enum ShareErrorCode {
DecodingError,
} ShareErrorCode;

/**
* Finite field GF(2^8) with AES modulus x^8 + x^4 + x^3 + x + 1 (0x11B)
*/
typedef struct F2_8 F2_8;

typedef struct U256 {
uint64_t data[4];
} U256;
Expand Down Expand Up @@ -170,33 +190,35 @@ typedef struct ShamirShare {
uintptr_t degree;
} ShamirShare;

typedef struct ShamirShareSlice {
struct ShamirShare *pointer;
uintptr_t len;
} ShamirShareSlice;

typedef struct RobustShare {
struct FieldOpaque *share;
uintptr_t id;
uintptr_t degree;
} RobustShare;

typedef struct RobustShareSlice {
struct RobustShare *pointer;
uintptr_t len;
} RobustShareSlice;

typedef struct NonRobustShare {
struct FieldOpaque *share;
uintptr_t id;
uintptr_t degree;
} NonRobustShare;

typedef struct ShamirShareSlice {
struct ShamirShare *pointer;
uintptr_t len;
} ShamirShareSlice;

typedef struct RobustShareSlice {
struct RobustShare *pointer;
uintptr_t len;
} RobustShareSlice;

typedef struct NonRobustShareSlice {
struct NonRobustShare *pointer;
uintptr_t len;
} NonRobustShareSlice;



#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -216,22 +238,25 @@ struct ByteSlice u256_to_be_bytes(struct U256 num);
struct ByteSlice u256_to_le_bytes(struct U256 num);

uint64_t new_session_id(enum ProtocolType caller,
uint8_t exec_id,
uint8_t sub_id,
uint8_t round_id,
uint64_t instance_id);
uint32_t instance_id);

enum ProtocolType calling_protocol(uint64_t session_id);

uint8_t exec_id(uint64_t session_id);

uint8_t sub_id(uint64_t session_id);

uint8_t round_id(uint64_t session_id);

uint64_t instance_id(uint64_t session_id);
uint32_t instance_id(uint64_t session_id);

struct HoneyBadgerMPCClientOpaque *new_honey_badger_mpc_client(uintptr_t id,
uintptr_t n,
uintptr_t t,
uint64_t instance_id,
uint32_t instance_id,
struct U256Slice inputs,
uintptr_t input_len,
enum FieldKind field_kind);
Expand Down Expand Up @@ -263,6 +288,8 @@ struct NetworkOpaque *new_fake_network(uintptr_t n_nodes,
struct ByteSlice node_receiver_recv_sync(struct FakeNetworkReceiversOpaque *receivers,
uintptr_t node_index);

void free_fake_network_receivers(struct FakeNetworkReceiversOpaque *receivers);

/**
* Select crypto provider for rustls
* Must be called before using quic network
Expand Down Expand Up @@ -481,6 +508,12 @@ enum RbcErrorCode sync_aba_send(struct AbaOpaque *aba_pointer,

struct ByteSlice field_ptr_to_bytes(struct FieldOpaque *field, bool be);

void free_shamir_share(struct ShamirShare share);

void free_robust_share(struct RobustShare share);

void free_non_robust_share(struct NonRobustShare share);

void free_shamir_share_slice(struct ShamirShareSlice slice);

void free_robust_share_slice(struct RobustShareSlice slice);
Expand Down
15 changes: 9 additions & 6 deletions mpc/src/ffi/tests/rbc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ void *recv_msg(void *arg)
}
struct RbcMsg rbc_msg;
enum RbcErrorCode re = deserialize_rbc_msg(msg, &rbc_msg);

free_bytes_slice(msg);
if (re != RbcSuccess)
{
printf("Error in deserializing rbc message for party %zu, error code: %d\n", params->node_index, re);
// free_rbc_msg(rbc_msg);
pthread_exit(NULL);
}
printf("Party %zu received message of length %lu from sender %lu\n", params->node_index, rbc_msg.msg_len, rbc_msg.sender_id);
// process message
enum RbcErrorCode e = sync_bracha_process(params->node, rbc_msg, params->net);

free_rbc_msg(rbc_msg);
if (e == RbcSessionEnded)
{
printf("Bracha protocol finished for party %zu\n", params->node_index);
free_rbc_msg(rbc_msg);
pthread_exit(NULL);
}
if (e != RbcSuccess)
{
printf("Error in bracha process for party %zu, error code: %d\n", params->node_index, e);
free_rbc_msg(rbc_msg);
pthread_exit(NULL);
}
}
Expand All @@ -72,7 +72,7 @@ void test_bracha_rbc_basic()
size_t t = 1;
uintptr_t channel_buff_size = 500;
char myString[] = "Hello, MPC!";
new_session_id(Rbc, 0, 0, 12);
new_session_id(Rbc, 0, 0, 0, 12);
struct BrachaOpaque *prt_array[n];
struct FakeNetworkReceiversOpaque *receivers;
struct NetworkOpaque _net;
Expand All @@ -87,11 +87,12 @@ void test_bracha_rbc_basic()
payload.pointer = (uint8_t *)myString;
payload.len = strlen(myString) + 1;

uint64_t session_id = new_session_id(Rbc, 0, 0, 12);
uint64_t session_id = new_session_id(Rbc, 0, 0, 0, 12);
// party 0 init
enum RbcErrorCode e = sync_bracha_init(prt_array[0], payload, session_id, net);
if (e != RbcSuccess)
{
free_fake_network_receivers(receivers);
printf("Error in bracha init for party 1, error code: %d\n", e);
exit(1);
}
Expand Down Expand Up @@ -157,16 +158,18 @@ void test_bracha_rbc_basic()
}
printf("Output for party %zu: %s\n", i, output.pointer);
assert(strcmp((char *)output.pointer, myString) == 0);
free_bytes_slice(output);
}
for (size_t i = 0; i < n; i++)
{
free_bracha(prt_array[i]);
}
free_fake_network_receivers(receivers);
free_network(net);
}

int main()
{
test_bracha_rbc_basic();
return 0;
}
}
Loading