-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into newhint32-is_zero_int
- Loading branch information
Showing
7 changed files
with
455 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
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,344 @@ | ||
%builtins range_check bitwise | ||
|
||
from starkware.cairo.common.uint256 import ( | ||
Uint256, | ||
uint256_reverse_endian, | ||
uint256_unsigned_div_rem, | ||
uint256_mul, | ||
uint256_add, | ||
uint256_pow2, | ||
) | ||
|
||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
from starkware.cairo.common.registers import get_label_location | ||
from starkware.cairo.common.math import unsigned_div_rem as felt_divmod, split_felt | ||
from starkware.cairo.common.math_cmp import is_le | ||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.pow import pow | ||
|
||
// y MUST be a power of 2 | ||
func bitwise_divmod{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(x: felt, y: felt) -> ( | ||
q: felt, r: felt | ||
) { | ||
assert bitwise_ptr.x = x; | ||
assert bitwise_ptr.y = y - 1; | ||
let x_and_y = bitwise_ptr.x_and_y; | ||
|
||
let bitwise_ptr = bitwise_ptr + BitwiseBuiltin.SIZE; | ||
return (q=(x - x_and_y) / y, r=x_and_y); | ||
} | ||
func felt_divmod_no_input_check{range_check_ptr}(value, div) -> (q: felt, r: felt) { | ||
// let r = [range_check_ptr]; | ||
// let q = [range_check_ptr + 1]; | ||
// let range_check_ptr = range_check_ptr + 2; | ||
alloc_locals; | ||
local r; | ||
local q; | ||
%{ | ||
from starkware.cairo.common.math_utils import assert_integer | ||
assert_integer(ids.div) | ||
assert 0 < ids.div <= PRIME // range_check_builtin.bound, \ | ||
f'div={hex(ids.div)} is out of the valid range.' | ||
ids.q, ids.r = divmod(ids.value, ids.div) | ||
%} | ||
|
||
assert [range_check_ptr] = div - 1 - r; | ||
let range_check_ptr = range_check_ptr + 1; | ||
// assert_le(r, div - 1); | ||
|
||
assert value = q * div + r; | ||
return (q, r); | ||
} | ||
|
||
func get_felt_bitlength{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(x: felt) -> felt { | ||
alloc_locals; | ||
local bit_length; | ||
%{ | ||
x = ids.x | ||
ids.bit_length = x.bit_length() | ||
%} | ||
|
||
// Next two lines Not necessary : will fail if pow2(bit_length) is too big, unknown cell. | ||
// let le = is_le(bit_length, 252); | ||
// assert le = 1; | ||
assert bitwise_ptr[0].x = x; | ||
let n = pow2(bit_length); | ||
assert bitwise_ptr[0].y = n - 1; | ||
tempvar word = bitwise_ptr[0].x_and_y; | ||
assert word = x; | ||
|
||
assert bitwise_ptr[1].x = x; | ||
|
||
let n = pow2(bit_length - 1); | ||
|
||
assert bitwise_ptr[1].y = n - 1; | ||
tempvar word = bitwise_ptr[1].x_and_y; | ||
assert word = x - n; | ||
|
||
let bitwise_ptr = bitwise_ptr + 2 * BitwiseBuiltin.SIZE; | ||
return bit_length; | ||
} | ||
|
||
func pow2(i) -> felt { | ||
let (data_address) = get_label_location(data); | ||
return [data_address + i]; | ||
data: | ||
dw 0x1; | ||
dw 0x2; | ||
dw 0x4; | ||
dw 0x8; | ||
dw 0x10; | ||
dw 0x20; | ||
dw 0x40; | ||
dw 0x80; | ||
dw 0x100; | ||
dw 0x200; | ||
dw 0x400; | ||
dw 0x800; | ||
dw 0x1000; | ||
dw 0x2000; | ||
dw 0x4000; | ||
dw 0x8000; | ||
dw 0x10000; | ||
dw 0x20000; | ||
dw 0x40000; | ||
dw 0x80000; | ||
dw 0x100000; | ||
dw 0x200000; | ||
dw 0x400000; | ||
dw 0x800000; | ||
dw 0x1000000; | ||
dw 0x2000000; | ||
dw 0x4000000; | ||
dw 0x8000000; | ||
dw 0x10000000; | ||
dw 0x20000000; | ||
dw 0x40000000; | ||
dw 0x80000000; | ||
dw 0x100000000; | ||
dw 0x200000000; | ||
dw 0x400000000; | ||
dw 0x800000000; | ||
dw 0x1000000000; | ||
dw 0x2000000000; | ||
dw 0x4000000000; | ||
dw 0x8000000000; | ||
dw 0x10000000000; | ||
dw 0x20000000000; | ||
dw 0x40000000000; | ||
dw 0x80000000000; | ||
dw 0x100000000000; | ||
dw 0x200000000000; | ||
dw 0x400000000000; | ||
dw 0x800000000000; | ||
dw 0x1000000000000; | ||
dw 0x2000000000000; | ||
dw 0x4000000000000; | ||
dw 0x8000000000000; | ||
dw 0x10000000000000; | ||
dw 0x20000000000000; | ||
dw 0x40000000000000; | ||
dw 0x80000000000000; | ||
dw 0x100000000000000; | ||
dw 0x200000000000000; | ||
dw 0x400000000000000; | ||
dw 0x800000000000000; | ||
dw 0x1000000000000000; | ||
dw 0x2000000000000000; | ||
dw 0x4000000000000000; | ||
dw 0x8000000000000000; | ||
dw 0x10000000000000000; | ||
dw 0x20000000000000000; | ||
dw 0x40000000000000000; | ||
dw 0x80000000000000000; | ||
dw 0x100000000000000000; | ||
dw 0x200000000000000000; | ||
dw 0x400000000000000000; | ||
dw 0x800000000000000000; | ||
dw 0x1000000000000000000; | ||
dw 0x2000000000000000000; | ||
dw 0x4000000000000000000; | ||
dw 0x8000000000000000000; | ||
dw 0x10000000000000000000; | ||
dw 0x20000000000000000000; | ||
dw 0x40000000000000000000; | ||
dw 0x80000000000000000000; | ||
dw 0x100000000000000000000; | ||
dw 0x200000000000000000000; | ||
dw 0x400000000000000000000; | ||
dw 0x800000000000000000000; | ||
dw 0x1000000000000000000000; | ||
dw 0x2000000000000000000000; | ||
dw 0x4000000000000000000000; | ||
dw 0x8000000000000000000000; | ||
dw 0x10000000000000000000000; | ||
dw 0x20000000000000000000000; | ||
dw 0x40000000000000000000000; | ||
dw 0x80000000000000000000000; | ||
dw 0x100000000000000000000000; | ||
dw 0x200000000000000000000000; | ||
dw 0x400000000000000000000000; | ||
dw 0x800000000000000000000000; | ||
dw 0x1000000000000000000000000; | ||
dw 0x2000000000000000000000000; | ||
dw 0x4000000000000000000000000; | ||
dw 0x8000000000000000000000000; | ||
dw 0x10000000000000000000000000; | ||
dw 0x20000000000000000000000000; | ||
dw 0x40000000000000000000000000; | ||
dw 0x80000000000000000000000000; | ||
dw 0x100000000000000000000000000; | ||
dw 0x200000000000000000000000000; | ||
dw 0x400000000000000000000000000; | ||
dw 0x800000000000000000000000000; | ||
dw 0x1000000000000000000000000000; | ||
dw 0x2000000000000000000000000000; | ||
dw 0x4000000000000000000000000000; | ||
dw 0x8000000000000000000000000000; | ||
dw 0x10000000000000000000000000000; | ||
dw 0x20000000000000000000000000000; | ||
dw 0x40000000000000000000000000000; | ||
dw 0x80000000000000000000000000000; | ||
dw 0x100000000000000000000000000000; | ||
dw 0x200000000000000000000000000000; | ||
dw 0x400000000000000000000000000000; | ||
dw 0x800000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000000000000000000; | ||
dw 0x800000000000000000000000000000000000000000000000000000000000; | ||
dw 0x1000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x2000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x4000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x8000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x10000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x20000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x40000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x80000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x100000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x200000000000000000000000000000000000000000000000000000000000000; | ||
dw 0x400000000000000000000000000000000000000000000000000000000000000; | ||
} | ||
|
||
func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() { | ||
let x = get_felt_bitlength(42); | ||
assert x = 6; | ||
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
Oops, something went wrong.