Skip to content

Commit 5541b8b

Browse files
committed
manage non-canonical bytes when deserializing scalar field
Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent 0fed61c commit 5541b8b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/stealth_address.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ pub const EIP5564 = struct {
6767
Keccak256.hash(&s.toCompressedSec1(), &s_hashed, .{});
6868

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

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

0 commit comments

Comments
 (0)