authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2022-11-01 07:10:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-02 14:09:18-07:00
log26e7d4c9a37d170896b5f5b9170ccdee077d229f
tree46f552268a46ba49ee21ab61826417c856ce6858
parentf366f381f57cbc4d4599107d4d151887d8d0eb17

Ed25519.KeyPair.fromSecretKey() didn't compile after the API changes (#13386)

Fixes #13378

1 files changed, 14 insertions(+), 7 deletions(-)

lib/std/crypto/25519/ed25519.zig+14-7
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
1const std = @import("std");2const std = @import("std");
2const crypto = std.crypto;3const crypto = std.crypto;
3const debug = std.debug;4const debug = std.debug;
...@@ -268,18 +269,17 @@ pub const Ed25519 = struct {...@@ -268,18 +269,17 @@ pub const Ed25519 = struct {
268 }269 }
269270
270 /// Create a KeyPair from a secret key.271 /// Create a KeyPair from a secret key.
271 pub fn fromSecretKey(secret_key: SecretKey) IdentityElementError!KeyPair {272 pub fn fromSecretKey(secret_key: SecretKey) (NonCanonicalError || EncodingError || IdentityElementError)!KeyPair {
272 const pk_p = try Curve.fromBytes(secret_key.publicKeyBytes());
273
274 // It is critical for EdDSA to use the correct public key.273 // It is critical for EdDSA to use the correct public key.
275 // In order to enforce this, a SecretKey implicitly includes a copy of the public key.274 // In order to enforce this, a SecretKey implicitly includes a copy of the public key.
276 // In Debug mode, we can still afford checking that the public key is correct for extra safety.275 // In Debug mode, we can still afford checking that the public key is correct for extra safety.
277 if (std.builtin.mode == .Debug) {276 if (builtin.mode == .Debug) {
278 const recomputed_kp = try create(secret_key[0..seed_length].*);277 const pk_p = try Curve.fromBytes(secret_key.publicKeyBytes());
279 debug.assert(recomputed_kp.public_key.p.toBytes() == pk_p.toBytes());278 const recomputed_kp = try create(secret_key.seed());
279 debug.assert(mem.eql(u8, &recomputed_kp.public_key.toBytes(), &pk_p.toBytes()));
280 }280 }
281 return KeyPair{281 return KeyPair{
282 .public_key = PublicKey{ .p = pk_p },282 .public_key = try PublicKey.fromBytes(secret_key.publicKeyBytes()),
283 .secret_key = secret_key,283 .secret_key = secret_key,
284 };284 };
285 }285 }
...@@ -674,3 +674,10 @@ test "ed25519 signatures with streaming" {...@@ -674,3 +674,10 @@ test "ed25519 signatures with streaming" {
674 verifier.update("age");674 verifier.update("age");
675 try verifier.verify();675 try verifier.verify();
676}676}
677
678test "ed25519 key pair from secret key" {
679 const kp = try Ed25519.KeyPair.create(null);
680 const kp2 = try Ed25519.KeyPair.fromSecretKey(kp.secret_key);
681 try std.testing.expectEqualSlices(u8, &kp.secret_key.toBytes(), &kp2.secret_key.toBytes());
682 try std.testing.expectEqualSlices(u8, &kp.public_key.toBytes(), &kp2.public_key.toBytes());
683}