-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assert 250 Bits and SPLIT_FELT (#233)
* [WIP] Assert 250 Bits and SPLIT_FELT * Add tests for ASSERT_250_BIT * Implement SPLIT_FELT * Add tests for SPLIT_FELT * Incorporate the GetConst function * Skip checking for the common library path first * Remove redundant nil check
- Loading branch information
Showing
9 changed files
with
425 additions
and
3 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,31 @@ | ||
%builtins range_check | ||
|
||
from starkware.cairo.common.math import assert_250_bit | ||
from starkware.cairo.common.alloc import alloc | ||
|
||
func assert_250_bit_element_array{range_check_ptr: felt}( | ||
array: felt*, array_length: felt, iterator: felt | ||
) { | ||
if (iterator == array_length) { | ||
return (); | ||
} | ||
assert_250_bit(array[iterator]); | ||
return assert_250_bit_element_array(array, array_length, iterator + 1); | ||
} | ||
|
||
func fill_array(array: felt*, base: felt, step: felt, array_length: felt, iterator: felt) { | ||
if (iterator == array_length) { | ||
return (); | ||
} | ||
assert array[iterator] = base + step * iterator; | ||
return fill_array(array, base, step, array_length, iterator + 1); | ||
} | ||
|
||
func main{range_check_ptr: felt}() { | ||
alloc_locals; | ||
tempvar array_length = 10; | ||
let (array: felt*) = alloc(); | ||
fill_array(array, 70000000000000000000, 300000000000000000, array_length, 0); | ||
assert_250_bit_element_array(array, array_length, 0); | ||
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,42 @@ | ||
%builtins range_check | ||
|
||
from starkware.cairo.common.math import assert_le | ||
from starkware.cairo.common.math import split_felt | ||
|
||
func split_felt_manual_implemetation{range_check_ptr}(value) -> (high: felt, low: felt) { | ||
// Note: the following code works because PRIME - 1 is divisible by 2**128. | ||
const MAX_HIGH = (-1) / 2 ** 128; | ||
const MAX_LOW = 0; | ||
|
||
// Guess the low and high parts of the integer. | ||
let low = [range_check_ptr]; | ||
let high = [range_check_ptr + 1]; | ||
let range_check_ptr = range_check_ptr + 2; | ||
|
||
%{ | ||
from starkware.cairo.common.math_utils import assert_integer | ||
assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128 | ||
assert PRIME - 1 == ids.MAX_HIGH * 2**128 + ids.MAX_LOW | ||
assert_integer(ids.value) | ||
ids.low = ids.value & ((1 << 128) - 1) | ||
ids.high = ids.value >> 128 | ||
%} | ||
|
||
assert value = high * (2 ** 128) + low; | ||
if (high == MAX_HIGH) { | ||
assert_le(low, MAX_LOW); | ||
} else { | ||
assert_le(high, MAX_HIGH - 1); | ||
} | ||
return (high=high, low=low); | ||
} | ||
|
||
func main{range_check_ptr: felt}() { | ||
let (m, n) = split_felt_manual_implemetation(5784800237655953878877368326340059594760); | ||
assert m = 17; | ||
assert n = 8; | ||
let (x, y) = split_felt(5784800237655953878877368326340059594760); | ||
assert x = 17; | ||
assert y = 8; | ||
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
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
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
Oops, something went wrong.