diff --git a/CHANGELOG.md b/CHANGELOG.md index d842c2f619..70f8368e7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -223,6 +223,43 @@ ids.res.high = res_split[1] ``` +* Add missing hint on vrf.json lib [#1030](https://github.com/lambdaclass/cairo-rs/pull/1030): + + `BuiltinHintProcessor` now supports the following hint: + + ```python + def split(num: int, num_bits_shift: int, length: int): + a = [] + for _ in range(length): + a.append( num & ((1 << num_bits_shift) - 1) ) + num = num >> num_bits_shift + return tuple(a) + + 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)) + + def pack_extended(z, num_bits_shift: int) -> int: + limbs = (z.d0, z.d1, z.d2, z.d3) + return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) + + x = pack_extended(ids.x, num_bits_shift = 128) + div = pack(ids.div, num_bits_shift = 128) + + quotient, remainder = divmod(x, div) + + quotient_split = split(quotient, num_bits_shift=128, length=4) + + ids.quotient.d0 = quotient_split[0] + ids.quotient.d1 = quotient_split[1] + ids.quotient.d2 = quotient_split[2] + ids.quotient.d3 = quotient_split[3] + + remainder_split = split(remainder, num_bits_shift=128, length=2) + ids.remainder.low = remainder_split[0] + ids.remainder.high = remainder_split[1] + ``` + * Add method `Program::data_len(&self) -> usize` to get the number of data cells in a given program [#1022](https://github.com/lambdaclass/cairo-rs/pull/1022) * Add missing hint on uint256_improvements lib [#1013](https://github.com/lambdaclass/cairo-rs/pull/1013): diff --git a/cairo_programs/benchmarks/field_arithmetic_get_square_benchmark.cairo b/cairo_programs/benchmarks/field_arithmetic_get_square_benchmark.cairo index 28b95e3c34..8cd40d7641 100644 --- a/cairo_programs/benchmarks/field_arithmetic_get_square_benchmark.cairo +++ b/cairo_programs/benchmarks/field_arithmetic_get_square_benchmark.cairo @@ -1,34 +1,35 @@ from starkware.cairo.common.cairo_builtins import BitwiseBuiltin from starkware.cairo.common.bool import TRUE -from cairo_programs.uint384 import uint384_lib, Uint384, Uint384_expand -from cairo_programs.uint384_extension import uint384_extension_lib +from cairo_programs.uint384 import u384, Uint384, Uint384_expand +from cairo_programs.uint384_extension import u384_ext from cairo_programs.field_arithmetic import field_arithmetic - -func run_get_square{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(prime: Uint384, generator: Uint384, num: Uint384, iterations: felt) { +func run_get_square{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}( + prime: Uint384, generator: Uint384, num: Uint384, iterations: felt +) { alloc_locals; if (iterations == 0) { return (); } - let (square) = field_arithmetic.mul(num, num, prime); + let (square) = field_arithmetic.mul(num, num, prime); let (success, root_1) = field_arithmetic.get_square_root(square, prime, generator); assert success = 1; // We calculate this before in order to prevent revoked range_check_ptr reference due to branching - let (root_2) = uint384_lib.sub(prime, root_1); - let (is_first_root) = uint384_lib.eq(root_1, num); + let (root_2) = u384.sub(prime, root_1); + let (is_first_root) = u384.eq(root_1, num); - if ( is_first_root != TRUE) { + if (is_first_root != TRUE) { assert root_2 = num; } - return run_get_square(prime, generator, square, iterations -1); + return run_get_square(prime, generator, square, iterations - 1); } func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() { - let p = Uint384(18446744069414584321, 0, 0); // Goldilocks Prime + let p = Uint384(18446744069414584321, 0, 0); // Goldilocks Prime let x = Uint384(5, 0, 0); let g = Uint384(7, 0, 0); run_get_square(p, g, x, 100); diff --git a/cairo_programs/field_arithmetic.cairo b/cairo_programs/field_arithmetic.cairo index 4386b44d96..dd2586cefd 100644 --- a/cairo_programs/field_arithmetic.cairo +++ b/cairo_programs/field_arithmetic.cairo @@ -1,3 +1,5 @@ +%builtins range_check bitwise + // Code taken from https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/fbf532651959f27037d70cd70ec6dbaf987f535c/lib/field_arithmetic.cairo from starkware.cairo.common.bitwise import bitwise_and, bitwise_or, bitwise_xor from starkware.cairo.common.cairo_builtins import BitwiseBuiltin @@ -5,28 +7,28 @@ from starkware.cairo.common.math import assert_in_range, assert_le, assert_nn_le from starkware.cairo.common.math_cmp import is_le from starkware.cairo.common.pow import pow from starkware.cairo.common.registers import get_ap, get_fp_and_pc -from cairo_programs.uint384 import uint384_lib, Uint384, Uint384_expand, SHIFT, HALF_SHIFT -from cairo_programs.uint384_extension import uint384_extension_lib, Uint768 +from cairo_programs.uint384 import u384, Uint384, Uint384_expand, SHIFT, HALF_SHIFT +from cairo_programs.uint384_extension import u384_ext, Uint768 // Functions for operating elements in a finite field F_p (i.e. modulo a prime p), with p of at most 384 bits namespace field_arithmetic { // Computes a * b modulo p func mul{range_check_ptr}(a: Uint384, b: Uint384, p: Uint384) -> (res: Uint384) { - let (low: Uint384, high: Uint384) = uint384_lib.mul_d(a, b); + let (low: Uint384, high: Uint384) = u384.mul_d(a, b); let full_mul_result: Uint768 = Uint768(low.d0, low.d1, low.d2, high.d0, high.d1, high.d2); - let ( - quotient: Uint768, remainder: Uint384 - ) = uint384_extension_lib.unsigned_div_rem_uint768_by_uint384(full_mul_result, p); + let (quotient: Uint768, remainder: Uint384) = u384_ext.unsigned_div_rem_uint768_by_uint384( + full_mul_result, p + ); return (remainder,); } // Computes a**2 modulo p func square{range_check_ptr}(a: Uint384, p: Uint384) -> (res: Uint384) { - let (low: Uint384, high: Uint384) = uint384_lib.square_e(a); + let (low: Uint384, high: Uint384) = u384.square_e(a); let full_mul_result: Uint768 = Uint768(low.d0, low.d1, low.d2, high.d0, high.d1, high.d2); - let ( - quotient: Uint768, remainder: Uint384 - ) = uint384_extension_lib.unsigned_div_rem_uint768_by_uint384(full_mul_result, p); + let (quotient: Uint768, remainder: Uint384) = u384_ext.unsigned_div_rem_uint768_by_uint384( + full_mul_result, p + ); return (remainder,); } @@ -43,7 +45,7 @@ namespace field_arithmetic { alloc_locals; // TODO: Create an equality function within field_arithmetic to avoid overflow bugs - let (is_zero) = uint384_lib.eq(x, Uint384(0, 0, 0)); + let (is_zero) = u384.eq(x, Uint384(0, 0, 0)); if (is_zero == 1) { return (1, Uint384(0, 0, 0)); } @@ -101,22 +103,22 @@ namespace field_arithmetic { // Verify that the values computed in the hint are what they are supposed to be let (gx: Uint384) = mul(generator, x, p); if (success_x == 1) { - uint384_lib.check(sqrt_x); - let (is_valid) = uint384_lib.lt(sqrt_x, p); - assert is_valid = 1; + u384.check(sqrt_x); + let (is_valid) = u384.lt(sqrt_x, p); + assert is_valid = 1; let (sqrt_x_squared: Uint384) = mul(sqrt_x, sqrt_x, p); // Note these checks may fail if the input x does not satisfy 0<= x < p // TODO: Create a equality function within field_arithmetic to avoid overflow bugs - let (check_x) = uint384_lib.eq(x, sqrt_x_squared); + let (check_x) = u384.eq(x, sqrt_x_squared); assert check_x = 1; return (1, sqrt_x); } else { // In this case success_gx = 1 - uint384_lib.check(sqrt_gx); - let (is_valid) = uint384_lib.lt(sqrt_gx, p); - assert is_valid = 1; + u384.check(sqrt_gx); + let (is_valid) = u384.lt(sqrt_gx, p); + assert is_valid = 1; let (sqrt_gx_squared: Uint384) = mul(sqrt_gx, sqrt_gx, p); - let (check_gx) = uint384_lib.eq(gx, sqrt_gx_squared); + let (check_gx) = u384.eq(gx, sqrt_gx_squared); assert check_gx = 1; // No square roots were found // Note that Uint384(0, 0, 0) is not a square root here, but something needs to be returned @@ -158,7 +160,7 @@ namespace field_arithmetic { ids.b_inverse_mod_p.d1 = b_inverse_mod_p_split[1] ids.b_inverse_mod_p.d2 = b_inverse_mod_p_split[2] %} - uint384_lib.check(b_inverse_mod_p); + u384.check(b_inverse_mod_p); let (b_times_b_inverse) = mul(b, b_inverse_mod_p, p); assert b_times_b_inverse = Uint384(1, 0, 0); @@ -171,7 +173,7 @@ func test_field_arithmetics_extension_operations{range_check_ptr, bitwise_ptr: B alloc_locals; // Test get_square - //Small prime + // Small prime let p_a = Uint384(7, 0, 0); let x_a = Uint384(2, 0, 0); let generator_a = Uint384(3, 0, 0); @@ -183,7 +185,7 @@ func test_field_arithmetics_extension_operations{range_check_ptr, bitwise_ptr: B assert r_a.d2 = 0; // Goldilocks Prime - let p_b = Uint384(18446744069414584321, 0, 0); // Goldilocks Prime + let p_b = Uint384(18446744069414584321, 0, 0); // Goldilocks Prime let x_b = Uint384(25, 0, 0); let generator_b = Uint384(7, 0, 0); let (s_b, r_b) = field_arithmetic.get_square_root(x_b, p_b, generator_b); diff --git a/cairo_programs/fq.cairo b/cairo_programs/fq.cairo new file mode 100644 index 0000000000..348df77a6f --- /dev/null +++ b/cairo_programs/fq.cairo @@ -0,0 +1,195 @@ +from starkware.cairo.common.uint256 import Uint256, split_64 +from starkware.cairo.common.math_cmp import is_le + +from cairo_programs.uint384_extension import Uint384, Uint768, u384 + +struct Uint512 { + d0: felt, + d1: felt, + d2: felt, + d3: felt, +} + +const SHIFT = 2 ** 128; +const ALL_ONES = 2 ** 128 - 1; +const HALF_SHIFT = 2 ** 64; + +namespace u512 { + func add_u512_and_u256{range_check_ptr}(a: Uint512, b: Uint256) -> Uint512 { + alloc_locals; + + let a_low = Uint256(low=a.d0, high=a.d1); + let a_high = Uint256(low=a.d2, high=a.d3); + + let (sum_low, carry0) = add_carry(a_low, b); + + local res: Uint512; + + res.d0 = sum_low.low; + res.d1 = sum_low.high; + // res.d2 = sum_low.d2; + + // TODO : create add_one (high bits not needed) + let a_high_plus_carry = add(a_high, Uint256(carry0, 0)); + + res.d2 = a_high_plus_carry.low; + res.d3 = a_high_plus_carry.high; + + return res; + } + + func mul_u512_by_u256{range_check_ptr}(a: Uint512, b: Uint256) -> Uint768 { + alloc_locals; + let (a0, a1) = split_64(a.d0); + let (a2, a3) = split_64(a.d1); + let (a4, a5) = split_64(a.d2); + let (a6, a7) = split_64(a.d3); + + let (b0, b1) = split_64(b.low); + let (b2, b3) = split_64(b.high); + + local B0 = b0 * HALF_SHIFT; + local b12 = b1 + b2 * HALF_SHIFT; + + let (res0, carry) = u384.split_128(a1 * B0 + a0 * b.low); + let (res2, carry) = u384.split_128(a3 * B0 + a2 * b.low + a1 * b12 + a0 * b.high + carry); + let (res4, carry) = u384.split_128( + a5 * B0 + a4 * b.low + a3 * b12 + a2 * b.high + a1 * b3 + carry + ); + let (res6, carry) = u384.split_128( + a7 * B0 + a6 * b.low + a5 * b12 + a4 * b.high + a3 * b3 + carry + ); + let (res8, carry) = u384.split_128(a7 * b12 + a6 * b.high + a5 * b3 + carry); + let (res10, carry) = u384.split_128(a7 * b3 + carry); + let res = Uint768(d0=res0, d1=res2, d2=res4, d3=res6, d4=res8, d5=res10); + return res; + } + + func u512_unsigned_div_rem{range_check_ptr}(x: Uint512, div: Uint256) -> ( + q: Uint512, r: Uint256 + ) { + alloc_locals; + local quotient: Uint512; + local remainder: Uint256; + + %{ + def split(num: int, num_bits_shift: int, length: int): + a = [] + for _ in range(length): + a.append( num & ((1 << num_bits_shift) - 1) ) + num = num >> num_bits_shift + return tuple(a) + + 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)) + + def pack_extended(z, num_bits_shift: int) -> int: + limbs = (z.d0, z.d1, z.d2, z.d3) + return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) + + x = pack_extended(ids.x, num_bits_shift = 128) + div = pack(ids.div, num_bits_shift = 128) + + quotient, remainder = divmod(x, div) + + quotient_split = split(quotient, num_bits_shift=128, length=4) + + ids.quotient.d0 = quotient_split[0] + ids.quotient.d1 = quotient_split[1] + ids.quotient.d2 = quotient_split[2] + ids.quotient.d3 = quotient_split[3] + + remainder_split = split(remainder, num_bits_shift=128, length=2) + ids.remainder.low = remainder_split[0] + ids.remainder.high = remainder_split[1] + %} + + let res_mul: Uint768 = mul_u512_by_u256(quotient, div); + + assert res_mul.d4 = 0; + assert res_mul.d5 = 0; + + let check_val: Uint512 = add_u512_and_u256( + Uint512(res_mul.d0, res_mul.d1, res_mul.d2, res_mul.d3), remainder + ); + + // assert add_carry = 0; + assert check_val = x; + + let is_valid = lt(remainder, div); + assert is_valid = 1; + + return (quotient, remainder); + } + + // Verifies that the given integer is valid. + func check{range_check_ptr}(a: Uint256) { + // tempvar h = a.high - 2 ** 127; + [range_check_ptr] = a.low; + [range_check_ptr + 1] = a.high; + let range_check_ptr = range_check_ptr + 2; + return (); + } + + // Assume a and b are lower than 2**255-19 + func add{range_check_ptr}(a: Uint256, b: Uint256) -> Uint256 { + alloc_locals; + local res: Uint256; + local carry_low: felt; + // unused. added to use UINT256_ADD + local carry_high: felt; + // this hint is not implemented: + // %{ + // sum_low = ids.a.low + ids.b.low + // ids.carry_low = 1 if sum_low >= ids.SHIFT else 0 + // %} + %{ + sum_low = ids.a.low + ids.b.low + ids.carry_low = 1 if sum_low >= ids.SHIFT else 0 + sum_high = ids.a.high + ids.b.high + ids.carry_low + ids.carry_high = 1 if sum_high >= ids.SHIFT else 0 + %} + // changed hint, no carry_high + assert carry_low * carry_low = carry_low; + + assert res.low = a.low + b.low - carry_low * SHIFT; + assert res.high = a.high + b.high + carry_low; + // check(res); + + return res; + } + + func add_carry{range_check_ptr}(a: Uint256, b: Uint256) -> (res: Uint256, carry: felt) { + alloc_locals; + local res: Uint256; + local carry_low: felt; + local carry_high: felt; + %{ + sum_low = ids.a.low + ids.b.low + ids.carry_low = 1 if sum_low >= ids.SHIFT else 0 + sum_high = ids.a.high + ids.b.high + ids.carry_low + ids.carry_high = 1 if sum_high >= ids.SHIFT else 0 + %} + + assert carry_low * carry_low = carry_low; + assert carry_high * carry_high = carry_high; + + assert res.low = a.low + b.low - carry_low * SHIFT; + assert res.high = a.high + b.high + carry_low - carry_high * SHIFT; + check(res); + + return (res, carry_high); + } + + func lt{range_check_ptr}(a: Uint256, b: Uint256) -> felt { + if (a.high == b.high) { + return is_le(a.low + 1, b.low); + } + return is_le(a.high + 1, b.high); + } +} + +func main() { + return (); +} diff --git a/cairo_programs/fq_test.cairo b/cairo_programs/fq_test.cairo new file mode 100644 index 0000000000..621ed21ef5 --- /dev/null +++ b/cairo_programs/fq_test.cairo @@ -0,0 +1,42 @@ +%builtins range_check + +from cairo_programs.fq import u512, Uint256, Uint512 + +func test_u512_unsigned_div_rem{range_check_ptr}() { + let x = Uint512(26362362, 32523523, 135525, 15521); + let div = Uint256(1, 0); + + let (q, r) = u512.u512_unsigned_div_rem(x, div); + + // x / 1 = x + assert q = Uint512(26362362, 32523523, 135525, 15521); + // x % 1 = 0 + assert r = Uint256(0, 0); + + let x = Uint512( + 154693353187447763037373048681595478410, + 49972746532502551770198072697358847685, + 274245096733591597256384467752461786671, + 218971140682455857220416392230637548564, + ); + let div = Uint256( + 103510830969771876705678198448587782120, 321696934602460025966614305804515599536 + ); + + let (q, r) = u512.u512_unsigned_div_rem(x, div); + + assert q = Uint512( + 203702859112426540420143348051200561496, 231621784431619772183895351989849416356, 0, 0 + ); + assert r = Uint256( + 294644766503248848032677663267093316042, 283333580363207111408148984050656446476 + ); + + return (); +} + +func main{range_check_ptr}() { + test_u512_unsigned_div_rem(); + + return (); +} diff --git a/cairo_programs/uint384.cairo b/cairo_programs/uint384.cairo index 6a7d0ca2a4..d66ddbc921 100644 --- a/cairo_programs/uint384.cairo +++ b/cairo_programs/uint384.cairo @@ -1,4 +1,3 @@ -%builtins range_check bitwise // Code taken from https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint384.cairo from starkware.cairo.common.bitwise import bitwise_and, bitwise_or, bitwise_xor from starkware.cairo.common.cairo_builtins import BitwiseBuiltin @@ -37,7 +36,7 @@ const SHIFT = 2 ** 128; const ALL_ONES = 2 ** 128 - 1; const HALF_SHIFT = 2 ** 64; -namespace uint384_lib { +namespace u384 { // Verifies that the given integer is valid. func check{range_check_ptr}(a: Uint384) { [range_check_ptr] = a.d0; @@ -498,73 +497,6 @@ namespace uint384_lib { } } -func test_uint384_operations{range_check_ptr}() { - // Test unsigned_div_rem - let a = Uint384(83434123481193248, 82349321849739284, 839243219401320423); - let div = Uint384(9283430921839492319493, 313248123482483248, 3790328402913840); - let (quotient: Uint384, remainder: Uint384) = uint384_lib.unsigned_div_rem{ - range_check_ptr=range_check_ptr - }(a, div); - assert quotient.d0 = 221; - assert quotient.d1 = 0; - assert quotient.d2 = 0; - - assert remainder.d0 = 340282366920936411825224315027446796751; - assert remainder.d1 = 340282366920938463394229121463989152931; - assert remainder.d2 = 1580642357361782; - - // Test split_128 - let b = 6805647338418769269267492148635364229100; - let (low, high) = uint384_lib.split_128{range_check_ptr=range_check_ptr}(b); - assert high = 19; - assert low = 340282366920938463463374607431768211436; - - // Test _add_no_uint384_test - - let c = Uint384(3789423292314891293, 21894, 340282366920938463463374607431768211455); - let d = Uint384(32838232, 17, 8); - let (sum_res, carry) = uint384_lib._add_no_uint384_check(c, d); - - assert sum_res.d0 = 3789423292347729525; - assert sum_res.d1 = 21911; - assert sum_res.d2 = 7; - assert carry = 1; - - // Test unsigned_div_rem_expanded - let e = Uint384(83434123481193248, 82349321849739284, 839243219401320423); - let div_expand = Uint384_expand( - 9283430921839492319493, 313248123482483248, 3790328402913840, 13, 78990, 109, 7 - ); - let (quotient: Uint384, remainder: Uint384) = uint384_lib.unsigned_div_rem_expanded{ - range_check_ptr=range_check_ptr - }(a, div_expand); - assert quotient.d0 = 7699479077076334; - assert quotient.d1 = 0; - assert quotient.d2 = 0; - - assert remainder.d0 = 340279955073565776659831804641277151872; - assert remainder.d1 = 340282366920938463463356863525615958397; - assert remainder.d2 = 16; - - // Test sqrt - let f = Uint384(83434123481193248, 82349321849739284, 839243219401320423); - let (root) = uint384_lib.sqrt(f); - assert root.d0 = 100835122758113432298839930225328621183; - assert root.d1 = 916102188; - assert root.d2 = 0; - - let g = Uint384(1, 1, 1); - let (sign_g) = uint384_lib.signed_nn(g); - assert sign_g = 1; - - let h = Uint384(0, 0, 170141183460469231731687303715884105729); - let (sign_h) = uint384_lib.signed_nn(h); - assert sign_h = 0; - - return (); -} - -func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() { - test_uint384_operations(); +func main() { return (); } diff --git a/cairo_programs/uint384_extension.cairo b/cairo_programs/uint384_extension.cairo index 4ddeb24499..f09e703aaf 100644 --- a/cairo_programs/uint384_extension.cairo +++ b/cairo_programs/uint384_extension.cairo @@ -6,7 +6,7 @@ from starkware.cairo.common.math_cmp import is_le from starkware.cairo.common.pow import pow from starkware.cairo.common.registers import get_ap, get_fp_and_pc // Import uint384 files -from cairo_programs.uint384 import uint384_lib, Uint384, Uint384_expand, ALL_ONES +from cairo_programs.uint384 import u384, Uint384, Uint384_expand, ALL_ONES // Functions for operating 384-bit integers with 768-bit integers // Represents an integer in the range [0, 2^768). @@ -22,7 +22,7 @@ struct Uint768 { const HALF_SHIFT = 2 ** 64; -namespace uint384_extension_lib { +namespace u384_ext { // Verifies that the given integer is valid. func check{range_check_ptr}(a: Uint768) { [range_check_ptr] = a.d0; @@ -44,7 +44,7 @@ namespace uint384_extension_lib { let a_low = Uint384(d0=a.d0, d1=a.d1, d2=a.d2); let a_high = Uint384(d0=a.d3, d1=a.d4, d2=a.d5); - let (sum_low, carry0) = uint384_lib.add(a_low, b); + let (sum_low, carry0) = u384.add(a_low, b); local res: Uint768; @@ -52,7 +52,7 @@ namespace uint384_extension_lib { res.d1 = sum_low.d1; res.d2 = sum_low.d2; - let (a_high_plus_carry, carry1) = uint384_lib.add(a_high, Uint384(carry0, 0, 0)); + let (a_high_plus_carry, carry1) = u384.add(a_high, Uint384(carry0, 0, 0)); res.d3 = a_high_plus_carry.d0; res.d4 = a_high_plus_carry.d1; @@ -65,40 +65,38 @@ namespace uint384_extension_lib { low: Uint768, high: Uint384 ) { alloc_locals; - let (a0, a1) = uint384_lib.split_64(a.d0); - let (a2, a3) = uint384_lib.split_64(a.d1); - let (a4, a5) = uint384_lib.split_64(a.d2); - let (a6, a7) = uint384_lib.split_64(a.d3); - let (a8, a9) = uint384_lib.split_64(a.d4); - let (a10, a11) = uint384_lib.split_64(a.d5); - let (b0, b1) = uint384_lib.split_64(b.d0); - let (b2, b3) = uint384_lib.split_64(b.d1); - let (b4, b5) = uint384_lib.split_64(b.d2); + let (a0, a1) = u384.split_64(a.d0); + let (a2, a3) = u384.split_64(a.d1); + let (a4, a5) = u384.split_64(a.d2); + let (a6, a7) = u384.split_64(a.d3); + let (a8, a9) = u384.split_64(a.d4); + let (a10, a11) = u384.split_64(a.d5); + let (b0, b1) = u384.split_64(b.d0); + let (b2, b3) = u384.split_64(b.d1); + let (b4, b5) = u384.split_64(b.d2); local B0 = b0 * HALF_SHIFT; local b12 = b1 + b2 * HALF_SHIFT; local b34 = b3 + b4 * HALF_SHIFT; - let (res0, carry) = uint384_lib.split_128(a1 * B0 + a0 * b.d0); - let (res2, carry) = uint384_lib.split_128( - a3 * B0 + a2 * b.d0 + a1 * b12 + a0 * b.d1 + carry - ); - let (res4, carry) = uint384_lib.split_128( + let (res0, carry) = u384.split_128(a1 * B0 + a0 * b.d0); + let (res2, carry) = u384.split_128(a3 * B0 + a2 * b.d0 + a1 * b12 + a0 * b.d1 + carry); + let (res4, carry) = u384.split_128( a5 * B0 + a4 * b.d0 + a3 * b12 + a2 * b.d1 + a1 * b34 + a0 * b.d2 + carry ); - let (res6, carry) = uint384_lib.split_128( + let (res6, carry) = u384.split_128( a7 * B0 + a6 * b.d0 + a5 * b12 + a4 * b.d1 + a3 * b34 + a2 * b.d2 + a1 * b5 + carry ); - let (res8, carry) = uint384_lib.split_128( + let (res8, carry) = u384.split_128( a9 * B0 + a8 * b.d0 + a7 * b12 + a6 * b.d1 + a5 * b34 + a4 * b.d2 + a3 * b5 + carry ); - let (res10, carry) = uint384_lib.split_128( + let (res10, carry) = u384.split_128( a11 * B0 + a10 * b.d0 + a9 * b12 + a8 * b.d1 + a7 * b34 + a6 * b.d2 + a5 * b5 + carry ); - let (res12, carry) = uint384_lib.split_128( + let (res12, carry) = u384.split_128( a11 * b12 + a10 * b.d1 + a9 * b34 + a8 * b.d2 + a7 * b5 + carry ); - let (res14, carry) = uint384_lib.split_128(a11 * b34 + a10 * b.d2 + a9 * b5 + carry); + let (res14, carry) = u384.split_128(a11 * b34 + a10 * b.d2 + a9 * b5 + carry); // let (res16, carry) = split_64(a11 * b5 + carry) return ( @@ -151,7 +149,7 @@ namespace uint384_extension_lib { ids.remainder.d2 = remainder_split[2] %} check(quotient); - uint384_lib.check(remainder); + u384.check(remainder); let (res_mul_low: Uint768, res_mul_high: Uint384) = mul_uint768_by_uint384_expanded( quotient, div @@ -165,7 +163,7 @@ namespace uint384_extension_lib { assert check_val = a; let div2 = Uint384(div.b01, div.b23, div.b45); - let (is_valid) = uint384_lib.lt(remainder, div2); + let (is_valid) = u384.lt(remainder, div2); assert is_valid = 1; return (quotient=quotient, remainder=remainder); @@ -174,36 +172,36 @@ namespace uint384_extension_lib { func mul_uint768_by_uint384_expanded{range_check_ptr}(a: Uint768, b: Uint384_expand) -> ( low: Uint768, high: Uint384 ) { - let (a0, a1) = uint384_lib.split_64(a.d0); - let (a2, a3) = uint384_lib.split_64(a.d1); - let (a4, a5) = uint384_lib.split_64(a.d2); - let (a6, a7) = uint384_lib.split_64(a.d3); - let (a8, a9) = uint384_lib.split_64(a.d4); - let (a10, a11) = uint384_lib.split_64(a.d5); - - let (res0, carry) = uint384_lib.split_128(a1 * b.B0 + a0 * b.b01); - let (res2, carry) = uint384_lib.split_128( + let (a0, a1) = u384.split_64(a.d0); + let (a2, a3) = u384.split_64(a.d1); + let (a4, a5) = u384.split_64(a.d2); + let (a6, a7) = u384.split_64(a.d3); + let (a8, a9) = u384.split_64(a.d4); + let (a10, a11) = u384.split_64(a.d5); + + let (res0, carry) = u384.split_128(a1 * b.B0 + a0 * b.b01); + let (res2, carry) = u384.split_128( a3 * b.B0 + a2 * b.b01 + a1 * b.b12 + a0 * b.b23 + carry ); - let (res4, carry) = uint384_lib.split_128( + let (res4, carry) = u384.split_128( a5 * b.B0 + a4 * b.b01 + a3 * b.b12 + a2 * b.b23 + a1 * b.b34 + a0 * b.b45 + carry ); - let (res6, carry) = uint384_lib.split_128( + let (res6, carry) = u384.split_128( a7 * b.B0 + a6 * b.b01 + a5 * b.b12 + a4 * b.b23 + a3 * b.b34 + a2 * b.b45 + a1 * b.b5 + carry, ); - let (res8, carry) = uint384_lib.split_128( + let (res8, carry) = u384.split_128( a9 * b.B0 + a8 * b.b01 + a7 * b.b12 + a6 * b.b23 + a5 * b.b34 + a4 * b.b45 + a3 * b.b5 + carry, ); - let (res10, carry) = uint384_lib.split_128( + let (res10, carry) = u384.split_128( a11 * b.B0 + a10 * b.b01 + a9 * b.b12 + a8 * b.b23 + a7 * b.b34 + a6 * b.b45 + a5 * b.b5 + carry, ); - let (res12, carry) = uint384_lib.split_128( + let (res12, carry) = u384.split_128( a11 * b.b12 + a10 * b.b23 + a9 * b.b34 + a8 * b.b45 + a7 * b.b5 + carry ); - let (res14, carry) = uint384_lib.split_128(a11 * b.b34 + a10 * b.b45 + a9 * b.b5 + carry); + let (res14, carry) = u384.split_128(a11 * b.b34 + a10 * b.b45 + a9 * b.b5 + carry); // let (res16, carry) = split_64(a11 * b.b5 + carry) return ( @@ -262,7 +260,7 @@ namespace uint384_extension_lib { ids.remainder.d2 = remainder_split[2] %} check(quotient); - uint384_lib.check(remainder); + u384.check(remainder); let (res_mul_low: Uint768, res_mul_high: Uint384) = mul_uint768_by_uint384_d(quotient, div); @@ -273,32 +271,13 @@ namespace uint384_extension_lib { assert add_carry = 0; assert check_val = a; - let (is_valid) = uint384_lib.lt(remainder, div); + let (is_valid) = u384.lt(remainder, div); assert is_valid = 1; return (quotient=quotient, remainder=remainder); } } -func test_uint384_extension_operations{range_check_ptr}() { - // Test unsigned_div_rem_uint768_by_uint384 - let a = Uint768(1, 2, 3, 4, 5, 6); - let div = Uint384(6, 7, 8); - let (q, r) = uint384_extension_lib.unsigned_div_rem_uint768_by_uint384(a, div); - assert q.d0 = 328319314958874220607240343889245110272; - assert q.d1 = 329648542954659136480144150949525454847; - assert q.d2 = 255211775190703847597530955573826158591; - assert q.d3 = 0; - assert q.d4 = 0; - assert q.d5 = 0; - - assert r.d0 = 71778311772385457136805581255138607105; - assert r.d1 = 147544307532125661892322583691118247938; - assert r.d2 = 3; - return (); -} - -func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() { - test_uint384_extension_operations(); +func main() { return (); } diff --git a/cairo_programs/uint384_extension_test.cairo b/cairo_programs/uint384_extension_test.cairo new file mode 100644 index 0000000000..279a9c4a0d --- /dev/null +++ b/cairo_programs/uint384_extension_test.cairo @@ -0,0 +1,26 @@ +%builtins range_check + +from cairo_programs.uint384_extension import u384_ext, Uint768, Uint384 + +func test_uint384_extension_operations{range_check_ptr}() { + // Test unsigned_div_rem_uint768_by_uint384 + let a = Uint768(1, 2, 3, 4, 5, 6); + let div = Uint384(6, 7, 8); + let (q, r) = u384_ext.unsigned_div_rem_uint768_by_uint384(a, div); + assert q.d0 = 328319314958874220607240343889245110272; + assert q.d1 = 329648542954659136480144150949525454847; + assert q.d2 = 255211775190703847597530955573826158591; + assert q.d3 = 0; + assert q.d4 = 0; + assert q.d5 = 0; + + assert r.d0 = 71778311772385457136805581255138607105; + assert r.d1 = 147544307532125661892322583691118247938; + assert r.d2 = 3; + return (); +} + +func main{range_check_ptr: felt}() { + test_uint384_extension_operations(); + return (); +} diff --git a/cairo_programs/uint384_test.cairo b/cairo_programs/uint384_test.cairo new file mode 100644 index 0000000000..b84625eea5 --- /dev/null +++ b/cairo_programs/uint384_test.cairo @@ -0,0 +1,74 @@ +%builtins range_check + +from cairo_programs.uint384 import u384, Uint384, Uint384_expand + +func test_uint384_operations{range_check_ptr}() { + // Test unsigned_div_rem + let a = Uint384(83434123481193248, 82349321849739284, 839243219401320423); + let div = Uint384(9283430921839492319493, 313248123482483248, 3790328402913840); + let (quotient: Uint384, remainder: Uint384) = u384.unsigned_div_rem{ + range_check_ptr=range_check_ptr + }(a, div); + assert quotient.d0 = 221; + assert quotient.d1 = 0; + assert quotient.d2 = 0; + + assert remainder.d0 = 340282366920936411825224315027446796751; + assert remainder.d1 = 340282366920938463394229121463989152931; + assert remainder.d2 = 1580642357361782; + + // Test split_128 + let b = 6805647338418769269267492148635364229100; + let (low, high) = u384.split_128{range_check_ptr=range_check_ptr}(b); + assert high = 19; + assert low = 340282366920938463463374607431768211436; + + // Test _add_no_uint384_test + + let c = Uint384(3789423292314891293, 21894, 340282366920938463463374607431768211455); + let d = Uint384(32838232, 17, 8); + let (sum_res, carry) = u384._add_no_uint384_check(c, d); + + assert sum_res.d0 = 3789423292347729525; + assert sum_res.d1 = 21911; + assert sum_res.d2 = 7; + assert carry = 1; + + // Test unsigned_div_rem_expanded + let e = Uint384(83434123481193248, 82349321849739284, 839243219401320423); + let div_expand = Uint384_expand( + 9283430921839492319493, 313248123482483248, 3790328402913840, 13, 78990, 109, 7 + ); + let (quotient: Uint384, remainder: Uint384) = u384.unsigned_div_rem_expanded{ + range_check_ptr=range_check_ptr + }(a, div_expand); + assert quotient.d0 = 7699479077076334; + assert quotient.d1 = 0; + assert quotient.d2 = 0; + + assert remainder.d0 = 340279955073565776659831804641277151872; + assert remainder.d1 = 340282366920938463463356863525615958397; + assert remainder.d2 = 16; + + // Test sqrt + let f = Uint384(83434123481193248, 82349321849739284, 839243219401320423); + let (root) = u384.sqrt(f); + assert root.d0 = 100835122758113432298839930225328621183; + assert root.d1 = 916102188; + assert root.d2 = 0; + + let g = Uint384(1, 1, 1); + let (sign_g) = u384.signed_nn(g); + assert sign_g = 1; + + let h = Uint384(0, 0, 170141183460469231731687303715884105729); + let (sign_h) = u384.signed_nn(h); + assert sign_h = 0; + + return (); +} + +func main{range_check_ptr: felt}() { + test_uint384_operations(); + return (); +} diff --git a/src/hint_processor/builtin_hint_processor/bigint.rs b/src/hint_processor/builtin_hint_processor/bigint.rs index 3194b75fa2..b9a0f83101 100644 --- a/src/hint_processor/builtin_hint_processor/bigint.rs +++ b/src/hint_processor/builtin_hint_processor/bigint.rs @@ -6,7 +6,7 @@ use crate::stdlib::prelude::String; use crate::types::exec_scope::ExecutionScopes; use crate::{ hint_processor::{ - builtin_hint_processor::secp::{bigint_utils::BigInt3, secp_utils::pack}, + builtin_hint_processor::secp::bigint_utils::BigInt3, hint_processor_definition::HintReference, }, serde::deserialize_program::ApTracking, @@ -37,7 +37,10 @@ pub fn bigint_pack_div_mod_hint( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let p: BigInt = pack(BigInt3::from_var_name("P", vm, ids_data, ap_tracking)?); + let p: BigInt = BigInt3::from_var_name("P", vm, ids_data, ap_tracking)? + .pack86() + .to_bigint() + .unwrap_or_default(); let x: BigInt = { let x_bigint5 = BigInt5::from_var_name("x", vm, ids_data, ap_tracking)?; @@ -46,11 +49,15 @@ pub fn bigint_pack_div_mod_hint( d1: x_bigint5.d1, d2: x_bigint5.d2, }; + let x_lower = x_lower.pack86().to_bigint().unwrap_or_default(); let d3 = x_bigint5.d3.as_ref().to_biguint().to_bigint().unwrap(); let d4 = x_bigint5.d4.as_ref().to_biguint().to_bigint().unwrap(); - pack(x_lower) + d3 * BigInt::from(BASE.pow(3)) + d4 * BigInt::from(BASE.pow(4)) + x_lower + d3 * BigInt::from(BASE.pow(3)) + d4 * BigInt::from(BASE.pow(4)) }; - let y: BigInt = pack(BigInt3::from_var_name("y", vm, ids_data, ap_tracking)?); + let y: BigInt = BigInt3::from_var_name("y", vm, ids_data, ap_tracking)? + .pack86() + .to_bigint() + .unwrap_or_default(); let res = div_mod(&x, &y, &p); exec_scopes.insert_value("res", res.clone()); @@ -103,11 +110,8 @@ mod test { use crate::hint_processor::hint_processor_definition::HintReference; use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; - use crate::types::relocatable::MaybeRelocatable; use crate::utils::test_utils::*; - use crate::vm::errors::memory_errors::MemoryError; use crate::vm::vm_core::VirtualMachine; - use crate::vm::vm_memory::{memory::Memory, memory_segments::MemorySegmentManager}; use assert_matches::assert_matches; use num_bigint::BigInt; diff --git a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index 3c10196e4b..a9a9f3e051 100644 --- a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -207,7 +207,6 @@ pub fn blake2s_add_uint256_bigend( mod tests { use super::*; use crate::types::errors::math_errors::MathError; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use crate::{ any_box, hint_processor::{ @@ -219,7 +218,7 @@ mod tests { relocatable, types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory}, + vm::errors::memory_errors::MemoryError, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index b4a0d811d5..9107b538d9 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -1,3 +1,8 @@ +use super::{ + ec_recover::{ec_recover_divmod_n_packed, ec_recover_sub_a_b}, + field_arithmetic::uint384_div, + vrf::{fq::uint512_unsigned_div_rem, inv_mod_p_uint512::inv_mod_p_uint512}, +}; use crate::{ hint_processor::{ builtin_hint_processor::{ @@ -85,10 +90,6 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; -use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_sub_a_b}; -use super::field_arithmetic::uint384_div; -use super::vrf::inv_mod_p_uint512::inv_mod_p_uint512; - pub struct HintProcessorData { pub code: String, pub ap_tracking: ApTracking, @@ -578,6 +579,9 @@ impl HintProcessor for BuiltinHintProcessor { hint_code::UINT256_MUL_DIV_MOD => { uint256_mul_div_mod(vm, &hint_data.ids_data, &hint_data.ap_tracking) } + hint_code::UINT512_UNSIGNED_DIV_REM => { + uint512_unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) + } hint_code::HI_MAX_BITLEN => { hi_max_bitlen(vm, &hint_data.ids_data, &hint_data.ap_tracking) } @@ -607,7 +611,7 @@ mod tests { use super::*; use crate::stdlib::any::Any; use crate::types::relocatable::Relocatable; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::hint_processor_definition::HintProcessor, @@ -616,7 +620,6 @@ mod tests { vm::{ errors::{exec_scope_errors::ExecScopeError, memory_errors::MemoryError}, vm_core::VirtualMachine, - vm_memory::memory::Memory, }, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs b/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs index dbd789ae30..e9197656b7 100644 --- a/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs +++ b/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs @@ -263,7 +263,7 @@ pub fn u64_array_to_mayberelocatable_vec(array: &[u64]) -> Vec mod tests { use super::*; use crate::stdlib::string::ToString; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -274,10 +274,7 @@ mod tests { }, types::{exec_scope::ExecutionScopes, relocatable::Relocatable}, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs b/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs index f3108bf776..472c277f9b 100644 --- a/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs @@ -260,8 +260,7 @@ mod tests { use crate::hint_processor::hint_processor_definition::HintProcessor; use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ hint_processor::builtin_hint_processor::dict_manager::{DictManager, DictTracker}, relocatable, diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 603f148d0d..6b54d3abc5 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -1,4 +1,7 @@ -use super::secp::{bigint_utils::BigInt3, secp_utils::pack}; +use num_integer::Integer; + +use super::secp::bigint_utils::BigInt3; +use super::secp::secp_utils::bigint3_pack; use crate::stdlib::{collections::HashMap, prelude::*}; use crate::{ hint_processor::hint_processor_definition::HintReference, @@ -7,7 +10,6 @@ use crate::{ types::exec_scope::ExecutionScopes, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; -use num_integer::Integer; /* Implements Hint: %{ @@ -26,9 +28,9 @@ pub fn ec_recover_divmod_n_packed( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let n = pack(BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?); - let x = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&n); - let s = pack(BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&n); + let n = bigint3_pack(BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?); + let x = bigint3_pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&n); + let s = bigint3_pack(BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&n); let value = div_mod(&x, &s, &n); exec_scopes.insert_value("value", value.clone()); @@ -52,8 +54,8 @@ pub fn ec_recover_sub_a_b( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?); - let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?); + let a = bigint3_pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?); + let b = bigint3_pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?); let value = a - b; exec_scopes.insert_value("value", value.clone()); @@ -69,10 +71,7 @@ mod tests { use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::hint_processor_definition::HintReference; use crate::utils::test_utils::*; - use crate::vm::errors::memory_errors::MemoryError; use crate::vm::vm_core::VirtualMachine; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use crate::{ any_box, hint_processor::{ @@ -81,7 +80,7 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::exec_scope::ExecutionScopes, }; #[cfg(target_arch = "wasm32")] diff --git a/src/hint_processor/builtin_hint_processor/field_arithmetic.rs b/src/hint_processor/builtin_hint_processor/field_arithmetic.rs index 9b72a9788e..c103724304 100644 --- a/src/hint_processor/builtin_hint_processor/field_arithmetic.rs +++ b/src/hint_processor/builtin_hint_processor/field_arithmetic.rs @@ -3,6 +3,8 @@ use num_bigint::{BigUint, ToBigInt}; use num_integer::Integer; use num_traits::Zero; +use super::hint_utils::insert_value_from_var_name; +use super::secp::bigint_utils::Uint384; use crate::math_utils::{is_quad_residue, mul_inv, sqrt_prime_power}; use crate::serde::deserialize_program::ApTracking; use crate::stdlib::{collections::HashMap, prelude::*}; @@ -12,9 +14,6 @@ use crate::{ hint_processor::hint_processor_definition::HintReference, vm::vm_core::VirtualMachine, }; -use super::hint_utils::{get_relocatable_from_var_name, insert_value_from_var_name}; -use super::secp::bigint_utils::BigInt3; -use super::uint384::{pack, split}; /* Implements Hint: %{ from starkware.python.math_utils import is_quad_residue, sqrt @@ -66,14 +65,9 @@ pub fn get_square_root( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let sqrt_x_addr = get_relocatable_from_var_name("sqrt_x", vm, ids_data, ap_tracking)?; - let sqrt_gx_addr = get_relocatable_from_var_name("sqrt_gx", vm, ids_data, ap_tracking)?; - let generator = pack( - BigInt3::from_var_name("generator", vm, ids_data, ap_tracking)?, - 128, - ); - let x = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?, 128); - let p = pack(BigInt3::from_var_name("p", vm, ids_data, ap_tracking)?, 128); + let generator = Uint384::from_var_name("generator", vm, ids_data, ap_tracking)?.pack(); + let x = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack(); + let p = Uint384::from_var_name("p", vm, ids_data, ap_tracking)?.pack(); let success_x = is_quad_residue(&x, &p)?; let root_x = if success_x { @@ -103,15 +97,8 @@ pub fn get_square_root( ids_data, ap_tracking, )?; - let split_root_x = split::<3>(&root_x, 128); - for (i, root_x) in split_root_x.iter().enumerate() { - vm.insert_value((sqrt_x_addr + i)?, Felt252::from(root_x))?; - } - let split_root_gx = split::<3>(&root_gx, 128); - for (i, root_gx) in split_root_gx.iter().enumerate() { - vm.insert_value((sqrt_gx_addr + i)?, Felt252::from(root_gx))?; - } - + Uint384::split(&root_x).insert_from_var_name("sqrt_x", vm, ids_data, ap_tracking)?; + Uint384::split(&root_gx).insert_from_var_name("sqrt_gx", vm, ids_data, ap_tracking)?; Ok(()) } @@ -152,14 +139,15 @@ pub fn uint384_div( ap_tracking: &ApTracking, ) -> Result<(), HintError> { // Note: ids.a is not used here, nor is it used by following hints, so we dont need to extract it. - let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?, 128) + let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)? + .pack() .to_bigint() .unwrap_or_default(); - let p = pack(BigInt3::from_var_name("p", vm, ids_data, ap_tracking)?, 128) + let p = Uint384::from_var_name("p", vm, ids_data, ap_tracking)? + .pack() .to_bigint() .unwrap_or_default(); - let b_inverse_mod_p_addr = - get_relocatable_from_var_name("b_inverse_mod_p", vm, ids_data, ap_tracking)?; + if b.is_zero() { return Err(MathError::DividedByZero.into()); } @@ -167,20 +155,14 @@ pub fn uint384_div( .mod_floor(&p) .to_biguint() .unwrap_or_default(); - let b_inverse_mod_p_split = split::<3>(&b_inverse_mod_p, 128); - for (i, b_inverse_mod_p_split) in b_inverse_mod_p_split.iter().enumerate() { - vm.insert_value( - (b_inverse_mod_p_addr + i)?, - Felt252::from(b_inverse_mod_p_split), - )?; - } - Ok(()) + let b_inverse_mod_p_split = Uint384::split(&b_inverse_mod_p); + b_inverse_mod_p_split.insert_from_var_name("b_inverse_mod_p", vm, ids_data, ap_tracking) } #[cfg(test)] mod tests { use super::*; use crate::hint_processor::builtin_hint_processor::hint_code; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::vm::errors::memory_errors::MemoryError; use crate::{ any_box, hint_processor::{ @@ -189,12 +171,9 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 2df3602b8c..9407acc895 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -980,6 +980,7 @@ ids.x_inverse_mod_p.high = x_inverse_mod_p_split[1]"; pub const DI_BIT: &str = r#"ids.dibit = ((ids.scalar_u >> ids.m) & 1) + 2 * ((ids.scalar_v >> ids.m) & 1)"#; + pub const EC_RECOVER_DIV_MOD_N_PACKED: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div @@ -988,6 +989,37 @@ x = pack(ids.x, PRIME) % N s = pack(ids.s, PRIME) % N value = res = div_mod(x, s, N)"#; +pub const UINT512_UNSIGNED_DIV_REM: &str = r#"def split(num: int, num_bits_shift: int, length: int): + a = [] + for _ in range(length): + a.append( num & ((1 << num_bits_shift) - 1) ) + num = num >> num_bits_shift + return tuple(a) + +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)) + +def pack_extended(z, num_bits_shift: int) -> int: + limbs = (z.d0, z.d1, z.d2, z.d3) + return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) + +x = pack_extended(ids.x, num_bits_shift = 128) +div = pack(ids.div, num_bits_shift = 128) + +quotient, remainder = divmod(x, div) + +quotient_split = split(quotient, num_bits_shift=128, length=4) + +ids.quotient.d0 = quotient_split[0] +ids.quotient.d1 = quotient_split[1] +ids.quotient.d2 = quotient_split[2] +ids.quotient.d3 = quotient_split[3] + +remainder_split = split(remainder, num_bits_shift=128, length=2) +ids.remainder.low = remainder_split[0] +ids.remainder.high = remainder_split[1]"#; + pub const EC_RECOVER_SUB_A_B: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div diff --git a/src/hint_processor/builtin_hint_processor/hint_utils.rs b/src/hint_processor/builtin_hint_processor/hint_utils.rs index 66c891ff5e..073e24efb3 100644 --- a/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -123,15 +123,13 @@ pub fn get_reference_from_var_name<'a>( mod tests { use super::*; use crate::stdlib::string::ToString; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ hint_processor::hint_processor_definition::HintReference, relocatable, serde::deserialize_program::OffsetValue, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::{vm_core::VirtualMachine, vm_memory::memory::Memory}, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/keccak_utils.rs b/src/hint_processor/builtin_hint_processor/keccak_utils.rs index ce4a60068b..3b7afd5bfa 100644 --- a/src/hint_processor/builtin_hint_processor/keccak_utils.rs +++ b/src/hint_processor/builtin_hint_processor/keccak_utils.rs @@ -307,13 +307,9 @@ mod tests { }, hint_processor_definition::{HintProcessor, HintReference}, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, - vm_core::VirtualMachine, - vm_memory::{memory::Memory, memory_segments::MemorySegmentManager}, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/math_utils.rs b/src/hint_processor/builtin_hint_processor/math_utils.rs index 77a3508b58..4ed24b95c9 100644 --- a/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -585,7 +585,7 @@ fn div_prime_by_bound(bound: Felt252) -> Result { mod tests { use super::*; use crate::stdlib::ops::Shl; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -599,10 +599,7 @@ mod tests { types::exec_scope::ExecutionScopes, types::relocatable::Relocatable, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; use assert_matches::assert_matches; use felt::felt_str; diff --git a/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs b/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs index 6cfa942c9e..b38e96fed9 100644 --- a/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs @@ -81,12 +81,8 @@ pub fn memcpy_continue_copying( #[cfg(test)] mod tests { use super::*; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; - use crate::{ - types::relocatable::MaybeRelocatable, - utils::test_utils::*, - vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory}, - }; + + use crate::utils::test_utils::*; use assert_matches::assert_matches; #[cfg(target_arch = "wasm32")] diff --git a/src/hint_processor/builtin_hint_processor/memset_utils.rs b/src/hint_processor/builtin_hint_processor/memset_utils.rs index 46cca7ea40..9f9e481f01 100644 --- a/src/hint_processor/builtin_hint_processor/memset_utils.rs +++ b/src/hint_processor/builtin_hint_processor/memset_utils.rs @@ -59,7 +59,7 @@ mod tests { use super::*; use crate::stdlib::string::ToString; use crate::types::relocatable::Relocatable; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -70,7 +70,7 @@ mod tests { }, types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, utils::test_utils::*, - vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory}, + vm::errors::memory_errors::MemoryError, }; use assert_matches::assert_matches; use num_traits::{One, Zero}; diff --git a/src/hint_processor/builtin_hint_processor/mod.rs b/src/hint_processor/builtin_hint_processor/mod.rs index 3c8aa69f77..1c1c640e1a 100644 --- a/src/hint_processor/builtin_hint_processor/mod.rs +++ b/src/hint_processor/builtin_hint_processor/mod.rs @@ -30,5 +30,7 @@ pub mod squash_dict_utils; pub mod uint256_utils; pub mod uint384; pub mod uint384_extension; +pub mod uint512_utils; +pub mod uint_utils; pub mod usort; pub mod vrf; diff --git a/src/hint_processor/builtin_hint_processor/poseidon_utils.rs b/src/hint_processor/builtin_hint_processor/poseidon_utils.rs index 32af5fd832..34f5f03062 100644 --- a/src/hint_processor/builtin_hint_processor/poseidon_utils.rs +++ b/src/hint_processor/builtin_hint_processor/poseidon_utils.rs @@ -46,11 +46,9 @@ mod tests { use crate::hint_processor::hint_processor_definition::HintReference; use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; - use crate::types::relocatable::MaybeRelocatable; - use crate::vm::errors::memory_errors::MemoryError; + use crate::vm::vm_core::VirtualMachine; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*}; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/pow_utils.rs b/src/hint_processor/builtin_hint_processor/pow_utils.rs index ac813c5064..085fe7d2ce 100644 --- a/src/hint_processor/builtin_hint_processor/pow_utils.rs +++ b/src/hint_processor/builtin_hint_processor/pow_utils.rs @@ -44,7 +44,7 @@ pub fn pow( mod tests { use super::*; use crate::types::relocatable::Relocatable; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -54,10 +54,7 @@ mod tests { }, types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; use assert_matches::assert_matches; use num_traits::One; diff --git a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index f1847b1923..c584a88d97 100644 --- a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -1,9 +1,12 @@ +use core::ops::Shl; + +use crate::hint_processor::builtin_hint_processor::uint_utils::{pack, split}; use crate::stdlib::{borrow::Cow, collections::HashMap, prelude::*}; use crate::{ hint_processor::{ builtin_hint_processor::{ hint_utils::{get_relocatable_from_var_name, insert_value_from_var_name}, - secp::secp_utils::{split, BASE_86}, + secp::secp_utils::{bigint3_split, BASE_86}, }, hint_processor_definition::HintReference, }, @@ -15,8 +18,12 @@ use crate::{ vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; use felt::Felt252; +use num_bigint::{BigInt, BigUint}; use num_traits::Bounded; +// Uint384 and BigInt3 are used interchangeably with BigInt3 +pub(crate) type Uint384<'a> = BigInt3<'a>; + #[derive(Debug, PartialEq)] pub(crate) struct BigInt3<'a> { pub d0: Cow<'a, Felt252>, @@ -52,6 +59,48 @@ impl BigInt3<'_> { let base_addr = get_relocatable_from_var_name(name, vm, ids_data, ap_tracking)?; BigInt3::from_base_addr(base_addr, name, vm) } + + pub(crate) fn from_values(limbs: [Felt252; 3]) -> Self { + let [d0, d1, d2] = limbs; + let d0 = Cow::Owned(d0); + let d1 = Cow::Owned(d1); + let d2 = Cow::Owned(d2); + Self { d0, d1, d2 } + } + + pub(crate) fn insert_from_var_name( + self, + var_name: &str, + vm: &mut VirtualMachine, + ids_data: &HashMap, + ap_tracking: &ApTracking, + ) -> Result<(), HintError> { + let addr = get_relocatable_from_var_name(var_name, vm, ids_data, ap_tracking)?; + + vm.insert_value(addr, self.d0.into_owned())?; + vm.insert_value((addr + 1)?, self.d1.into_owned())?; + vm.insert_value((addr + 2)?, self.d2.into_owned())?; + + Ok(()) + } + + pub(crate) fn pack(self) -> BigUint { + pack([self.d0, self.d1, self.d2], 128) + } + + pub(crate) fn pack86(self) -> BigInt { + let limbs = [self.d0, self.d1, self.d2]; + limbs + .into_iter() + .enumerate() + .map(|(idx, value)| value.to_bigint().shl(idx * 86)) + .sum() + } + + pub(crate) fn split(num: &BigUint) -> Self { + let limbs = split(num, 128); + Self::from_values(limbs) + } } #[derive(Debug, PartialEq)] @@ -117,7 +166,7 @@ pub fn nondet_bigint3( .get_ref::("value")? .to_biguint() .ok_or(HintError::BigIntToBigUintFail)?; - let arg: Vec = split(&value)? + let arg: Vec = bigint3_split(&value)? .into_iter() .map(|n| MaybeRelocatable::from(Felt252::new(n))) .collect(); @@ -181,14 +230,12 @@ mod tests { use crate::stdlib::ops::Shl; use crate::stdlib::string::ToString; use crate::types::exec_scope::ExecutionScopes; - use crate::types::relocatable::MaybeRelocatable; + use crate::types::relocatable::Relocatable; use crate::utils::test_utils::*; - use crate::vm::errors::memory_errors::MemoryError; - use crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner; + use crate::vm::vm_core::VirtualMachine; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use assert_matches::assert_matches; use num_traits::One; diff --git a/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs b/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs index 036f6e1359..e18872d65f 100644 --- a/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs @@ -5,10 +5,7 @@ use crate::{ get_integer_from_var_name, get_relocatable_from_var_name, insert_value_from_var_name, insert_value_into_ap, }, - secp::{ - bigint_utils::BigInt3, - secp_utils::{pack, SECP_P}, - }, + secp::{bigint_utils::BigInt3, secp_utils::SECP_P}, }, hint_processor_definition::HintReference, }, @@ -66,7 +63,7 @@ pub fn ec_negate( //ids.point let point_y = (get_relocatable_from_var_name("point", vm, ids_data, ap_tracking)? + 3i32)?; let y_bigint3 = BigInt3::from_base_addr(point_y, "point.y", vm)?; - let y = pack(y_bigint3); + let y = y_bigint3.pack86(); let value = (-y).mod_floor(&SECP_P); exec_scopes.insert_value("value", value); Ok(()) @@ -95,7 +92,11 @@ 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(&(pack(point.x), pack(point.y)), &BigInt::zero(), &SECP_P); + let value = ec_double_slope( + &(point.x.pack86(), point.y.pack86()), + &BigInt::zero(), + &SECP_P, + ); exec_scopes.insert_value("value", value.clone()); exec_scopes.insert_value("slope", value); Ok(()) @@ -150,8 +151,8 @@ pub fn compute_slope( let secp_p: BigInt = exec_scopes.get("SECP_P")?; let value = line_slope( - &(pack(point0.x), pack(point0.y)), - &(pack(point1.x), pack(point1.y)), + &(point0.x.pack86(), point0.y.pack86()), + &(point1.x.pack86(), point1.y.pack86()), &secp_p, ); exec_scopes.insert_value("value", value.clone()); @@ -183,9 +184,9 @@ pub fn ec_double_assign_new_x( //ids.point let point = EcPoint::from_var_name("point", vm, ids_data, ap_tracking)?; - let slope = pack(slope); - let x = pack(point.x); - let y = pack(point.y); + let slope = slope.pack86(); + let x = point.x.pack86(); + let y = point.y.pack86(); let value = (slope.pow(2) - (&x << 1u32)).mod_floor(&SECP_P); @@ -244,10 +245,10 @@ pub fn fast_ec_add_assign_new_x( //ids.point1.x let point1 = EcPoint::from_var_name("point1", vm, ids_data, ap_tracking)?; - let slope = pack(slope); - let x0 = pack(point0.x); - let x1 = pack(point1.x); - let y0 = pack(point0.y); + let slope = slope.pack86(); + let x0 = point0.x.pack86(); + let x1 = point1.x.pack86(); + let y0 = point0.y.pack86(); let value = (&slope * &slope - &x0 - &x1).mod_floor(&SECP_P); //Assign variables to vm scope @@ -381,7 +382,7 @@ mod tests { use super::*; use crate::hint_processor::builtin_hint_processor::hint_code; use crate::stdlib::string::ToString; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -390,15 +391,9 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::{ - exec_scope::ExecutionScopes, - relocatable::{MaybeRelocatable, Relocatable}, - }, + types::{exec_scope::ExecutionScopes, relocatable::Relocatable}, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index 0234487d68..1009b96ab3 100644 --- a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -3,8 +3,8 @@ use crate::{ builtin_hint_processor::{ hint_utils::{insert_value_from_var_name, insert_value_into_ap}, secp::{ - bigint_utils::BigInt3, - secp_utils::{pack, SECP_P}, + bigint_utils::Uint384, + secp_utils::{bigint3_pack, SECP_P}, }, }, hint_processor_definition::HintReference, @@ -37,7 +37,7 @@ pub fn verify_zero( ap_tracking: &ApTracking, ) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); - let val = pack(BigInt3::from_var_name("val", vm, ids_data, ap_tracking)?); + let val = bigint3_pack(Uint384::from_var_name("val", vm, ids_data, ap_tracking)?); let (q, r) = val.div_rem(&SECP_P); if !r.is_zero() { return Err(HintError::SecpVerifyZero(val)); @@ -63,7 +63,7 @@ pub fn verify_zero_with_external_const( ap_tracking: &ApTracking, ) -> Result<(), HintError> { let secp_p = exec_scopes.get_ref("SECP_P")?; - let val = pack(BigInt3::from_var_name("val", vm, ids_data, ap_tracking)?); + let val = bigint3_pack(Uint384::from_var_name("val", vm, ids_data, ap_tracking)?); let (q, r) = val.div_rem(secp_p); if !r.is_zero() { return Err(HintError::SecpVerifyZero(val)); @@ -87,7 +87,7 @@ pub fn reduce( ap_tracking: &ApTracking, ) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); - let value = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?); + let value = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?); exec_scopes.insert_value("value", value.mod_floor(&SECP_P)); Ok(()) } @@ -107,7 +107,7 @@ pub fn is_zero_pack( ap_tracking: &ApTracking, ) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); - let x_packed = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?); + let x_packed = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?); let x = x_packed.mod_floor(&SECP_P); exec_scopes.insert_value("x", x); Ok(()) @@ -120,7 +120,7 @@ pub fn is_zero_pack_external_secp( ap_tracking: &ApTracking, ) -> Result<(), HintError> { let secp_p = exec_scopes.get_ref("SECP_P")?; - let x_packed = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?); + let x_packed = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?); let x = x_packed.mod_floor(secp_p); exec_scopes.insert_value("x", x); Ok(()) @@ -195,7 +195,7 @@ mod tests { use super::*; use crate::hint_processor::builtin_hint_processor::hint_code; use crate::stdlib::string::ToString; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -209,10 +209,7 @@ mod tests { relocatable::{MaybeRelocatable, Relocatable}, }, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_memory::memory::Memory, - }, + vm::errors::memory_errors::MemoryError, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs b/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs index 32d72e9c2c..7dfbd55d5f 100644 --- a/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs @@ -8,7 +8,7 @@ use lazy_static::lazy_static; use num_bigint::{BigInt, BigUint}; use num_traits::Zero; -use super::bigint_utils::BigInt3; +use super::bigint_utils::Uint384; // Constants in package "starkware.cairo.common.cairo_secp.constants". pub const BASE_86: &str = "starkware.cairo.common.cairo_secp.constants.BASE"; @@ -62,7 +62,7 @@ Takes a 256-bit integer and returns its canonical representation as: d0 + BASE * d1 + BASE**2 * d2, where BASE = 2**86. */ -pub fn split(integer: &num_bigint::BigUint) -> Result<[num_bigint::BigUint; 3], HintError> { +pub fn bigint3_split(integer: &num_bigint::BigUint) -> Result<[num_bigint::BigUint; 3], HintError> { let mut canonical_repr: [num_bigint::BigUint; 3] = Default::default(); let mut num = integer.clone(); for item in &mut canonical_repr { @@ -81,8 +81,8 @@ Takes an UnreducedFelt2523 struct which represents a triple of limbs (d0, d1, d2 elements and reconstructs the corresponding 256-bit integer (see split()). Note that the limbs do not have to be in the range [0, BASE). */ -pub(crate) fn pack(num: BigInt3) -> num_bigint::BigInt { - let limbs = vec![num.d0, num.d1, num.d2]; +pub(crate) fn bigint3_pack(num: Uint384) -> num_bigint::BigInt { + let limbs = [num.d0, num.d1, num.d2]; #[allow(deprecated)] limbs .into_iter() @@ -110,22 +110,22 @@ mod tests { let mut constants = HashMap::new(); constants.insert(BASE_86.to_string(), Felt252::one() << 86_usize); - let array_1 = split(&BigUint::zero()); + let array_1 = bigint3_split(&BigUint::zero()); #[allow(deprecated)] - let array_2 = split( + let array_2 = bigint3_split( &bigint!(999992) .to_biguint() .expect("Couldn't convert to BigUint"), ); #[allow(deprecated)] - let array_3 = split( + let array_3 = bigint3_split( &bigint_str!("7737125245533626718119526477371252455336267181195264773712524553362") .to_biguint() .expect("Couldn't convert to BigUint"), ); //TODO, Check SecpSplitutOfRange limit #[allow(deprecated)] - let array_4 = split( + let array_4 = bigint3_split( &bigint_str!( "773712524553362671811952647737125245533626718119526477371252455336267181195264" ) @@ -175,7 +175,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn secp_pack() { - let pack_1 = pack(BigInt3 { + let pack_1 = bigint3_pack(Uint384 { d0: Cow::Borrowed(&Felt252::new(10_i32)), d1: Cow::Borrowed(&Felt252::new(10_i32)), d2: Cow::Borrowed(&Felt252::new(10_i32)), @@ -185,7 +185,7 @@ mod tests { bigint_str!("59863107065073783529622931521771477038469668772249610") ); - let pack_2 = pack(BigInt3 { + let pack_2 = bigint3_pack(Uint384 { d0: Cow::Borrowed(&felt_str!("773712524553362")), d1: Cow::Borrowed(&felt_str!("57408430697461422066401280")), d2: Cow::Borrowed(&felt_str!("1292469707114105")), diff --git a/src/hint_processor/builtin_hint_processor/secp/signature.rs b/src/hint_processor/builtin_hint_processor/secp/signature.rs index c4086fc99e..b8f369ca1c 100644 --- a/src/hint_processor/builtin_hint_processor/secp/signature.rs +++ b/src/hint_processor/builtin_hint_processor/secp/signature.rs @@ -3,7 +3,7 @@ use crate::{ hint_processor::{ builtin_hint_processor::{ hint_utils::get_integer_from_var_name, - secp::secp_utils::{pack, BETA}, + secp::secp_utils::{bigint3_pack, BETA}, }, hint_processor_definition::HintReference, }, @@ -19,7 +19,7 @@ use num_bigint::BigInt; use num_integer::Integer; use super::{ - bigint_utils::BigInt3, + bigint_utils::Uint384, secp_utils::{N, SECP_P}, }; @@ -38,8 +38,8 @@ pub fn div_mod_n_packed( ap_tracking: &ApTracking, n: &BigInt, ) -> Result<(), HintError> { - let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?); - let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?); + let a = bigint3_pack(Uint384::from_var_name("a", vm, ids_data, ap_tracking)?); + let b = bigint3_pack(Uint384::from_var_name("b", vm, ids_data, ap_tracking)?); let value = div_mod(&a, &b, n); exec_scopes.insert_value("a", a); @@ -116,8 +116,8 @@ pub fn get_point_from_x( .ok_or(HintError::MissingConstant(BETA))? .to_bigint(); - let x_cube_int = - pack(BigInt3::from_var_name("x_cube", vm, ids_data, ap_tracking)?).mod_floor(&SECP_P); + let x_cube_int = bigint3_pack(Uint384::from_var_name("x_cube", vm, ids_data, ap_tracking)?) + .mod_floor(&SECP_P); let y_cube_int = (x_cube_int + beta).mod_floor(&SECP_P); // Divide by 4 let mut y = y_cube_int.modpow(&(&*SECP_P + 1_u32).shr(2_u32), &SECP_P); @@ -145,8 +145,8 @@ pub fn pack_modn_div_modn( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let x = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&N); - let s = pack(BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&N); + let x = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&N); + let s = bigint3_pack(Uint384::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&N); let value = div_mod(&x, &s, &N); exec_scopes.insert_value("x", x); @@ -161,7 +161,7 @@ mod tests { use super::*; use crate::stdlib::string::ToString; use crate::types::errors::math_errors::MathError; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -171,9 +171,8 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory}, }; use assert_matches::assert_matches; use num_traits::{One, Zero}; diff --git a/src/hint_processor/builtin_hint_processor/segments.rs b/src/hint_processor/builtin_hint_processor/segments.rs index 6ca1e525ff..bc3fd8e214 100644 --- a/src/hint_processor/builtin_hint_processor/segments.rs +++ b/src/hint_processor/builtin_hint_processor/segments.rs @@ -47,7 +47,7 @@ pub fn temporary_array( #[cfg(test)] mod tests { use super::*; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -57,11 +57,9 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/set.rs b/src/hint_processor/builtin_hint_processor/set.rs index b72fa9b90c..fa61c98f0a 100644 --- a/src/hint_processor/builtin_hint_processor/set.rs +++ b/src/hint_processor/builtin_hint_processor/set.rs @@ -61,7 +61,7 @@ pub fn set_add( #[cfg(test)] mod tests { use super::*; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -72,10 +72,7 @@ mod tests { }, types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/sha256_utils.rs b/src/hint_processor/builtin_hint_processor/sha256_utils.rs index e6ccffc8c6..7e84b1f250 100644 --- a/src/hint_processor/builtin_hint_processor/sha256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/sha256_utils.rs @@ -118,15 +118,10 @@ pub fn sha256_finalize( #[cfg(test)] mod tests { use super::*; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ - hint_processor::hint_processor_definition::HintReference, - types::relocatable::MaybeRelocatable, - utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + hint_processor::hint_processor_definition::HintReference, utils::test_utils::*, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/signature.rs b/src/hint_processor/builtin_hint_processor/signature.rs index 9526dd9803..575d7fe5f2 100644 --- a/src/hint_processor/builtin_hint_processor/signature.rs +++ b/src/hint_processor/builtin_hint_processor/signature.rs @@ -44,7 +44,7 @@ pub fn verify_ecdsa_signature( #[cfg(test)] mod tests { use super::*; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -55,15 +55,10 @@ mod tests { hint_processor_definition::HintProcessor, }, types::{ - exec_scope::ExecutionScopes, - instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, - relocatable::MaybeRelocatable, + exec_scope::ExecutionScopes, instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, }, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::SignatureBuiltinRunner, - vm_memory::memory::Memory, - }, + vm::runners::builtin_runner::SignatureBuiltinRunner, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs b/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs index 8aa830422a..53925a1678 100644 --- a/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs +++ b/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs @@ -299,8 +299,7 @@ pub fn squash_dict( #[cfg(test)] mod tests { use super::*; - use crate::types::relocatable::MaybeRelocatable; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -311,10 +310,7 @@ mod tests { }, types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; use felt::felt_str; diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 439b92fec5..067b643986 100644 --- a/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -72,6 +72,23 @@ impl<'a> Uint256<'a> { Ok(()) } + + pub(crate) fn pack(self) -> BigUint { + (self.high.to_biguint() << 128) + self.low.to_biguint() + } + + pub(crate) fn split(num: &BigUint) -> Self { + let mask_low: BigUint = u128::MAX.into(); + let low = Felt252::from(num & mask_low); + let high = Felt252::from(num >> 128); + Self::from_values(low, high) + } +} + +impl<'a> From<&BigUint> for Uint256<'a> { + fn from(value: &BigUint) -> Self { + Self::split(value) + } } impl<'a> From for Uint256<'a> { @@ -82,17 +99,6 @@ impl<'a> From for Uint256<'a> { } } -pub(crate) fn pack(num: Uint256) -> BigUint { - (num.high.to_biguint() << 128) + num.low.to_biguint() -} - -pub(crate) fn split(num: &BigUint) -> [Felt252; 2] { - let mask_low: BigUint = u128::MAX.into(); - let low = Felt252::from(num & mask_low); - let high = Felt252::from(num >> 128); - [low, high] -} - /* Implements hint: %{ @@ -182,11 +188,8 @@ pub fn uint256_sub( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let ids_a = Uint256::from_var_name("a", vm, ids_data, ap_tracking)?; - let ids_b = Uint256::from_var_name("b", vm, ids_data, ap_tracking)?; - - let a = pack(ids_a); - let b = pack(ids_b); + let a = Uint256::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); + let b = Uint256::from_var_name("b", vm, ids_data, ap_tracking)?.pack(); // Main logic: // res = (a - b)%2**256 @@ -197,13 +200,9 @@ pub fn uint256_sub( ((BigUint::one() << 256) - b) + a }; - let [low, high] = split(&res); - - let res_addr = get_relocatable_from_var_name("res", vm, ids_data, ap_tracking)?; + let res = Uint256::split(&res); - vm.insert_value(res_addr, low)?; - vm.insert_value((res_addr + 1)?, high)?; - Ok(()) + res.insert_from_var_name("res", vm, ids_data, ap_tracking) } /* @@ -247,8 +246,7 @@ pub fn uint256_sqrt( ap_tracking: &ApTracking, only_low: bool, ) -> Result<(), HintError> { - let n = Uint256::from_var_name("n", vm, ids_data, ap_tracking)?; - let n = pack(n); + let n = Uint256::from_var_name("n", vm, ids_data, ap_tracking)?.pack(); // Main logic // from starkware.python.math_utils import isqrt @@ -480,12 +478,7 @@ mod tests { relocatable::{MaybeRelocatable, Relocatable}, }, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, - runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, - vm_memory::{memory::Memory, memory_segments::MemorySegmentManager}, - }, + vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; use assert_matches::assert_matches; use felt::felt_str; diff --git a/src/hint_processor/builtin_hint_processor/uint384.rs b/src/hint_processor/builtin_hint_processor/uint384.rs index 3fd08af63c..d20c1362c5 100644 --- a/src/hint_processor/builtin_hint_processor/uint384.rs +++ b/src/hint_processor/builtin_hint_processor/uint384.rs @@ -2,7 +2,7 @@ use core::ops::Shl; use felt::Felt252; use num_bigint::BigUint; use num_integer::Integer; -use num_traits::{One, Zero}; +use num_traits::Zero; use crate::math_utils::isqrt; use crate::stdlib::{borrow::Cow, collections::HashMap, prelude::*}; @@ -18,7 +18,7 @@ use super::hint_utils::{ get_integer_from_var_name, get_relocatable_from_var_name, insert_value_from_var_name, insert_value_into_ap, }; -use super::secp::bigint_utils::BigInt3; +use super::secp::bigint_utils::Uint384; // Notes: Hints in this lib use the type Uint384, which is equal to common lib's BigInt3 /* Reduced version of Uint384_expand @@ -60,36 +60,18 @@ impl Uint384ExpandReduced<'_> { let base_addr = get_relocatable_from_var_name(name, vm, ids_data, ap_tracking)?; Uint384ExpandReduced::from_base_addr(base_addr, name, vm) } -} - -pub(crate) fn split(num: &BigUint, num_bits_shift: u32) -> [BigUint; T] { - let mut num = num.clone(); - [0; T].map(|_| { - let a = &num & &((BigUint::one() << num_bits_shift) - 1_u32); - num = &num >> num_bits_shift; - a - }) -} -pub(crate) fn pack(num: BigInt3, num_bits_shift: usize) -> BigUint { - let limbs = [num.d0, num.d1, num.d2]; - #[allow(deprecated)] - limbs - .into_iter() - .enumerate() - .map(|(idx, value)| value.to_biguint().shl(idx * num_bits_shift)) - .sum() + fn pack(self) -> BigUint { + let limbs = [self.b01, self.b23, self.b45]; + #[allow(deprecated)] + limbs + .into_iter() + .enumerate() + .map(|(idx, value)| value.to_biguint().shl(idx * 128)) + .sum() + } } -fn pack2(num: Uint384ExpandReduced, num_bits_shift: usize) -> BigUint { - let limbs = [num.b01, num.b23, num.b45]; - #[allow(deprecated)] - limbs - .into_iter() - .enumerate() - .map(|(idx, value)| value.to_biguint().shl(idx * num_bits_shift)) - .sum() -} /* Implements Hint: %{ def split(num: int, num_bits_shift: int, length: int): @@ -125,26 +107,19 @@ pub fn uint384_unsigned_div_rem( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?, 128); - let div = pack( - BigInt3::from_var_name("div", vm, ids_data, ap_tracking)?, - 128, - ); - let quotient_addr = get_relocatable_from_var_name("quotient", vm, ids_data, ap_tracking)?; - let remainder_addr = get_relocatable_from_var_name("remainder", vm, ids_data, ap_tracking)?; + let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); + let div = Uint384::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); + if div.is_zero() { return Err(MathError::DividedByZero.into()); } let (quotient, remainder) = a.div_mod_floor(&div); - let quotient_split = split::<3>("ient, 128); - for (i, quotient_split) in quotient_split.iter().enumerate() { - vm.insert_value((quotient_addr + i)?, Felt252::from(quotient_split))?; - } - let remainder_split = split::<3>(&remainder, 128); - for (i, remainder_split) in remainder_split.iter().enumerate() { - vm.insert_value((remainder_addr + i)?, Felt252::from(remainder_split))?; - } - Ok(()) + + let quotient_split = Uint384::split("ient); + quotient_split.insert_from_var_name("quotient", vm, ids_data, ap_tracking)?; + + let remainder_split = Uint384::split(&remainder); + remainder_split.insert_from_var_name("remainder", vm, ids_data, ap_tracking) } /* Implements Hint: @@ -185,8 +160,8 @@ pub fn add_no_uint384_check( ap_tracking: &ApTracking, constants: &HashMap, ) -> Result<(), HintError> { - let a = BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?; - let b = BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?; + let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?; + let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)?; // This hint is not from the cairo commonlib, and its lib can be found under different paths, so we cant rely on a full path name let shift = constants .iter() @@ -245,26 +220,19 @@ pub fn uint384_unsigned_div_rem_expanded( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?, 128); - let div = pack2( - Uint384ExpandReduced::from_var_name("div", vm, ids_data, ap_tracking)?, - 128, - ); - let quotient_addr = get_relocatable_from_var_name("quotient", vm, ids_data, ap_tracking)?; - let remainder_addr = get_relocatable_from_var_name("remainder", vm, ids_data, ap_tracking)?; + let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); + let div = Uint384ExpandReduced::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); + if div.is_zero() { return Err(MathError::DividedByZero.into()); } let (quotient, remainder) = a.div_mod_floor(&div); - let quotient_split = split::<3>("ient, 128); - for (i, quotient_split) in quotient_split.iter().enumerate() { - vm.insert_value((quotient_addr + i)?, Felt252::from(quotient_split))?; - } - let remainder_split = split::<3>(&remainder, 128); - for (i, remainder_split) in remainder_split.iter().enumerate() { - vm.insert_value((remainder_addr + i)?, Felt252::from(remainder_split))?; - } - Ok(()) + + let quotient_split = Uint384::split("ient); + quotient_split.insert_from_var_name("quotient", vm, ids_data, ap_tracking)?; + + let remainder_split = Uint384::split(&remainder); + remainder_split.insert_from_var_name("remainder", vm, ids_data, ap_tracking) } /* Implements Hint @@ -296,19 +264,17 @@ pub fn uint384_sqrt( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?, 128); - let root_addr = get_relocatable_from_var_name("root", vm, ids_data, ap_tracking)?; + let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); + let root = isqrt(&a)?; + if root.is_zero() || root.bits() > 192 { return Err(HintError::AssertionFailed(String::from( "assert 0 <= root < 2 ** 192", ))); } - let root_split = split::<3>(&root, 128); - for (i, root_split) in root_split.iter().enumerate() { - vm.insert_value((root_addr + i)?, Felt252::from(root_split))?; - } - Ok(()) + let root_split = Uint384::split(&root); + root_split.insert_from_var_name("root", vm, ids_data, ap_tracking) } /* Implements Hint: @@ -331,7 +297,7 @@ pub fn uint384_signed_nn( mod tests { use super::*; use crate::hint_processor::builtin_hint_processor::hint_code; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -345,14 +311,12 @@ mod tests { relocatable::{MaybeRelocatable, Relocatable}, }, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; use assert_matches::assert_matches; use felt::felt_str; + use num_traits::One; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; diff --git a/src/hint_processor/builtin_hint_processor/uint384_extension.rs b/src/hint_processor/builtin_hint_processor/uint384_extension.rs index 083ed8bf72..318ce1a532 100644 --- a/src/hint_processor/builtin_hint_processor/uint384_extension.rs +++ b/src/hint_processor/builtin_hint_processor/uint384_extension.rs @@ -1,7 +1,5 @@ -use core::ops::Shl; - -use super::secp::bigint_utils::BigInt3; -use super::uint384::{pack, split}; +use super::secp::bigint_utils::Uint384; +use super::uint_utils::{pack, split}; use crate::stdlib::{borrow::Cow, collections::HashMap, prelude::*}; use crate::types::errors::math_errors::MathError; use crate::{ @@ -65,16 +63,47 @@ impl Uint768<'_> { let base_addr = get_relocatable_from_var_name(name, vm, ids_data, ap_tracking)?; Uint768::from_base_addr(base_addr, name, vm) } -} -fn pack_extended(num: Uint768, num_bits_shift: usize) -> BigUint { - let limbs = [num.d0, num.d1, num.d2, num.d3, num.d4, num.d5]; - #[allow(deprecated)] - limbs - .into_iter() - .enumerate() - .map(|(idx, value)| value.to_biguint().shl(idx * num_bits_shift)) - .sum() + pub(crate) fn from_values(limbs: [Felt252; 6]) -> Self { + let [d0, d1, d2, d3, d4, d5] = limbs; + Self { + d0: Cow::Owned(d0), + d1: Cow::Owned(d1), + d2: Cow::Owned(d2), + d3: Cow::Owned(d3), + d4: Cow::Owned(d4), + d5: Cow::Owned(d5), + } + } + + pub(crate) fn insert_from_var_name( + self, + var_name: &str, + vm: &mut VirtualMachine, + ids_data: &HashMap, + ap_tracking: &ApTracking, + ) -> Result<(), HintError> { + let addr = get_relocatable_from_var_name(var_name, vm, ids_data, ap_tracking)?; + + vm.insert_value(addr, self.d0.into_owned())?; + vm.insert_value((addr + 1)?, self.d1.into_owned())?; + vm.insert_value((addr + 2)?, self.d2.into_owned())?; + vm.insert_value((addr + 3)?, self.d3.into_owned())?; + vm.insert_value((addr + 4)?, self.d4.into_owned())?; + vm.insert_value((addr + 5)?, self.d5.into_owned())?; + + Ok(()) + } + + pub(crate) fn pack(self) -> BigUint { + let limbs = [self.d0, self.d1, self.d2, self.d3, self.d4, self.d5]; + pack(limbs, 128) + } + + pub(crate) fn split(num: &BigUint) -> Self { + let limbs = split(num, 128); + Self::from_values(limbs) + } } /* Implements Hint: @@ -119,27 +148,17 @@ pub fn unsigned_div_rem_uint768_by_uint384( ids_data: &HashMap, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let a = pack_extended(Uint768::from_var_name("a", vm, ids_data, ap_tracking)?, 128); - let div = pack( - BigInt3::from_var_name("div", vm, ids_data, ap_tracking)?, - 128, - ); - let quotient_addr = get_relocatable_from_var_name("quotient", vm, ids_data, ap_tracking)?; - let remainder_addr = get_relocatable_from_var_name("remainder", vm, ids_data, ap_tracking)?; + let a = Uint768::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); + let div = Uint384::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); + if div.is_zero() { return Err(MathError::DividedByZero.into()); } let (quotient, remainder) = a.div_mod_floor(&div); - let quotient_split = split::<6>("ient, 128); - for (i, quotient_split) in quotient_split.iter().enumerate() { - vm.insert_value((quotient_addr + i)?, Felt252::from(quotient_split))?; - } - let remainder_split = split::<3>(&remainder, 128); - for (i, remainder_split) in remainder_split.iter().enumerate() { - vm.insert_value((remainder_addr + i)?, Felt252::from(remainder_split))?; - } - - Ok(()) + let quotient_split = Uint768::split("ient); + quotient_split.insert_from_var_name("quotient", vm, ids_data, ap_tracking)?; + let remainder_split = Uint384::split(&remainder); + remainder_split.insert_from_var_name("remainder", vm, ids_data, ap_tracking) } #[cfg(test)] @@ -151,12 +170,9 @@ mod tests { use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::hint_processor_definition::HintProcessor; use crate::types::exec_scope::ExecutionScopes; - use crate::types::relocatable::MaybeRelocatable; + use crate::utils::test_utils::*; - use crate::vm::errors::memory_errors::MemoryError; - use crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use assert_matches::assert_matches; use felt::felt_str; diff --git a/src/hint_processor/builtin_hint_processor/uint512_utils.rs b/src/hint_processor/builtin_hint_processor/uint512_utils.rs new file mode 100644 index 0000000000..b6f2a604f4 --- /dev/null +++ b/src/hint_processor/builtin_hint_processor/uint512_utils.rs @@ -0,0 +1,95 @@ +use crate::{ + hint_processor::hint_processor_definition::HintReference, + serde::deserialize_program::ApTracking, + stdlib::{borrow::Cow, collections::HashMap, prelude::*}, + types::relocatable::Relocatable, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, +}; +use felt::Felt252; +use num_bigint::BigUint; + +use super::{ + hint_utils::get_relocatable_from_var_name, + uint_utils::{pack, split}, +}; + +pub(crate) struct Uint512<'a> { + pub d0: Cow<'a, Felt252>, + pub d1: Cow<'a, Felt252>, + pub d2: Cow<'a, Felt252>, + pub d3: Cow<'a, Felt252>, +} + +impl<'a> Uint512<'a> { + pub(crate) fn from_base_addr( + addr: Relocatable, + name: &str, + vm: &'a VirtualMachine, + ) -> Result { + Ok(Self { + d0: vm.get_integer(addr).map_err(|_| { + HintError::IdentifierHasNoMember(name.to_string(), "d0".to_string()) + })?, + d1: vm.get_integer((addr + 1)?).map_err(|_| { + HintError::IdentifierHasNoMember(name.to_string(), "d1".to_string()) + })?, + d2: vm.get_integer((addr + 2)?).map_err(|_| { + HintError::IdentifierHasNoMember(name.to_string(), "d2".to_string()) + })?, + d3: vm.get_integer((addr + 3)?).map_err(|_| { + HintError::IdentifierHasNoMember(name.to_string(), "d3".to_string()) + })?, + }) + } + + pub(crate) fn from_var_name( + name: &str, + vm: &'a VirtualMachine, + ids_data: &HashMap, + ap_tracking: &ApTracking, + ) -> Result { + let base_addr = get_relocatable_from_var_name(name, vm, ids_data, ap_tracking)?; + Self::from_base_addr(base_addr, name, vm) + } + + pub(crate) fn from_values(limbs: [Felt252; 4]) -> Self { + let [d0, d1, d2, d3] = limbs; + let d0 = Cow::Owned(d0); + let d1 = Cow::Owned(d1); + let d2 = Cow::Owned(d2); + let d3 = Cow::Owned(d3); + Self { d0, d1, d2, d3 } + } + + pub(crate) fn insert_from_var_name( + self, + var_name: &str, + vm: &mut VirtualMachine, + ids_data: &HashMap, + ap_tracking: &ApTracking, + ) -> Result<(), HintError> { + let addr = get_relocatable_from_var_name(var_name, vm, ids_data, ap_tracking)?; + + [self.d0, self.d1, self.d2, self.d3] + .into_iter() + .enumerate() + .try_for_each(|(i, limb)| vm.insert_value((addr + i)?, limb.into_owned()))?; + + Ok(()) + } + + pub(crate) fn pack(self) -> BigUint { + pack([self.d0, self.d1, self.d2, self.d3], 128) + } + + pub(crate) fn split(num: &'a BigUint) -> Uint512 { + let limbs = split(num, 128); + Self::from_values(limbs) + } +} + +impl<'a> From<&'a BigUint> for Uint512<'a> { + fn from(value: &'a BigUint) -> Self { + Self::split(value) + } +} diff --git a/src/hint_processor/builtin_hint_processor/uint_utils.rs b/src/hint_processor/builtin_hint_processor/uint_utils.rs new file mode 100644 index 0000000000..fbda93dd03 --- /dev/null +++ b/src/hint_processor/builtin_hint_processor/uint_utils.rs @@ -0,0 +1,24 @@ +use felt::Felt252; +use num_bigint::BigUint; +use num_traits::One; + +pub(crate) fn split(num: &BigUint, num_bits_shift: u32) -> [Felt252; T] { + let mut num = num.clone(); + let bitmask = &((BigUint::one() << num_bits_shift) - 1_u32); + [0; T].map(|_| { + let a = &num & bitmask; + num >>= num_bits_shift; + Felt252::from(a) + }) +} + +pub(crate) fn pack( + limbs: [impl AsRef; T], + num_bits_shift: usize, +) -> BigUint { + limbs + .into_iter() + .enumerate() + .map(|(i, limb)| limb.as_ref().to_biguint() << (i * num_bits_shift)) + .sum() +} diff --git a/src/hint_processor/builtin_hint_processor/usort.rs b/src/hint_processor/builtin_hint_processor/usort.rs index 5ee4640517..e22aebba0c 100644 --- a/src/hint_processor/builtin_hint_processor/usort.rs +++ b/src/hint_processor/builtin_hint_processor/usort.rs @@ -136,7 +136,7 @@ pub fn verify_multiplicity_body( #[cfg(test)] mod tests { use super::*; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ any_box, hint_processor::{ @@ -146,12 +146,9 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::exec_scope::ExecutionScopes, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/src/hint_processor/builtin_hint_processor/vrf/fq.rs b/src/hint_processor/builtin_hint_processor/vrf/fq.rs new file mode 100644 index 0000000000..483c3bf1b7 --- /dev/null +++ b/src/hint_processor/builtin_hint_processor/vrf/fq.rs @@ -0,0 +1,110 @@ +//! Fq stands for "a finite field of q elements" + +use crate::{ + hint_processor::builtin_hint_processor::{uint256_utils::Uint256, uint512_utils::Uint512}, + hint_processor::hint_processor_definition::HintReference, + serde::deserialize_program::ApTracking, + stdlib::{collections::HashMap, prelude::*}, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, +}; +use num_integer::div_rem; + +/// Implements hint: +/// ```python +/// def split(num: int, num_bits_shift: int, length: int): +/// a = [] +/// for _ in range(length): +/// a.append( num & ((1 << num_bits_shift) - 1) ) +/// num = num >> num_bits_shift +/// return tuple(a) +/// +/// 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)) +/// +/// def pack_extended(z, num_bits_shift: int) -> int: +/// limbs = (z.d0, z.d1, z.d2, z.d3) +/// return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) +/// +/// x = pack_extended(ids.x, num_bits_shift = 128) +/// div = pack(ids.div, num_bits_shift = 128) +/// +/// quotient, remainder = divmod(x, div) +/// +/// quotient_split = split(quotient, num_bits_shift=128, length=4) +/// +/// ids.quotient.d0 = quotient_split[0] +/// ids.quotient.d1 = quotient_split[1] +/// ids.quotient.d2 = quotient_split[2] +/// ids.quotient.d3 = quotient_split[3] +/// +/// remainder_split = split(remainder, num_bits_shift=128, length=2) +/// ids.remainder.low = remainder_split[0] +/// ids.remainder.high = remainder_split[1] +/// ``` +pub fn uint512_unsigned_div_rem( + vm: &mut VirtualMachine, + ids_data: &HashMap, + ap_tracking: &ApTracking, +) -> Result<(), HintError> { + let x = Uint512::from_var_name("x", vm, ids_data, ap_tracking)?.pack(); + let div = Uint256::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); + + // Main logic: + // quotient, remainder = divmod(x, div) + let (quotient, remainder) = div_rem(x, div); + + Uint512::from("ient).insert_from_var_name("quotient", vm, ids_data, ap_tracking)?; + Uint256::from(&remainder).insert_from_var_name("remainder", vm, ids_data, ap_tracking) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::any_box; + use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; + 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::HintProcessor; + use crate::types::exec_scope::ExecutionScopes; + use crate::utils::test_utils::*; + use assert_matches::assert_matches; + + #[cfg(target_arch = "wasm32")] + use wasm_bindgen_test::*; + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn test_uint512_unsigned_div_rem_ok() { + let hint_code = hint_code::UINT512_UNSIGNED_DIV_REM; + let mut vm = vm_with_range_check!(); + + vm.segments = segments![ + ((1, 0), 2363463), + ((1, 1), 566795), + ((1, 2), 8760799), + ((1, 3), 62362634), + ((1, 4), 8340843), + ((1, 5), 124152) + ]; + // Create hint_data + let ids_data = + non_continuous_ids_data![("x", 0), ("div", 4), ("quotient", 6), ("remainder", 10)]; + assert_matches!( + run_hint!(vm, ids_data, hint_code, exec_scopes_ref!()), + Ok(()) + ); + //Check hint memory inserts + check_memory![ + vm.segments.memory, + // quotient + ((1, 6), 158847186690949537631480225217589612243), + ((1, 7), 105056890940778813909974456334651647691), + ((1, 8), 502), + ((1, 9), 0), + // remainder + ((1, 10), ("235556430256711128858231095164527378198", 10)), + ((1, 11), 83573), + ]; + } +} diff --git a/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs b/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs index 8376620411..cd327e53f9 100644 --- a/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs +++ b/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs @@ -96,15 +96,11 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::hint_processor_definition::HintProcessor; - use crate::types::relocatable::{MaybeRelocatable, Relocatable}; + use crate::types::relocatable::Relocatable; use crate::utils::test_utils::mayberelocatable; use crate::utils::test_utils::memory; use crate::utils::test_utils::memory_from_memory; use crate::utils::test_utils::memory_inner; - use crate::vm::errors::memory_errors::MemoryError; - use crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use crate::{ hint_processor::builtin_hint_processor::hint_code::INV_MOD_P_UINT512, types::exec_scope::ExecutionScopes, diff --git a/src/hint_processor/builtin_hint_processor/vrf/mod.rs b/src/hint_processor/builtin_hint_processor/vrf/mod.rs index a176c078b4..fbf90b7f9e 100644 --- a/src/hint_processor/builtin_hint_processor/vrf/mod.rs +++ b/src/hint_processor/builtin_hint_processor/vrf/mod.rs @@ -1 +1,2 @@ +pub mod fq; pub mod inv_mod_p_uint512; diff --git a/src/hint_processor/hint_processor_utils.rs b/src/hint_processor/hint_processor_utils.rs index 1df0a373e0..283fb21fca 100644 --- a/src/hint_processor/hint_processor_utils.rs +++ b/src/hint_processor/hint_processor_utils.rs @@ -179,13 +179,11 @@ fn get_offset_value_reference( mod tests { use super::*; use crate::stdlib::collections::HashMap; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ relocatable, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::{vm_core::VirtualMachine, vm_memory::memory::Memory}, }; use assert_matches::assert_matches; diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index a118bf7587..cf73276b76 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -697,14 +697,14 @@ fn keccak_alternative_hint() { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn uint384() { - let program_data = include_bytes!("../../cairo_programs/uint384.json"); + let program_data = include_bytes!("../../cairo_programs/uint384_test.json"); run_program_simple_with_memory_holes(program_data.as_slice(), 74); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn uint384_extension() { - let program_data = include_bytes!("../../cairo_programs/uint384_extension.json"); + let program_data = include_bytes!("../../cairo_programs/uint384_extension_test.json"); run_program_simple_with_memory_holes(program_data.as_slice(), 20); } @@ -819,3 +819,10 @@ fn cairo_run_inv_mod_p_uint512() { let program_data = include_bytes!("../../cairo_programs/inv_mod_p_uint512.json"); run_program_simple(program_data.as_slice()); } + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn cairo_run_fq_test() { + let program_data = include_bytes!("../../cairo_programs/fq_test.json"); + run_program_simple_with_memory_holes(program_data.as_slice(), 42); +} diff --git a/src/utils.rs b/src/utils.rs index 7021f866ae..777add36f9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -102,7 +102,7 @@ pub mod test_utils { ($( (($si:expr, $off:expr), $val:tt) ),* $(,)? ) => { { let memory = memory!($( (($si, $off), $val) ),*); - MemorySegmentManager { + $crate::vm::vm_memory::memory_segments::MemorySegmentManager { memory, segment_sizes: HashMap::new(), segment_used_sizes: None, @@ -118,7 +118,7 @@ pub mod test_utils { macro_rules! memory { ( $( (($si:expr, $off:expr), $val:tt) ),* ) => { { - let mut memory = Memory::new(); + let mut memory = $crate::vm::vm_memory::memory::Memory::new(); memory_from_memory!(memory, ( $( (($si, $off), $val) ),* )); memory } @@ -141,7 +141,10 @@ pub mod test_utils { ($mem:expr, ($si:expr, $off:expr), ($sival:expr, $offval: expr)) => { let (k, v) = (($si, $off).into(), &mayberelocatable!($sival, $offval)); let mut res = $mem.insert(k, v); - while matches!(res, Err(MemoryError::UnallocatedSegment(_, _))) { + while matches!( + res, + Err($crate::vm::errors::memory_errors::MemoryError::UnallocatedSegment(_, _)) + ) { if $si < 0 { $mem.temp_data.push($crate::stdlib::vec::Vec::new()) } else { @@ -153,7 +156,10 @@ pub mod test_utils { ($mem:expr, ($si:expr, $off:expr), $val:expr) => { let (k, v) = (($si, $off).into(), &mayberelocatable!($val)); let mut res = $mem.insert(k, v); - while matches!(res, Err(MemoryError::UnallocatedSegment(_, _))) { + while matches!( + res, + Err($crate::vm::errors::memory_errors::MemoryError::UnallocatedSegment(_, _)) + ) { if $si < 0 { $mem.temp_data.push($crate::stdlib::vec::Vec::new()) } else { @@ -192,10 +198,10 @@ pub mod test_utils { macro_rules! mayberelocatable { ($val1 : expr, $val2 : expr) => { - MaybeRelocatable::from(($val1, $val2)) + $crate::types::relocatable::MaybeRelocatable::from(($val1, $val2)) }; ($val1 : expr) => { - MaybeRelocatable::from(felt::Felt252::new($val1 as i128)) + $crate::types::relocatable::MaybeRelocatable::from(felt::Felt252::new($val1 as i128)) }; } pub(crate) use mayberelocatable; @@ -214,7 +220,10 @@ pub mod test_utils { macro_rules! vm_with_range_check { () => {{ let mut vm = VirtualMachine::new(false); - vm.builtin_runners = vec![RangeCheckBuiltinRunner::new(Some(8), 8, true).into()]; + vm.builtin_runners = vec![ + $crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner::new(Some(8), 8, true) + .into(), + ]; vm }}; } @@ -555,10 +564,7 @@ mod test { serde::deserialize_program::{BuiltinName, ReferenceManager}, types::{exec_scope::ExecutionScopes, program::Program, relocatable::MaybeRelocatable}, utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, trace::trace_entry::TraceEntry, - vm_core::VirtualMachine, vm_memory::memory::Memory, - }, + vm::{trace::trace_entry::TraceEntry, vm_core::VirtualMachine, vm_memory::memory::Memory}, }; use felt::Felt252; use num_traits::One; diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index ca5f2fa6e3..764fc66164 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -190,7 +190,6 @@ mod tests { use crate::vm::errors::memory_errors::MemoryError; use crate::vm::runners::builtin_runner::BuiltinRunner; use crate::vm::vm_core::VirtualMachine; - use crate::vm::vm_memory::memory::Memory; use crate::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, types::program::Program, utils::test_utils::*, vm::runners::cairo_runner::CairoRunner, diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index b90e1cf894..aa5099b627 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -309,7 +309,7 @@ mod tests { use crate::vm::errors::cairo_run_errors::CairoRunError; use crate::vm::errors::vm_errors::VirtualMachineError; use crate::vm::runners::cairo_runner::CairoRunner; - use crate::vm::vm_memory::memory::Memory; + use crate::vm::{ errors::{memory_errors::MemoryError, runner_errors::RunnerError}, runners::builtin_runner::BuiltinRunner, diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 639a9568e3..ec9e24cd8f 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -189,7 +189,7 @@ mod tests { use crate::types::program::Program; use crate::utils::test_utils::*; use crate::vm::runners::cairo_runner::CairoRunner; - use crate::vm::vm_memory::memory::Memory; + use crate::vm::{ errors::memory_errors::MemoryError, runners::builtin_runner::BuiltinRunner, vm_core::VirtualMachine, diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 213f0be5a0..01c883c569 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -240,7 +240,7 @@ mod tests { use crate::types::program::Program; use crate::utils::test_utils::*; use crate::vm::runners::cairo_runner::CairoRunner; - use crate::vm::vm_memory::memory::Memory; + use crate::vm::{ errors::{memory_errors::MemoryError, runner_errors::RunnerError}, runners::builtin_runner::BuiltinRunner, diff --git a/src/vm/runners/builtin_runner/output.rs b/src/vm/runners/builtin_runner/output.rs index 5778ab02d7..805fc65c5a 100644 --- a/src/vm/runners/builtin_runner/output.rs +++ b/src/vm/runners/builtin_runner/output.rs @@ -120,7 +120,7 @@ mod tests { use super::*; use crate::relocatable; use crate::stdlib::collections::HashMap; - use crate::vm::vm_memory::memory::Memory; + use crate::{ utils::test_utils::*, vm::{ diff --git a/src/vm/runners/builtin_runner/poseidon.rs b/src/vm/runners/builtin_runner/poseidon.rs index 79a09e19d4..b37e617edf 100644 --- a/src/vm/runners/builtin_runner/poseidon.rs +++ b/src/vm/runners/builtin_runner/poseidon.rs @@ -173,11 +173,8 @@ mod tests { use crate::types::program::Program; use crate::utils::test_utils::*; use crate::vm::runners::cairo_runner::CairoRunner; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::BuiltinRunner, - vm_core::VirtualMachine, - }; + + use crate::vm::{runners::builtin_runner::BuiltinRunner, vm_core::VirtualMachine}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index d040a3e8f4..1f031354e8 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -1170,14 +1170,14 @@ mod tests { SEGMENT_ARENA_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, }; use crate::vm::vm_memory::memory::MemoryCell; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, relocatable, serde::deserialize_program::{Identifier, ReferenceManager}, types::instance_definitions::bitwise_instance_def::BitwiseInstanceDef, utils::test_utils::*, - vm::{trace::trace_entry::TraceEntry, vm_memory::memory::Memory}, + vm::trace::trace_entry::TraceEntry, }; use assert_matches::assert_matches; use felt::felt_str; diff --git a/src/vm/security.rs b/src/vm/security.rs index 29b38371e6..64655a074c 100644 --- a/src/vm/security.rs +++ b/src/vm/security.rs @@ -88,11 +88,9 @@ mod test { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; use crate::serde::deserialize_program::BuiltinName; use crate::stdlib::collections::HashMap; - use crate::types::relocatable::MaybeRelocatable; + use crate::types::relocatable::Relocatable; - use crate::vm::errors::memory_errors::MemoryError; - use crate::vm::vm_memory::memory::Memory; - use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{relocatable, types::program::Program, utils::test_utils::*}; use assert_matches::assert_matches; use felt::Felt252; diff --git a/src/vm/trace/mod.rs b/src/vm/trace/mod.rs index 142e7661ac..c10b521ac9 100644 --- a/src/vm/trace/mod.rs +++ b/src/vm/trace/mod.rs @@ -52,7 +52,7 @@ pub fn get_perm_range_check_limits( #[cfg(test)] mod test { use super::*; - use crate::{utils::test_utils::*, vm::errors::memory_errors::MemoryError}; + use crate::utils::test_utils::*; use assert_matches::assert_matches; #[cfg(target_arch = "wasm32")]