From 2815cbc740ebbb92990faa0e1b54a818c8126922 Mon Sep 17 00:00:00 2001 From: Jay Logelin Date: Wed, 9 Oct 2024 13:53:35 -0300 Subject: [PATCH 1/2] refactor(compute): changed type conversion to idiomatic rust --- benchmark/benches/benchmarks.rs | 61 ++- compute/src/int.rs | 385 +++++++------- compute/src/lib.rs | 1 + compute/src/operations/arithmetic.rs | 220 ++++---- compute/src/operations/bitwise.rs | 729 ++++++++++++--------------- compute/src/simulator.rs | 39 ++ compute/src/uint.rs | 249 ++++----- 7 files changed, 815 insertions(+), 869 deletions(-) create mode 100644 compute/src/simulator.rs diff --git a/benchmark/benches/benchmarks.rs b/benchmark/benches/benchmarks.rs index ed821e9..473da9a 100644 --- a/benchmark/benches/benchmarks.rs +++ b/benchmark/benches/benchmarks.rs @@ -36,11 +36,12 @@ fn gateway_encrypted_addition() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = &a + &b; - assert_eq!(result.to_u128(), clear_a + clear_b); + let result: u128 = result.into(); + assert_eq!(result, clear_a + clear_b); Ok(()) } @@ -78,11 +79,12 @@ fn gateway_encrypted_bitwise_and() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = &a & &b; - assert_eq!(result.to_u128(), clear_a & clear_b); + let result: u128 = result.into(); + assert_eq!(result, clear_a & clear_b); Ok(()) } @@ -120,11 +122,12 @@ fn gateway_encrypted_bitwise_xor() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = &a ^ &b; - assert_eq!(result.to_u128(), clear_a ^ clear_b); + let result: u128 = result.into(); + assert_eq!(result, clear_a ^ clear_b); Ok(()) } @@ -161,11 +164,12 @@ fn gateway_encrypted_bitwise_or() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 42424242424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = &a | &b; - assert_eq!(result.to_u128(), clear_a | clear_b); + let result: u128 = result.into(); + assert_eq!(result, clear_a | clear_b); Ok(()) } @@ -200,10 +204,11 @@ fn gateway_encrypted_bitwise_not() -> Result<(), Box> { let clear_a = 12297829382473034410u128; - let a = GarbledUint128::from_u128(clear_a); + let a: GarbledUint128 = clear_a.into(); let result = !&a; - assert_eq!(result.to_u128(), !clear_a); + let result: u128 = result.into(); + assert_eq!(result, !clear_a); Ok(()) } @@ -241,11 +246,12 @@ fn gateway_encrypted_subtraction() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = &a - &b; - assert_eq!(result.to_u128(), clear_a - clear_b); + let result: u128 = result.into(); + assert_eq!(result, clear_a - clear_b); Ok(()) } @@ -325,11 +331,12 @@ fn gateway_encrypted_nand() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = a.nand(b); - assert_eq!(result.to_u128(), !(clear_a & clear_b)); + let result: u128 = result.into(); + assert_eq!(result, !(clear_a & clear_b)); Ok(()) } @@ -367,11 +374,12 @@ fn gateway_encrypted_nor() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = a.nor(b); - assert_eq!(result.to_u128(), !(clear_a | clear_b)); + let result: u128 = result.into(); + assert_eq!(result, !(clear_a | clear_b)); Ok(()) } @@ -409,11 +417,12 @@ fn gateway_encrypted_xnor() -> Result<(), Box> { let clear_a = 12297829382473034410u128; let clear_b = 424242424242u128; - let a = GarbledUint128::from_u128(clear_a); - let b = GarbledUint128::from_u128(clear_b); + let a: GarbledUint128 = clear_a.into(); + let b: GarbledUint128 = clear_b.into(); let result = a.xnor(b); - assert_eq!(result.to_u128(), !(clear_a ^ clear_b)); + let result: u128 = result.into(); + assert_eq!(result, !(clear_a ^ clear_b)); Ok(()) } diff --git a/compute/src/int.rs b/compute/src/int.rs index d424936..71dbe12 100644 --- a/compute/src/int.rs +++ b/compute/src/int.rs @@ -15,6 +15,61 @@ pub type GarbledInt32 = GarbledInt<32>; pub type GarbledInt64 = GarbledInt<64>; pub type GarbledInt128 = GarbledInt<128>; +// Define a new type GarbledInt +#[derive(Debug, Clone)] +pub struct GarbledInt { + pub(crate) bits: Vec, // Store the bits of the signed integer (in two's complement) + _phantom: PhantomData<[bool; N]>, // PhantomData to ensure the N bit size +} + +// Implement GarbledInt +impl GarbledInt { + // Constructor for GarbledInt from a boolean vector + pub fn new(bits: Vec) -> Self { + assert_eq!(bits.len(), N, "The number of bits must be {}", N); + GarbledInt { + bits, + _phantom: PhantomData, + } + } + + /// Simulates the local execution of the circuit using a 2 Party MPC protocol. + /// + /// The Multi-Party Computation is performed using the full cryptographic protocol exposed by the + /// [`Contributor`] and [`Evaluator`]. The messages between contributor and evaluator are exchanged + /// using local message queues. This function thus simulates an MPC execution on a local machine + /// under ideal network conditions, without any latency or bandwidth restrictions. + pub fn simulate( + &self, + circuit: &Circuit, + input_contributor: &[bool], + input_evaluator: &[bool], + ) -> anyhow::Result> { + let mut eval = Evaluator::new( + circuit.clone(), + input_evaluator, + ChaCha20Rng::from_entropy(), + )?; + let (mut contrib, mut msg_for_eval) = + Contributor::new(circuit, input_contributor, ChaCha20Rng::from_entropy())?; + + tracing::debug!("contributor ciphertext: {:?}", hex::encode(&msg_for_eval)); + + assert_eq!(contrib.steps(), eval.steps()); + + for _ in 0..eval.steps() { + let (next_state, msg_for_contrib) = eval.run(&msg_for_eval)?; + eval = next_state; + + let (next_state, reply) = contrib.run(&msg_for_contrib)?; + contrib = next_state; + + msg_for_eval = reply; + } + Ok(eval.output(&msg_for_eval)?) + } +} + impl From> for GarbledInt { fn from(uint: GarbledUint) -> Self { // Directly copy the bits from the unsigned Uint to the signed Int @@ -35,110 +90,93 @@ impl From<&GarbledUint> for GarbledInt { } } -// Define a new type GarbledInt -#[derive(Debug, Clone)] -pub struct GarbledInt { - pub(crate) bits: Vec, // Store the bits of the signed integer (in two's complement) - _phantom: PhantomData<[bool; N]>, // PhantomData to ensure the N bit size -} - -// Implement GarbledInt -impl GarbledInt { - // Constructor for GarbledInt from a boolean vector - pub fn new(bits: Vec) -> Self { - assert_eq!(bits.len(), N, "The number of bits must be {}", N); - GarbledInt { - bits, - _phantom: PhantomData, - } - } - - // Create a GarbledInt from an i8 value - pub fn from_i8(value: i8) -> Self { - assert!(N <= 8, "Int can only support up to 8 bits for from_i8"); +impl From for GarbledInt { + fn from(value: i8) -> Self { + assert!(N <= 8, "Int can only support up to 8 bits for i8"); - // Convert i8 to bits, least-significant bit first (little-endian, two's complement) + // Convert i8 to bits, least-significant bit first (two's complement) let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut mask = 1; + + for _ in 0..N { + bits.push((value & mask) != 0); + mask <<= 1; } GarbledInt::new(bits) } +} - // Convert a GarbledInt to an i8 value - pub fn to_i8(&self) -> i8 { - assert!(N <= 8, "Int can only be converted to i8 if N <= 8"); +impl From for GarbledInt { + fn from(value: i16) -> Self { + assert!(N <= 16, "Int can only support up to 16 bits for i16"); - let mut value: i8 = 0; + let mut bits = Vec::with_capacity(N); + let mut mask = 1; - // Iterate through the bits and reconstruct the i8 value (two's complement) - for (i, &bit) in self.bits.iter().enumerate() { - if bit { - value |= 1 << i; - } + for _ in 0..N { + bits.push((value & mask) != 0); + mask <<= 1; } - value + GarbledInt::new(bits) } +} - // Create a GarbledInt from an i16 value - pub fn from_i16(value: i16) -> Self { - assert!( - N <= 16, - "Int can only support up to 16 bits for from_i16" - ); +impl From for GarbledInt { + fn from(value: i32) -> Self { + assert!(N <= 32, "Int can only support up to 32 bits for i32"); - // Convert i16 to bits, least-significant bit first (little-endian, two's complement) let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut mask = 1; + + for _ in 0..N { + bits.push((value & mask) != 0); + mask <<= 1; } GarbledInt::new(bits) } +} - // Convert a GarbledInt to an i16 value - // Convert a GarbledInt to an i16 value - pub fn to_i16(&self) -> i16 { - assert!(N <= 16, "Int can only be converted to i16 if N <= 16"); +impl From for GarbledInt { + fn from(value: i64) -> Self { + assert!(N <= 64, "Int can only support up to 64 bits for i64"); - let mut value: i16 = 0; + let mut bits = Vec::with_capacity(N); + let mut mask = 1; - // Iterate through the bits and reconstruct the i16 value - for (i, &bit) in self.bits.iter().enumerate() { - if bit { - value |= 1 << i; - } + for _ in 0..N { + bits.push((value & mask) != 0); + mask <<= 1; } - value + GarbledInt::new(bits) } +} - // Create a GarbledInt from an i32 value - pub fn from_i32(value: i32) -> Self { - assert!( - N <= 32, - "Int can only support up to 32 bits for from_i32" - ); +impl From for GarbledInt { + fn from(value: i128) -> Self { + assert!(N <= 128, "Int can only support up to 128 bits for i128"); - // Convert i32 to bits, least-significant bit first (little-endian, two's complement) let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut mask = 1; + + for _ in 0..N { + bits.push((value & mask) != 0); + mask <<= 1; } GarbledInt::new(bits) } +} - // Convert a GarbledInt to an i32 value - pub fn to_i32(&self) -> i32 { - assert!(N <= 32, "Int can only be converted to i32 if N <= 32"); - - let mut value: i32 = 0; +impl From> for i8 { + fn from(gint: GarbledInt) -> Self { + assert!(N <= 8, "Int can only be converted to i8 if N <= 8"); - // Iterate through the bits and reconstruct the i32 value - for (i, &bit) in self.bits.iter().enumerate() { + let mut value: i8 = 0; + for (i, &bit) in gint.bits.iter().enumerate() { if bit { value |= 1 << i; } @@ -146,31 +184,29 @@ impl GarbledInt { value } +} - // Create a GarbledInt from an i64 value - pub fn from_i64(value: i64) -> Self { - assert!( - N <= 64, - "Int can only support up to 64 bits for from_i64" - ); +impl From> for i16 { + fn from(gint: GarbledInt) -> Self { + assert!(N <= 16, "Int can only be converted to i16 if N <= 16"); - // Convert i64 to bits, least-significant bit first (little-endian, two's complement) - let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut value: i16 = 0; + for (i, &bit) in gint.bits.iter().enumerate() { + if bit { + value |= 1 << i; + } } - GarbledInt::new(bits) + value } +} - // Convert a GarbledInt to an i64 value - pub fn to_i64(&self) -> i64 { - assert!(N <= 64, "Int can only be converted to i64 if N <= 64"); - - let mut value: i64 = 0; +impl From> for i32 { + fn from(gint: GarbledInt) -> Self { + assert!(N <= 32, "Int can only be converted to i32 if N <= 32"); - // Iterate through the bits and reconstruct the i64 value - for (i, &bit) in self.bits.iter().enumerate() { + let mut value: i32 = 0; + for (i, &bit) in gint.bits.iter().enumerate() { if bit { value |= 1 << i; } @@ -178,30 +214,29 @@ impl GarbledInt { value } +} - pub fn from_i128(value: i128) -> Self { - assert!( - N <= 128, - "Int can only support up to 128 bits for from_i128" - ); +impl From> for i64 { + fn from(gint: GarbledInt) -> Self { + assert!(N <= 64, "Int can only be converted to i64 if N <= 64"); - // Convert i128 to bits, least-significant bit first (little-endian, two's complement) - let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut value: i64 = 0; + for (i, &bit) in gint.bits.iter().enumerate() { + if bit { + value |= 1 << i; + } } - GarbledInt::new(bits) + value } +} - // Convert a GarbledInt to an i128 value - pub fn to_i128(&self) -> i128 { +impl From> for i128 { + fn from(gint: GarbledInt) -> Self { assert!(N <= 128, "Int can only be converted to i128 if N <= 128"); let mut value: i128 = 0; - - // Iterate through the bits and reconstruct the i128 value - for (i, &bit) in self.bits.iter().enumerate() { + for (i, &bit) in gint.bits.iter().enumerate() { if bit { value |= 1 << i; } @@ -209,112 +244,120 @@ impl GarbledInt { value } +} - /// Simulates the local execution of the circuit using a 2 Party MPC protocol. - /// - /// The Multi-Party Computation is performed using the full cryptographic protocol exposed by the - /// [`Contributor`] and [`Evaluator`]. The messages between contributor and evaluator are exchanged - /// using local message queues. This function thus simulates an MPC execution on a local machine - /// under ideal network conditions, without any latency or bandwidth restrictions. - pub fn simulate( - &self, - circuit: &Circuit, - input_contributor: &[bool], - input_evaluator: &[bool], - ) -> anyhow::Result> { - let mut eval = Evaluator::new( - circuit.clone(), - input_evaluator, - ChaCha20Rng::from_entropy(), - )?; - let (mut contrib, mut msg_for_eval) = - Contributor::new(circuit, input_contributor, ChaCha20Rng::from_entropy())?; - - tracing::debug!("contributor ciphertext: {:?}", hex::encode(&msg_for_eval)); +#[cfg(test)] +mod tests { + use super::*; + use crate::uint::{GarbledUint128, GarbledUint16, GarbledUint32, GarbledUint64, GarbledUint8}; - assert_eq!(contrib.steps(), eval.steps()); + #[test] + fn test_from_negative_i8() { + let a: GarbledInt8 = (-2_i8).into(); // Two's complement binary for -2 is 11111110 + let result: i8 = a.into(); + assert_eq!(result, -2_i8); + } - for _ in 0..eval.steps() { - let (next_state, msg_for_contrib) = eval.run(&msg_for_eval)?; - eval = next_state; + #[test] + fn test_from_positive_i8() { + let a: GarbledInt8 = 3_i8.into(); // Binary for 3 is 00000011 + let result: i8 = a.into(); + assert_eq!(result, 3); + } - let (next_state, reply) = contrib.run(&msg_for_contrib)?; - contrib = next_state; + #[test] + fn test_from_negative_i16() { + let a: GarbledInt16 = (-21845_i16).into(); // Two's complement binary for -21845 is 1010101010101011 + let result: i16 = a.into(); + assert_eq!(result, -21845); + } - msg_for_eval = reply; - } - Ok(eval.output(&msg_for_eval)?) + #[test] + fn test_from_positive_i16() { + let a: GarbledInt16 = 21845_i16.into(); // Binary for 21845 is 0101010101010101 + let result: i16 = a.into(); + assert_eq!(result, 21845); } -} -// Test conversions -#[cfg(test)] -mod tests { - use super::*; - use crate::uint::{GarbledUint128, GarbledUint16, GarbledUint32, GarbledUint64, GarbledUint8}; + #[test] + fn test_from_negative_i32() { + let a: GarbledInt32 = (-1431655765_i32).into(); // Two's complement binary for -1431655765 is 10101010101010101010101010101011 + let result: i32 = a.into(); + assert_eq!(result, -1431655765); + } #[test] - fn test_from_i8() { - let a = GarbledInt8::from_i8(-86); // Two's complement binary for -86 is 10101010 - assert_eq!(a.to_i8(), -86); + fn test_from_positive_i32() { + let a: GarbledInt32 = 1431655765_i32.into(); // Binary for 1431655765 is 01010101010101010101010101010101 + let result: i32 = a.into(); + assert_eq!(result, 1431655765); } #[test] - fn test_from_i16() { - let a = GarbledInt16::from_i16(-21845); // Two's complement binary for -21845 is 1010101010101011 - assert_eq!(a.to_i16(), -21845); + fn test_from_negative_i64() { + let a: GarbledInt64 = (-6148914691236517205_i64).into(); // Two's complement binary for -6148914691236517205 is 1010101010101010101010101010101010101010101010101010101010101011 + let result: i64 = a.into(); + assert_eq!(result, -6148914691236517205); } #[test] - fn test_from_i32() { - let a = GarbledInt32::from_i32(-1431655765); // Two's complement binary for -1431655765 is 10101010101010101010101010101011 - assert_eq!(a.to_i32(), -1431655765); + fn test_from_positive_i64() { + let a: GarbledInt64 = 6148914691236517205_i64.into(); // Binary for 6148914691236517205 is 0101010101010101010101010101010101010101010101010101010101010101 + let result: i64 = a.into(); + assert_eq!(result, 6148914691236517205); } #[test] - fn test_from_i64() { - let a = GarbledInt64::from_i64(-6148914691236517205); // Two's complement binary for -6148914691236517205 is 1010101010101010101010101010101010101010101010101010101010101011 - assert_eq!(a.to_i64(), -6148914691236517205); + fn test_from_negative_i128() { + let a: GarbledInt128 = (-6148914691236517205_i128).into(); // Two's complement binary for -6148914691236517205 is 1010101010101010101010101010101010101010101010101010101010101011 + let result: i128 = a.into(); + assert_eq!(result, -6148914691236517205); } #[test] - fn test_from_i128() { - let a = GarbledInt128::from_i128(-6148914691236517205); // Two's complement binary for -6148914691236517205 is 1010101010101010101010101010101010101010101010101010101010101011 - assert_eq!(a.to_i128(), -6148914691236517205); + fn test_from_positive_i128() { + let a: GarbledInt128 = 6148914691236517205_i128.into(); // Binary for 6148914691236517205 is 0101010101010101010101010101010101010101010101010101010101010101 + let result: i128 = a.into(); + assert_eq!(result, 6148914691236517205); } #[test] - fn test_from_uint_i8() { - let uint = GarbledUint8::from_u8(170); // 10101010 (unsigned) - let int: GarbledInt8 = GarbledInt::from(uint); // Interpreted as -86 (two's complement signed) - assert_eq!(int.to_i8(), -86); + fn test_from_uint_to_int_i8() { + let uint: GarbledUint8 = 170_u8.into(); // 10101010 (unsigned) + let int: GarbledInt8 = uint.into(); // Interpreted as -86 (two's complement signed) + let result: i8 = int.into(); + assert_eq!(result, 170_u8 as i8); } #[test] - fn test_from_uint_i16() { - let uint = GarbledUint16::from_u16(43707); // 1010101010101011 (unsigned) - let int: GarbledInt16 = GarbledInt::from(uint); // Interpreted as -21845 (two's complement signed) - assert_eq!(int.to_i16(), 43707_u16 as i16); + fn test_from_uint_to_int_i16() { + let uint: GarbledUint16 = 43707_u16.into(); // 1010101010101011 (unsigned) + let int: GarbledInt16 = uint.into(); // Interpreted as -21845 (two's complement signed) + let result: i16 = int.into(); + assert_eq!(result, 43707_u16 as i16); } #[test] - fn test_into_uint_i32() { - let uint = GarbledUint32::from_u32(2863311530); // 10101010101010101010101010101010 (unsigned) + fn test_from_uint_to_int_i32() { + let uint: GarbledUint32 = 2863311530_u32.into(); // 10101010101010101010101010101010 (unsigned) let int: GarbledInt32 = uint.into(); // Interpreted as -1431655766 (two's complement signed) - assert_eq!(int.to_i32(), 2863311530_u32 as i32); + let result: i32 = int.into(); + assert_eq!(result, 2863311530_u32 as i32); } #[test] - fn test_into_uint_i64() { - let uint = GarbledUint64::from_u64(12297829382473034410); // 1010101010101010101010101010101010101010101010101010101010101010 (unsigned) + fn test_from_uint_to_int_i64() { + let uint: GarbledUint64 = 12297829382473034410_u64.into(); // 1010101010101010101010101010101010101010101010101010101010101010 (unsigned) let int: GarbledInt64 = uint.into(); // Interpreted as -6148914691236517206 (two's complement signed) - assert_eq!(int.to_i64(), 12297829382473034410_u64 as i64); + let result: i64 = int.into(); + assert_eq!(result, 12297829382473034410_u64 as i64); } #[test] - fn test_into_uint_i128() { - let uint = GarbledUint128::from_u128(12297829382473034410); // 1010101010101010101010101010101010101010101010101010101010101010 (unsigned) + fn test_from_uint_to_int_i128() { + let uint: GarbledUint128 = 12297829382473034410_u128.into(); // 1010101010101010101010101010101010101010101010101010101010101010 (unsigned) let int: GarbledInt128 = uint.into(); // Interpreted as -6148914691236517206 (two's complement signed) - assert_eq!(int.to_i128(), 12297829382473034410_u128 as i128); + let result: i128 = int.into(); + assert_eq!(result, 12297829382473034410_u128 as i128); } } diff --git a/compute/src/lib.rs b/compute/src/lib.rs index 0b44978..4c381bf 100644 --- a/compute/src/lib.rs +++ b/compute/src/lib.rs @@ -1,3 +1,4 @@ pub mod int; pub mod operations; +mod simulator; pub mod uint; diff --git a/compute/src/operations/arithmetic.rs b/compute/src/operations/arithmetic.rs index 336fb12..dacd674 100644 --- a/compute/src/operations/arithmetic.rs +++ b/compute/src/operations/arithmetic.rs @@ -1,4 +1,5 @@ use crate::int::GarbledInt; +use crate::simulator::simulate; use crate::uint::GarbledUint; use std::ops::{Add, Mul, Sub}; use tandem::{Circuit, Gate}; @@ -48,7 +49,7 @@ fn build_and_simulate_arithmetic( let program = Circuit::new(gates, output_indices); // Simulate the circuit - let result = lhs.simulate(&program, &lhs.bits, &rhs.bits).unwrap(); + let result = simulate(&program, &lhs.bits, &rhs.bits).unwrap(); // Return the resulting Uint GarbledUint::new(result) @@ -310,184 +311,150 @@ impl Sub for &GarbledInt { // tests #[cfg(test)] mod tests { - use super::*; use crate::int::{ GarbledInt128, GarbledInt16, GarbledInt32, GarbledInt4, GarbledInt64, GarbledInt8, }; use crate::uint::{GarbledUint128, GarbledUint16, GarbledUint32, GarbledUint64, GarbledUint8}; - #[test] - fn test_uint_add() { - let a = GarbledUint::<4>::new(vec![true, true, false, false]); // Binary 1100 - let b = GarbledUint::<4>::new(vec![false, false, true, true]); // Binary 0011 - - let result = a + b; // Perform addition on the 4-bit values - assert_eq!(result.to_u8(), 0b1111); // Binary 1111 (Addition result of 1100 + 0011) - } - #[test] fn test_from_u8_add() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = a + b; // Perform addition on the 4-bit values - assert_eq!(result.to_u8(), 170 + 85); // Expected result of addition between 10101010 and 01010101 + let result: u8 = (a + b).into(); // Perform addition on the 4-bit values + assert_eq!(result, 170_u8 + 85_u8); // Expected result of addition between 10101010 and 01010101 } #[test] fn test_from_u16_add() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 4370_u16.into(); // Binary 1010101010101011 + let b: GarbledUint16 = 2184_u16.into(); // Binary 0101010101010101 - let result = a + b; - assert_eq!(result.to_u16(), 43690 + 21845); // Expected result of addition between 1010101010101010 and 0101010101010101 + let result: u16 = (a + b).into(); // Perform addition on the 4-bit values + assert_eq!(result, 4370_u16 + 2184_u16); // Expected result of addition between 1010101010101011 and 0101010101010101 } #[test] fn test_from_u32_add() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a + b; - assert_eq!(result.to_u32(), 2863311530 + 1431655765); // Expected result of addition between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = (a + b).into(); // Perform addition on the 4-bit values + assert_eq!(result, 2863311530_u32 + 1431655765_u32); // Expected result of addition between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_add() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a + b; - assert_eq!(result.to_u64(), 12297829382473034410 + 6148914691236517205); + let result: u64 = (a + b).into(); // Perform addition on the 4-bit values + assert_eq!(result, 12297829382473034410_u64 + 6148914691236517205_u64); // Expected result of addition between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_add() { - let a = GarbledUint128::from_u128(12297829382473034410); // Binary 10101010 - let b = GarbledUint128::from_u128(6148914691236517205); // Binary 01010101 - - let result = a + b; - assert_eq!(result.to_u128(), 12297829382473034410 + 6148914691236517205); - - println!("{}", result.to_u128()); - // Expected result of addition between 10101010 and 01010101 - } - - #[test] - fn test_int_add() { - let a = GarbledInt4::from_i8(3); - let b = GarbledInt4::from_i8(2); + let a: GarbledUint128 = 12297829382473034410_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 6148914691236517205_u128.into(); // Binary 01010101 - let result = a + b; // Perform addition on the 4-bit values - assert_eq!(result.to_i8(), 3 + 2); + let result: u128 = (a + b).into(); // Perform addition on the 4-bit values + assert_eq!(result, 12297829382473034410_u128 + 6148914691236517205_u128); } #[test] fn test_from_i8_add() { - let a = GarbledInt8::from_i8(3_i8); - let b = GarbledInt8::from_i8(2_i8); + let a: GarbledInt8 = 3_i8.into(); + let b: GarbledInt8 = (-2_i8).into(); - let result = a + b; // Perform addition on the 8-bit values - assert_eq!(result.to_i8(), 3_i8 + 2_i8); + let result: i8 = (a + b).into(); // Perform addition on the 8-bit values + assert_eq!(result, 3_i8 - 2_i8); // Expected result of addition between 3 and -2 } #[test] fn test_from_i16_add() { // use larger values to test the 16-bit addition - let a = GarbledInt16::from_i16(1340_i16); - let b = GarbledInt16::from_i16(8543_i16); + let a: GarbledInt16 = 1340_i16.into(); + let b: GarbledInt16 = 8543_i16.into(); - let result = a + b; // Perform addition on the 16-bit values - assert_eq!(result.to_i16(), 1340_i16 + 8543_i16); + let result: i16 = (a + b).into(); // Perform addition on the 16-bit values + assert_eq!(result, 1340_i16 + 8543_i16); } #[test] fn test_from_i32_add() { // use larger values to test the 32-bit addition - let a = GarbledInt32::from_i32(17034322_i32); - let b = GarbledInt32::from_i32(84928323_i32); + let a: GarbledInt32 = 17034322_i32.into(); + let b: GarbledInt32 = 84928323_i32.into(); - let result = a + b; // Perform addition on the 32-bit values - assert_eq!(result.to_i32(), 17034322_i32 + 84928323_i32); + let result: i32 = (a + b).into(); // Perform addition on the 32-bit values + assert_eq!(result, 17034322_i32 + 84928323_i32); } #[test] fn test_from_i64_add() { // use larger values to test the 64-bit addition - let a = GarbledInt64::from_i64(170343221234_i64); - let b = GarbledInt64::from_i64(849283231234_i64); + let a: GarbledInt64 = 170343221234_i64.into(); + let b: GarbledInt64 = 849283231234_i64.into(); - let result = a + b; // Perform addition on the 64-bit values - assert_eq!(result.to_i64(), 170343221234_i64 + 849283231234_i64); + let result: i64 = (a + b).into(); // Perform addition on the 64-bit values + assert_eq!(result, 170343221234_i64 + 849283231234_i64); } #[test] fn test_from_i128_add() { // use larger values to test the 128-bit addition - let a = GarbledInt128::from_i128(170343221234567890_i128); - let b = GarbledInt128::from_i128(849283231234567890_i128); + let a: GarbledInt128 = 170343221234567890_i128.into(); + let b: GarbledInt128 = 849283231234567890_i128.into(); - let result = a + b; // Perform addition on the 128-bit values - assert_eq!( - result.to_i128(), - 170343221234567890_i128 + 849283231234567890_i128 - ); - } - - #[test] - fn test_uint_sub() { - let a = GarbledUint::<4>::from_u8(3); - let b = GarbledUint::<4>::from_u8(2); - - let result = a - b; // Perform subtraction on the 4-bit values - assert_eq!(result.to_u8(), 3 - 2); + let result: i128 = (a + b).into(); // Perform addition on the 128-bit values + assert_eq!(result, 170343221234567890_i128 + 849283231234567890_i128); } #[test] fn test_from_u8_sub() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(100); // Binary 01100100 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 100_u8.into(); // Binary 01100100 - let result = a - b; - assert_eq!(result.to_u8(), 170 - 100); // Expected result of subtraction between 10101010 and 01010101 + let result: u8 = (a - b).into(); + assert_eq!(result, 170_u8 - 100_u8); // Expected result of subtraction between 10101010 and 01010101 } #[test] fn test_from_u16_sub() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43707_u16.into(); // Binary 1010101010101011 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a - b; - assert_eq!(result.to_u16(), 43690 - 21845); // Expected result of subtraction between 1010101010101010 and 0101010101010101 + let result: u16 = (a - b).into(); + assert_eq!(result, 43707_u16 - 21845_u16); // Expected result of subtraction between 1010101010101011 and 0101010101010101 } #[test] fn test_from_u32_sub() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a - b; - assert_eq!(result.to_u32(), 2863311530 - 1431655765); // Expected result of subtraction between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = (a - b).into(); + assert_eq!(result, 2863311530_u32 - 1431655765_u32); // Expected result of subtraction between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_sub() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a - b; - assert_eq!(result.to_u64(), 12297829382473034410 - 6148914691236517205); + let result: u64 = (a - b).into(); + assert_eq!(result, 12297829382473034410_u64 - 6148914691236517205_u64); // Expected result of subtraction between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_sub() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 12297829382473034410_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 6148914691236517205_u128.into(); // Binary 01010101 - let result = a - b; - assert_eq!(result.to_u128(), 170 - 85); // Expected result of subtraction between 10101010 and 01010101 + let result: u128 = (a - b).into(); + assert_eq!(result, 12297829382473034410_u128 - 6148914691236517205_u128); } #[test] @@ -519,62 +486,71 @@ mod tests { #[test] fn test_int_sub() { - let a = GarbledInt4::from_i8(3); - let b = GarbledInt4::from_i8(2); + let a: GarbledInt4 = 3_i8.into(); + let b: GarbledInt4 = 2_i8.into(); - let result = a - b; // Perform subtraction on the 4-bit values - assert_eq!(result.to_i8(), 3 - 2); + let result: i8 = (a - b).into(); + assert_eq!(result, 3_i8 - 2_i8); } #[test] fn test_from_i8_sub() { - let a = GarbledInt8::from_i8(3_i8); - let b = GarbledInt8::from_i8(2_i8); + let a: GarbledInt8 = 3_i8.into(); + let b: GarbledInt8 = (-2_i8).into(); - let result = a - b; // Perform subtraction on the 8-bit values - assert_eq!(result.to_i8(), 3_i8 - 2_i8); + let result: i8 = (a - b).into(); // Perform subtraction on the 8-bit values + assert_eq!(result, 3_i8 - (-2_i8)); // Expected result of subtraction between 3 and -2 } #[test] fn test_from_i16_sub() { // use larger values to test the 16-bit subtraction - let a = GarbledInt16::from_i16(1340_i16); - let b = GarbledInt16::from_i16(8543_i16); + let a: GarbledInt16 = 1340_i16.into(); + let b: GarbledInt16 = 8543_i16.into(); - let result = a - b; // Perform subtraction on the 16-bit values - assert_eq!(result.to_i16(), 1340_i16 - 8543_i16); + let result: i16 = (a - b).into(); // Perform subtraction on the 16-bit values + assert_eq!(result, 1340_i16 - 8543_i16); } #[test] fn test_from_i32_sub() { // use larger values to test the 32-bit subtraction - let a = GarbledInt32::from_i32(17034322_i32); - let b = GarbledInt32::from_i32(84928323_i32); + let a: GarbledInt32 = 17034322_i32.into(); + let b: GarbledInt32 = 84928323_i32.into(); - let result = a - b; // Perform subtraction on the 32-bit values - assert_eq!(result.to_i32(), 17034322_i32 - 84928323_i32); + let result: i32 = (a - b).into(); // Perform subtraction on the 32-bit values + assert_eq!(result, 17034322_i32 - 84928323_i32); } #[test] fn test_from_i64_sub() { // use larger values to test the 64-bit subtraction - let a = GarbledInt64::from_i64(170343221234_i64); - let b = GarbledInt64::from_i64(849283231234_i64); + let a: GarbledInt64 = 170343221234_i64.into(); + let b: GarbledInt64 = 849283231234_i64.into(); - let result = a - b; // Perform subtraction on the 64-bit values - assert_eq!(result.to_i64(), 170343221234_i64 - 849283231234_i64); + let result: i64 = (a - b).into(); // Perform subtraction on the 64-bit values + assert_eq!(result, 170343221234_i64 - 849283231234_i64); } #[test] fn test_from_i128_sub() { // use larger values to test the 128-bit subtraction - let a = GarbledInt128::from_i128(170343221234567890_i128); - let b = GarbledInt128::from_i128(849283231234567890_i128); + let a: GarbledInt128 = 170343221234567890_i128.into(); + let b: GarbledInt128 = 849283231234567890_i128.into(); - let result = a - b; // Perform subtraction on the 128-bit values - assert_eq!( - result.to_i128(), - 170343221234567890_i128 - 849283231234567890_i128 - ); + let result: i128 = (a - b).into(); // Perform subtraction on the 128-bit values + assert_eq!(result, 170343221234567890_i128 - 849283231234567890_i128); + } + + #[test] + fn test_multiple_additions() { + let a: GarbledUint32 = 170_u32.into(); + let b: GarbledUint32 = 85_u32.into(); + let c: GarbledUint32 = 42_u32.into(); + let d: GarbledUint32 = 21_u32.into(); + let e: GarbledUint32 = 10_u32.into(); + + let result: u32 = (a + b + c + d + e).into(); + assert_eq!(result, 170_u32 + 85_u32 + 42_u32 + 21_u32 + 10_u32); } } diff --git a/compute/src/operations/bitwise.rs b/compute/src/operations/bitwise.rs index 279a46e..b69aac3 100644 --- a/compute/src/operations/bitwise.rs +++ b/compute/src/operations/bitwise.rs @@ -1,4 +1,5 @@ use crate::int::GarbledInt; +use crate::simulator::simulate; use crate::uint::GarbledUint; use std::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr}; use tandem::{Circuit, Gate}; @@ -34,7 +35,7 @@ fn build_and_simulate( // Simulate the circuit let bits_rhs = rhs.map_or(lhs.bits.clone(), |r| r.bits.clone()); - let result = lhs.simulate(&program, &lhs.bits, &bits_rhs).unwrap(); + let result = simulate(&program, &lhs.bits, &bits_rhs).unwrap(); // Return the resulting Uint GarbledUint::new(result) @@ -138,7 +139,7 @@ fn build_and_simulate_not(input: &GarbledUint) -> GarbledUint let program = Circuit::new(gates, output_indices); // Simulate the circuit - let result = input.simulate(&program, &input.bits, &input.bits).unwrap(); + let result = simulate(&program, &input.bits, &input.bits).unwrap(); // Return the resulting Uint GarbledUint::new(result) @@ -207,7 +208,7 @@ fn build_and_simulate_or( // Simulate the circuit let bits_rhs = rhs.map_or(lhs.bits.clone(), |r| r.bits.clone()); - let result = lhs.simulate(&program, &lhs.bits, &bits_rhs).unwrap(); + let result = simulate(&program, &lhs.bits, &bits_rhs).unwrap(); // Return the resulting Uint GarbledUint::new(result) @@ -401,7 +402,7 @@ fn build_and_simulate_nand( let program = Circuit::new(gates, output_indices); let bits_rhs = rhs.map_or(lhs.bits.clone(), |r| r.bits.clone()); - let result = lhs.simulate(&program, &lhs.bits, &bits_rhs).unwrap(); + let result = simulate(&program, &lhs.bits, &bits_rhs).unwrap(); GarbledUint::new(result) } @@ -447,7 +448,7 @@ fn build_and_simulate_nor( let program = Circuit::new(gates, output_indices); let bits_rhs = rhs.map_or(lhs.bits.clone(), |r| r.bits.clone()); - let result = lhs.simulate(&program, &lhs.bits, &bits_rhs).unwrap(); + let result = simulate(&program, &lhs.bits, &bits_rhs).unwrap(); GarbledUint::new(result) } @@ -484,7 +485,7 @@ fn build_and_simulate_xnor( let program = Circuit::new(gates, output_indices); let bits_rhs = rhs.map_or(lhs.bits.clone(), |r| r.bits.clone()); - let result = lhs.simulate(&program, &lhs.bits, &bits_rhs).unwrap(); + let result = simulate(&program, &lhs.bits, &bits_rhs).unwrap(); GarbledUint::new(result) } @@ -520,303 +521,259 @@ impl GarbledInt { #[cfg(test)] mod tests { use super::*; - use crate::int::GarbledInt8; + use crate::int::{GarbledInt128, GarbledInt16, GarbledInt32, GarbledInt64, GarbledInt8}; use crate::uint::{GarbledUint128, GarbledUint16, GarbledUint32, GarbledUint64, GarbledUint8}; - #[test] - fn test_uint_xor() { - let a = GarbledUint::<2>::new(vec![true, false]); // Binary 10 - let b = GarbledUint::<2>::new(vec![false, true]); // Binary 01 - - let result = a ^ b; // Perform XOR on the 2-bit values - assert_eq!(result.to_u8(), 3); // Expected result of XOR between 10 and 01 - - let a = GarbledUint::<4>::new(vec![true, true, false, false]); // Binary 1100 - let b = GarbledUint::<4>::new(vec![false, false, true, true]); // Binary 0011 - - let result = a ^ b; // Perform XOR on the 4-bit values - assert_eq!(result.to_u8(), 15); // Expected result of XOR between 1100 and 0011 - } - #[test] fn test_from_u8_xor() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = &a ^ &b; - assert_eq!(result.to_u8(), 255); // Expected result of XOR between 10101010 and 01010101 + let result: u8 = (&a ^ &b).into(); + assert_eq!(result, 255); // Expected result of XOR between 10101010 and 01010101 } #[test] fn test_from_u16_xor() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43690_u16.into(); // Binary 1010101010101010 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a ^ b; - assert_eq!(result.to_u16(), 65535); // Expected result of XOR between 1010101010101010 and 0101010101010101 + let result: u16 = (&a ^ &b).into(); + assert_eq!(result, 65535); // Expected result of XOR between 1010101010101010 and 0101010101010101 } #[test] fn test_from_u32_xor() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a ^ b; - assert_eq!(result.to_u32(), 4294967295); // Expected result of XOR between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = (&a ^ &b).into(); + assert_eq!(result, 4294967295); // Expected result of XOR between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_xor() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a ^ b; - assert_eq!(result.to_u64(), 18446744073709551615); // Expected result of XOR between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 + let result: u64 = (&a ^ &b).into(); + assert_eq!(result, 18446744073709551615); // Expected result of XOR between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_xor() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 85_u128.into(); // Binary 01010101 - let result = a ^ b; - assert_eq!(result.to_u128(), 255); // Expected result of XOR between 10101010 and 01010101 + let result: u128 = (&a ^ &b).into(); + assert_eq!(result, 255); // Expected result of XOR between 10101010 and 01010101 } #[test] fn test_from_i8_xor() { - let a = GarbledInt8::from_i8(-86); // Two's complement binary for -86 is 10101010 - let b = GarbledInt8::from_i8(-43); // Two's complement binary for -43 is 11010101 + let a: GarbledInt8 = (-86_i8).into(); // Two's complement binary for -86 is 10101010 + let b: GarbledInt8 = (-43).into(); // Two's complement binary for -43 is 11010101 - let result = a ^ b; - assert_eq!(result.to_i8(), -86_i8 ^ -43_i8); // Expected result of XOR between 10101010 and 11010101 + let result: i8 = (a ^ b).into(); + assert_eq!(result, -86_i8 ^ -43_i8); // Expected result of XOR between 10101010 and 11010101 } #[test] fn test_from_i16_xor() { - let a = GarbledInt::<16>::from_i16(-21846); // Two's complement binary for -21846 is 1010101010101010 - let b = GarbledInt::<16>::from_i16(-10923); // Two's complement binary for -10923 is 1101010101010101 - - let result = a ^ b; - assert_eq!(result.to_i16(), -21846_i16 ^ -10923_i16); // Expected result of XOR between 1010101010101010 and 1101010101010101 - } - - #[test] - fn test_uint_and() { - let a = GarbledUint::<2>::new(vec![true, false]); // Binary 10 - let b = GarbledUint::<2>::new(vec![false, true]); // Binary 01 - - let result = a & b; // Perform AND on the 2-bit values - assert_eq!(result.to_u8(), 0); // Expected result of AND between 10 and 01 - - let a = GarbledUint::<4>::new(vec![true, true, false, false]); // Binary 1100 - let b = GarbledUint::<4>::new(vec![false, false, true, true]); // Binary 0011 - - let result = a & b; // Perform AND on the 4-bit values - assert_eq!(result.to_u8(), 0); // Expected result of AND between 1100 and 0011 - - let a = GarbledUint::<4>::new(vec![true, false, false, true]); // Binary 1001 - let b = GarbledUint::<4>::new(vec![false, false, false, false]); // Binary 0000 + let a: GarbledInt<16> = (-21846_i16).into(); // Two's complement binary for -21846 is 1010101010101010 + let b: GarbledInt<16> = (-10923_i16).into(); // Two's complement binary for -10923 is 1101010101010101 - let result = a & b; // Perform AND on the 4-bit values - assert_eq!(result.to_u8(), 0); // Expected result of AND between 1001 and 0000 + let result: i16 = (a ^ b).into(); + assert_eq!(result, -21846_i16 ^ -10923_i16); // Expected result of XOR between 1010101010101010 and 1101010101010101 } #[test] fn test_from_u8_and() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = a & b; - assert_eq!(result.to_u8(), 170 & 85); // Expected result of AND between 10101010 and 01010101 + let result: u8 = (a & b).into(); + assert_eq!(result, 170 & 85); // Expected result of AND between 10101010 and 01010101 } #[test] fn test_from_u16_and() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43690_u16.into(); // Binary 1010101010101010 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a & b; - assert_eq!(result.to_u16(), 43690 & 21845); // Expected result of AND between 1010101010101010 and 0101010101010101 + let result: u16 = (a & b).into(); + assert_eq!(result, 43690 & 21845); // Expected result of AND between 1010101010101010 and 0101010101010101 } #[test] fn test_from_u32_and() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a & b; - assert_eq!(result.to_u32(), 2863311530 & 1431655765); // Expected result of AND between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = (a & b).into(); + assert_eq!(result, 2863311530 & 1431655765); // Expected result of AND between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_and() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a & b; - assert_eq!(result.to_u64(), 12297829382473034410 & 6148914691236517205); + let result: u64 = (a & b).into(); + assert_eq!(result, 12297829382473034410 & 6148914691236517205); // Expected result of AND between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_and() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 85_u128.into(); // Binary 01010101 - let result = a & b; - assert_eq!(result.to_u128(), 170 & 85); // Expected result of AND between 10101010 and 01010101 + let result: u128 = (a & b).into(); + assert_eq!(result, 170 & 85); // Expected result of AND between 10101010 and 01010101 } #[test] fn test_from_u8_or() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = a | b; - assert_eq!(result.to_u8(), 170 | 85); // Expected result of OR between 10101010 and 01010101 + let result: u8 = (a | b).into(); + assert_eq!(result, 170 | 85); // Expected result of OR between 10101010 and 01010101 } #[test] fn test_from_u16_or() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43707_u16.into(); // Binary 1010101010101011 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a | b; - assert_eq!(result.to_u16(), 43690 | 21845); // Expected result of OR between 1010101010101010 and 0101010101010101 + let result: u16 = (a | b).into(); + assert_eq!(result, 43707 | 21845); // Expected result of OR between 1010101010101011 and 0101010101010101 } #[test] fn test_from_u32_or() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a | b; - assert_eq!(result.to_u32(), 2863311530 | 1431655765); // Expected result of OR between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = (a | b).into(); + assert_eq!(result, 2863311530 | 1431655765); // Expected result of OR between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_or() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a | b; - assert_eq!(result.to_u64(), 12297829382473034410 | 6148914691236517205); + let result: u64 = (a | b).into(); + assert_eq!(result, 12297829382473034410 | 6148914691236517205); // Expected result of OR between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_or() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 85_u128.into(); // Binary 01010101 - let result = a | b; - assert_eq!(result.to_u128(), 170 | 85); // Expected result of OR between 10101010 and 01010101 + let result: u128 = (a | b).into(); + assert_eq!(result, 170 | 85); // Expected result of OR between 10101010 and 01010101 } #[test] fn test_from_i8_or() { - let a = GarbledInt8::from_i8(-86); // Two's complement binary for -86 is 10101010 - let b = GarbledInt8::from_i8(-43); // Two's complement binary for -43 is 11010101 + let a: GarbledInt8 = (-86).into(); // Two's complement binary for -86 is 10101010 + let b: GarbledInt8 = (-43).into(); // Two's complement binary for -43 is 11010101 - let result = a | b; - assert_eq!(result.to_i8(), -86_i8 | -43_i8); // Expected result of OR between 10101010 and 11010101 + let result: i8 = (a | b).into(); + assert_eq!(result, -86_i8 | -43_i8); // Expected result of OR between 10101010 and 11010101 } #[test] fn test_from_i16_or() { - let a = GarbledInt::<16>::from_i16(-21846); // Two's complement binary for -21846 is 1010101010101010 - let b = GarbledInt::<16>::from_i16(-10923); // Two's complement binary for -10923 is 1101010101010101 + let a: GarbledInt<16> = (-21846).into(); // Two's complement binary for -21846 is 1010101010101010 + let b: GarbledInt<16> = (-10923).into(); // Two's complement binary for -10923 is 1101010101010101 - let result = a | b; - assert_eq!(result.to_i16(), -21846_i16 | -10923_i16); // Expected result of OR between 1010101010101010 and 1101010101010101 + let result: i16 = (a | b).into(); + assert_eq!(result, -21846_i16 | -10923_i16); // Expected result of OR between 1010101010101010 and 1101010101010101 } #[test] fn test_from_i32_or() { - let a = GarbledInt::<32>::from_i32(-1431655766); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 - let b = GarbledInt::<32>::from_i32(-715827883); // Two's complement binary for -715827883 is 11010101010101010101010101010101 + let a: GarbledInt<32> = (-1431655766).into(); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 + let b: GarbledInt<32> = (-715827883).into(); // Two's complement binary for -715827883 is 11010101010101010101010101010101 - let result = a | b; - assert_eq!(result.to_i32(), -1431655766_i32 | -715827883_i32); + let result: i32 = (a | b).into(); + assert_eq!(result, -1431655766_i32 | -715827883_i32); // Expected result of OR between 10101010101010101010101010101010 and 11010101010101010101010101010101 } #[test] fn test_from_i64_or() { - let a = GarbledInt::<64>::from_i64(-6148914691236517206); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<64>::from_i64(-3074457345618258603); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledInt<64> = (-6148914691236517206_i64).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt<64> = (-3074457345618258603_i64).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a | b; - assert_eq!( - result.to_i64(), - -6148914691236517206_i64 | -3074457345618258603_i64 - ); - // Expected result of OR between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 + let result: i64 = (a | b).into(); + assert_eq!(result, -6148914691236517206_i64 | -3074457345618258603_i64); } #[test] fn test_from_i128_or() { - let a = GarbledInt::<128>::from_i128(-6148914691236517206); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<128>::from_i128(-3074457345618258603); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledInt<128> = (-6148914691236517206_i128).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt<128> = (-3074457345618258603_i128).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a | b; + let result: i128 = (a | b).into(); assert_eq!( - result.to_i128(), + result, -6148914691236517206_i128 | -3074457345618258603_i128 ); - // Expected result of OR between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_int_and() { - let a = GarbledInt8::from_i8(-86); // Two's complement binary for -86 is 10101010 - let b = GarbledInt8::from_i8(-43); // Two's complement binary for -43 is 11010101 + let a: GarbledInt8 = (-86).into(); // Two's complement binary for -86 is 10101010 + let b: GarbledInt8 = (-43).into(); // Two's complement binary for -43 is 11010101 - let result = a & b; - assert_eq!(result.to_i8(), -86_i8 & -43_i8); // Expected result of AND between 10101010 and 11010101 + let result: i8 = (a & b).into(); + assert_eq!(result, -86_i8 & -43_i8); // Expected result of AND between 10101010 and 11010101 } #[test] fn test_from_i16_and() { - let a = GarbledInt::<16>::from_i16(-21846); // Two's complement binary for -21846 is 1010101010101010 - let b = GarbledInt::<16>::from_i16(-10923); // Two's complement binary for -10923 is 1101010101010101 + let a: GarbledInt16 = (-21846).into(); // Two's complement binary for -21846 is 1010101010101010 + let b: GarbledInt16 = (-10923).into(); // Two's complement binary for -10923 is 1101010101010101 - let result = a & b; - assert_eq!(result.to_i16(), -21846_i16 & -10923_i16); // Expected result of AND between 1010101010101010 and 1101010101010101 + let result: i16 = (a & b).into(); + assert_eq!(result, -21846_i16 & -10923_i16); // Expected result of AND between 1010101010101010 and 1101010101010101 } #[test] fn test_from_i32_and() { - let a = GarbledInt::<32>::from_i32(-1431655766); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 - let b = GarbledInt::<32>::from_i32(-715827883); // Two's complement binary for -715827883 is 11010101010101010101010101010101 + let a: GarbledInt32 = (-1431655766).into(); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 + let b: GarbledInt32 = (-715827883).into(); // Two's complement binary for -715827883 is 11010101010101010101010101010101 - let result = a & b; - assert_eq!(result.to_i32(), -1431655766_i32 & -715827883_i32); + let result: i32 = (a & b).into(); + assert_eq!(result, -1431655766_i32 & -715827883_i32); // Expected result of AND between 10101010101010101010101010101010 and 11010101010101010101010101010101 } #[test] fn test_from_i64_and() { - let a = GarbledInt::<64>::from_i64(-6148914691236517206); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<64>::from_i64(-3074457345618258603); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledInt64 = (-6148914691236517206_i64).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt64 = (-3074457345618258603_i64).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a & b; - assert_eq!( - result.to_i64(), - -6148914691236517206_i64 & -3074457345618258603_i64 - ); + let result: i64 = (a & b).into(); + assert_eq!(result, -6148914691236517206_i64 & -3074457345618258603_i64); // Expected result of AND between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_i128_and() { - let a = GarbledInt::<128>::from_i128(-6148914691236517206); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<128>::from_i128(-3074457345618258603); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledInt128 = (-6148914691236517206_i128).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt128 = (-3074457345618258603_i128).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a & b; + let result: i128 = (a & b).into(); assert_eq!( - result.to_i128(), + result, -6148914691236517206_i128 & -3074457345618258603_i128 ); // Expected result of AND between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 @@ -824,518 +781,488 @@ mod tests { #[test] fn test_from_u8_not() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 - let result = !a; - assert_eq!(result.to_u8(), !170); // Expected result of NOT on 10101010 + let result: u8 = (!a).into(); + assert_eq!(result, !170); // Expected result of NOT on 10101010 } #[test] fn test_from_u16_not() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 + let a: GarbledUint16 = 43690_u16.into(); // Binary 1010101010101010 - let result = !a; - assert_eq!(result.to_u16(), !43690); // Expected result of NOT on 1010101010101010 + let result: u16 = (!a).into(); + assert_eq!(result, !43690); // Expected result of NOT on 1010101010101010 } #[test] fn test_from_u32_not() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 - let result = !a; - assert_eq!(result.to_u32(), !2863311530); // Expected result of NOT on 10101010101010101010101010101010 + let result: u32 = (!a).into(); + assert_eq!(result, !2863311530); // Expected result of NOT on 10101010101010101010101010101010 } #[test] fn test_from_u64_not() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let result = !a; - assert_eq!(result.to_u64(), !12297829382473034410); - // Expected result of NOT on 1010101010101010101010101010101010101010101010101010101010101010 + let result: u64 = (!a).into(); + assert_eq!(result, !12297829382473034410); // Expected result of NOT on 1010101010101010101010101010101010101010101010101010101010101010 } #[test] fn test_from_u128_not() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 - let result = !a; - assert_eq!(result.to_u128(), !170); // Expected result of NOT on 10101010 + let result: u128 = (!a).into(); + assert_eq!(result, !170); // Expected result of NOT on 10101010 } #[test] fn test_from_i8_not() { - let a = GarbledInt8::from_i8(-86); // Two's complement binary for -86 is 10101010 + let a: GarbledInt8 = (-86).into(); // Two's complement binary for -86 is 10101010 - let result = !a; - assert_eq!(result.to_i8(), !-86_i8); // Expected result of NOT on 10101010 + let result: i8 = (!a).into(); + assert_eq!(result, !-86_i8); // Expected result of NOT on 10101010 } #[test] fn test_from_i16_not() { - let a = GarbledInt::<16>::from_i16(-21846); // Two's complement binary for -21846 is 1010101010101010 + let a: GarbledInt16 = (-21846).into(); // Two's complement binary for -21846 is 1010101010101010 - let result = !a; - assert_eq!(result.to_i16(), !-21846_i16); // Expected result of NOT on 1010101010101010 + let result: i16 = (!a).into(); + assert_eq!(result, !-21846_i16); // Expected result of NOT on 1010101010101010 } #[test] fn test_from_i32_not() { - let a = GarbledInt::<32>::from_i32(-1431655766); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 + let a: GarbledInt32 = (-1431655766).into(); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 - let result = !a; - assert_eq!(result.to_i32(), !-1431655766_i32); // Expected result of NOT on 10101010101010101010101010101010 + let result: i32 = (!a).into(); + assert_eq!(result, !-1431655766_i32); // Expected result of NOT on 10101010101010101010101010101010 } #[test] fn test_from_i64_not() { - let a = GarbledInt::<64>::from_i64(-6148914691236517206); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let a: GarbledInt64 = (-6148914691236517206_i64).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let result = !a; - assert_eq!(result.to_i64(), !-6148914691236517206_i64); - // Expected result of NOT on 1010101010101010101010101010101010101010101010101010101010101010 + let result: i64 = (!a).into(); + assert_eq!(result, !-6148914691236517206_i64); // Expected result of NOT on 1010101010101010101010101010101010101010101010101010101010101010 } #[test] fn test_from_i128_not() { - let a = GarbledInt::<128>::from_i128(-6148914691236517206); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let a: GarbledInt128 = (-6148914691236517206_i128).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let result = !a; - assert_eq!(result.to_i128(), !-6148914691236517206_i128); - // Expected result of NOT on 1010101010101010101010101010101010101010101010101010101010101010 + let result: i128 = (!a).into(); + assert_eq!(result, !-6148914691236517206_i128); // Expected result of NOT on 1010101010101010101010101010101010101010101010101010101010101010 } #[test] fn test_left_shift_uint() { - let a = GarbledUint::<4>::new(vec![false, false, false, true]); // Binary 1000 - - let result = a << 1; // Perform left shift by 1 - assert_eq!(result.to_u8(), 0b0000_u8); // Binary 0000 (Left shift result of 1000) - - // binary literal of 0000 - - let a = GarbledUint::<4>::new(vec![false, false, false, true]); // Binary 1000 - - let result = a << 2; // Perform left shift by 2 - assert_eq!(result.to_u8(), 0b0000_u8); // Binary 0000 (Left shift result of 1000) - - let a = GarbledUint::<4>::new(vec![false, false, false, true]); // Binary 1000 - - let result = a << 3; // Perform left shift by 3 - assert_eq!(result.to_u8(), 0b0000); // Binary 0000 (Left shift result of 1000) + let a: GarbledUint8 = 0b0001_u8.into(); // Binary 0001 + let result: u8 = (a << 1).into(); // Perform left shift by 1 + assert_eq!(result, 0b0010_u8); - //let a = Uint::<4>::new(vec![false, false, false, true]); // Binary 0001 + let a: GarbledUint8 = 0b0001_u8.into(); // Binary 0001 + let result: u8 = (a << 2).into(); // Perform left shift by 2 + assert_eq!(result, 0b0100_u8); - let a = GarbledUint8::from_u8(1); // Binary 0001 - - let result = a << 1; // Perform left shift by 1 - assert_eq!(result.to_u8(), 0b0010); // Binary 0010 (Left shift result of 0001) + let a: GarbledUint8 = 0b0001_u8.into(); // Binary 0001 + let result: u8 = (a << 3).into(); // Perform left shift by 3 + assert_eq!(result, 0b1000); let a = GarbledUint::<4>::new(vec![true, false, false, false]); // Binary 0001 - - let result = a << 2; // Perform left shift by 2 - assert_eq!(result.to_u8(), 0b0100); // Binary 0100 (Left shift result of 0001) + let result: u8 = (a << 2).into(); // Perform left shift by 2 + assert_eq!(result, 0b0100); // Binary 0100 (Left shift result of 0001) let a = GarbledUint::<4>::new(vec![true, false, false, false]); // Binary 0001 - let result = a << 3; // Perform left shift by 3 - assert_eq!(result.to_u8(), 0b1000); // Binary 1000 (Left shift result of 0001) + let result: u8 = (a << 3).into(); // Perform left shift by 3 + assert_eq!(result, 0b1000); // Binary 1000 (Left shift result of 0001) } #[test] fn test_left_shift_int() { - let a = GarbledInt8::from_i8(-128); // Two's complement binary for -128 is 10000000 + let a: GarbledInt8 = 0b1000_i8.into(); // Binary 1000 - let result = a << 1; // Perform left shift by 1 - assert_eq!(result.to_i8(), -128_i8 << 1); // Expected result of left shift by 1 on 10000000 + let result: i8 = (a << 1).into(); // Perform left shift by 1 + assert_eq!(result, 0b10000_i8); // Binary 0000 (Left shift result of 1000) - let a = GarbledInt8::from_i8(-128); // Two's complement binary for -128 is 10000000 + let a: GarbledInt8 = 0b1000_i8.into(); // Binary 1000 - let result = a << 2; // Perform left shift by 2 - assert_eq!(result.to_i8(), -128_i8 << 2); // Expected result of left shift by 2 on 10000000 + let result: i8 = (a << 2).into(); // Perform left shift by 2 + assert_eq!(result, 0b100000_i8); // Binary 0000 (Left shift result of 1000) - let a = GarbledInt8::from_i8(-128); // Two's complement binary for -128 is 10000000 + let a: GarbledInt8 = 0b1000_i8.into(); // Binary 1000 - let result = a << 3; // Perform left shift by 3 - assert_eq!(result.to_i8(), -128_i8 << 3); // Expected result of left shift by 3 on 10000000 + let result: i8 = (a << 3).into(); // Perform left shift by 3 + assert_eq!(result, 0b1000000_i8); // Binary 0000 (Left shift result of 1000) - let a = GarbledInt8::from_i8(-1); // Two's complement binary for -1 is 11111111 + let a: GarbledInt8 = 1_i8.into(); // Binary 1000 - let result = a << 1; // Perform left shift by 1 - assert_eq!(result.to_i8(), -1_i8 << 1); // Expected result of left shift by 1 on 11111111 + let result: i8 = (a << 1).into(); // Perform left shift by 1 + assert_eq!(result, 0b0010_i8); // Binary 0010 (Left shift result of 0001) - let a = GarbledInt8::from_i8(-1); // Two's complement binary for -1 is 11111111 + let a = GarbledInt::<4>::new(vec![true, false, false, false]); // Binary 0001 - let result = a << 2; // Perform left shift by 2 - assert_eq!(result.to_i8(), -1_i8 << 2); // Expected result of left shift by 2 on 11111111 + let result: i8 = (a << 2).into(); // Perform left shift by 2 + assert_eq!(result, 0b0100_i8); // Binary 0100 (Left shift result of 0001) - let a = GarbledInt8::from_i8(-1); // Two's complement binary for -1 is 11111111 + let a = GarbledInt::<4>::new(vec![true, false, false, false]); // Binary 0001 - let result = a << 3; // Perform left shift by 3 - assert_eq!(result.to_i8(), -1_i8 << 3); // Expected result of left shift by 3 on 11111111 + let result: i8 = (a << 3).into(); // Perform left shift by 3 + assert_eq!(result, 0b1000_i8); // Binary 1000 (Left shift result of 0001) } #[test] fn test_right_shift_uint() { let a = GarbledUint::<4>::new(vec![false, false, false, true]); // Binary 1000 - let result = a >> 1; // Perform right shift by 1 - assert_eq!(result.to_u8(), 0b0100); // Binary 0100 (Right shift result of 1000) + let result: u8 = (a >> 1).into(); // Perform right shift by 1 + assert_eq!(result, 0b0100); // Binary 0100 (Right shift result of 1000) let a = GarbledUint::<4>::new(vec![false, false, false, true]); // Binary 1000 - let result = a >> 2; // Perform right shift by 2 - assert_eq!(result.to_u8(), 0b0010); // Binary 0010 (Right shift result of 1000) + let result: u8 = (a >> 2).into(); // Perform right shift by 2 + assert_eq!(result, 0b0010); // Binary 0010 (Right shift result of 1000) let a = GarbledUint::<4>::new(vec![false, false, false, true]); // Binary 1000 - let result = a >> 3; // Perform right shift by 3 - assert_eq!(result.to_u8(), 0b0001); // Binary 0001 (Right shift result of 1000) + let result: u8 = (a >> 3).into(); // Perform right shift by 3 + assert_eq!(result, 0b0001); // Binary 0001 (Right shift result of 1000) } #[test] fn test_from_u8_nand() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = a.nand(b); - assert_eq!(result.to_u8(), !(170 & 85)); // Expected result of NAND between 10101010 and 01010101 + let result: u8 = a.nand(b).into(); + assert_eq!(result, !(170 & 85)); // Expected result of NAND between 10101010 and 01010101 } #[test] fn test_from_u16_nand() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43690_u16.into(); // Binary 1010101010101010 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a.nand(b); - assert_eq!(result.to_u16(), !(43690 & 21845)); // Expected result of NAND between 1010101010101010 and 0101010101010101 + let result: u16 = a.nand(b).into(); + assert_eq!(result, !(43690 & 21845)); // Expected result of NAND between 1010101010101010 and 0101010101010101 } #[test] fn test_from_u32_nand() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a.nand(b); - assert_eq!(result.to_u32(), !(2863311530 & 1431655765)); - // Expected result of NAND between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = a.nand(b).into(); + assert_eq!(result, !(2863311530 & 1431655765)); // Expected result of NAND between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_nand() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a.nand(b); - assert_eq!( - result.to_u64(), - !(12297829382473034410 & 6148914691236517205) - ); + let result: u64 = a.nand(b).into(); + assert_eq!(result, !(12297829382473034410 & 6148914691236517205)); // Expected result of NAND between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_nand() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 85_u128.into(); // Binary 01010101 - let result = a.nand(b); - assert_eq!(result.to_u128(), !(170 & 85)); // Expected result of NAND between 10101010 and 01010101 + let result: u128 = a.nand(b).into(); + assert_eq!(result, !(170 & 85)); // Expected result of NAND between 10101010 and 01010101 } #[test] fn test_from_i8_nand() { - let a = GarbledInt8::from_i8(-86_i8); // Two's complement binary for -86 is 10101010 - let b = GarbledInt8::from_i8(-43_i8); // Two's complement binary for -43 is 11010101 + let a: GarbledInt8 = (-86).into(); // Two's complement binary for -86 is 10101010 + let b: GarbledInt8 = (-43).into(); // Two's complement binary for -43 is 11010101 - let result = a.nand(b); - assert_eq!(result.to_i8(), !(-86_i8 & -43_i8)); // Expected result of NAND between 10101010 and 11010101 + let result: i8 = a.nand(b).into(); + assert_eq!(result, !(-86_i8 & -43_i8)); // Expected result of NAND between 10101010 and 11010101 } #[test] fn test_from_i16_nand() { - let a = GarbledInt::<16>::from_i16(-21846_i16); // Two's complement binary for -21846 is 1010101010101010 - let b = GarbledInt::<16>::from_i16(-10923_i16); // Two's complement binary for -10923 is 11010101 + let a: GarbledInt<16> = (-21846).into(); // Two's complement binary for -21846 is 1010101010101010 + let b: GarbledInt<16> = (-10923).into(); // Two's complement binary for -10923 is 1101010101010101 - let result = a.nand(b); - assert_eq!(result.to_i16(), !(-21846_i16 & -10923_i16)); // Expected result of NAND between 1010101010101010 and 11010101 + let result: i16 = a.nand(b).into(); + assert_eq!(result, !(-21846_i16 & -10923_i16)); // Expected result of NAND between 1010101010101010 and 1101010101010101 } #[test] fn test_from_i32_nand() { - let a = GarbledInt::<32>::from_i32(-1431655766_i32); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 - let b = GarbledInt::<32>::from_i32(-715827883_i32); // Two's complement binary for -715827883 is 11010101 + let a: GarbledInt<32> = (-1431655766).into(); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 + let b: GarbledInt<32> = (-715827883).into(); // Two's complement binary for -715827883 is 11010101010101010101010101010101 - let result = a.nand(b); - assert_eq!(result.to_i32(), !(-1431655766_i32 & -715827883_i32)); - // Expected result of NAND between 10101010101010101010101010101010 and 11010101 + let result: i32 = a.nand(b).into(); + assert_eq!(result, !(-1431655766_i32 & -715827883_i32)); + // Expected result of NAND between 10101010101010101010101010101010 and 11010101010101010101010101010101 } #[test] fn test_from_i64_nand() { - let a = GarbledInt::<64>::from_i64(-6148914691236517206_i64); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<64>::from_i64(-3074457345618258603_i64); // Two's complement binary for -3074457345618258603 is 11010101 + let a: GarbledInt64 = (-6148914691236517206_i64).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt64 = (-3074457345618258603_i64).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a.nand(b); + let result: i64 = a.nand(b).into(); assert_eq!( - result.to_i64(), + result, !(-6148914691236517206_i64 & -3074457345618258603_i64) ); - // Expected result of NAND between 1010101010101010101010101010101010101010101010101010101010101010 and 11010101 } #[test] fn test_from_i128_nand() { - let a = GarbledInt::<128>::from_i128(-6148914691236517206_i128); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<128>::from_i128(-3074457345618258603_i128); // Two's complement binary for -3074457345618258603 is 11010101 + let a: GarbledInt<128> = (-6148914691236517206_i128).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt<128> = (-3074457345618258603_i128).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a.nand(b); + let result: i128 = a.nand(b).into(); assert_eq!( - result.to_i128(), + result, !(-6148914691236517206_i128 & -3074457345618258603_i128) ); - // Expected result of NAND between 1010101010101010101010101010101010101010101010101010101010101010 and 11010101 } #[test] fn test_from_u8_nor() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = a.nor(b); - assert_eq!(result.to_u8(), !(170 | 85)); // Expected result of NOR between 10101010 and 01010101 + let result: u8 = a.nor(b).into(); + assert_eq!(result, !(170 | 85)); // Expected result of NOR between 10101010 and 01010101 } #[test] fn test_from_u16_nor() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43707_u16.into(); // Binary 1010101010101011 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a.nor(b); - assert_eq!(result.to_u16(), !(43690 | 21845)); // Expected result of NOR between 1010101010101010 and 0101010101010101 + let result: u16 = a.nor(b).into(); + assert_eq!(result, !(43707 | 21845)); // Expected result of NOR between 1010101010101011 and 0101010101010101 } #[test] fn test_from_u32_nor() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a.nor(b); - assert_eq!(result.to_u32(), !(2863311530 | 1431655765)); - // Expected result of NOR between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = a.nor(b).into(); + assert_eq!(result, !(2863311530 | 1431655765)); // Expected result of NOR between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_nor() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a.nor(b); - assert_eq!( - result.to_u64(), - !(12297829382473034410 | 6148914691236517205) - ); + let result: u64 = a.nor(b).into(); + assert_eq!(result, !(12297829382473034410 | 6148914691236517205)); // Expected result of NOR between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_nor() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 85_u128.into(); // Binary 01010101 - let result = a.nor(b); - assert_eq!(result.to_u128(), !(170 | 85)); // Expected result of NOR between 10101010 and 01010101 + let result: u128 = a.nor(b).into(); + assert_eq!(result, !(170 | 85)); // Expected result of NOR between 10101010 and 01010101 } #[test] fn test_from_i8_nor() { - let a = GarbledInt8::from_i8(-86_i8); // Two's complement binary for -86 is 10101010 - let b = GarbledInt8::from_i8(-43_i8); // Two's complement binary for -43 is 11010101 + let a: GarbledInt8 = (-86).into(); // Two's complement binary for -86 is 10101010 + let b: GarbledInt8 = (-43).into(); // Two's complement binary for -43 is 11010101 - let result = a.nor(b); - assert_eq!(result.to_i8(), !(-86_i8 | -43_i8)); // Expected result of NOR between 10101010 and 11010101 + let result: i8 = a.nor(b).into(); + assert_eq!(result, !(-86_i8 | -43_i8)); // Expected result of NOR between 10101010 and 11010101 } #[test] fn test_from_i16_nor() { - let a = GarbledInt::<16>::from_i16(-21846_i16); // Two's complement binary for -21846 is 1010101010101010 - let b = GarbledInt::<16>::from_i16(-10923_i16); // Two's complement binary for -10923 is 11010101 + let a: GarbledInt<16> = (-21846).into(); // Two's complement binary for -21846 is 1010101010101010 + let b: GarbledInt<16> = (-10923).into(); // Two's complement binary for -10923 is 1101010101010101 - let result = a.nor(b); - assert_eq!(result.to_i16(), !(-21846_i16 | -10923_i16)); // Expected result of NOR between 1010101010101010 and 11010101 + let result: i16 = a.nor(b).into(); + assert_eq!(result, !(-21846_i16 | -10923_i16)); // Expected result of NOR between 1010101010101010 and 1101010101010101 } #[test] fn test_from_i32_nor() { - let a = GarbledInt::<32>::from_i32(-1431655766_i32); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 - let b = GarbledInt::<32>::from_i32(-715827883_i32); // Two's complement binary for -715827883 is 11010101 + let a: GarbledInt<32> = (-1431655766).into(); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 + let b: GarbledInt<32> = (-715827883).into(); // Two's complement binary for -715827883 is 11010101010101010101010101010101 - let result = a.nor(b); - assert_eq!(result.to_i32(), !(-1431655766_i32 | -715827883_i32)); - // Expected result of NOR between 10101010101010101010101010101010 and 11010101 + let result: i32 = a.nor(b).into(); + assert_eq!(result, !(-1431655766_i32 | -715827883_i32)); + // Expected result of NOR between 10101010101010101010101010101010 and 11010101010101010101010101010101 } #[test] fn test_from_i64_nor() { - let a = GarbledInt::<64>::from_i64(-6148914691236517206_i64); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<64>::from_i64(-3074457345618258603_i64); // Two's complement binary for -3074457345618258603 is 11010101 + let a: GarbledInt<64> = (-6148914691236517206_i64).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt<64> = (-3074457345618258603_i64).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a.nor(b); + let result: i64 = a.nor(b).into(); assert_eq!( - result.to_i64(), + result, !(-6148914691236517206_i64 | -3074457345618258603_i64) ); - // Expected result of NOR between 1010101010101010101010101010101010101010101010101010101010101010 and 11010101 + // Expected result of NOR between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_i128_nor() { - let a = GarbledInt::<128>::from_i128(-6148914691236517206_i128); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<128>::from_i128(-3074457345618258603_i128); // Two's complement binary for -3074457345618258603 is 11010101 + let a: GarbledInt<128> = (-6148914691236517206_i128).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt<128> = (-3074457345618258603_i128).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a.nor(b); + let result: i128 = a.nor(b).into(); assert_eq!( - result.to_i128(), + result, !(-6148914691236517206_i128 | -3074457345618258603_i128) ); - // Expected result of NOR between 1010101010101010101010101010101010101010101010101010101010101010 and 11010101 + // Expected result of NOR between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u8_xnor() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - let b = GarbledUint8::from_u8(85); // Binary 01010101 + let a: GarbledUint8 = 170_u8.into(); // Binary 10101010 + let b: GarbledUint8 = 85_u8.into(); // Binary 01010101 - let result = a.xnor(b); - assert_eq!(result.to_u8(), !(170 ^ 85)); // Expected result of XNOR between 10101010 and 01010101 + let result: u8 = a.xnor(b).into(); + assert_eq!(result, !(170 ^ 85)); // Expected result of XNOR between 10101010 and 01010101 } #[test] fn test_from_u16_xnor() { - let a = GarbledUint16::from_u16(43690); // Binary 1010101010101010 - let b = GarbledUint16::from_u16(21845); // Binary 0101010101010101 + let a: GarbledUint16 = 43690_u16.into(); // Binary 1010101010101010 + let b: GarbledUint16 = 21845_u16.into(); // Binary 0101010101010101 - let result = a.xnor(b); - assert_eq!(result.to_u16(), !(43690 ^ 21845)); // Expected result of XNOR between 1010101010101010 and 0101010101010101 + let result: u16 = a.xnor(b).into(); + assert_eq!(result, !(43690 ^ 21845)); // Expected result of XNOR between 1010101010101010 and 0101010101010101 } #[test] fn test_from_u32_xnor() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - let b = GarbledUint32::from_u32(1431655765); // Binary 01010101010101010101010101010101 + let a: GarbledUint32 = 2863311530_u32.into(); // Binary 10101010101010101010101010101010 + let b: GarbledUint32 = 1431655765_u32.into(); // Binary 01010101010101010101010101010101 - let result = a.xnor(b); - assert_eq!(result.to_u32(), !(2863311530 ^ 1431655765)); - // Expected result of XNOR between 10101010101010101010101010101010 and 01010101010101010101010101010101 + let result: u32 = a.xnor(b).into(); + assert_eq!(result, !(2863311530 ^ 1431655765)); // Expected result of XNOR between 10101010101010101010101010101010 and 01010101010101010101010101010101 } #[test] fn test_from_u64_xnor() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledUint64::from_u64(6148914691236517205); // Binary 0101010101010101010101010101010101010101010101010101010101010101 + let a: GarbledUint64 = 12297829382473034410_u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledUint64 = 6148914691236517205_u64.into(); // Binary 0101010101010101010101010101010101010101010101010101010101010101 - let result = a.xnor(b); - assert_eq!( - result.to_u64(), - !(12297829382473034410 ^ 6148914691236517205) - ); + let result: u64 = a.xnor(b).into(); + assert_eq!(result, !(12297829382473034410 ^ 6148914691236517205)); // Expected result of XNOR between 1010101010101010101010101010101010101010101010101010101010101010 and 0101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_u128_xnor() { - let a = GarbledUint128::from_u128(170); // Binary 10101010 - let b = GarbledUint128::from_u128(85); // Binary 01010101 + let a: GarbledUint128 = 170_u128.into(); // Binary 10101010 + let b: GarbledUint128 = 85_u128.into(); // Binary 01010101 - let result = a.xnor(b); - assert_eq!(result.to_u128(), !(170 ^ 85)); // Expected result of XNOR between 10101010 and 01010101 + let result: u128 = a.xnor(b).into(); + assert_eq!(result, !(170 ^ 85)); // Expected result of XNOR between 10101010 and 01010101 } #[test] fn test_from_i8_xnor() { - let a = GarbledInt8::from_i8(-86_i8); // Two's complement binary for -86 is 10101010 - let b = GarbledInt8::from_i8(-43_i8); // Two's complement binary for -43 is 11010101 + let a: GarbledInt8 = (-86).into(); // Two's complement binary for -86 is 10101010 + let b: GarbledInt8 = (-43).into(); // Two's complement binary for -43 is 11010101 - let result = a.xnor(b); - assert_eq!(result.to_i8(), !(-86_i8 ^ -43_i8)); // Expected result of XNOR between 10101010 and 11010101 + let result: i8 = a.xnor(b).into(); + assert_eq!(result, !(-86_i8 ^ -43_i8)); // Expected result of XNOR between 10101010 and 11010101 } #[test] fn test_from_i16_xnor() { - let a = GarbledInt::<16>::from_i16(-21846_i16); // Two's complement binary for -21846 is 1010101010101010 - let b = GarbledInt::<16>::from_i16(-10923_i16); // Two's complement binary for -10923 is 11010101 + let a: GarbledInt16 = (-21846).into(); // Two's complement binary for -21846 is 1010101010101010 + let b: GarbledInt16 = (-10923).into(); // Two's complement binary for -10923 is 1101010101010101 - let result = a.xnor(b); - assert_eq!(result.to_i16(), !(-21846_i16 ^ -10923_i16)); // Expected result of XNOR between 1010101010101010 and 11010101 + let result: i16 = a.xnor(b).into(); + assert_eq!(result, !(-21846_i16 ^ -10923_i16)); // Expected result of XNOR between 1010101010101010 and 1101010101010101 } #[test] fn test_from_i32_xnor() { - let a = GarbledInt::<32>::from_i32(-1431655766_i32); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 - let b = GarbledInt::<32>::from_i32(-715827883_i32); // Two's complement binary for -715827883 is 11010101 + let a: GarbledInt32 = (-1431655766).into(); // Two's complement binary for -1431655766 is 10101010101010101010101010101010 + let b: GarbledInt32 = (-715827883).into(); // Two's complement binary for -715827883 is 11010101010101010101010101010101 - let result = a.xnor(b); - assert_eq!(result.to_i32(), !(-1431655766_i32 ^ -715827883_i32)); - // Expected result of XNOR between 10101010101010101010101010101010 and 11010101 + let result: i32 = a.xnor(b).into(); + assert_eq!(result, !(-1431655766_i32 ^ -715827883_i32)); + // Expected result of XNOR between 10101010101010101010101010101010 and 11010101010101010101010101010101 } #[test] fn test_from_i64_xnor() { - let a = GarbledInt::<64>::from_i64(-6148914691236517206_i64); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<64>::from_i64(-3074457345618258603_i64); // Two's complement binary for -3074457345618258603 is 11010101 + let a: GarbledInt64 = (-6148914691236517206_i64).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt64 = (-3074457345618258603_i64).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a.xnor(b); + let result: i64 = a.xnor(b).into(); assert_eq!( - result.to_i64(), + result, !(-6148914691236517206_i64 ^ -3074457345618258603_i64) ); - // Expected result of XNOR between 1010101010101010101010101010101010101010101010101010101010101010 and 11010101 + // Expected result of XNOR between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 } #[test] fn test_from_i128_xnor() { - let a = GarbledInt::<128>::from_i128(-6148914691236517206_i128); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 - let b = GarbledInt::<128>::from_i128(-3074457345618258603_i128); // Two's complement binary for -3074457345618258603 is 11010101 + let a: GarbledInt128 = (-6148914691236517206_i128).into(); // Two's complement binary for -6148914691236517206 is 1010101010101010101010101010101010101010101010101010101010101010 + let b: GarbledInt128 = (-3074457345618258603_i128).into(); // Two's complement binary for -3074457345618258603 is 1101010101010101010101010101010101010101010101010101010101010101 - let result = a.xnor(b); + let result: i128 = a.xnor(b).into(); assert_eq!( - result.to_i128(), + result, !(-6148914691236517206_i128 ^ -3074457345618258603_i128) ); - // Expected result of XNOR between 1010101010101010101010101010101010101010101010101010101010101010 and 11010101 + // Expected result of XNOR between 1010101010101010101010101010101010101010101010101010101010101010 and 1101010101010101010101010101010101010101010101010101010101010101 } #[ignore = "still testing bitwise right shift int"] #[test] fn test_right_shift_int() { - let a = GarbledInt8::from_i8(-128); // Two's complement binary for -128 is 10000000 + let a: GarbledInt8 = 0b1000_i8.into(); // Binary 1000 - let result = a >> 1; // Perform right shift by 1 - assert_eq!(result.to_i8(), -128_i8 >> 1); // Expected result of right shift by 1 on 10000000 + let result: i8 = (a >> 1).into(); // Perform right shift by 1 + assert_eq!(result, 0b0100_i8); // Binary 0100 (Right shift result of 1000) - let a = GarbledInt8::from_i8(-128); // Two's complement binary for -128 is 10000000 + let a: GarbledInt8 = 0b1000_i8.into(); // Binary 1000 - let result = a >> 2; // Perform right shift by 2 - assert_eq!(result.to_i8(), -128_i8 >> 2); // Expected result of right shift by 2 on 10000000 + let result: i8 = (a >> 2).into(); // Perform right shift by 2 + assert_eq!(result, 0b0010_i8); // Binary 0010 (Right shift result of 1000) - let a = GarbledInt8::from_i8(-128); // Two's complement binary for -128 is 10000000 + let a: GarbledInt8 = 0b1000_i8.into(); // Binary 1000 - let result = a >> 3; // Perform right shift by 3 - assert_eq!(result.to_i8(), -128_i8 >> 3); // Expected result of right shift by 3 on 10000000 + let result: i8 = (a >> 3).into(); // Perform right shift by 3 + assert_eq!(result, 0b0001_i8); // Binary 0001 (Right shift result of 1000) - let a = GarbledInt8::from_i8(-1); // Two's complement binary for -1 is 11111111 + let a: GarbledInt8 = 1_i8.into(); // Binary 0001 - let result = a >> 1; // Perform right shift by 1 - assert_eq!(result.to_i8(), -1_i8 >> 1); // Expected result of right shift by 1 on 11111111 + let result: i8 = (a >> 1).into(); // Perform right shift by 1 + assert_eq!(result, 0b0000_i8); // Binary 0000 (Right shift result of 0001) - let a = GarbledInt8::from_i8(-1); // Two's complement binary for -1 is 11111111 + let a = GarbledInt::<4>::new(vec![true, false, false, false]); // Binary 0001 - let result = a >> 2; // Perform right shift by 2 - assert_eq!(result.to_i8(), -1_i8 >> 2); // Expected result of right shift by 2 on 11111111 + let result: i8 = (a >> 2).into(); // Perform right shift by 2 + assert_eq!(result, 0b0000_i8); // Binary 0000 (Right shift result of 0001) - let a = GarbledInt8::from_i8(-1); // Two's complement binary for -1 is 11111111 + let a = GarbledInt::<4>::new(vec![true, false, false, false]); // Binary 0001 - let result = a >> 3; // Perform right shift by 3 - assert_eq!(result.to_i8(), -1_i8 >> 3); // Expected result of right shift by 3 on 11111111 + let result: i8 = (a >> 3).into(); // Perform right shift by 3 + assert_eq!(result, 0b0000_i8); // Binary 0000 (Right shift result of 0001) } } diff --git a/compute/src/simulator.rs b/compute/src/simulator.rs new file mode 100644 index 0000000..bf825e7 --- /dev/null +++ b/compute/src/simulator.rs @@ -0,0 +1,39 @@ +use rand_chacha::rand_core::SeedableRng; +use rand_chacha::ChaCha20Rng; +use tandem::states::{Contributor, Evaluator}; +use tandem::Circuit; + +/// Simulates the local execution of the circuit using a 2 Party MPC protocol. +/// +/// The Multi-Party Computation is performed using the full cryptographic protocol exposed by the +/// [`Contributor`] and [`Evaluator`]. The messages between contributor and evaluator are exchanged +/// using local message queues. This function thus simulates an MPC execution on a local machine +/// under ideal network conditions, without any latency or bandwidth restrictions. +pub fn simulate( + circuit: &Circuit, + input_contributor: &[bool], + input_evaluator: &[bool], +) -> anyhow::Result> { + let mut eval = Evaluator::new( + circuit.clone(), + input_evaluator, + ChaCha20Rng::from_entropy(), + )?; + let (mut contrib, mut msg_for_eval) = + Contributor::new(circuit, input_contributor, ChaCha20Rng::from_entropy())?; + + tracing::debug!("contributor ciphertext: {:?}", hex::encode(&msg_for_eval)); + + assert_eq!(contrib.steps(), eval.steps()); + + for _ in 0..eval.steps() { + let (next_state, msg_for_contrib) = eval.run(&msg_for_eval)?; + eval = next_state; + + let (next_state, reply) = contrib.run(&msg_for_contrib)?; + contrib = next_state; + + msg_for_eval = reply; + } + Ok(eval.output(&msg_for_eval)?) +} diff --git a/compute/src/uint.rs b/compute/src/uint.rs index fff3733..6fd41d3 100644 --- a/compute/src/uint.rs +++ b/compute/src/uint.rs @@ -1,9 +1,5 @@ use crate::int::GarbledInt; -use rand_chacha::rand_core::SeedableRng; -use rand_chacha::ChaCha20Rng; use std::marker::PhantomData; -use tandem::states::{Contributor, Evaluator}; -use tandem::Circuit; pub type GarbledUint1 = GarbledUint<1>; pub type GarbledUint2 = GarbledUint<2>; @@ -14,6 +10,25 @@ pub type GarbledUint32 = GarbledUint<32>; pub type GarbledUint64 = GarbledUint<64>; pub type GarbledUint128 = GarbledUint<128>; +// Define a new type Uint +#[derive(Debug, Clone)] +pub struct GarbledUint { + pub(crate) bits: Vec, // Store the bits of the unsigned integer + _phantom: PhantomData<[bool; N]>, // PhantomData to ensure the N bit size +} + +// Implement Uint +impl GarbledUint { + // Constructor for GarbledUint from a boolean vector + pub fn new(bits: Vec) -> Self { + assert_eq!(bits.len(), N, "The number of bits must be {}", N); + GarbledUint { + bits, + _phantom: PhantomData, + } + } +} + impl From> for GarbledUint { fn from(uint: GarbledInt) -> Self { // Directly copy the bits from the unsigned Uint to the signed Int @@ -33,29 +48,10 @@ impl From<&GarbledInt> for GarbledUint { } } -// Define a new type Uint -#[derive(Debug, Clone)] -pub struct GarbledUint { - pub(crate) bits: Vec, // Store the bits of the unsigned integer - _phantom: PhantomData<[bool; N]>, // PhantomData to ensure the N bit size -} - -// Implement Uint -impl GarbledUint { - // Constructor for GarbledUint from a boolean vector - pub fn new(bits: Vec) -> Self { - assert_eq!(bits.len(), N, "The number of bits must be {}", N); - GarbledUint { - bits, - _phantom: PhantomData, - } - } - - // Create a GarbledUint from a u8 value - pub fn from_u8(value: u8) -> Self { - assert!(N <= 8, "Uint can only support up to 8 bits for from_u8"); +impl From for GarbledUint { + fn from(value: u8) -> Self { + assert!(N <= 8, "Uint can only support up to 8 bits for u8"); - // Convert u8 to bits, least-significant bit first (little-endian) let mut bits = Vec::with_capacity(N); for i in 0..N { bits.push((value >> i) & 1 == 1); @@ -63,31 +59,25 @@ impl GarbledUint { GarbledUint::new(bits) } +} - // Convert a GarbledUint to a u8 value - pub fn to_u8(&self) -> u8 { - assert!(N <= 8, "Uint can only be converted to u8 if N <= 8"); - - let mut value: u8 = 0; +impl From for GarbledUint { + fn from(value: u16) -> Self { + assert!(N <= 16, "Uint can only support up to 16 bits for u16"); - // Iterate through the bits and reconstruct the u8 value - for (i, &bit) in self.bits.iter().enumerate() { - if bit { - value |= 1 << i; // Set the corresponding bit in the u8 - } + let mut bits = Vec::with_capacity(N); + for i in 0..N { + bits.push((value >> i) & 1 == 1); } - value + GarbledUint::new(bits) } +} - // Create a GarbledUint from a u16 value - pub fn from_u16(value: u16) -> Self { - assert!( - N <= 16, - "Uint can only support up to 16 bits for from_u16" - ); +impl From for GarbledUint { + fn from(value: u32) -> Self { + assert!(N <= 32, "Uint can only support up to 32 bits for u32"); - // Convert u16 to bits, least-significant bit first (little-endian) let mut bits = Vec::with_capacity(N); for i in 0..N { bits.push((value >> i) & 1 == 1); @@ -95,31 +85,25 @@ impl GarbledUint { GarbledUint::new(bits) } +} - // Convert a GarbledUint to a u16 value - pub fn to_u16(&self) -> u16 { - assert!(N <= 16, "Uint can only be converted to u16 if N <= 16"); +impl From for GarbledUint { + fn from(value: u64) -> Self { + assert!(N <= 64, "Uint can only support up to 64 bits for u64"); - let mut value: u16 = 0; - - // Iterate through the bits and reconstruct the u16 value - for (i, &bit) in self.bits.iter().enumerate() { - if bit { - value |= 1 << i; // Set the corresponding bit in the u16 - } + let mut bits = Vec::with_capacity(N); + for i in 0..N { + bits.push((value >> i) & 1 == 1); } - value + GarbledUint::new(bits) } +} - // Create a GarbledUint from a u32 value - pub fn from_u32(value: u32) -> Self { - assert!( - N <= 32, - "Uint can only support up to 32 bits for from_u32" - ); +impl From for GarbledUint { + fn from(value: u128) -> Self { + assert!(N <= 128, "Uint can only support up to 128 bits for u128"); - // Convert u32 to bits, least-significant bit first (little-endian) let mut bits = Vec::with_capacity(N); for i in 0..N { bits.push((value >> i) & 1 == 1); @@ -127,122 +111,84 @@ impl GarbledUint { GarbledUint::new(bits) } +} - pub fn to_u32(&self) -> u32 { - assert!(N <= 32, "Uint can only be converted to u32 if N <= 32"); - - let mut value: u32 = 0; +impl From> for u8 { + fn from(guint: GarbledUint) -> Self { + assert!(N <= 8, "Uint can only be converted to u8 if N <= 8"); - // Iterate through the bits and reconstruct the u32 value - for (i, &bit) in self.bits.iter().enumerate() { + let mut value: u8 = 0; + for (i, &bit) in guint.bits.iter().enumerate() { if bit { - value |= 1 << i; // Set the corresponding bit in the u32 + value |= 1 << i; } } value } +} - // Create a GarbledUint from a u64 value - pub fn from_u64(value: u64) -> Self { - assert!( - N <= 64, - "Uint can only support up to 64 bits for from_u64" - ); +impl From> for u16 { + fn from(guint: GarbledUint) -> Self { + assert!(N <= 16, "Uint can only be converted to u16 if N <= 16"); - // Convert u64 to bits, least-significant bit first (little-endian) - let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut value: u16 = 0; + for (i, &bit) in guint.bits.iter().enumerate() { + if bit { + value |= 1 << i; + } } - GarbledUint::new(bits) + value } +} - // Convert a GarbledUint to a u64 value - pub fn to_u64(&self) -> u64 { - assert!(N <= 64, "Uint can only be converted to u64 if N <= 64"); - - let mut value: u64 = 0; +impl From> for u32 { + fn from(guint: GarbledUint) -> Self { + assert!(N <= 32, "Uint can only be converted to u32 if N <= 32"); - // Iterate through the bits and reconstruct the u64 value - for (i, &bit) in self.bits.iter().enumerate() { + let mut value: u32 = 0; + for (i, &bit) in guint.bits.iter().enumerate() { if bit { - value |= 1 << i; // Set the corresponding bit in the u64 + value |= 1 << i; } } value } +} - pub fn from_u128(value: u128) -> Self { - assert!( - N <= 128, - "Uint can only support up to 128 bits for from_u128" - ); +impl From> for u64 { + fn from(guint: GarbledUint) -> Self { + assert!(N <= 64, "Uint can only be converted to u64 if N <= 64"); - // Convert u128 to bits, least-significant bit first (little-endian) - let mut bits = Vec::with_capacity(N); - for i in 0..N { - bits.push((value >> i) & 1 == 1); + let mut value: u64 = 0; + for (i, &bit) in guint.bits.iter().enumerate() { + if bit { + value |= 1 << i; + } } - GarbledUint::new(bits) + value } +} - pub fn to_u128(&self) -> u128 { +impl From> for u128 { + fn from(guint: GarbledUint) -> Self { assert!( N <= 128, "Uint can only be converted to u128 if N <= 128" ); let mut value: u128 = 0; - - // Iterate through the bits and reconstruct the u128 value - for (i, &bit) in self.bits.iter().enumerate() { + for (i, &bit) in guint.bits.iter().enumerate() { if bit { - value |= 1 << i; // Set the corresponding bit in the u128 + value |= 1 << i; } } value } - - /// Simulates the local execution of the circuit using a 2 Party MPC protocol. - /// - /// The Multi-Party Computation is performed using the full cryptographic protocol exposed by the - /// [`Contributor`] and [`Evaluator`]. The messages between contributor and evaluator are exchanged - /// using local message queues. This function thus simulates an MPC execution on a local machine - /// under ideal network conditions, without any latency or bandwidth restrictions. - pub fn simulate( - &self, - circuit: &Circuit, - input_contributor: &[bool], - input_evaluator: &[bool], - ) -> anyhow::Result> { - let mut eval = Evaluator::new( - circuit.clone(), - input_evaluator, - ChaCha20Rng::from_entropy(), - )?; - let (mut contrib, mut msg_for_eval) = - Contributor::new(circuit, input_contributor, ChaCha20Rng::from_entropy())?; - - tracing::debug!("contributor ciphertext: {:?}", hex::encode(&msg_for_eval)); - - assert_eq!(contrib.steps(), eval.steps()); - - for _ in 0..eval.steps() { - let (next_state, msg_for_contrib) = eval.run(&msg_for_eval)?; - eval = next_state; - - let (next_state, reply) = contrib.run(&msg_for_contrib)?; - contrib = next_state; - - msg_for_eval = reply; - } - Ok(eval.output(&msg_for_eval)?) - } } // test conversions @@ -252,31 +198,36 @@ mod tests { #[test] fn test_from_u8() { - let a = GarbledUint8::from_u8(170); // Binary 10101010 - assert_eq!(a.to_u8(), 170); + let a: GarbledUint8 = 170u8.into(); // Binary 10101010 + let value: u8 = a.into(); + assert_eq!(value, 170); } #[test] fn test_from_u16() { - let a = GarbledUint16::from_u16(43707); // Binary 1010101010101011 - assert_eq!(a.to_u16(), 43707); + let a: GarbledUint16 = 43707u16.into(); // Binary 1010101010101011 + let value: u16 = a.into(); + assert_eq!(value, 43707); } #[test] fn test_from_u32() { - let a = GarbledUint32::from_u32(2863311530); // Binary 10101010101010101010101010101010 - assert_eq!(a.to_u32(), 2863311530); + let a: GarbledUint32 = 2863311530u32.into(); // Binary 10101010101010101010101010101010 + let value: u32 = a.into(); + assert_eq!(value, 2863311530); } #[test] fn test_from_u64() { - let a = GarbledUint64::from_u64(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - assert_eq!(a.to_u64(), 12297829382473034410); + let a: GarbledUint64 = 12297829382473034410u64.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let value: u64 = a.into(); + assert_eq!(value, 12297829382473034410); } #[test] fn test_from_u128() { - let a = GarbledUint128::from_u128(12297829382473034410); // Binary 1010101010101010101010101010101010101010101010101010101010101010 - assert_eq!(a.to_u128(), 12297829382473034410); + let a: GarbledUint128 = 12297829382473034410u128.into(); // Binary 1010101010101010101010101010101010101010101010101010101010101010 + let value: u128 = a.into(); + assert_eq!(value, 12297829382473034410); } } From 8a7687322b97cd1fca90972b079ed856a71aba0d Mon Sep 17 00:00:00 2001 From: Jay Logelin Date: Wed, 9 Oct 2024 14:05:45 -0300 Subject: [PATCH 2/2] refactor(compute): clean up --- compute/src/int.rs | 42 +--------------------------- compute/src/operations/arithmetic.rs | 2 +- compute/src/operations/bitwise.rs | 2 +- 3 files changed, 3 insertions(+), 43 deletions(-) diff --git a/compute/src/int.rs b/compute/src/int.rs index 71dbe12..9ccfb52 100644 --- a/compute/src/int.rs +++ b/compute/src/int.rs @@ -1,10 +1,6 @@ use crate::uint::GarbledUint; -use rand_chacha::rand_core::SeedableRng; -use rand_chacha::ChaCha20Rng; use std::convert::From; use std::marker::PhantomData; -use tandem::states::{Contributor, Evaluator}; -use tandem::Circuit; pub type GarbledInt1 = GarbledInt<1>; pub type GarbledInt2 = GarbledInt<2>; @@ -18,7 +14,7 @@ pub type GarbledInt128 = GarbledInt<128>; // Define a new type GarbledInt #[derive(Debug, Clone)] pub struct GarbledInt { - pub(crate) bits: Vec, // Store the bits of the signed integer (in two's complement) + pub(crate) bits: Vec, // Store the bits of the signed integer (in two's complement form) _phantom: PhantomData<[bool; N]>, // PhantomData to ensure the N bit size } @@ -32,42 +28,6 @@ impl GarbledInt { _phantom: PhantomData, } } - - /// Simulates the local execution of the circuit using a 2 Party MPC protocol. - /// - /// The Multi-Party Computation is performed using the full cryptographic protocol exposed by the - /// [`Contributor`] and [`Evaluator`]. The messages between contributor and evaluator are exchanged - /// using local message queues. This function thus simulates an MPC execution on a local machine - /// under ideal network conditions, without any latency or bandwidth restrictions. - pub fn simulate( - &self, - circuit: &Circuit, - input_contributor: &[bool], - input_evaluator: &[bool], - ) -> anyhow::Result> { - let mut eval = Evaluator::new( - circuit.clone(), - input_evaluator, - ChaCha20Rng::from_entropy(), - )?; - let (mut contrib, mut msg_for_eval) = - Contributor::new(circuit, input_contributor, ChaCha20Rng::from_entropy())?; - - tracing::debug!("contributor ciphertext: {:?}", hex::encode(&msg_for_eval)); - - assert_eq!(contrib.steps(), eval.steps()); - - for _ in 0..eval.steps() { - let (next_state, msg_for_contrib) = eval.run(&msg_for_eval)?; - eval = next_state; - - let (next_state, reply) = contrib.run(&msg_for_contrib)?; - contrib = next_state; - - msg_for_eval = reply; - } - Ok(eval.output(&msg_for_eval)?) - } } impl From> for GarbledInt { diff --git a/compute/src/operations/arithmetic.rs b/compute/src/operations/arithmetic.rs index dacd674..3178fcb 100644 --- a/compute/src/operations/arithmetic.rs +++ b/compute/src/operations/arithmetic.rs @@ -14,7 +14,7 @@ fn build_and_simulate_arithmetic( gate_fn: fn(u32, u32, Option, &mut Vec, &mut Option) -> u32, ) -> GarbledUint { let mut gates = Vec::new(); - let mut carry_or_borrow_index = None; // Carry/borrow bit + let mut carry_or_borrow_index = None; // Push input gates for both Uint objects for _ in 0..N { diff --git a/compute/src/operations/bitwise.rs b/compute/src/operations/bitwise.rs index b69aac3..b87e143 100644 --- a/compute/src/operations/bitwise.rs +++ b/compute/src/operations/bitwise.rs @@ -12,7 +12,7 @@ fn build_and_simulate( ) -> GarbledUint { let mut gates = Vec::new(); - // Push input gates for both Uint objects + // Push input gates for both Uints for _ in 0..N { gates.push(Gate::InContrib); // From first Uint (lhs) }