Skip to content

Commit

Permalink
feat(hints): add NewHint#51 (#1049)
Browse files Browse the repository at this point in the history
* Add NewHint#51

* Update changelog

* Add tests

* Add comment in changelog
  • Loading branch information
MegaRedHand authored Apr 25, 2023
1 parent 9899f81 commit 33690df
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 14 deletions.
48 changes: 45 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,48 @@
ids.res.high = res_split[1]
```

* Implement hint on vrf.json lib [#1049](https://github.com/lambdaclass/cairo-rs/pull/1049)

`BuiltinHintProcessor` now supports the following hint:

```python
def split(num: int, num_bits_shift: int, length: int):
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
return tuple(a)

def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

def pack_extended(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2, z.d3, z.d4, z.d5)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

a = pack_extended(ids.a, num_bits_shift = 128)
div = pack(ids.div, num_bits_shift = 128)

quotient, remainder = divmod(a, div)

quotient_split = split(quotient, num_bits_shift=128, length=6)

ids.quotient.d0 = quotient_split[0]
ids.quotient.d1 = quotient_split[1]
ids.quotient.d2 = quotient_split[2]
ids.quotient.d3 = quotient_split[3]
ids.quotient.d4 = quotient_split[4]
ids.quotient.d5 = quotient_split[5]

remainder_split = split(remainder, num_bits_shift=128, length=3)
ids.remainder.d0 = remainder_split[0]
ids.remainder.d1 = remainder_split[1]
ids.remainder.d2 = remainder_split[2]
```

_Note: this hint is similar to the one in #983, but with some trailing whitespace removed_

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

`BuiltinHintProcessor` now supports the following hint:
Expand Down Expand Up @@ -501,13 +543,13 @@
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
num = num >> num_bits_shift
return tuple(a)

def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

def pack_extended(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2, z.d3, z.d4, z.d5)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))
Expand Down Expand Up @@ -1001,4 +1043,4 @@
* `pub fn from_vm_error(runner: &CairoRunner, error: VirtualMachineError, pc: usize) -> Self` is now `pub fn from_vm_error(runner: &CairoRunner, vm: &VirtualMachine, error: VirtualMachineError) -> Self`
* `pub fn get_location(pc: &usize, runner: &CairoRunner) -> Option<Location>` is now `pub fn get_location(pc: usize, runner: &CairoRunner) -> Option<Location>`
* `pub fn decode_instruction(encoded_instr: i64, mut imm: Option<BigInt>) -> Result<instruction::Instruction, VirtualMachineError>` is now `pub fn decode_instruction(encoded_instr: i64, mut imm: Option<&BigInt>) -> Result<instruction::Instruction, VirtualMachineError>`
* `VmExcepion` field's string format now mirror their cairo-lang conterparts.
* `VmExcepion` field's string format now mirror their cairo-lang conterparts.
66 changes: 66 additions & 0 deletions cairo_programs/uint384_extension.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,72 @@ namespace u384_ext {

return (quotient=quotient, remainder=remainder);
}

// same as `unsigned_div_rem_uint768_by_uint384` but with different hint
func unsigned_div_rem_uint768_by_uint384_alt{range_check_ptr}(a: Uint768, div: Uint384) -> (
quotient: Uint768, remainder: Uint384
) {
alloc_locals;
local quotient: Uint768;
local remainder: Uint384;

// If div == 0, return (0, 0).
if (div.d0 + div.d1 + div.d2 == 0) {
return (quotient=Uint768(0, 0, 0, 0, 0, 0), remainder=Uint384(0, 0, 0));
}

%{
def split(num: int, num_bits_shift: int, length: int):
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
return tuple(a)
def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))
def pack_extended(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2, z.d3, z.d4, z.d5)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))
a = pack_extended(ids.a, num_bits_shift = 128)
div = pack(ids.div, num_bits_shift = 128)
quotient, remainder = divmod(a, div)
quotient_split = split(quotient, num_bits_shift=128, length=6)
ids.quotient.d0 = quotient_split[0]
ids.quotient.d1 = quotient_split[1]
ids.quotient.d2 = quotient_split[2]
ids.quotient.d3 = quotient_split[3]
ids.quotient.d4 = quotient_split[4]
ids.quotient.d5 = quotient_split[5]
remainder_split = split(remainder, num_bits_shift=128, length=3)
ids.remainder.d0 = remainder_split[0]
ids.remainder.d1 = remainder_split[1]
ids.remainder.d2 = remainder_split[2]
%}
check(quotient);
u384.check(remainder);

let (res_mul_low: Uint768, res_mul_high: Uint384) = mul_uint768_by_uint384_d(quotient, div);

assert res_mul_high = Uint384(0, 0, 0);

let (check_val: Uint768, add_carry: felt) = add_uint768_and_uint384(res_mul_low, remainder);

assert add_carry = 0;
assert check_val = a;

let (is_valid) = u384.lt(remainder, div);
assert is_valid = 1;

return (quotient=quotient, remainder=remainder);
}
}

func main() {
Expand Down
19 changes: 19 additions & 0 deletions cairo_programs/uint384_extension_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,26 @@ func test_uint384_extension_operations{range_check_ptr}() {
return ();
}

func test_uint384_unsigned_div_rem_alt{range_check_ptr}() {
// Test unsigned_div_rem_uint768_by_uint384
let a = Uint768(1, 2, 3, 4, 5, 6);
let div = Uint384(6, 7, 8);
let (q, r) = u384_ext.unsigned_div_rem_uint768_by_uint384_alt(a, div);
assert q.d0 = 328319314958874220607240343889245110272;
assert q.d1 = 329648542954659136480144150949525454847;
assert q.d2 = 255211775190703847597530955573826158591;
assert q.d3 = 0;
assert q.d4 = 0;
assert q.d5 = 0;

assert r.d0 = 71778311772385457136805581255138607105;
assert r.d1 = 147544307532125661892322583691118247938;
assert r.d2 = 3;
return ();
}

func main{range_check_ptr: felt}() {
test_uint384_extension_operations();
test_uint384_unsigned_div_rem_alt();
return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,8 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::UINT384_SQRT => {
uint384_sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384 => {
hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384
| hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384_STRIPPED => {
unsigned_div_rem_uint768_by_uint384(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::GET_SQUARE_ROOT => {
Expand Down
38 changes: 38 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ root_split = split(root, num_bits_shift=128, length=3)
ids.root.d0 = root_split[0]
ids.root.d1 = root_split[1]
ids.root.d2 = root_split[2]";

pub const UNSIGNED_DIV_REM_UINT768_BY_UINT384: &str =
"def split(num: int, num_bits_shift: int, length: int):
a = []
Expand Down Expand Up @@ -948,6 +949,43 @@ remainder_split = split(remainder, num_bits_shift=128, length=3)
ids.remainder.d0 = remainder_split[0]
ids.remainder.d1 = remainder_split[1]
ids.remainder.d2 = remainder_split[2]";

// equal to UNSIGNED_DIV_REM_UINT768_BY_UINT384 but with some whitespace removed
// in the `num = num >> num_bits_shift` and between `pack` and `pack_extended`
pub const UNSIGNED_DIV_REM_UINT768_BY_UINT384_STRIPPED: &str = r#"def split(num: int, num_bits_shift: int, length: int):
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
return tuple(a)
def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))
def pack_extended(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2, z.d3, z.d4, z.d5)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))
a = pack_extended(ids.a, num_bits_shift = 128)
div = pack(ids.div, num_bits_shift = 128)
quotient, remainder = divmod(a, div)
quotient_split = split(quotient, num_bits_shift=128, length=6)
ids.quotient.d0 = quotient_split[0]
ids.quotient.d1 = quotient_split[1]
ids.quotient.d2 = quotient_split[2]
ids.quotient.d3 = quotient_split[3]
ids.quotient.d4 = quotient_split[4]
ids.quotient.d5 = quotient_split[5]
remainder_split = split(remainder, num_bits_shift=128, length=3)
ids.remainder.d0 = remainder_split[0]
ids.remainder.d1 = remainder_split[1]
ids.remainder.d2 = remainder_split[2]"#;

pub const UINT384_SIGNED_NN: &str = "memory[ap] = 1 if 0 <= (ids.a.d2 % PRIME) < 2 ** 127 else 0";

pub(crate) const GET_SQUARE_ROOT: &str =
Expand Down
20 changes: 11 additions & 9 deletions src/hint_processor/builtin_hint_processor/uint384_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ mod tests {

use felt::felt_str;
use num_traits::One;
use rstest::rstest;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

Expand Down Expand Up @@ -298,9 +299,11 @@ mod tests {
assert_matches!(r, Err(HintError::UnknownIdentifier(x)) if x == "x")
}

#[test]
#[rstest]
#[case(hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384)]
#[case(hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384_STRIPPED)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_unsigned_div_rem_ok() {
fn run_unsigned_div_rem_ok(#[case] hint_code: &str) {
let mut vm = vm_with_range_check!();
//Initialize fp
vm.run_context.fp = 17;
Expand All @@ -326,10 +329,7 @@ mod tests {
((1, 8), 8)
];
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384),
Ok(())
);
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check hint memory inserts
check_memory![
vm.segments.memory,
Expand Down Expand Up @@ -371,9 +371,11 @@ mod tests {
);
}

#[test]
#[rstest]
#[case(hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384)]
#[case(hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384_STRIPPED)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_unsigned_div_rem_divide_by_zero() {
fn run_unsigned_div_rem_divide_by_zero(#[case] hint_code: &str) {
let mut vm = vm_with_range_check!();
//Initialize fp
vm.run_context.fp = 17;
Expand All @@ -400,7 +402,7 @@ mod tests {
];
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384),
run_hint!(vm, ids_data, hint_code),
Err(HintError::Math(MathError::DividedByZero))
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ fn uint384() {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn uint384_extension() {
let program_data = include_bytes!("../../cairo_programs/uint384_extension_test.json");
run_program_simple_with_memory_holes(program_data.as_slice(), 20);
run_program_simple_with_memory_holes(program_data.as_slice(), 40);
}

#[test]
Expand Down

0 comments on commit 33690df

Please sign in to comment.