Skip to content

Commit

Permalink
Impl inv mod
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Oct 16, 2024
1 parent 9965b94 commit cb774dc
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/vm/uint252.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::EvalAction;
use crate::{debug::debug_signature, Value};
use crate::Value;
use cairo_lang_sierra::{
extensions::{
core::{CoreLibfunc, CoreType},
int::unsigned256::Uint256Concrete,
lib_func::SignatureOnlyConcreteLibfunc,
ConcreteLibfunc,
},
program_registry::ProgramRegistry,
};
Expand All @@ -26,18 +25,46 @@ pub fn eval(
}

fn eval_inv_mod_n(
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
info: &SignatureOnlyConcreteLibfunc,
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
debug_signature(
registry,
info.param_signatures(),
info.branch_signatures(),
&args,
);

todo!();
let [range_check @ Value::Unit, Value::Struct(x), Value::Struct(modulo)]: [Value; 3] =
args.try_into().unwrap()
else {
panic!()
};

let [Value::U128(x_lo), Value::U128(x_hi)]: [Value; 2] = x.clone().try_into().unwrap() else {
panic!()
};

let [Value::U128(mod_lo), Value::U128(mod_hi)]: [Value; 2] = modulo.clone().try_into().unwrap()
else {
panic!()
};

let x = u256_to_biguint(x_lo, x_hi);
let modulo = u256_to_biguint(mod_lo, mod_hi);

match x.modinv(&modulo) {
Some(r) => EvalAction::NormalBranch(
0,
smallvec![
range_check,
u256_to_value(r),
Value::Unit,
Value::Unit,
Value::Unit,
Value::Unit,
Value::Unit,
Value::Unit,
Value::Unit,
Value::Unit
],
),
None => EvalAction::NormalBranch(1, smallvec![range_check, Value::Unit, Value::Unit]),
}
}

pub fn eval_is_zero(
Expand Down

0 comments on commit cb774dc

Please sign in to comment.