Skip to content

Commit

Permalink
refactor(compute): Clean up and separate operations module
Browse files Browse the repository at this point in the history
  • Loading branch information
10d9e committed Oct 4, 2024
1 parent 0c061ea commit 422fcd0
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 241 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[workspace]
resolver = "2"

members = [
"benchmark",
"compute",
Expand Down
10 changes: 5 additions & 5 deletions benchmark/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, Criterion};

// Function to benchmark (example function)
fn tfhe_encrypted_addition() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -21,7 +21,7 @@ fn tfhe_encrypted_addition() -> Result<(), Box<dyn std::error::Error>> {
set_server_key(server_keys);

// Clear equivalent computations: 12297829382473034410 + 1
let mut encrypted_res_mul = &encrypted_a + &encrypted_b;
let encrypted_res_mul = &encrypted_a + &encrypted_b;

let clear_res: u128 = encrypted_res_mul.decrypt(&client_key);
assert_eq!(clear_res, clear_a + clear_b);
Expand All @@ -31,7 +31,7 @@ fn tfhe_encrypted_addition() -> Result<(), Box<dyn std::error::Error>> {

// Another function to benchmark
fn gateway_encrypted_addition() -> Result<(), Box<dyn std::error::Error>> {
use compute::operations::Uint;
use compute::uint::Uint;

let clear_a = 12297829382473034410u128;
let clear_b = 424242424242u128;
Expand All @@ -47,14 +47,14 @@ fn gateway_encrypted_addition() -> Result<(), Box<dyn std::error::Error>> {
// Benchmark 1: Benchmarking benchmark_gateway_encrypted_addition
fn benchmark_gateway_encrypted_addition(c: &mut Criterion) {
c.bench_function("gateway_encrypted_addition", |b| {
b.iter(|| gateway_encrypted_addition())
b.iter(gateway_encrypted_addition)
});
}

// Benchmark 2: Benchmarking benchmark_tfhe_encrypted_addition
fn benchmark_tfhe_encrypted_addition(c: &mut Criterion) {
c.bench_function("tfhe_encrypted_addition", |b| {
b.iter(|| tfhe_encrypted_addition())
b.iter(tfhe_encrypted_addition)
});
}

Expand Down
1 change: 0 additions & 1 deletion compute/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod operations;
pub mod u256;
pub mod uint;
5 changes: 3 additions & 2 deletions compute/src/operations/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl<const N: usize> Not for &Uint<N> {
}

// Implement Shift Left operation for Uint<N> and &Uint<N>
#[allow(clippy::suspicious_arithmetic_impl)]
impl<const N: usize> Shl<usize> for Uint<N> {
type Output = Self;

Expand Down Expand Up @@ -445,14 +446,14 @@ mod tests {
let a = Uint::<4>::new(vec![false, false, false, true]); // Binary 1000

let result = a << 1; // Perform left shift by 1
assert_eq!(result.to_u8(), 0b0000 as u8); // Binary 0000 (Left shift result of 1000)
assert_eq!(result.to_u8(), 0b0000_u8); // Binary 0000 (Left shift result of 1000)

// binary literal of 0000

let a = Uint::<4>::new(vec![false, false, false, true]); // Binary 1000

let result = a << 2; // Perform left shift by 2
assert_eq!(result.to_u8(), 0b0000); // Binary 0000 (Left shift result of 1000)
assert_eq!(result.to_u8(), 0b0000_u8); // Binary 0000 (Left shift result of 1000)

let a = Uint::<4>::new(vec![false, false, false, true]); // Binary 1000

Expand Down
229 changes: 0 additions & 229 deletions compute/src/u256.rs

This file was deleted.

5 changes: 1 addition & 4 deletions compute/src/uint.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::u256::U256;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha20Rng;
use std::marker::PhantomData;
use std::ops::{BitOr, BitXor, Not, Shl, Shr};
use tandem::states::{Contributor, Evaluator};
use tandem::GateIndex;
use tandem::{Circuit, Error, Gate};
use tandem::{Circuit, Error};

// Define a new type Uint<N>
#[derive(Debug, Clone)]
Expand Down

0 comments on commit 422fcd0

Please sign in to comment.