Skip to content

Commit 3809696

Browse files
authored
crypto.sign.ecdsa: fix toCompressedSec1()/toUnompressedSec1() (#12009)
The Ecdsa.PublicKey type is not a direct alias for a curve element. So, use the inner field containing the curve element for serialization.
1 parent 6279a1d commit 3809696

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lib/std/crypto/ecdsa.zig

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
6262
}
6363

6464
/// Encode the public key using the compressed SEC-1 format.
65-
pub fn toCompressedSec1(p: Curve) [compressed_sec1_encoded_length]u8 {
66-
return p.toCompressedSec1();
65+
pub fn toCompressedSec1(pk: PublicKey) [compressed_sec1_encoded_length]u8 {
66+
return pk.p.toCompressedSec1();
6767
}
6868

6969
/// Encoding the public key using the uncompressed SEC-1 format.
70-
pub fn toUncompressedSec1(p: Curve) [uncompressed_sec1_encoded_length]u8 {
71-
return p.toUncompressedSec1();
70+
pub fn toUncompressedSec1(pk: PublicKey) [uncompressed_sec1_encoded_length]u8 {
71+
return pk.p.toUncompressedSec1();
7272
}
7373
};
7474

@@ -743,3 +743,15 @@ fn tvTry(vector: TestVector) !void {
743743
const sig = try Scheme.Signature.fromDer(sig_der);
744744
try sig.verify(msg, pk);
745745
}
746+
747+
test "ECDSA - Sec1 encoding/decoding" {
748+
const Scheme = EcdsaP384Sha384;
749+
const kp = try Scheme.KeyPair.create(null);
750+
const pk = kp.public_key;
751+
const pk_compressed_sec1 = pk.toCompressedSec1();
752+
const pk_recovered1 = try Scheme.PublicKey.fromSec1(&pk_compressed_sec1);
753+
try testing.expectEqualSlices(u8, &pk_recovered1.toCompressedSec1(), &pk_compressed_sec1);
754+
const pk_uncompressed_sec1 = pk.toUncompressedSec1();
755+
const pk_recovered2 = try Scheme.PublicKey.fromSec1(&pk_uncompressed_sec1);
756+
try testing.expectEqualSlices(u8, &pk_recovered2.toUncompressedSec1(), &pk_uncompressed_sec1);
757+
}

0 commit comments

Comments
 (0)