Skip to content

Commit

Permalink
upgrade to zig v0.12
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed May 4, 2024
1 parent f800699 commit 1763ed9
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/eip5564.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub const EIP5564 = struct {

var ephemeral_priv: Privkey = undefined;
std.crypto.random.bytes(&ephemeral_priv);
const ephemeral_pubkey = try Secp256k1.mul(Secp256k1.basePoint, ephemeral_priv, Endian.Big);
const ephemeral_pubkey = try Secp256k1.mul(Secp256k1.basePoint, ephemeral_priv, .big);

const spend_pubkey = try pubKeyFromHex(sma[format_prefix.len .. format_prefix.len + 2 * n]);
const view_pubkey = try pubKeyFromHex(sma[format_prefix.len + 2 * n ..]);

const s = try Secp256k1.mul(view_pubkey, ephemeral_priv, Endian.Big);
const s = try Secp256k1.mul(view_pubkey, ephemeral_priv, .big);
var s_hashed: [Keccak256.digest_length]u8 = undefined;
Keccak256.hash(&s.toCompressedSec1(), &s_hashed, .{});
const view_tag = s_hashed[0];

const pub_s_hashed = try Secp256k1.mul(Secp256k1.basePoint, s_hashed, Endian.Big);
const pub_s_hashed = try Secp256k1.mul(Secp256k1.basePoint, s_hashed, .big);
const pub_stealth_address_point = Secp256k1.add(spend_pubkey, pub_s_hashed);

return .{
Expand All @@ -46,34 +46,34 @@ pub const EIP5564 = struct {
}

pub fn checkStealthAddress(stealth_address: EthAddress, ephemeral_pubkey: Secp256k1, viewing_key: Privkey, spending_pubkey: Secp256k1, view_tag: ?u8) !bool {
const s = try Secp256k1.mul(ephemeral_pubkey, viewing_key, Endian.Big);
const s = try Secp256k1.mul(ephemeral_pubkey, viewing_key, .big);
var s_hashed: [Keccak256.digest_length]u8 = undefined;
Keccak256.hash(&s.toCompressedSec1(), &s_hashed, .{});

// If the view tag is provided, we do the optimized check.
if (view_tag != null and view_tag.? != s_hashed[0])
return false;

const pub_s_hashed = try Secp256k1.mul(Secp256k1.basePoint, s_hashed, Endian.Big);
const pub_s_hashed = try Secp256k1.mul(Secp256k1.basePoint, s_hashed, .big);
const pub_stealth_address = Secp256k1.add(spending_pubkey, pub_s_hashed);
const exp_stealth_address = pointToEthAddr(pub_stealth_address);

return std.mem.eql(u8, &stealth_address, &exp_stealth_address);
}

pub fn computeStealthKey(ephemeral_pubkey: Secp256k1, viewing_key: Privkey, spending_key: Privkey) !Privkey {
const s = try Secp256k1.mul(ephemeral_pubkey, viewing_key, Endian.Big);
const s = try Secp256k1.mul(ephemeral_pubkey, viewing_key, .big);
var s_hashed: [Keccak256.digest_length]u8 = undefined;
Keccak256.hash(&s.toCompressedSec1(), &s_hashed, .{});

const fe_spending_key = try Secp256k1.scalar.Scalar.fromBytes(spending_key, Endian.Big);
const fe_spending_key = try Secp256k1.scalar.Scalar.fromBytes(spending_key, .big);
// A direct .fromBytes(...) errors on non-canonical representations, so we pad it to use
// .fromBytes48(...) which does the (potentially needed) wrapping.
var padded_s_hashed: [48]u8 = [_]u8{0} ** 48;
@memcpy(padded_s_hashed[padded_s_hashed.len - 32 ..], &s_hashed);
const fe_s_hashed = Secp256k1.scalar.Scalar.fromBytes48(padded_s_hashed, Endian.Big);
const fe_s_hashed = Secp256k1.scalar.Scalar.fromBytes48(padded_s_hashed, .big);

return Secp256k1.scalar.Scalar.add(fe_spending_key, fe_s_hashed).toBytes(Endian.Big);
return Secp256k1.scalar.Scalar.add(fe_spending_key, fe_s_hashed).toBytes(.big);
}

fn pubKeyFromHex(hex: []const u8) !Secp256k1 {
Expand Down Expand Up @@ -145,7 +145,7 @@ test "generate and check" {
// Compute stealth key and verify with expected stealth address.
{
const got_privkey = try EIP5564.computeStealthKey(ga.ephemeral_pubkey, viewing_key, spending_key);
const got_stealth_addr_point = try Secp256k1.mul(Secp256k1.basePoint, got_privkey, Endian.Big);
const got_stealth_addr_point = try Secp256k1.mul(Secp256k1.basePoint, got_privkey, .big);
const got_eth_addr = EIP5564.pointToEthAddr(got_stealth_addr_point);
try std.testing.expect(std.mem.eql(u8, &ga.stealth_address, &got_eth_addr));
}
Expand Down

0 comments on commit 1763ed9

Please sign in to comment.