Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remaining uint256 hints #313

Draft
wants to merge 37 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1b85c7d
draft uint256 add
toni-calvin Sep 21, 2023
a7b78c0
implement hint uint256_add
toni-calvin Sep 21, 2023
6c00a5f
add tests
toni-calvin Sep 21, 2023
b968f3d
add hint uint256_add_low
toni-calvin Sep 21, 2023
4717989
implement split_64 hint
toni-calvin Sep 21, 2023
60b009a
change location of uint256 struct
toni-calvin Sep 21, 2023
bf6e11e
implement auxiliar methods to ids manager to insert u256 structs
toni-calvin Sep 21, 2023
9ee1dc0
implement uint256sqrt hint
toni-calvin Sep 21, 2023
f8ddddd
add unit test sqrt
toni-calvin Sep 21, 2023
444f61a
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into uint25…
toni-calvin Sep 22, 2023
dbd88bb
fix unit test
toni-calvin Sep 22, 2023
8cbd0b5
add unit tests uint256_sqrt
toni-calvin Sep 22, 2023
eda6e2e
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into uint25…
toni-calvin Sep 22, 2023
7494b44
implement hint uint256_signed_nn
toni-calvin Sep 26, 2023
6a550b7
add tests
toni-calvin Sep 26, 2023
ec08ea8
implement UINT256_UNSIGNED_DIV_REM hint
toni-calvin Sep 26, 2023
ea1a4a8
add tests
toni-calvin Sep 26, 2023
73c84c8
implement hint and test
toni-calvin Sep 26, 2023
e95ae5d
implement hint
toni-calvin Sep 27, 2023
c1cd05a
add test and improve commit
toni-calvin Sep 27, 2023
337f1b7
merge main
toni-calvin Sep 27, 2023
8ff45d8
fix test
toni-calvin Sep 27, 2023
e25b7b4
add integration tests
toni-calvin Sep 27, 2023
89d40bb
fix unit and integration tests
toni-calvin Sep 27, 2023
e9f51ee
remove prints
toni-calvin Sep 27, 2023
c597a0f
improve code·
toni-calvin Sep 27, 2023
5012e76
add uint256 utils
toni-calvin Sep 27, 2023
09dc5d2
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into uint25…
toni-calvin Sep 27, 2023
5c71c1f
add test
toni-calvin Sep 28, 2023
ef3080e
fix comments
toni-calvin Sep 28, 2023
c758392
improve ToString method on Uint256
toni-calvin Sep 29, 2023
97b5502
fix test
toni-calvin Sep 29, 2023
6fd3a7f
fix hint
toni-calvin Oct 3, 2023
5b37127
add extra test used to debug error
toni-calvin Oct 3, 2023
da80a29
add hint uint256_sub
toni-calvin Oct 3, 2023
0a7cc1e
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into uint25…
toni-calvin Oct 3, 2023
b3c532c
wip: remaining uint256 hints
toni-calvin Oct 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions cairo_programs/uint256.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
%builtins range_check

from starkware.cairo.common.uint256 import (
Uint256,
uint256_add,
split_64,
uint256_sqrt,
uint256_signed_nn,
uint256_unsigned_div_rem,
uint256_mul,
uint256_mul_div_mod
)
from starkware.cairo.common.alloc import alloc

func fill_array{range_check_ptr: felt}(
array: Uint256*, base: Uint256, step: Uint256, array_length: felt, iterator: felt
) {
if (iterator == array_length) {
return ();
}
let (res, carry_high) = uint256_add(step, base);
let (sqrt) = uint256_sqrt(res);

assert array[iterator] = sqrt;
return fill_array(array, base, array[iterator], array_length, iterator + 1);
}

func main{range_check_ptr: felt}() {
let x: Uint256 = Uint256(5, 2);
let y = Uint256(3, 7);
let (res, carry_high) = uint256_add(x, y);
assert res.low = 8;
assert res.high = 9;
assert carry_high = 0;

let (low, high) = split_64(850981239023189021389081239089023);
assert low = 7249717543555297151;
assert high = 46131785404667;

let (root) = uint256_sqrt(Uint256(17, 7));
assert root = Uint256(48805497317890012913, 0);

let (signed_nn) = uint256_signed_nn(Uint256(5, 2));
assert signed_nn = 1;
let (p) = uint256_signed_nn(Uint256(1, 170141183460469231731687303715884105728));
assert p = 0;
let (q) = uint256_signed_nn(Uint256(1, 170141183460469231731687303715884105727));
assert q = 1;

let (a_quotient, a_remainder) = uint256_unsigned_div_rem(Uint256(89, 72), Uint256(3, 7));
assert a_quotient = Uint256(10, 0);
assert a_remainder = Uint256(59, 2);

let (b_quotient, b_remainder) = uint256_unsigned_div_rem(
Uint256(-3618502788666131213697322783095070105282824848410658236509717448704103809099, 2),
Uint256(5, 2),
);
assert b_quotient = Uint256(1, 0);
assert b_remainder = Uint256(340282366920938463463374607431768211377, 0);

let (c_quotient, c_remainder) = uint256_unsigned_div_rem(
Uint256(340282366920938463463374607431768211455, 340282366920938463463374607431768211455),
Uint256(1, 0),
);

assert c_quotient = Uint256(340282366920938463463374607431768211455, 340282366920938463463374607431768211455);
assert c_remainder = Uint256(0, 0);

let (a_quotient_low, a_quotient_high, a_remainder) = uint256_mul_div_mod(
Uint256(89, 72),
Uint256(3, 7),
Uint256(107, 114),
);
assert a_quotient_low = Uint256(143276786071974089879315624181797141668, 4);
assert a_quotient_high = Uint256(0, 0);
assert a_remainder = Uint256(322372768661941702228460154409043568767, 101);

let (b_quotient_low, b_quotient_high, b_remainder) = uint256_mul_div_mod(
Uint256(-3618502788666131213697322783095070105282824848410658236509717448704103809099, 2),
Uint256(1, 1),
Uint256(5, 2),
);
assert b_quotient_low = Uint256(170141183460469231731687303715884105688, 1);
assert b_quotient_high = Uint256(0, 0);
assert b_remainder = Uint256(170141183460469231731687303715884105854, 1);

let (c_quotient_low, c_quotient_high, c_remainder) = uint256_mul_div_mod(
Uint256(340281070833283907490476236129005105807, 340282366920938463463374607431768211455),
Uint256(2447157533618445569039502, 0),
Uint256(0, 1),
);

assert c_quotient_low = Uint256(340282366920938463454053728725133866491, 2447157533618445569039501);
assert c_quotient_high = Uint256(0, 0);
assert c_remainder = Uint256(326588112914912836985603897252688572242, 0);

let (mult_low_a, mult_high_a) = uint256_mul(Uint256(59, 2), Uint256(10, 0));
assert mult_low_a = Uint256(590, 20);
assert mult_high_a = Uint256(0, 0);

let (mult_low_b: Uint256, mult_high_b: Uint256) = uint256_mul(
Uint256(271442546951262198976322048597925888860, 0),
Uint256(271442546951262198976322048597925888860, 0),
);
assert mult_low_b = Uint256(
42047520920204780886066537579778623760, 216529163594619381764978757921136443390
);
assert mult_high_b = Uint256(0, 0);

let array_length = 100;
let (sum_array: Uint256*) = alloc();
fill_array(sum_array, Uint256(57, 8), Uint256(17, 7), array_length, 0);

return ();
}
Loading
Loading