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

feat(hints): Add BLAKE2S_FINALIZE hint variant #1072

Merged
merged 18 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 31 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -114,7 +143,7 @@

`BuiltinHintProcessor` now supports the following hints:

```
```python
%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
Expand Down
1 change: 1 addition & 0 deletions cairo_programs/example_blake2s.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
48 changes: 48 additions & 0 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, HintReference>,
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::<u32>::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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down