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 9 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
32 changes: 31 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@

#### Upcoming Changes

* Add alternative hint code for hint on 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 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 @@ -36,6 +65,7 @@
y0 = pack(ids.point0.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P
%}
```

* Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs/pull/1053):
Expand All @@ -59,7 +89,7 @@

`BuiltinHintProcessor` now supports the following hints:

```
```python
%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
Expand Down
69 changes: 69 additions & 0 deletions cairo_programs/blake2s_finalize_v3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
%builtins range_check bitwise

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.registers import get_fp_and_pc
from starkware.cairo.common.math import assert_nn_le, split_felt, unsigned_div_rem
from starkware.cairo.common.cairo_blake2s.blake2s import blake2s
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.cairo_blake2s.blake2s import INPUT_BLOCK_FELTS, INSTANCE_SIZE, _get_sigma, _finalize_blake2s_inner, N_PACKED_INSTANCES

const BLAKE2S_INPUT_CHUNK_SIZE_FELTS = INPUT_BLOCK_FELTS;

// Verifies that the results of blake2s() are valid.
func finalize_blake2s{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(
blake2s_ptr_start: felt*, blake2s_ptr_end: felt*
) {
alloc_locals;

let (__fp__, _) = get_fp_and_pc();

let (sigma) = _get_sigma();

tempvar n = (blake2s_ptr_end - blake2s_ptr_start) / INSTANCE_SIZE;
if (n == 0) {
return ();
}

%{
# 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)
%}

// Compute the amount of chunks (rounded up).
let (local n_chunks, _) = unsigned_div_rem(n + N_PACKED_INSTANCES - 1, N_PACKED_INSTANCES);
let blake2s_ptr = blake2s_ptr_start;
_finalize_blake2s_inner{blake2s_ptr=blake2s_ptr}(n=n_chunks, sigma=sigma);
return ();
}


func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() {
alloc_locals;
let inputs: felt* = alloc();
assert inputs[0] = 'Hell';
assert inputs[1] = 'o Wo';
assert inputs[2] = 'rld';
let (local blake2s_ptr_start) = alloc();
let blake2s_ptr = blake2s_ptr_start;
let (output) = blake2s{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}(inputs, 9);
finalize_blake2s(blake2s_ptr_start, blake2s_ptr);
return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::REDUCE => {
reduce(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::BLAKE2S_FINALIZE | hint_code::BLAKE2S_FINALIZE_V2 => {
hint_code::BLAKE2S_FINALIZE
| hint_code::BLAKE2S_FINALIZE_V2
| hint_code::BLAKE2S_FINALIZE_V3 => {
finalize_blake2s(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::BLAKE2S_ADD_UINT256 => {
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
7 changes: 7 additions & 0 deletions src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,13 @@ fn ec_double_assign_new_x_v3() {
run_program_simple(program_data.as_slice());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn finalize_blake2s_v3() {
let program_data = include_bytes!("../../cairo_programs/blake2s_finalize_v3.json");
run_program_simple_with_memory_holes(program_data.as_slice(), 20);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn nondet_bigint3_v2() {
Expand Down