diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 9698dcd779..ca5b03ef31 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -612,29 +612,36 @@ impl Cairo1HintProcessor { remainder_high: &CellRef, sqrt_mul_2_minus_remainder_ge_u128: &CellRef, ) -> Result<(), HintError> { - let pow_2_128 = Felt252::from(u128::MAX) + 1u32; - let pow_2_64 = Felt252::from(u64::MAX) + 1u32; - let value_low = res_operand_get_val(vm, value_low)?; - let value_high = res_operand_get_val(vm, value_high)?; - let value = value_low + value_high * &pow_2_128; + let pow_2_128 = BigUint::from(u128::MAX) + 1u32; + let pow_2_64 = BigUint::from(u64::MAX) + 1u32; + let value_low = res_operand_get_val(vm, value_low)?.to_biguint(); + let value_high = res_operand_get_val(vm, value_high)?.to_biguint(); + let value = value_low + value_high * pow_2_128.clone(); let sqrt = value.sqrt(); - let remainder = value - &sqrt * &sqrt; + let remainder = value - sqrt.clone() * sqrt.clone(); let sqrt_mul_2_minus_remainder_ge_u128_val = - &sqrt * &Felt252::from(2u32) - &remainder >= pow_2_128; + sqrt.clone() * 2u32 - remainder.clone() >= pow_2_128; + // Guess sqrt limbs. let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(sqrt0, vm)?, sqrt0_val)?; - vm.insert_value(cell_ref_to_relocatable(sqrt1, vm)?, sqrt1_val)?; + vm.insert_value( + cell_ref_to_relocatable(sqrt0, vm)?, + Felt252::from(sqrt0_val), + )?; + vm.insert_value( + cell_ref_to_relocatable(sqrt1, vm)?, + Felt252::from(sqrt1_val), + )?; let (remainder_high_val, remainder_low_val) = remainder.div_rem(&pow_2_128); vm.insert_value( cell_ref_to_relocatable(remainder_low, vm)?, - remainder_low_val, + Felt252::from(remainder_low_val), )?; vm.insert_value( cell_ref_to_relocatable(remainder_high, vm)?, - remainder_high_val, + Felt252::from(remainder_high_val), )?; vm.insert_value( cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm)?, diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index d63984a83d..3a52063b11 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -373,6 +373,14 @@ fn u256_sqrt_zero() { ); } +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn u256_sqrt_max_num() { + let program_data = include_bytes!("../../cairo_programs/cairo-1-contracts/u256_sqrt.casm"); + + run_cairo_1_entrypoint(program_data.as_slice(), 257, &[], &[1.into()]); +} + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn u256_sqrt_big_num() {