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

Implement hint from get_felt_bitlength (Garaga) #993

Merged
merged 20 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

#### Upcoming Changes

* Implement hint on `get_felt_bitlength` [#993](https://github.com/lambdaclass/cairo-rs/pull/993)

`BuiltinHintProcessor` now supports the following hint:
```python
x = ids.x
ids.bit_length = x.bit_length()
```
Used by the [`Garaga` library function `get_felt_bitlength`](https://github.com/keep-starknet-strange/garaga/blob/249f8a372126b3a839f9c1e1080ea8c6f9374c0c/src/utils.cairo#L54)

* Add missing hint on cairo_secp lib [#1008](https://github.com/lambdaclass/cairo-rs/pull/1008):

`BuiltinHintProcessor` now supports the following hint:
Expand Down
344 changes: 344 additions & 0 deletions cairo_programs/garaga.cairo
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 ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ use felt::Felt252;
#[cfg(feature = "skip_next_instruction_hint")]
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;

use super::garaga::get_felt_bitlenght;

pub struct HintProcessorData {
pub code: String,
pub ap_tracking: ApTracking,
Expand Down Expand Up @@ -367,6 +369,9 @@ impl HintProcessor for BuiltinHintProcessor {
&hint_data.ids_data,
&hint_data.ap_tracking,
),
hint_code::GET_FELT_BIT_LENGTH => {
get_felt_bitlenght(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::DIV_MOD_N_PACKED_DIVMOD_EXTERNAL_N => div_mod_n_packed_external_n(
vm,
exec_scopes,
Expand Down
Loading