-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptimized_hash_v2.zig
More file actions
47 lines (37 loc) · 1.5 KB
/
optimized_hash_v2.zig
File metadata and controls
47 lines (37 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Optimized hash implementation for Rust-compatible signatures
//! Version 2 - focuses on performance without caching complexity
const std = @import("std");
const params = @import("params.zig");
const rust_poseidon2 = @import("poseidon2/poseidon2.zig").Poseidon2;
const Parameters = params.Parameters;
const Allocator = std.mem.Allocator;
/// Optimized hash implementation without caching for simplicity
pub const OptimizedHashV2 = struct {
params: Parameters,
allocator: Allocator,
pub fn init(allocator: Allocator, parameters: Parameters) !OptimizedHashV2 {
return .{
.params = parameters,
.allocator = allocator,
};
}
pub fn deinit(self: *OptimizedHashV2) void {
_ = self;
}
pub fn hash(self: *OptimizedHashV2, allocator: Allocator, data: []const u8, tweak: u64) ![]u8 {
_ = self;
// Create tweaked input: tweak (8 bytes) + data
var tweaked_data = try allocator.alloc(u8, 8 + data.len);
defer allocator.free(tweaked_data);
std.mem.writeInt(u64, tweaked_data[0..8], tweak, .big);
@memcpy(tweaked_data[8..], data);
// Use Rust-compatible Poseidon2
const hash_result = rust_poseidon2.hash(tweaked_data);
// Return a copy
const result = try allocator.dupe(u8, &hash_result);
return result;
}
pub fn prfHash(self: *OptimizedHashV2, allocator: Allocator, key: []const u8, index: u64) ![]u8 {
return self.hash(allocator, key, index);
}
};