Skip to content

Commit

Permalink
refactor(compute): add common CircuitExecutor trait; add tracing supp…
Browse files Browse the repository at this point in the history
…ort and refactor loan eligibility logic
  • Loading branch information
10d9e committed Nov 26, 2024
1 parent bd92b77 commit f44208e
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 210 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions circuit_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ proc-macro = true
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"
2 changes: 1 addition & 1 deletion circuit_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn generate_macro(item: TokenStream, mode: &str) -> TokenStream {
where
#type_name: Into<GarbledUint<N>> + From<GarbledUint<N>> + Clone,
{
let mut context = CircuitBuilder::default();
let mut context = WRK17CircuitBuilder::default();
#(#mapped_inputs)*
#(#constants)*
let const_true = &context.input::<N>(&true.into());
Expand Down
26 changes: 13 additions & 13 deletions compute/examples/loan_eligibility_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,40 @@ use compute::prelude::*;
/// ```
#[encrypted(execute)]
fn check_loan_eligibility(credit_score: u16, income: u16, debt_ratio: u16) -> u8 {
fn check_loan_eligibility(credit_score: u16, income: u16, debt_ratio: u16) -> u16 {
// Eligibility levels
let PRIME = 1;
let SUBPRIME = 2;
let HIGH_RISK = 3;
let INELIGIBLE = 0;
let prime = 1;
let subprime = 2;
let high_risk = 3;
let ineligible = 0;

// Use `if let` with range checks to determine eligibility
if let 750..=850 = credit_score {
if let 50..=200 = income {
if let 1..=30 = debt_ratio {
// High income, low debt ratio, prime credit score
PRIME
prime
} else {
HIGH_RISK // High debt ratio, even with prime credit score and high income
high_risk // High debt ratio, even with prime credit score and high income
}
} else {
SUBPRIME // Prime credit score but income does not meet high-income range
subprime // Prime credit score but income does not meet high-income range
}
} else if let 650..=749 = credit_score {
if let 35..=199 = income {
if let 1..=40 = debt_ratio {
// Moderate income and debt ratio, subprime credit score
SUBPRIME
subprime
} else {
HIGH_RISK // High debt ratio for subprime credit
high_risk // High debt ratio for subprime credit
}
} else {
HIGH_RISK // Income below acceptable range for subprime category
high_risk // Income below acceptable range for subprime category
}
} else if let 300..=649 = credit_score {
HIGH_RISK // Low credit score is always high risk
high_risk // Low credit score is always high risk
} else {
INELIGIBLE // Credit score out of bounds
ineligible // Credit score out of bounds
}
}

Expand Down
24 changes: 12 additions & 12 deletions compute/examples/loan_eligibility_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ use compute::prelude::*;
#[encrypted(execute)]
fn check_loan_eligibility(credit_score: u16, income: u16, debt_ratio: u16) -> u16 {
// Eligibility levels
let PRIME = 1;
let SUBPRIME = 2;
let HIGH_RISK = 3;
let INELIGIBLE = 0;
let prime = 1;
let subprime = 2;
let high_risk = 3;
let ineligible = 0;

match credit_score {
750..=850 => match income {
50..=200 => match debt_ratio {
1..=30 => PRIME, // High income, low debt ratio, prime credit score
_ => HIGH_RISK, // High debt ratio, even with prime credit score and high income
1..=30 => prime, // High income, low debt ratio, prime credit score
_ => high_risk, // High debt ratio, even with prime credit score and high income
},
_ => SUBPRIME, // Prime credit score but income does not meet high-income range
_ => subprime, // Prime credit score but income does not meet high-income range
},
650..=749 => match income {
35..=199 => match debt_ratio {
1..=40 => SUBPRIME, // Moderate income and debt ratio, subprime credit score
_ => HIGH_RISK, // High debt ratio for subprime credit
1..=40 => subprime, // Moderate income and debt ratio, subprime credit score
_ => high_risk, // High debt ratio for subprime credit
},
_ => HIGH_RISK, // Income below acceptable range for subprime category
_ => high_risk, // Income below acceptable range for subprime category
},
300..=649 => HIGH_RISK, // Low credit score is always high risk
_ => INELIGIBLE, // Credit score out of bounds
300..=649 => high_risk, // Low credit score is always high risk
_ => ineligible, // Credit score out of bounds
}
}

Expand Down
3 changes: 2 additions & 1 deletion compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod operations;
pub mod uint;

pub mod prelude {
pub use crate::operations::circuits::builder::CircuitBuilder;
pub use crate::operations::circuits::builder::WRK17CircuitBuilder;

pub use crate::executor::get_executor;
pub use crate::int::{
Expand All @@ -25,4 +25,5 @@ pub mod prelude {
pub use crate::evaluator::GatewayEvaluator;
pub use crate::garbler::Garbler;
pub use crate::garbler::GatewayGarbler;
pub use crate::operations::circuits::traits::CircuitExecutor;
}
Loading

0 comments on commit f44208e

Please sign in to comment.