-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
795f46d
commit cedb7dc
Showing
6 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Splits a field element in the range [0, 2^192) to its low 64-bit and high 128-bit parts. | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256, split_64 | ||
|
||
func main{range_check_ptr}() { | ||
|
||
// Test one | ||
let (a, b) = split_64(8746); | ||
assert a = 8746; | ||
assert b = 0; | ||
|
||
// Test two | ||
let (c,d) = split_64(2**127); | ||
assert c = 0; | ||
assert d = 2 ** 63; | ||
|
||
return (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Adds two integers. Returns the result as a 256-bit integer and the (1-bit) carry. | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256, uint256_add | ||
|
||
func main{range_check_ptr}() { | ||
alloc_locals; | ||
|
||
// Test one | ||
local a: Uint256; | ||
a.low = 2**127; | ||
a.high = 0; | ||
local b: Uint256; | ||
b.low = 2**127; | ||
b.high = 0; | ||
local res_one: Uint256; | ||
let (res_one, carry_high_one) = uint256_add(a, b); | ||
assert res_one.low = 0; | ||
assert res_one.high = 1; | ||
assert carry_high_one = 0; | ||
|
||
// Test two | ||
local c: Uint256; | ||
c.low = 2**127; | ||
c.high = 2**127; | ||
local d: Uint256; | ||
d.low = 2**127; | ||
d.high = 2**127; | ||
local res_two: Uint256; | ||
let (res_two, carry_high_two) = uint256_add(c, d); | ||
assert res_two.low = 0; | ||
assert res_two.high = 1; | ||
assert carry_high_two = 1; | ||
|
||
// Test three | ||
local e: Uint256; | ||
e.low = 0; | ||
e.high = 2**127; | ||
local f: Uint256; | ||
f.low = 0; | ||
f.high = 2**127; | ||
local res_three: Uint256; | ||
let (res_three, carry_high_three) = uint256_add(e, f); | ||
assert res_three.low = 0; | ||
assert res_three.high = 0; | ||
assert carry_high_three = 1; | ||
|
||
// Test four | ||
local g: Uint256; | ||
g.low = 2**127; | ||
g.high = 0; | ||
local h: Uint256; | ||
h.low = 0; | ||
h.high = 2**127; | ||
local res_four: Uint256; | ||
let (res_four, carry_high_four) = uint256_add(g, h); | ||
assert res_four.low = 2**127; | ||
assert res_four.high = 2**127; | ||
assert carry_high_four = 0; | ||
|
||
// Test five | ||
local i: Uint256; | ||
i.low = 426942694269; | ||
i.high = 426942694269; | ||
local j: Uint256; | ||
j.low = 426942694269; | ||
j.high = 426942694269; | ||
local res_five: Uint256; | ||
let (res_five, carry_high_five) = uint256_add(i, j); | ||
assert res_five.low = 853885388538; | ||
assert res_five.high = 853885388538; | ||
assert carry_high_five = 0; | ||
|
||
return (); | ||
} |
56 changes: 56 additions & 0 deletions
56
integration_tests/cairo_files/uint256_mul_div_mod.small.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Computes: | ||
// 1. The integer division `(a * b) // div` (as a 512-bit number). | ||
// 2. The remainder `(a * b) modulo div`. | ||
// Assumption: div != 0. | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256, uint256_mul_div_mod | ||
|
||
func main{range_check_ptr}() { | ||
alloc_locals; | ||
|
||
// Test one | ||
local a_one: Uint256; | ||
a_one.low = 6; | ||
a_one.high = 0; | ||
local b_one: Uint256; | ||
b_one.low = 6; | ||
b_one.high = 0; | ||
local div_one: Uint256; | ||
div_one.low = 2; | ||
div_one.high = 0; | ||
local quotient_one_low: Uint256; | ||
local quotient_one_high: Uint256; | ||
local remainder_one: Uint256; | ||
let (quotient_one_low, quotient_one_high, remainder_one) = uint256_mul_div_mod(a_one, b_one, div_one); | ||
assert quotient_one_low.low = 18; | ||
assert quotient_one_low.high = 0; | ||
assert quotient_one_high.low = 0; | ||
assert quotient_one_high.high = 0; | ||
assert remainder_one.low = 0; | ||
assert remainder_one.high = 0; | ||
|
||
// Test two | ||
local a_two: Uint256; | ||
a_two.low = 0; | ||
a_two.high = 2; | ||
local b_two: Uint256; | ||
b_two.low = 0; | ||
b_two.high = 3; | ||
local div_two: Uint256; | ||
div_two.low = 0; | ||
div_two.high = 2; | ||
local quotient_two_low: Uint256; | ||
local quotient_two_high: Uint256; | ||
local remainder_two: Uint256; | ||
let (quotient_two_low, quotient_two_high, remainder_two) = uint256_mul_div_mod(a_two, b_two, div_two); | ||
assert quotient_two_low.low = 0; | ||
assert quotient_two_low.high = 3; | ||
assert quotient_two_high.low = 0; | ||
assert quotient_two_high.high = 0; | ||
assert remainder_two.low = 0; | ||
assert remainder_two.high = 0; | ||
|
||
return(); | ||
} |
25 changes: 25 additions & 0 deletions
25
integration_tests/cairo_files/uint256_signedNN.small.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Returns 1 if the signed integer is nonnegative. | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256, uint256_signed_nn | ||
|
||
func main{range_check_ptr}() { | ||
alloc_locals; | ||
|
||
// Test one | ||
local a: Uint256; | ||
a.low = 0; | ||
a.high = 2**127; | ||
uint256_signed_nn(a); | ||
[ap - 1] = 0; | ||
|
||
// Test two | ||
local b: Uint256; | ||
b.low = 0; | ||
b.high = 1; | ||
uint256_signed_nn(b); | ||
[ap - 1] = 1; | ||
|
||
return(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Returns the floor value of the square root of a uint256 integer. | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256, uint256_sqrt | ||
|
||
func main{range_check_ptr}() { | ||
alloc_locals; | ||
|
||
// Test one | ||
local a: Uint256; | ||
a.low = 0; | ||
a.high = 0; | ||
local sqrt_a: Uint256; | ||
let (sqrt_a) = uint256_sqrt(a); | ||
assert sqrt_a.low = 0; | ||
assert sqrt_a.high = 0; | ||
|
||
// Test two | ||
local b: Uint256; | ||
b.low = 5; | ||
b.high = 0; | ||
local sqrt_b: Uint256; | ||
let (sqrt_b) = uint256_sqrt(b); | ||
assert sqrt_b.low = 2; | ||
assert sqrt_b.high = 0; | ||
|
||
// Test three | ||
local c: Uint256; | ||
c.low = 65536; | ||
c.high = 0; | ||
local sqrt_c: Uint256; | ||
let (sqrt_c) = uint256_sqrt(c); | ||
assert sqrt_c.low = 256; | ||
assert sqrt_c.high = 0; | ||
|
||
// Test four | ||
local d: Uint256; | ||
d.low = 4294967296; | ||
d.high = 0; | ||
local sqrt_d: Uint256; | ||
let (sqrt_d) = uint256_sqrt(d); | ||
assert sqrt_d.low = 65536; | ||
assert sqrt_d.high = 0; | ||
|
||
// Test five | ||
local e: Uint256; | ||
e.low = 2**127; | ||
e.high = 2**127; | ||
local sqrt_e: Uint256; | ||
let (sqrt_e) = uint256_sqrt(e); | ||
assert sqrt_e.low = 240615969168004511545033772477625056927; | ||
assert sqrt_e.high = 0; | ||
|
||
return(); | ||
} |
57 changes: 57 additions & 0 deletions
57
integration_tests/cairo_files/uint256_unsigned_div_rem.small.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Unsigned integer division between two integers. Returns the quotient and the remainder. | ||
// Conforms to EVM specifications: division by 0 yields 0. | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256, uint256_unsigned_div_rem | ||
|
||
func main{range_check_ptr}() { | ||
alloc_locals; | ||
|
||
// Test one | ||
local a_one: Uint256; | ||
a_one.low = 6; | ||
a_one.high = 0; | ||
local div_one: Uint256; | ||
div_one.low = 2; | ||
div_one.high = 0; | ||
local quotient_one: Uint256; | ||
local remainder_one: Uint256; | ||
let (quotient_one, remainder_one) = uint256_unsigned_div_rem(a_one, div_one); | ||
assert quotient_one.low = 3; | ||
assert quotient_one.high = 0; | ||
assert remainder_one.low = 0; | ||
assert remainder_one.high = 0; | ||
|
||
// Test two | ||
local a_two: Uint256; | ||
a_two.low = 2**127; | ||
a_two.high = 0; | ||
local div_two: Uint256; | ||
div_two.low = 2**127; | ||
div_two.high = 0; | ||
local quotient_two: Uint256; | ||
local remainder_two: Uint256; | ||
let (quotient_two, remainder_two) = uint256_unsigned_div_rem(a_two, div_two); | ||
assert quotient_two.low = 1; | ||
assert quotient_two.high = 0; | ||
assert remainder_two.low = 0; | ||
assert remainder_two.high = 0; | ||
|
||
// Test three | ||
local a_three: Uint256; | ||
a_three.low = 5; | ||
a_three.high = 2**127; | ||
local div_three: Uint256; | ||
div_three.low = 0; | ||
div_three.high = 2**127; | ||
local quotient_three: Uint256; | ||
local remainder_three: Uint256; | ||
let (quotient_three, remainder_three) = uint256_unsigned_div_rem(a_three, div_three); | ||
assert quotient_three.low = 1; | ||
assert quotient_three.high = 0; | ||
assert remainder_three.low = 5; | ||
assert remainder_three.high = 0; | ||
|
||
return(); | ||
} |