From c7fb24c2b072eb35fcbb458c57efcf4c66b128e4 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 17 Apr 2024 21:55:06 +0700 Subject: [PATCH] fix: fix integration tests for blake2s (#504) * fix typo and enable integration tests for blake2s * enable all blake2s tests * add blake2 v2 finalize to hint processor --- src/hint_processor/blake2s_utils.zig | 2 +- src/hint_processor/builtin_hint_codes.zig | 23 +++++++++++++++++++++++ src/hint_processor/hint_processor_def.zig | 3 ++- src/integration_tests.zig | 10 +++++----- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/hint_processor/blake2s_utils.zig b/src/hint_processor/blake2s_utils.zig index ea5b2797..7f74c424 100644 --- a/src/hint_processor/blake2s_utils.zig +++ b/src/hint_processor/blake2s_utils.zig @@ -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(); diff --git a/src/hint_processor/builtin_hint_codes.zig b/src/hint_processor/builtin_hint_codes.zig index 7c6bc844..d1d8820c 100644 --- a/src/hint_processor/builtin_hint_codes.zig +++ b/src/hint_processor/builtin_hint_codes.zig @@ -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 diff --git a/src/hint_processor/hint_processor_def.zig b/src/hint_processor/hint_processor_def.zig index fa01c432..27604b84 100644 --- a/src/hint_processor/hint_processor_def.zig +++ b/src/hint_processor/hint_processor_def.zig @@ -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"); @@ -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)) { diff --git a/src/integration_tests.zig b/src/integration_tests.zig index bdf1ee1f..fa4a922d 100644 --- a/src/integration_tests.zig +++ b/src/integration_tests.zig @@ -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" }, @@ -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