Skip to content

Commit

Permalink
base58: refactor and doc (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Sep 3, 2024
1 parent 997841f commit f3c8165
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/base58/alphabet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub const Alphabet = struct {
i += 1;
}

return Self{
return .{
.encode = encode,
.decode = decode,
};
Expand Down
27 changes: 11 additions & 16 deletions src/base58/decode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub const Decoder = struct {

/// Pass a `encoded` and a `dest` to write decoded value into. `decode` returns a
/// `usize` indicating how many bytes were written. Sizing/resizing, `dest` buffer is up to the caller.
///
/// For further information on the Base58 decoding algorithm, see: https://datatracker.ietf.org/doc/html/draft-msporny-base58-03
pub fn decode(self: *const Self, encoded: []const u8, dest: []u8) !usize {
var index: usize = 0;
const zero = self.alpha.encode[0];
Expand All @@ -44,7 +46,7 @@ pub const Decoder = struct {
while (x < index) : (x += 1) {
const byte = &dest[x];
val += @as(usize, @intCast(byte.*)) * 58;
byte.* = @as(u8, @intCast(val & 0xFF));
byte.* = @intCast(val & 0xFF);
val >>= 8;
}

Expand All @@ -53,21 +55,17 @@ pub const Decoder = struct {
return error.BufferTooSmall;
}

const byte = &dest[index];
byte.* = @as(u8, @intCast(val)) & 0xFF;
dest[index] = @as(u8, @intCast(val)) & 0xFF;
index += 1;
val >>= 8;
}
}

for (encoded) |*c| {
if (c.* == zero) {
const byte = &dest[index];
byte.* = 0;
index += 1;
} else {
break;
}
for (encoded) |c| {
if (c != zero) break;

dest[index] = 0;
index += 1;
}

std.mem.reverse(u8, dest[0..index]);
Expand All @@ -94,11 +92,8 @@ pub const Decoder = struct {
hasher = std.crypto.hash.sha2.Sha256.init(.{});
hasher.update(&fr);

const hash_check = hasher.finalResult()[0..4].*;
const data_check = decoded[check_start..][0..4].*;

const expected = std.mem.readInt(u32, &hash_check, .little);
const actual = std.mem.readInt(u32, &data_check, .little);
const expected = std.mem.readInt(u32, hasher.finalResult()[0..4], .little);
const actual = std.mem.readInt(u32, decoded[check_start..][0..4], .little);

if (expected != actual) return error.IncorrectChecksum;

Expand Down

0 comments on commit f3c8165

Please sign in to comment.