Skip to content

Commit

Permalink
Merge pull request #1334 from tigerbeetle/matklad/checksum-empty
Browse files Browse the repository at this point in the history
vsr: make `comptime checksum(&.{})` available everywhere
  • Loading branch information
matklad committed Dec 4, 2023
2 parents 4c2b0c4 + 047c5ff commit 7b70789
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/vsr/checksum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ fn seed_init() void {
// Lazily initialize the Aegis State instead of recomputing it on each call to checksum().
// Then, make a copy of the state and use that to hash the source input bytes.
pub fn checksum(source: []const u8) u128 {
if (@inComptime()) {
// Aegis128 uses hardware accelerated AES via inline asm which isn't available at comptime.
// Use a hard-coded value instead and verify via a test.
if (source.len == 0) return 0x49F174618255402DE6E7E3C40D60CC83;
}
var stream = ChecksumStream.init();
stream.add(source);
return stream.checksum();
}

test "checksum empty" {
var stream = ChecksumStream.init();
stream.add(&.{});
try std.testing.expectEqual(stream.checksum(), comptime checksum(&.{}));
}

pub const ChecksumStream = struct {
state: Aegis128LMac_128,

Expand Down
8 changes: 1 addition & 7 deletions src/vsr/message_header.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ const Command = vsr.Command;
const Operation = vsr.Operation;
const schema = @import("../lsm/schema.zig");

/// Aegis128 (used by checksum) uses hardware accelerated AES via inline asm which isn't
/// available at comptime. Use a hard-coded value instead and verify via a test.
const checksum_body_empty: u128 = 0x49F174618255402DE6E7E3C40D60CC83;

test "empty checksum" {
assert(checksum_body_empty == vsr.checksum(&.{}));
}
const checksum_body_empty = vsr.checksum(&.{});

/// Network message and journal entry header:
/// We reuse the same header for both so that prepare messages from the primary can simply be
Expand Down

0 comments on commit 7b70789

Please sign in to comment.