Skip to content

Commit

Permalink
chore: Encrypt circuit macro name update to #[encrypted(compile)]
Browse files Browse the repository at this point in the history
- Updated the circuit macros in the codebase to use the new #[encrypted] attribute instead of #[circuit].
- Modified the client.rs and server.rs files in the server/src/bin directory to use the #[encrypted(compile)] attribute.
- Updated the loan_eligibility_1.rs, loan_eligibility_2.rs, loan_eligibility_3.rs, threshold_voting.rs, temperature.rs, age_content_control.rs, spending.rs, discount_eligibility.rs, access_control.rs, and single_macro.rs files in the compute/examples directory to use the #[encrypted(execute)] attribute.
- Renamed the circuit_macro::circuit function to circuit_macro::encrypted in the circuit_macro/src/lib.rs file.
- Updated the compute::prelude module to use the circuit_macro::encrypted function instead of circuit_macro::circuit.
- Added the util.rs file in the compute/src/operations directory, which contains utility functions for serializing and deserializing circuits.
  • Loading branch information
10d9e committed Nov 5, 2024
1 parent 1ac072c commit 379853a
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 82 deletions.
2 changes: 1 addition & 1 deletion circuit_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syn::{
};

#[proc_macro_attribute]
pub fn circuit(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn encrypted(attr: TokenStream, item: TokenStream) -> TokenStream {
let mode = parse_macro_input!(attr as syn::Ident).to_string(); // Retrieve the mode (e.g., "compile" or "execute")
generate_macro(item, &mode)
}
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use compute::prelude::*;
/// Circuit macro annotation to indicate this function will be executed within a secure circuit context.
/// This access control function determines the level of access a user has based on their role, returning
/// only the data they are authorized to view in a privacy-preserving manner.
#[circuit(execute)]
#[encrypted(execute)]
fn access_control(role: u8) -> u8 {
// Define constants representing access to different types of data.
// Each constant holds a value that signifies specific data access rights.
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/age_content_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use compute::prelude::*;
/// let access_level = access_content(age);
/// assert_eq!(access_level, 0); // Invalid age
/// ```
#[circuit(execute)]
#[encrypted(execute)]
fn access_content(age: u8) -> u8 {
// Access level codes
let RESTRICTED = 1; // Restricted access for underage users
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/discount_eligibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use compute::prelude::*;
///
/// # Example
/// This example demonstrates checking if a purchase of 100 qualifies for a discount with a threshold of 80.
#[circuit(execute)]
#[encrypted(execute)]
fn qualifies_for_discount(purchase_amount: u16) -> bool {
let DISCOUNT_THRESHOLD = 80;
purchase_amount >= DISCOUNT_THRESHOLD
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/loan_eligibility_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use compute::prelude::*;
/// assert_eq!(eligibility, 1); // Prime eligibility
/// ```
#[circuit(execute)]
#[encrypted(execute)]
fn check_loan_eligibility(credit_score: u16, income: u16, debt_ratio: u16) -> u8 {
// Eligibility levels
let PRIME = 1;
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/loan_eligibility_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use compute::prelude::*;
/// assert_eq!(eligibility, 1); // Prime eligibility
/// ```
#[circuit(execute)]
#[encrypted(execute)]
fn check_loan_eligibility(credit_score: u16, income: u16, debt_ratio: u16) -> u16 {
// Eligibility levels
let PRIME = 1;
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/loan_eligibility_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use compute::prelude::*;
/// assert_eq!(eligibility, 1); // Prime eligibility
/// ```
#[circuit(execute)]
#[encrypted(execute)]
fn check_loan_eligibility(credit_score: u16, income: u16, debt_ratio: u16) -> u16 {
// Eligibility levels
let PRIME = 1;
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/spending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use compute::prelude::*;
/// # Example
/// This example demonstrates checking if a remaining budget of 3000 (after spending 2000 from a budget of 5000)
/// stays within the maximum allowable limit of 3000.
#[circuit(execute)]
#[encrypted(execute)]
fn can_spend(budget: u16, spent: u16) -> bool {
let MAX_ALLOWED = 5000;
let remaining_budget = budget - spent;
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use compute::prelude::*;
///
/// # Example
/// This example demonstrates verifying if a room with a temperature of 70°F is within the range of 65°F to 75°F.
#[circuit(execute)]
#[encrypted(execute)]
fn within_temperature_range(current_temp: u8) -> bool {
let MIN_TEMP = 65;
let MAX_TEMP = 75;
Expand Down
2 changes: 1 addition & 1 deletion compute/examples/threshold_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use compute::prelude::*;
///
/// # Example
/// This example demonstrates checking if a candidate with 150 votes meets a threshold of 100 votes.
#[circuit(execute)]
#[encrypted(execute)]
fn has_enough_votes(votes: u8) -> bool {
let THRESHOLD = 100;
votes >= THRESHOLD
Expand Down
2 changes: 1 addition & 1 deletion compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod prelude {
GarbledBoolean, GarbledUint, GarbledUint128, GarbledUint16, GarbledUint2, GarbledUint256,
GarbledUint32, GarbledUint4, GarbledUint512, GarbledUint64, GarbledUint8,
};
pub use circuit_macro::circuit;
pub use circuit_macro::encrypted;
pub use tandem::{Circuit, Gate};

pub use crate::evaluator::Evaluator;
Expand Down
136 changes: 136 additions & 0 deletions compute/src/operations/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use serde::{Deserialize, Serialize};
use tandem::Circuit;
use tandem::Gate;
use tandem::GateIndex;

// wrapper Gate
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum GateW {
/// A single input bit coming from the circuit contributor.
InContrib,
/// A single input bit coming from the circuit evaluator.
InEval,
/// A gate computing the XOR of the two specified gates.
Xor(GateIndex, GateIndex),
/// A gate computing the AND of the two specified gates.
And(GateIndex, GateIndex),
/// A gate computing the NOT of the specified gate.
Not(GateIndex),
}

impl Into<Gate> for GateW {
fn into(self) -> Gate {
match self {
GateW::InContrib => Gate::InContrib,
GateW::InEval => Gate::InEval,
GateW::Xor(a, b) => Gate::Xor(a, b),
GateW::And(a, b) => Gate::And(a, b),
GateW::Not(a) => Gate::Not(a),
}
}
}

impl From<Gate> for GateW {
fn from(gate: Gate) -> Self {
match gate {
Gate::InContrib => GateW::InContrib,
Gate::InEval => GateW::InEval,
Gate::Xor(a, b) => GateW::Xor(a, b),
Gate::And(a, b) => GateW::And(a, b),
Gate::Not(a) => GateW::Not(a),
}
}
}

// Assuming `Gate` and `GateIndex` implement `Serialize` and `Deserialize`
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CircuitWrapper {
gates: Vec<GateW>,
output_gates: Vec<GateIndex>,
and_gates: usize,
eval_inputs: usize,
contrib_inputs: usize,
}

// Implement conversions from `Circuit` to `CircuitWrapper` and vice versa
impl From<&Circuit> for CircuitWrapper {
fn from(circuit: &Circuit) -> Self {
CircuitWrapper {
gates: circuit
.gates()
.iter()
.map(|gate| gate.clone().into())
.collect(),
output_gates: circuit.output_gates().clone(),
and_gates: circuit.and_gates(),
eval_inputs: circuit.eval_inputs(),
contrib_inputs: circuit.contrib_inputs(),
}
}
}

impl Into<Circuit> for CircuitWrapper {
fn into(self) -> Circuit {
Circuit::new(
self.gates.iter().map(|gate| gate.clone().into()).collect(),
self.output_gates,
)
}
}

pub fn serialize_circuit(circuit: &Circuit) -> anyhow::Result<Vec<u8>> {
// Convert `Circuit` to `CircuitWrapper`
let wrapper: CircuitWrapper = circuit.into();

// Serialize `CircuitWrapper` using bincode
let serialized_data = bincode::serialize(&wrapper)?;
Ok(serialized_data)
}

pub fn deserialize_circuit(data: &[u8]) -> anyhow::Result<Circuit> {
// Deserialize into `CircuitWrapper`
let wrapper: CircuitWrapper = bincode::deserialize(data)?;

// Convert `CircuitWrapper` back into `Circuit`
let circuit: Circuit = wrapper.into();
Ok(circuit)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;

#[test]
fn test_serialize_deserialize_circuit_struct() -> anyhow::Result<()> {
#[circuit(compile)]
fn multi_arithmetic(a: u8, b: u8, c: u8, d: u8) -> u8 {
let res = a * b;
let res = res + c;
res - d
}

// Initialize the evaluator instance with circuit and dummy input
let (circuit, _) = multi_arithmetic(0_u8, 0_u8, 0_u8, 0_u8);

// Serialize the circuit
let serialized_data = serialize_circuit(&circuit)?;
println!("Serialized Circuit data: {:?}", serialized_data);

// Deserialize back into a `Circuit` struct
let deserialized_circuit = deserialize_circuit(&serialized_data)?;
println!("Deserialized Circuit: {:?}", deserialized_circuit);

// Check if the deserialized circuit is the same as the original circuit
assert_eq!(circuit.gates(), deserialized_circuit.gates());
assert_eq!(circuit.output_gates(), deserialized_circuit.output_gates());
assert_eq!(circuit.and_gates(), deserialized_circuit.and_gates());
assert_eq!(circuit.eval_inputs(), deserialized_circuit.eval_inputs());
assert_eq!(
circuit.contrib_inputs(),
deserialized_circuit.contrib_inputs()
);

Ok(())
}
}
Loading

0 comments on commit 379853a

Please sign in to comment.