Skip to content

Commit

Permalink
fix: fix integration tests for blake2s (#504)
Browse files Browse the repository at this point in the history
* fix typo and enable integration tests for blake2s

* enable all blake2s tests

* add blake2 v2 finalize to hint processor
  • Loading branch information
TropicalDog17 authored Apr 17, 2024
1 parent 12cfc59 commit c7fb24c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/hint_processor/blake2s_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn computeBlake2sFunc(allocator: Allocator, vm: *CairoVM, output_ptr: Relocatabl
const t = try feltToU32(try vm.getFelt(try output_ptr.subUint(2)));
const f = try feltToU32(try vm.getFelt(try output_ptr.subUint(1)));

const h_range = try blake2s_hash.blake2s_compress(allocator, h, message, t, 9, f, 0);
const h_range = try blake2s_hash.blake2s_compress(allocator, h, message, t, 0, f, 0);
defer h_range.deinit();
var new_state = try getMaybeRelocArrayFromU32Array(allocator, h_range);
defer new_state.deinit();
Expand Down
23 changes: 23 additions & 0 deletions src/hint_processor/builtin_hint_codes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,29 @@ pub const BLAKE2S_FINALIZE =
\\segments.write_arg(ids.blake2s_ptr_end, padding)
;

pub const BLAKE2S_FINALIZE_V2 =
\\# 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 = (modified_iv + message + [0, 0xffffffff] + output) * (_n_packed_instances - 1)
\\segments.write_arg(ids.blake2s_ptr_end, padding)
;

pub const BLAKE2S_ADD_UINT256 =
\\B = 32
\\MASK = 2 ** 32 - 1
Expand Down
3 changes: 2 additions & 1 deletion src/hint_processor/hint_processor_def.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const bigint = @import("bigint.zig");
const uint384 = @import("uint384.zig");
const inv_mod_p_uint512 = @import("vrf/inv_mod_p_uint512.zig");


const deserialize_utils = @import("../parser/deserialize_utils.zig");
const print_utils = @import("./print.zig");

Expand Down Expand Up @@ -383,6 +382,8 @@ pub const CairoVMHintProcessor = struct {
try blake2s_utils.blake2sCompute(allocator, vm, hint_data.ids_data, hint_data.ap_tracking);
} else if (std.mem.eql(u8, hint_codes.BLAKE2S_FINALIZE, hint_data.code)) {
try blake2s_utils.blake2sFinalize(allocator, vm, hint_data.ids_data, hint_data.ap_tracking);
} else if (std.mem.eql(u8, hint_codes.BLAKE2S_FINALIZE_V2, hint_data.code)) {
try blake2s_utils.blake2sFinalize(allocator, vm, hint_data.ids_data, hint_data.ap_tracking);
} else if (std.mem.eql(u8, hint_codes.BLAKE2S_ADD_UINT256, hint_data.code)) {
try blake2s_utils.blake2sAddUnit256(allocator, vm, hint_data.ids_data, hint_data.ap_tracking);
} else if (std.mem.eql(u8, hint_codes.BLAKE2S_ADD_UINT256_BIGEND, hint_data.code)) {
Expand Down
10 changes: 5 additions & 5 deletions src/integration_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub fn main() void {
.{ .pathname = "cairo_programs/bitwise_builtin_test.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/bitwise_recursion_compiled.json", .layout = "all_cairo" },
// TODO: merge blake hint
// .{ .pathname = "cairo_programs/blake2s_felts.json", .layout = "all_cairo" },
// .{ .pathname = "cairo_programs/blake2s_hello_world_hash.json", .layout = "all_cairo" },
// .{ .pathname = "cairo_programs/blake2s_integration_tests.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/blake2s_felts.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/blake2s_hello_world_hash.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/blake2s_integration_tests.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/cairo_finalize_keccak_compiled.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/cairo_finalize_keccak_block_size_1000.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/call_function_assign_param_by_name.json", .layout = "all_cairo" },
Expand Down Expand Up @@ -87,9 +87,9 @@ pub fn main() void {
// TODO: HintNotImplemented error secp signature hint
// .{ .pathname = "cairo_programs/field_arithmetic.json", .layout = "all_cairo" },
// TODO: merge blake hint
// .{ .pathname = "cairo_programs/finalize_blake2s.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/finalize_blake2s.json", .layout = "all_cairo" },
// TODO: merge blake hint
// .{ .pathname = "cairo_programs/finalize_blake2s_v2_hint.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/finalize_blake2s_v2_hint.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/find_element.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/fq.json", .layout = "all_cairo" },
// TODO: Hint not implemented error
Expand Down

0 comments on commit c7fb24c

Please sign in to comment.