Skip to content

Commit

Permalink
manage non-canonical bytes when deserializing scalar field
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <[email protected]>
  • Loading branch information
jsign committed Dec 17, 2023
1 parent 0fed61c commit 5541b8b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/stealth_address.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ pub const EIP5564 = struct {
Keccak256.hash(&s.toCompressedSec1(), &s_hashed, .{});

const fe_spending_key = try Secp256k1.scalar.Scalar.fromBytes(spending_key, Endian.Big);
const fe_s_hashed = try Secp256k1.scalar.Scalar.fromBytes(s_hashed, Endian.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);

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

0 comments on commit 5541b8b

Please sign in to comment.