From 5e48450d7f8c7adfb0c106049fe265bc1c3b8b39 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Thu, 24 Oct 2024 13:49:11 +0200 Subject: [PATCH] impl missing hint --- .../src/hints/lib/rlp_little/leading_zeros.rs | 32 +++++++++++++++++++ lib/rlp_little.cairo | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cairo_vm_hints/src/hints/lib/rlp_little/leading_zeros.rs b/cairo_vm_hints/src/hints/lib/rlp_little/leading_zeros.rs index dd19d6c..99c4a21 100644 --- a/cairo_vm_hints/src/hints/lib/rlp_little/leading_zeros.rs +++ b/cairo_vm_hints/src/hints/lib/rlp_little/leading_zeros.rs @@ -41,3 +41,35 @@ pub fn hint_expected_leading_zeroes( Ok(()) } + +pub const HINT_EXPECTED_NIBBLE: &str = "key_hex = ids.key_leading_zeroes_nibbles * '0' + hex(ids.key.low + (2 ** 128) * ids.key.high)[2:]\nexpected_nibble = int(key_hex[ids.nibble_index + ids.key_leading_zeroes_nibbles], 16)"; + +pub fn hint_expected_nibble( + vm: &mut VirtualMachine, + exec_scope: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let key_low: u128 = utils::get_value("key.low", vm, hint_data)? + .try_into() + .unwrap(); + let key_high: u128 = utils::get_value("key.high", vm, hint_data)? + .try_into() + .unwrap(); + let key_leading_zeroes_nibbles: usize = + utils::get_value("key_leading_zeroes_nibbles", vm, hint_data)? + .try_into() + .unwrap(); + let nibble_index: usize = utils::get_value("nibble_index", vm, hint_data)? + .try_into() + .unwrap(); + + let hex = hex::encode([key_low.to_be_bytes(), key_high.to_be_bytes()].concat()); + let nibble_char = format!("{:0width$}{}", "", hex, width = key_leading_zeroes_nibbles) + .chars() + .nth(nibble_index + key_leading_zeroes_nibbles) + .unwrap(); + exec_scope.insert_value("expected_nibble", nibble_char.to_digit(16).unwrap()); + + Ok(()) +} diff --git a/lib/rlp_little.cairo b/lib/rlp_little.cairo index 81dd7a6..eedd608 100644 --- a/lib/rlp_little.cairo +++ b/lib/rlp_little.cairo @@ -421,7 +421,7 @@ func extract_nibble_from_key_be{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}( // print(f"key_nibbles: {ids.key_nibbles}") // print(f"key_leading_zeroes_nibbles: {ids.key_leading_zeroes_nibbles}") // %} - %{ //TODO + %{ key_hex = ids.key_leading_zeroes_nibbles * '0' + hex(ids.key.low + (2 ** 128) * ids.key.high)[2:] expected_nibble = int(key_hex[ids.nibble_index + ids.key_leading_zeroes_nibbles], 16) %}