diff --git a/build.zig b/build.zig index c5658714..10099b77 100644 --- a/build.zig +++ b/build.zig @@ -156,6 +156,7 @@ pub fn build(b: *Builder) !void { }); zeam_utils.addImport("datetime", datetime); zeam_utils.addImport("yaml", yaml); + zeam_utils.addImport("ssz", ssz); // add zeam-params const zeam_params = b.addModule("@zeam/params", .{ @@ -221,6 +222,7 @@ pub fn build(b: *Builder) !void { }); zeam_key_manager.addImport("@zeam/xmss", zeam_xmss); zeam_key_manager.addImport("@zeam/types", zeam_types); + zeam_key_manager.addImport("@zeam/utils", zeam_utils); zeam_key_manager.addImport("@zeam/metrics", zeam_metrics); zeam_key_manager.addImport("ssz", ssz); @@ -728,6 +730,7 @@ fn build_zkvm_targets(b: *Builder, main_exe: *Builder.Step, host_target: std.Bui .optimize = optimize, .root_source_file = b.path("pkgs/utils/src/lib.zig"), }); + zeam_utils.addImport("ssz", ssz); // add zeam-metrics (core metrics definitions for ZKVM) const zeam_metrics = b.addModule("@zeam/metrics", .{ diff --git a/build.zig.zon b/build.zig.zon index 03b62d04..1d7ab667 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,8 +4,8 @@ .version = "0.0.0", .dependencies = .{ .ssz = .{ - .url = "git+https://github.com/blockblaz/ssz.zig#5ce7322fc45cab4f215021cae2579d1343e05d55", - .hash = "ssz-0.0.9-Lfwd61PEAgAUPJfUQiK4R5gRX_lTWOd_qYwNT-KAhRLA", + .url = "https://github.com/blockblaz/ssz.zig/archive/0ce92a8f093a321b0d0815eec6c90bd4e745d8e1.tar.gz", + .hash = "ssz-0.0.9-Lfwd6wvKAgA3JZjtfbeXeQG6zle1K_K2j6HPmQJJF4an", }, .zigcli = .{ .url = "git+https://github.com/jiacai2050/zigcli?ref=main#dcbc59d70b4787671c8a4e484ffd2b725aa17af5", diff --git a/pkgs/cli/src/node.zig b/pkgs/cli/src/node.zig index 7cf3ee7f..025e6354 100644 --- a/pkgs/cli/src/node.zig +++ b/pkgs/cli/src/node.zig @@ -656,7 +656,7 @@ fn verifyCheckpointState( // Calculate the block root from the properly constructed block header var block_root: types.Root = undefined; - try ssz.hashTreeRoot(types.BeamBlockHeader, state_block_header, &block_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamBlockHeader, state_block_header, &block_root, allocator); logger.info("checkpoint state verified: slot={d}, genesis_time={d}, validators={d}, state_root=0x{s}, block_root=0x{s}", .{ state.slot, @@ -1201,7 +1201,7 @@ test "compare roots from genGensisBlock and genGenesisState and genStateBlockHea // Get state root by hashing the state directly var state_root_from_genesis: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamState, genesis_state, &state_root_from_genesis, allocator); + try zeam_utils.hashTreeRoot(types.BeamState, genesis_state, &state_root_from_genesis, allocator); // Generate block header using genStateBlockHeader const state_block_header = try genesis_state.genStateBlockHeader(allocator); diff --git a/pkgs/key-manager/src/lib.zig b/pkgs/key-manager/src/lib.zig index 832e96de..ce3e933d 100644 --- a/pkgs/key-manager/src/lib.zig +++ b/pkgs/key-manager/src/lib.zig @@ -1,8 +1,8 @@ const std = @import("std"); const xmss = @import("@zeam/xmss"); const types = @import("@zeam/types"); +const zeam_utils = @import("@zeam/utils"); const zeam_metrics = @import("@zeam/metrics"); -const ssz = @import("ssz"); const Allocator = std.mem.Allocator; const KeyManagerError = error{ @@ -152,7 +152,7 @@ pub const KeyManager = struct { const signing_timer = zeam_metrics.lean_pq_signature_attestation_signing_time_seconds.start(); var message: [32]u8 = undefined; - try ssz.hashTreeRoot(types.AttestationData, attestation.data, &message, allocator); + try zeam_utils.hashTreeRoot(types.AttestationData, attestation.data, &message, allocator); const epoch: u32 = @intCast(attestation.data.slot); const signature = try keypair.sign(&message, epoch); diff --git a/pkgs/node/src/chain.zig b/pkgs/node/src/chain.zig index c37c67b2..76ec357c 100644 --- a/pkgs/node/src/chain.zig +++ b/pkgs/node/src/chain.zig @@ -354,7 +354,7 @@ pub const BeamChain = struct { // 3. cache state to save recompute while adding the block on publish var block_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamBlock, block, &block_root, self.allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, block, &block_root, self.allocator); try self.states.put(block_root, post_state); post_state_opt = null; @@ -491,7 +491,7 @@ pub const BeamChain = struct { .block => |signed_block| { const block = signed_block.message.block; var block_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamBlock, block, &block_root, self.allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, block, &block_root, self.allocator); //check if we have the block already in forkchoice const hasBlock = self.forkChoice.hasBlock(block_root); @@ -592,7 +592,7 @@ pub const BeamChain = struct { const block_root: types.Root = blockInfo.blockRoot orelse computedroot: { var cblock_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamBlock, block, &cblock_root, self.allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, block, &cblock_root, self.allocator); break :computedroot cblock_root; }; @@ -1339,7 +1339,7 @@ test "process and add mock blocks into a node's chain" { // should have matching states in the state const block_state = beam_chain.states.get(block_root) orelse @panic("state root should have been found"); var state_root: [32]u8 = undefined; - try ssz.hashTreeRoot(*types.BeamState, block_state, &state_root, allocator); + try zeam_utils.hashTreeRoot(*types.BeamState, block_state, &state_root, allocator); try std.testing.expect(std.mem.eql(u8, &state_root, &block.state_root)); // fcstore checkpoints should match diff --git a/pkgs/node/src/forkchoice.zig b/pkgs/node/src/forkchoice.zig index d26b0a83..7bc33879 100644 --- a/pkgs/node/src/forkchoice.zig +++ b/pkgs/node/src/forkchoice.zig @@ -242,7 +242,7 @@ pub const ForkChoice = struct { pub fn init(allocator: Allocator, opts: ForkChoiceParams) !Self { const anchor_block_header = try opts.anchorState.genStateBlockHeader(allocator); var anchor_block_root: [32]u8 = undefined; - try ssz.hashTreeRoot( + try zeam_utils.hashTreeRoot( types.BeamBlockHeader, anchor_block_header, &anchor_block_root, @@ -1062,7 +1062,7 @@ pub const ForkChoice = struct { const block_root: [32]u8 = opts.blockRoot orelse computedroot: { var cblock_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamBlock, block, &cblock_root, self.allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, block, &cblock_root, self.allocator); break :computedroot cblock_root; }; const is_timely = self.isBlockTimely(opts.blockDelayMs); diff --git a/pkgs/node/src/node.zig b/pkgs/node/src/node.zig index 884e88dd..4a1add54 100644 --- a/pkgs/node/src/node.zig +++ b/pkgs/node/src/node.zig @@ -139,7 +139,7 @@ pub const BeamNode = struct { } var block_root: types.Root = undefined; - if (ssz.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator)) |_| { + if (zeam_utils.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator)) |_| { _ = self.network.removePendingBlockRoot(block_root); } else |err| { self.logger.warn("failed to compute block root for incoming gossip block: {any}", .{err}); @@ -168,7 +168,7 @@ pub const BeamNode = struct { if (data.* == .block) { const signed_block = data.block; var block_root: types.Root = undefined; - if (ssz.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator)) |_| { + if (zeam_utils.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator)) |_| { self.logger.info( "gossip block 0x{s} rejected as pre-finalized; pruning cached descendants", .{std.fmt.fmtSliceHexLower(block_root[0..])}, @@ -338,7 +338,7 @@ pub const BeamNode = struct { fn processBlockByRootChunk(self: *Self, block_ctx: *const BlockByRootContext, signed_block: *const types.SignedBlockWithAttestation) !void { var block_root: types.Root = undefined; - if (ssz.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator)) |_| { + if (zeam_utils.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator)) |_| { const current_depth = self.network.getPendingBlockRootDepth(block_root) orelse 0; const removed = self.network.removePendingBlockRoot(block_root); if (!removed) { @@ -779,7 +779,7 @@ pub const BeamNode = struct { // 1. Process locally through chain so that produced block first can be confirmed var block_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, signed_block.message.block, &block_root, self.allocator); // check if the block has not already been received through the network const hasBlock = self.chain.forkChoice.hasBlock(block_root); diff --git a/pkgs/node/src/testing.zig b/pkgs/node/src/testing.zig index f892bc69..ad80db50 100644 --- a/pkgs/node/src/testing.zig +++ b/pkgs/node/src/testing.zig @@ -10,8 +10,6 @@ const zeam_utils = @import("@zeam/utils"); const xev = @import("xev"); const networks = @import("@zeam/network"); const xmss = @import("@zeam/xmss"); -const ssz = @import("ssz"); - const clockFactory = @import("./clock.zig"); pub const NodeTestOptions = struct { @@ -203,7 +201,7 @@ pub const NodeTestContext = struct { // Compute message hash var message_hash: [32]u8 = undefined; - try ssz.hashTreeRoot(types.AttestationData, aggregated_attestation.data, &message_hash, allocator); + try zeam_utils.hashTreeRoot(types.AttestationData, aggregated_attestation.data, &message_hash, allocator); const epoch: u32 = @intCast(aggregated_attestation.data.slot); diff --git a/pkgs/spectest/src/runner/fork_choice_runner.zig b/pkgs/spectest/src/runner/fork_choice_runner.zig index e15636f3..6bcb4550 100644 --- a/pkgs/spectest/src/runner/fork_choice_runner.zig +++ b/pkgs/spectest/src/runner/fork_choice_runner.zig @@ -291,7 +291,7 @@ fn runCase( defer label_map.deinit(allocator); var anchor_root: types.Root = undefined; - ssz.hashTreeRoot(types.BeamBlock, anchor_block, &anchor_root, allocator) catch |err| { + zeam_utils.hashTreeRoot(types.BeamBlock, anchor_block, &anchor_root, allocator) catch |err| { std.debug.print( "fixture {s} case {s}: anchor block hashing failed ({s})\n", .{ ctx.fixture_label, ctx.case_name, @errorName(err) }, @@ -607,7 +607,7 @@ fn processBlockStep( defer block.deinit(); var block_root: types.Root = undefined; - ssz.hashTreeRoot(types.BeamBlock, block, &block_root, ctx.allocator) catch |err| { + zeam_utils.hashTreeRoot(types.BeamBlock, block, &block_root, ctx.allocator) catch |err| { std.debug.print( "fixture {s} case {s}{}: hashing block failed ({s})\n", .{ fixture_path, case_name, formatStep(step_index), @errorName(err) }, diff --git a/pkgs/spectest/src/runner/state_transition_runner.zig b/pkgs/spectest/src/runner/state_transition_runner.zig index e043516f..25807049 100644 --- a/pkgs/spectest/src/runner/state_transition_runner.zig +++ b/pkgs/spectest/src/runner/state_transition_runner.zig @@ -41,8 +41,6 @@ pub fn baseRelRoot(comptime spec_fork: Fork) []const u8 { const types = @import("@zeam/types"); const state_transition = @import("@zeam/state-transition"); const zeam_utils = @import("@zeam/utils"); -const ssz = @import("ssz"); - const JsonValue = std.json.Value; const Context = expect.Context; @@ -245,7 +243,7 @@ fn runCase( var header_for_check = pre_state.latest_block_header; if (std.mem.eql(u8, &header_for_check.state_root, &types.ZERO_HASH)) { var pre_state_root: types.Root = undefined; - ssz.hashTreeRoot(types.BeamState, pre_state, &pre_state_root, allocator) catch |err| { + zeam_utils.hashTreeRoot(types.BeamState, pre_state, &pre_state_root, allocator) catch |err| { std.debug.print( "fixture {s} case {s}: unable to hash pre-state ({s})\n", .{ ctx.fixture_label, ctx.case_name, @errorName(err) }, @@ -256,7 +254,7 @@ fn runCase( } var header_root: types.Root = undefined; - ssz.hashTreeRoot(types.BeamBlockHeader, header_for_check, &header_root, allocator) catch |err| { + zeam_utils.hashTreeRoot(types.BeamBlockHeader, header_for_check, &header_root, allocator) catch |err| { std.debug.print( "fixture {s} case {s}: unable to hash latest block header ({s})\n", .{ ctx.fixture_label, ctx.case_name, @errorName(err) }, diff --git a/pkgs/state-transition/src/lib.zig b/pkgs/state-transition/src/lib.zig index e8002225..d2439ee8 100644 --- a/pkgs/state-transition/src/lib.zig +++ b/pkgs/state-transition/src/lib.zig @@ -55,7 +55,7 @@ test "apply transition on mocked chain" { // check the post state root to be equal to block2's stateroot // this is reduant though because apply_transition already checks this for each block's state root var post_state_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamState, beam_state, &post_state_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamState, beam_state, &post_state_root, allocator); try std.testing.expect(std.mem.eql(u8, &post_state_root, &mock_chain.blocks[mock_chain.blocks.len - 1].message.block.state_root)); } @@ -73,11 +73,11 @@ test "genStateBlockHeader" { // get applied block const applied_block = mock_chain.blocks[i]; var applied_block_root: types.Root = undefined; - try ssz.hashTreeRoot(types.BeamBlock, applied_block.message.block, &applied_block_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, applied_block.message.block, &applied_block_root, allocator); const state_block_header = try beam_state.genStateBlockHeader(allocator); var state_block_header_root: types.Root = undefined; - try ssz.hashTreeRoot(types.BeamBlockHeader, state_block_header, &state_block_header_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamBlockHeader, state_block_header, &state_block_header_root, allocator); try std.testing.expect(std.mem.eql(u8, &applied_block_root, &state_block_header_root)); diff --git a/pkgs/state-transition/src/mock.zig b/pkgs/state-transition/src/mock.zig index b4f83f08..034bd5bd 100644 --- a/pkgs/state-transition/src/mock.zig +++ b/pkgs/state-transition/src/mock.zig @@ -115,7 +115,7 @@ pub fn genMockChain(allocator: Allocator, numBlocks: usize, from_genesis: ?types }, }; var block_root: types.Root = undefined; - try ssz.hashTreeRoot(types.BeamBlock, genesis_block, &block_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, genesis_block, &block_root, allocator); try blockList.append(gen_signed_block); try blockRootList.append(block_root); @@ -145,7 +145,7 @@ pub fn genMockChain(allocator: Allocator, numBlocks: usize, from_genesis: ?types for (1..numBlocks) |slot| { var parent_root: [32]u8 = undefined; - try ssz.hashTreeRoot(types.BeamBlock, prev_block, &parent_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, prev_block, &parent_root, allocator); const state_root: [32]u8 = types.ZERO_HASH; // const timestamp = genesis_config.genesis_time + slot * params.SECONDS_PER_SLOT; @@ -331,7 +331,7 @@ pub fn genMockChain(allocator: Allocator, numBlocks: usize, from_genesis: ?types // prepare pre state to process block for that slot, may be rename prepare_pre_state try transition.apply_raw_block(allocator, &beam_state, &block, block_building_logger); - try ssz.hashTreeRoot(types.BeamBlock, block, &block_root, allocator); + try zeam_utils.hashTreeRoot(types.BeamBlock, block, &block_root, allocator); // generate the signed beam block and add to block list const block_with_attestation = types.BlockWithAttestation{ diff --git a/pkgs/state-transition/src/transition.zig b/pkgs/state-transition/src/transition.zig index d2bf061c..bcfda6de 100644 --- a/pkgs/state-transition/src/transition.zig +++ b/pkgs/state-transition/src/transition.zig @@ -1,4 +1,3 @@ -const ssz = @import("ssz"); const std = @import("std"); const json = std.json; const types = @import("@zeam/types"); @@ -51,7 +50,7 @@ pub fn apply_raw_block(allocator: Allocator, state: *types.BeamState, block: *ty logger.debug("extracting state root\n", .{}); // extract the post state root var state_root: [32]u8 = undefined; - try ssz.hashTreeRoot(*types.BeamState, state, &state_root, allocator); + try zeam_utils.hashTreeRoot(*types.BeamState, state, &state_root, allocator); block.state_root = state_root; } @@ -117,7 +116,7 @@ pub fn verifySignatures( // Compute message hash from attestation data var message_hash: [32]u8 = undefined; - try ssz.hashTreeRoot(types.AttestationData, aggregated_attestation.data, &message_hash, allocator); + try zeam_utils.hashTreeRoot(types.AttestationData, aggregated_attestation.data, &message_hash, allocator); const epoch: u64 = aggregated_attestation.data.slot; @@ -156,7 +155,7 @@ pub fn verifySingleAttestation( const verification_timer = zeam_metrics.lean_pq_signature_attestation_verification_time_seconds.start(); var message: [32]u8 = undefined; - try ssz.hashTreeRoot(types.AttestationData, attestation_data.*, &message, allocator); + try zeam_utils.hashTreeRoot(types.AttestationData, attestation_data.*, &message, allocator); const epoch: u32 = @intCast(attestation_data.slot); @@ -187,7 +186,7 @@ pub fn apply_transition(allocator: Allocator, state: *types.BeamState, block: ty if (validateResult) { // verify the post state root var state_root: [32]u8 = undefined; - try ssz.hashTreeRoot(*types.BeamState, state, &state_root, allocator); + try zeam_utils.hashTreeRoot(*types.BeamState, state, &state_root, allocator); if (!std.mem.eql(u8, &state_root, &block.state_root)) { opts.logger.debug("state root={x:02} block root={x:02}\n", .{ state_root, block.state_root }); return StateTransitionError.InvalidPostState; diff --git a/pkgs/types/src/aggregation.zig b/pkgs/types/src/aggregation.zig index e1bade18..9bcbf102 100644 --- a/pkgs/types/src/aggregation.zig +++ b/pkgs/types/src/aggregation.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const ssz = @import("ssz"); const params = @import("@zeam/params"); const xmss = @import("@zeam/xmss"); diff --git a/pkgs/types/src/attestation.zig b/pkgs/types/src/attestation.zig index 874256c6..2a950514 100644 --- a/pkgs/types/src/attestation.zig +++ b/pkgs/types/src/attestation.zig @@ -2,6 +2,7 @@ const std = @import("std"); const ssz = @import("ssz"); const params = @import("@zeam/params"); +const zeam_utils = @import("@zeam/utils"); const mini_3sf = @import("./mini_3sf.zig"); const utils = @import("./utils.zig"); @@ -31,7 +32,7 @@ pub const AttestationData = struct { pub fn sszRoot(self: *const AttestationData, allocator: Allocator) !Root { var root: Root = undefined; - try ssz.hashTreeRoot(AttestationData, self.*, &root, allocator); + try zeam_utils.hashTreeRoot(AttestationData, self.*, &root, allocator); return root; } diff --git a/pkgs/types/src/block.zig b/pkgs/types/src/block.zig index 24775f65..0fb43e20 100644 --- a/pkgs/types/src/block.zig +++ b/pkgs/types/src/block.zig @@ -3,6 +3,7 @@ const ssz = @import("ssz"); const params = @import("@zeam/params"); const xmss = @import("@zeam/xmss"); +const zeam_utils = @import("@zeam/utils"); const aggregation = @import("./aggregation.zig"); const attestation = @import("./attestation.zig"); @@ -154,7 +155,7 @@ pub const BeamBlock = struct { pub fn blockToHeader(self: *const Self, allocator: Allocator) !BeamBlockHeader { var body_root: [32]u8 = undefined; - try ssz.hashTreeRoot( + try zeam_utils.hashTreeRoot( BeamBlockBody, self.body, &body_root, @@ -172,7 +173,7 @@ pub const BeamBlock = struct { pub fn blockToLatestBlockHeader(self: *const Self, allocator: Allocator, header: *BeamBlockHeader) !void { var body_root: [32]u8 = undefined; - try ssz.hashTreeRoot( + try zeam_utils.hashTreeRoot( BeamBlockBody, self.body, &body_root, @@ -381,7 +382,7 @@ pub const AggregatedAttestationsResult = struct { const data_root = group.data_root; const epoch: u64 = group.data.slot; var message_hash: [32]u8 = undefined; - try ssz.hashTreeRoot(attestation.AttestationData, group.data, &message_hash, allocator); + try zeam_utils.hashTreeRoot(attestation.AttestationData, group.data, &message_hash, allocator); // Phase 1: Collect signatures from signatures_map const max_validator = group.validator_bits.capacity(); @@ -744,7 +745,7 @@ test "ssz seralize/deserialize signed beam block" { try std.testing.expect(std.mem.eql(u8, &signed_block.message.block.parent_root, &deserialized_signed_block.message.block.parent_root)); var block_root: [32]u8 = undefined; - try ssz.hashTreeRoot(BeamBlock, signed_block.message.block, &block_root, std.testing.allocator); + try zeam_utils.hashTreeRoot(BeamBlock, signed_block.message.block, &block_root, std.testing.allocator); } test "blockToLatestBlockHeader and blockToHeader" { diff --git a/pkgs/types/src/block_signatures_testing.zig b/pkgs/types/src/block_signatures_testing.zig index bfe81a88..7cb4758f 100644 --- a/pkgs/types/src/block_signatures_testing.zig +++ b/pkgs/types/src/block_signatures_testing.zig @@ -3,6 +3,7 @@ const ssz = @import("ssz"); const params = @import("@zeam/params"); const xmss = @import("@zeam/xmss"); +const zeam_utils = @import("@zeam/utils"); const aggregation = @import("./aggregation.zig"); const attestation = @import("./attestation.zig"); @@ -162,7 +163,7 @@ const TestContext = struct { // Compute message hash var message_hash: [32]u8 = undefined; - try ssz.hashTreeRoot(attestation.AttestationData, self.attestation_data, &message_hash, self.allocator); + try zeam_utils.hashTreeRoot(attestation.AttestationData, self.attestation_data, &message_hash, self.allocator); // Aggregate var proof = try aggregation.AggregatedSignatureProof.init(self.allocator); @@ -858,7 +859,7 @@ test "computeAggregatedSignatures: complex 3 groups" { } var message_hash: [32]u8 = undefined; - try ssz.hashTreeRoot(attestation.AttestationData, att_data_2, &message_hash, allocator); + try zeam_utils.hashTreeRoot(attestation.AttestationData, att_data_2, &message_hash, allocator); var proof = try aggregation.AggregatedSignatureProof.init(allocator); errdefer proof.deinit(); @@ -921,7 +922,7 @@ test "computeAggregatedSignatures: complex 3 groups" { } var message_hash: [32]u8 = undefined; - try ssz.hashTreeRoot(attestation.AttestationData, att_data_3, &message_hash, allocator); + try zeam_utils.hashTreeRoot(attestation.AttestationData, att_data_3, &message_hash, allocator); var proof = try aggregation.AggregatedSignatureProof.init(allocator); errdefer proof.deinit(); diff --git a/pkgs/types/src/mini_3sf.zig b/pkgs/types/src/mini_3sf.zig index c86f248e..7761361b 100644 --- a/pkgs/types/src/mini_3sf.zig +++ b/pkgs/types/src/mini_3sf.zig @@ -1,6 +1,4 @@ const std = @import("std"); -const ssz = @import("ssz"); - const params = @import("@zeam/params"); const utils = @import("./utils.zig"); diff --git a/pkgs/types/src/state.zig b/pkgs/types/src/state.zig index 15b0a27a..e738436b 100644 --- a/pkgs/types/src/state.zig +++ b/pkgs/types/src/state.zig @@ -245,7 +245,7 @@ pub const BeamState = struct { if (std.mem.eql(u8, &self.latest_block_header.state_root, &utils.ZERO_HASH)) { var prev_state_root: [32]u8 = undefined; - try ssz.hashTreeRoot(*BeamState, self, &prev_state_root, allocator); + try zeam_utils.hashTreeRoot(*BeamState, self, &prev_state_root, allocator); self.latest_block_header.state_root = prev_state_root; } } @@ -296,7 +296,7 @@ pub const BeamState = struct { // 4. verify latest block header is the parent var head_root: [32]u8 = undefined; - try ssz.hashTreeRoot(block.BeamBlockHeader, self.latest_block_header, &head_root, allocator); + try zeam_utils.hashTreeRoot(block.BeamBlockHeader, self.latest_block_header, &head_root, allocator); if (!std.mem.eql(u8, &head_root, &staged_block.parent_root)) { logger.err("state root={x:02} block root={x:02}\n", .{ head_root, staged_block.parent_root }); return StateTransitionError.InvalidParentRoot; @@ -539,7 +539,7 @@ pub const BeamState = struct { pub fn genGenesisBlock(self: *const Self, allocator: Allocator, genesis_block: *block.BeamBlock) !void { var state_root: [32]u8 = undefined; - try ssz.hashTreeRoot( + try zeam_utils.hashTreeRoot( BeamState, self.*, &state_root, @@ -554,7 +554,7 @@ pub const BeamState = struct { // check does it need cloning? var beam_block_header = self.latest_block_header; var state_root: [32]u8 = undefined; - try ssz.hashTreeRoot( + try zeam_utils.hashTreeRoot( BeamState, self.*, &state_root, @@ -719,7 +719,7 @@ test "ssz seralize/deserialize signed beam state" { // successful merklization var state_root: [32]u8 = undefined; - try ssz.hashTreeRoot( + try zeam_utils.hashTreeRoot( BeamState, state, &state_root, @@ -774,7 +774,7 @@ fn makeBlock( attestations: []const attestation.AggregatedAttestation, ) !block.BeamBlock { var parent_root: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &parent_root, allocator); + try zeam_utils.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &parent_root, allocator); var attestations_list = try block.AggregatedAttestations.init(allocator); errdefer attestations_list.deinit(); @@ -829,7 +829,7 @@ test "justified_slots rebases when finalization advances" { try state.process_slots(std.testing.allocator, 2, logger); var block_2_parent_root: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &block_2_parent_root, std.testing.allocator); + try zeam_utils.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &block_2_parent_root, std.testing.allocator); var att_0_to_1 = try makeAggregatedAttestation( std.testing.allocator, @@ -848,7 +848,7 @@ test "justified_slots rebases when finalization advances" { try state.process_slots(std.testing.allocator, 3, logger); var block_3_parent_root: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &block_3_parent_root, std.testing.allocator); + try zeam_utils.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &block_3_parent_root, std.testing.allocator); var att_1_to_2 = try makeAggregatedAttestation( std.testing.allocator, @@ -897,7 +897,7 @@ test "pruning keeps pending justifications" { try state.process_slots(std.testing.allocator, 2, logger); var block_2_parent_root: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &block_2_parent_root, std.testing.allocator); + try zeam_utils.hashTreeRoot(block.BeamBlockHeader, state.latest_block_header, &block_2_parent_root, std.testing.allocator); var att_0_to_1 = try makeAggregatedAttestation( std.testing.allocator, @@ -1083,7 +1083,7 @@ test "genesis block hash comparison" { // Compute hash of first genesis block var genesis_block_hash1: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlock, genesis_block1, &genesis_block_hash1, allocator); + try zeam_utils.hashTreeRoot(block.BeamBlock, genesis_block1, &genesis_block_hash1, allocator); std.debug.print("genesis_block_hash1 =0x{s}\n", .{std.fmt.fmtSliceHexLower(&genesis_block_hash1)}); // Create a second genesis state with same config but regenerated (should produce same hash) @@ -1096,7 +1096,7 @@ test "genesis block hash comparison" { defer genesis_block1_copy.deinit(); var genesis_block_hash1_copy: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlock, genesis_block1_copy, &genesis_block_hash1_copy, allocator); + try zeam_utils.hashTreeRoot(block.BeamBlock, genesis_block1_copy, &genesis_block_hash1_copy, allocator); // Same genesis spec should produce same hash try std.testing.expect(std.mem.eql(u8, &genesis_block_hash1, &genesis_block_hash1_copy)); @@ -1125,7 +1125,7 @@ test "genesis block hash comparison" { defer genesis_block2.deinit(); var genesis_block_hash2: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlock, genesis_block2, &genesis_block_hash2, allocator); + try zeam_utils.hashTreeRoot(block.BeamBlock, genesis_block2, &genesis_block_hash2, allocator); std.debug.print("genesis_block_hash2 =0x{s}\n", .{std.fmt.fmtSliceHexLower(&genesis_block_hash2)}); // Different validators should produce different genesis block hash @@ -1155,7 +1155,7 @@ test "genesis block hash comparison" { defer genesis_block3.deinit(); var genesis_block_hash3: Root = undefined; - try ssz.hashTreeRoot(block.BeamBlock, genesis_block3, &genesis_block_hash3, allocator); + try zeam_utils.hashTreeRoot(block.BeamBlock, genesis_block3, &genesis_block_hash3, allocator); std.debug.print("genesis_block_hash3 =0x{s}\n", .{std.fmt.fmtSliceHexLower(&genesis_block_hash3)}); // Different genesis_time should produce different genesis block hash diff --git a/pkgs/utils/src/lib.zig b/pkgs/utils/src/lib.zig index a00e8bf9..b93f686c 100644 --- a/pkgs/utils/src/lib.zig +++ b/pkgs/utils/src/lib.zig @@ -37,6 +37,9 @@ const json_factory = @import("./json.zig"); // Avoid to use `usingnamespace` to make upgrade easier in the future. pub const jsonToString = json_factory.jsonToString; +const ssz_factory = @import("./ssz.zig"); +pub const hashTreeRoot = ssz_factory.hashTreeRoot; + const fmt_factory = @import("./fmt.zig"); // Avoid to use `usingnamespace` to make upgrade easier in the future. pub const LazyJson = fmt_factory.LazyJson; diff --git a/pkgs/utils/src/ssz.zig b/pkgs/utils/src/ssz.zig new file mode 100644 index 00000000..a92164cf --- /dev/null +++ b/pkgs/utils/src/ssz.zig @@ -0,0 +1,14 @@ +const std = @import("std"); +const ssz = @import("ssz"); + +const Allocator = std.mem.Allocator; +const Sha256 = std.crypto.hash.sha2.Sha256; + +pub fn hashTreeRoot( + comptime T: type, + value: T, + out: *[Sha256.digest_length]u8, + allocator: Allocator, +) !void { + try ssz.hashTreeRoot(Sha256, T, value, out, allocator); +}