From d41c959e23ef077470ea7f5cb42da6c98397708d Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 28 Apr 2023 18:01:35 +0300 Subject: [PATCH] feat(hints): Add BLAKE2S_FINALIZE hint variant (#1072) * Add alternative hint code for BLAKE2S_FINALIZE * Add changelog entry * Add integration test * Update cairo_run_test.rs * Fix memory hole value in test * Call finalize_blake2s in test * Simplify integration test * fmt * Implement BLAKE2S_FINALIZE_V3 hint * fmt * Fix changelog description * Remove test file, use proper one * Fix test * fmt --- CHANGELOG.md | 33 ++++++++++++- cairo_programs/example_blake2s.cairo | 1 + .../builtin_hint_processor/blake2s_utils.rs | 48 +++++++++++++++++++ .../builtin_hint_processor_definition.rs | 4 ++ .../builtin_hint_processor/hint_code.rs | 21 ++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 498b11448f..6d45ab1f61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,35 @@ #### Upcoming Changes +* Implement hint variant for finalize_blake2s[#1072](https://github.com/lambdaclass/cairo-rs/pull/1072) + + `BuiltinHintProcessor` now supports the following hint: + + ```python + %{ + # Add dummy pairs of input and output. + from starkware.cairo.common.cairo_blake2s.blake2s_utils import IV, blake2s_compress + + _n_packed_instances = int(ids.N_PACKED_INSTANCES) + assert 0 <= _n_packed_instances < 20 + _blake2s_input_chunk_size_felts = int(ids.BLAKE2S_INPUT_CHUNK_SIZE_FELTS) + assert 0 <= _blake2s_input_chunk_size_felts < 100 + + message = [0] * _blake2s_input_chunk_size_felts + modified_iv = [IV[0] ^ 0x01010020] + IV[1:] + output = blake2s_compress( + message=message, + h=modified_iv, + t0=0, + t1=0, + f0=0xffffffff, + f1=0, + ) + padding = (message + modified_iv + [0, 0xffffffff] + output) * (_n_packed_instances - 1) + segments.write_arg(ids.blake2s_ptr_end, padding) + %} + ``` + * Implement fast_ec_add hint variant [#1087](https://github.com/lambdaclass/cairo-rs/pull/1087) `BuiltinHintProcessor` now supports the following hint: @@ -55,7 +84,7 @@ from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack x = pack(ids.x, PRIME) % SECP_P %} - + * Implement hint for `starkware.cairo.common.cairo_keccak.keccak._copy_inputs` as described by whitelist `starknet/security/whitelists/cairo_keccak.json` [#1058](https://github.com/lambdaclass/cairo-rs/pull/1058) `BuiltinHintProcessor` now supports the following hint: @@ -114,7 +143,7 @@ `BuiltinHintProcessor` now supports the following hints: - ``` + ```python %{ ids.a_lsb = ids.a & 1 ids.b_lsb = ids.b & 1 diff --git a/cairo_programs/example_blake2s.cairo b/cairo_programs/example_blake2s.cairo index 708d439df7..64db496614 100644 --- a/cairo_programs/example_blake2s.cairo +++ b/cairo_programs/example_blake2s.cairo @@ -649,5 +649,6 @@ func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { assert output[5] = 1978410869; assert output[6] = 3956807281; assert output[7] = 3738027290; + finalize_blake2s(blake2s_ptr_start, blake2s_ptr); return (); } diff --git a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index d9257a8b71..af8d79cb8b 100644 --- a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -120,6 +120,54 @@ pub fn finalize_blake2s( Ok(()) } +/* Implements Hint: + # Add dummy pairs of input and output. + from starkware.cairo.common.cairo_blake2s.blake2s_utils import IV, blake2s_compress + + _n_packed_instances = int(ids.N_PACKED_INSTANCES) + assert 0 <= _n_packed_instances < 20 + _blake2s_input_chunk_size_felts = int(ids.BLAKE2S_INPUT_CHUNK_SIZE_FELTS) + assert 0 <= _blake2s_input_chunk_size_felts < 100 + + message = [0] * _blake2s_input_chunk_size_felts + modified_iv = [IV[0] ^ 0x01010020] + IV[1:] + output = blake2s_compress( + message=message, + h=modified_iv, + t0=0, + t1=0, + f0=0xffffffff, + f1=0, + ) + padding = (message + modified_iv + [0, 0xffffffff] + output) * (_n_packed_instances - 1) + segments.write_arg(ids.blake2s_ptr_end, padding) +*/ +pub fn finalize_blake2s_v3( + vm: &mut VirtualMachine, + ids_data: &HashMap, + ap_tracking: &ApTracking, +) -> Result<(), HintError> { + const N_PACKED_INSTANCES: usize = 7; + let blake2s_ptr_end = get_ptr_from_var_name("blake2s_ptr_end", vm, ids_data, ap_tracking)?; + let message: [u32; 16] = [0; 16]; + let mut modified_iv = IV; + modified_iv[0] = IV[0] ^ 0x01010020; + let output = blake2s_compress(&modified_iv, &message, 0, 0, 0xffffffff, 0); + let mut padding = message.to_vec(); + padding.extend(modified_iv); + padding.extend([0, 0xffffffff]); + padding.extend(output); + let padding = padding.as_slice(); + let mut full_padding = Vec::::with_capacity(padding.len() * N_PACKED_INSTANCES); + for _ in 0..N_PACKED_INSTANCES - 1 { + full_padding.extend_from_slice(padding); + } + let data = get_maybe_relocatable_array_from_u32(&full_padding); + vm.load_data(blake2s_ptr_end, &data) + .map_err(HintError::Memory)?; + Ok(()) +} + /* Implements Hint: B = 32 MASK = 2 ** 32 - 1 diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index aad00c19cd..2cd865df4f 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -1,4 +1,5 @@ use super::{ + blake2s_utils::finalize_blake2s_v3, ec_recover::{ ec_recover_divmod_n_packed, ec_recover_product_div_m, ec_recover_product_mod, ec_recover_sub_a_b, @@ -311,6 +312,9 @@ impl HintProcessor for BuiltinHintProcessor { hint_code::BLAKE2S_FINALIZE | hint_code::BLAKE2S_FINALIZE_V2 => { finalize_blake2s(vm, &hint_data.ids_data, &hint_data.ap_tracking) } + hint_code::BLAKE2S_FINALIZE_V3 => { + finalize_blake2s_v3(vm, &hint_data.ids_data, &hint_data.ap_tracking) + } hint_code::BLAKE2S_ADD_UINT256 => { blake2s_add_uint256(vm, &hint_data.ids_data, &hint_data.ap_tracking) } diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index feb3f46648..fc3eb17066 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -454,6 +454,27 @@ output = blake2s_compress( padding = (modified_iv + message + [0, 0xffffffff] + output) * (_n_packed_instances - 1) segments.write_arg(ids.blake2s_ptr_end, padding)"#; +pub const BLAKE2S_FINALIZE_V3: &str = r#"# Add dummy pairs of input and output. +from starkware.cairo.common.cairo_blake2s.blake2s_utils import IV, blake2s_compress + +_n_packed_instances = int(ids.N_PACKED_INSTANCES) +assert 0 <= _n_packed_instances < 20 +_blake2s_input_chunk_size_felts = int(ids.BLAKE2S_INPUT_CHUNK_SIZE_FELTS) +assert 0 <= _blake2s_input_chunk_size_felts < 100 + +message = [0] * _blake2s_input_chunk_size_felts +modified_iv = [IV[0] ^ 0x01010020] + IV[1:] +output = blake2s_compress( + message=message, + h=modified_iv, + t0=0, + t1=0, + f0=0xffffffff, + f1=0, +) +padding = (message + modified_iv + [0, 0xffffffff] + output) * (_n_packed_instances - 1) +segments.write_arg(ids.blake2s_ptr_end, padding)"#; + pub const BLAKE2S_ADD_UINT256: &str = r#"B = 32 MASK = 2 ** 32 - 1 segments.write_arg(ids.data, [(ids.low >> (B * i)) & MASK for i in range(4)])