Skip to content

Commit

Permalink
fix conflict in changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
PanGan21 committed Aug 17, 2023
2 parents 9856d31 + 4d36f96 commit 9b716c5
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

* refactor: combine `Program.hints` and `Program.hints_ranges` into custom collection [#1366](https://github.com/lambdaclass/cairo-vm/pull/1366)

* fix: Fix div_mod [#1383](https://github.com/lambdaclass/cairo-vm/pull/1383)

* Fixes `div_mod` function so that it behaves like the cairo-lang version
* Various functions in the `math_utils` crate can now return a `MathError` : `div_mod`, `ec_add`, `line_slope`, `ec_double`, `ec_double_slope`.
* Fixes `UINT256_MUL_INV_MOD_P` hint so that it behaves like the python code.

* fix: Handle error in hint `UINT256_MUL_DIV_MOD` when divides by zero [#1367](https://github.com/lambdaclass/cairo-vm/pull/1367)

* Add HintError::SyscallError and VmErrors::HINT_ERROR_STR constant [#1357](https://github.com/lambdaclass/cairo-rs/pull/1357)
Expand Down
42 changes: 42 additions & 0 deletions cairo_programs/bad_programs/divmod_igcdex_not_one.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
struct MyStruct0 {
high: felt,
low: felt,
}

func main() {
let a = MyStruct0(high=1, low=340282366920938463463374607431768211456);
let b = MyStruct0(high=1, low=1);
let p = MyStruct0(high=1, low=1);
let (a, b, p, b_inverse_mod_p) = hint_func(a, b, p);

return();
}

func hint_func(a: MyStruct0, b: MyStruct0, p: MyStruct0) -> (MyStruct0, MyStruct0, MyStruct0, MyStruct0) {
alloc_locals;
local b_inverse_mod_p: MyStruct0;
%{
from starkware.python.math_utils import div_mod

def split(a: int):
return (a & ((1 << 128) - 1), a >> 128)

def pack(z, num_bits_shift: int) -> int:
limbs = (z.low, z.high)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

a = pack(ids.a, 128)
b = pack(ids.b, 128)
p = pack(ids.p, 128)
# For python3.8 and above the modular inverse can be computed as follows:
# b_inverse_mod_p = pow(b, -1, p)
# Instead we use the python3.7-friendly function div_mod from starkware.python.math_utils
b_inverse_mod_p = div_mod(1, b, p)

b_inverse_mod_p_split = split(b_inverse_mod_p)

ids.b_inverse_mod_p.low = b_inverse_mod_p_split[0]
ids.b_inverse_mod_p.high = b_inverse_mod_p_split[1]
%}
return(a, b, p, b_inverse_mod_p);
}
2 changes: 1 addition & 1 deletion vm/src/hint_processor/builtin_hint_processor/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn bigint_pack_div_mod_hint(
};
let y: BigInt = BigInt3::from_var_name("y", vm, ids_data, ap_tracking)?.pack86();

let res = div_mod(&x, &y, &p);
let res = div_mod(&x, &y, &p)?;
exec_scopes.insert_value("res", res.clone());
exec_scopes.insert_value("value", res);
exec_scopes.insert_value("x", x);
Expand Down
2 changes: 1 addition & 1 deletion vm/src/hint_processor/builtin_hint_processor/ec_recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn ec_recover_divmod_n_packed(
.pack86()
.mod_floor(&n);

let value = div_mod(&x, &s, &n);
let value = div_mod(&x, &s, &n)?;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("res", value);
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn compute_doubling_slope(
//ids.point
let point = EcPoint::from_var_name(point_alias, vm, ids_data, ap_tracking)?;

let value = ec_double_slope(&(point.x.pack86(), point.y.pack86()), alpha, secp_p);
let value = ec_double_slope(&(point.x.pack86(), point.y.pack86()), alpha, secp_p)?;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("slope", value);
Ok(())
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn compute_doubling_slope_external_consts(
let secp_p: BigInt = exec_scopes.get("SECP_P")?;
let alpha: BigInt = exec_scopes.get("ALPHA")?;

let value = ec_double_slope(&(point.x.pack86(), point.y.pack86()), &alpha, &secp_p);
let value = ec_double_slope(&(point.x.pack86(), point.y.pack86()), &alpha, &secp_p)?;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("slope", value);
Ok(())
Expand Down Expand Up @@ -220,7 +220,7 @@ pub fn compute_slope(
&(point0.x.pack86(), point0.y.pack86()),
&(point1.x.pack86(), point1.y.pack86()),
&secp_p,
);
)?;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("slope", value);
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn is_zero_assign_scope_variables(exec_scopes: &mut ExecutionScopes) -> Resu
//Get `x` variable from vm scope
let x = exec_scopes.get::<BigInt>("x")?;

let value = div_mod(&BigInt::one(), &x, &SECP_P);
let value = div_mod(&BigInt::one(), &x, &SECP_P)?;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("x_inv", value);
Ok(())
Expand All @@ -182,7 +182,7 @@ pub fn is_zero_assign_scope_variables_external_const(
let secp_p = exec_scopes.get_ref::<BigInt>("SECP_P")?;
let x = exec_scopes.get_ref::<BigInt>("x")?;

let value = div_mod(&BigInt::one(), x, secp_p);
let value = div_mod(&BigInt::one(), x, secp_p)?;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("x_inv", value);
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn div_mod_n_packed(
let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack86();
let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)?.pack86();

let value = div_mod(&a, &b, n);
let value = div_mod(&a, &b, n)?;
exec_scopes.insert_value("a", a);
exec_scopes.insert_value("b", b);
exec_scopes.insert_value("value", value.clone());
Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn pack_modn_div_modn(
.pack86()
.mod_floor(&N);

let value = div_mod(&x, &s, &N);
let value = div_mod(&x, &s, &N)?;
exec_scopes.insert_value("x", x);
exec_scopes.insert_value("s", s);
exec_scopes.insert_value("N", N.clone());
Expand Down
39 changes: 30 additions & 9 deletions vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
use crate::{
hint_processor::builtin_hint_processor::{uint256_utils::Uint256, uint512_utils::Uint512},
hint_processor::hint_processor_definition::HintReference,
math_utils::mul_inv,
math_utils::div_mod,
serde::deserialize_program::ApTracking,
stdlib::{collections::HashMap, prelude::*},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use num_bigint::ToBigInt;
use num_integer::{div_rem, Integer};
use num_bigint::{BigInt, ToBigInt};
use num_integer::div_rem;
use num_traits::One;

/// Implements hint:
/// ```python
Expand Down Expand Up @@ -100,13 +101,9 @@ pub fn inv_mod_p_uint256(
.unwrap_or_default();

// Main logic:
// b_inverse_mod_p = div_mod(1, b, p)
let b_inverse_mod_p = mul_inv(&b, &p)
.mod_floor(&p)
.to_biguint()
.unwrap_or_default();
let b_inverse_mod_p = div_mod(&BigInt::one(), &b, &p)?;

let res = Uint256::from(&b_inverse_mod_p);
let res = Uint256::from(&b_inverse_mod_p.to_biguint().unwrap_or_default());
res.insert_from_var_name("b_inverse_mod_p", vm, ids_data, ap_tracking)
}

Expand All @@ -118,6 +115,7 @@ mod tests {
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
use crate::types::errors::math_errors::MathError;
use crate::types::exec_scope::ExecutionScopes;
use crate::utils::test_utils::*;
use assert_matches::assert_matches;
Expand Down Expand Up @@ -189,4 +187,27 @@ mod tests {
((1, 7), 106713),
];
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_inv_mod_p_uint256_igcdex_not_1() {
let hint_code = hint_code::INV_MOD_P_UINT256;
let mut vm = vm_with_range_check!();

vm.segments = segments![
((1, 0), 2363463),
((1, 1), 566795),
((1, 2), 1),
((1, 3), 1),
((1, 4), 1),
((1, 5), 1)
];
// Create hint_data
let ids_data =
non_continuous_ids_data![("a", 0), ("b", 2), ("p", 4), ("b_inverse_mod_p", 6)];
assert_matches!(
run_hint!(vm, ids_data, hint_code, exec_scopes_ref!()),
Err(HintError::Math(MathError::DivModIgcdexNotZero(_)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn inv_mod_p_uint512(
let p = Uint256::from_var_name("p", vm, ids_data, ap_tracking)?.pack();

let x_inverse_mod_p =
Felt252::from(div_mod(&BigInt::one(), &BigInt::from(x), &BigInt::from(p)));
Felt252::from(div_mod(&BigInt::one(), &BigInt::from(x), &BigInt::from(p))?);

let x_inverse_mod_p = Uint256::from(x_inverse_mod_p);
x_inverse_mod_p.insert_from_var_name("x_inverse_mod_p", vm, ids_data, ap_tracking)?;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn ed25519_is_zero_assign_scope_vars(
exec_scopes: &mut ExecutionScopes,
) -> Result<(), HintError> {
let x = exec_scopes.get::<BigInt>("x")?;
let x_inv = div_mod(&BigInt::one(), &x, &SECP_P_V2);
let x_inv = div_mod(&BigInt::one(), &x, &SECP_P_V2)?;
exec_scopes.insert_value("x_inv", x_inv.clone());
exec_scopes.insert_value("value", x_inv);
exec_scopes.insert_value("SECP_P", SECP_P_V2.clone());
Expand Down
Loading

0 comments on commit 9b716c5

Please sign in to comment.