-
Notifications
You must be signed in to change notification settings - Fork 10
Feat/Hashers: Implement sha256d :fixes #3 #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
src/hashes/sha256d.zig
Outdated
|
|
||
| const result = engine.final(); | ||
|
|
||
| try std.testing.expect(std.mem.eql(u8, result, expected_hash)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u can use here std.testing.expectEqualSlices
src/hashes/sha256d.zig
Outdated
| @@ -0,0 +1,56 @@ | |||
| const std = @import("std"); | |||
| const Hash = std.crypto.sha256; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it okay? std.crypto.sha256 is located in std.crypto.hash.sha2.Sha256
|
also u need import here sha256d, currently zig build test dont see ur test bitcoin-primitives/src/hashes/lib.zig Line 1 in 501399a
|
src/hashes/sha256d.zig
Outdated
| return HashEngine{ | ||
| .sha256_engine = Hash.Context.init(), | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return HashEngine{ | |
| .sha256_engine = Hash.Context.init(), | |
| }; | |
| return .{ | |
| .sha256_engine = Hash.Context.init(), | |
| }; |
src/hashes/sha256d.zig
Outdated
| const std = @import("std"); | ||
| const Hash = std.crypto.sha256; | ||
|
|
||
| pub const HashEngine = struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub const HashEngine = struct { | |
| pub const HashEngine = struct { | |
| const Self = @This(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will also suggest we rename this from HashEngine to Sha256d
src/hashes/sha256d.zig
Outdated
| pub const HashEngine = struct { | ||
| sha256_engine: Hash.Context, | ||
|
|
||
| pub fn new() HashEngine { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub fn new() HashEngine { | |
| pub fn init() Self { |
src/hashes/sha256d.zig
Outdated
| }; | ||
| } | ||
|
|
||
| pub fn input(self: *HashEngine, data: []const u8) void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub fn input(self: *HashEngine, data: []const u8) void { | |
| pub fn input(self: *Self, data: []const u8) void { |
src/hashes/sha256d.zig
Outdated
| self.sha256_engine.update(data); | ||
| } | ||
|
|
||
| pub fn n_bytes_hashed(self: *const HashEngine) usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub fn n_bytes_hashed(self: *const HashEngine) usize { | |
| pub fn n_bytes_hashed(self: *const Self) usize { |
src/hashes/sha256d.zig
Outdated
| return self.sha256_engine.total_len; | ||
| } | ||
|
|
||
| pub fn final(self: *HashEngine) []u8 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub fn final(self: *HashEngine) []u8 { | |
| pub fn final(self: *Self) []u8 { |
src/hashes/sha256d.zig
Outdated
|
|
||
| const result = engine.final(); | ||
|
|
||
| try std.testing.expect(std.mem.eql(u8, result, expected_hash)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectEqualSlices is too
src/hashes/sha256d.zig
Outdated
| return self.sha256_engine.total_len; | ||
| } | ||
|
|
||
| pub fn final(self: *HashEngine) []u8 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here why can't you return a [32]u8 as you know the size of the output at compile time no?
src/hashes/sha256d.zig
Outdated
| sha2_engine.update(&sha1_result); | ||
|
|
||
| var sha2_result: [32]u8 = undefined; | ||
| sha2_engine.final(&sha2_result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we reuse here sha1_result, and not create a second buffer for output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
No description provided.