Skip to content

Feat/Hashers: Implement sha256d :fixes #3#23

Open
manoahLinks wants to merge 6 commits intozig-bitcoin:mainfrom
manoahLinks:sha256d
Open

Feat/Hashers: Implement sha256d :fixes #3#23
manoahLinks wants to merge 6 commits intozig-bitcoin:mainfrom
manoahLinks:sha256d

Conversation

@manoahLinks
Copy link
Copy Markdown

No description provided.

Comment thread src/hashes/sha256d.zig Outdated

const result = engine.final();

try std.testing.expect(std.mem.eql(u8, result, expected_hash));
Copy link
Copy Markdown
Collaborator

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

Comment thread src/hashes/sha256d.zig Outdated
@@ -0,0 +1,56 @@
const std = @import("std");
const Hash = std.crypto.sha256;
Copy link
Copy Markdown
Collaborator

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

@StringNick
Copy link
Copy Markdown
Collaborator

also u need import here sha256d, currently zig build test dont see ur test

pub const Ripemd160 = @import("ripemd160.zig").Ripemd160;

Comment thread src/hashes/sha256d.zig Outdated
Comment on lines +8 to +10
return HashEngine{
.sha256_engine = Hash.Context.init(),
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return HashEngine{
.sha256_engine = Hash.Context.init(),
};
return .{
.sha256_engine = Hash.Context.init(),
};

Comment thread src/hashes/sha256d.zig Outdated
const std = @import("std");
const Hash = std.crypto.sha256;

pub const HashEngine = struct {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const HashEngine = struct {
pub const HashEngine = struct {
const Self = @This();

Copy link
Copy Markdown
Contributor

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

Comment thread src/hashes/sha256d.zig Outdated
pub const HashEngine = struct {
sha256_engine: Hash.Context,

pub fn new() HashEngine {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn new() HashEngine {
pub fn init() Self {

Comment thread src/hashes/sha256d.zig Outdated
};
}

pub fn input(self: *HashEngine, data: []const u8) void {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn input(self: *HashEngine, data: []const u8) void {
pub fn input(self: *Self, data: []const u8) void {

Comment thread src/hashes/sha256d.zig Outdated
self.sha256_engine.update(data);
}

pub fn n_bytes_hashed(self: *const HashEngine) usize {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn n_bytes_hashed(self: *const HashEngine) usize {
pub fn n_bytes_hashed(self: *const Self) usize {

Comment thread src/hashes/sha256d.zig Outdated
return self.sha256_engine.total_len;
}

pub fn final(self: *HashEngine) []u8 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn final(self: *HashEngine) []u8 {
pub fn final(self: *Self) []u8 {

Comment thread src/hashes/sha256d.zig Outdated

const result = engine.final();

try std.testing.expect(std.mem.eql(u8, result, expected_hash));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expectEqualSlices is too

Comment thread src/hashes/sha256d.zig Outdated
return self.sha256_engine.total_len;
}

pub fn final(self: *HashEngine) []u8 {
Copy link
Copy Markdown
Collaborator

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?

Comment thread src/hashes/sha256d.zig Outdated
sha2_engine.update(&sha1_result);

var sha2_result: [32]u8 = undefined;
sha2_engine.final(&sha2_result);
Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants