| ... | @@ -32,24 +32,18 @@ pub const X25519 = struct { | ... | @@ -32,24 +32,18 @@ pub const X25519 = struct { |
| 32 | /// Deterministically derive a key pair from a cryptograpically secure secret seed. | 32 | /// Deterministically derive a key pair from a cryptograpically secure secret seed. |
| 33 | /// | 33 | /// |
| 34 | /// Except in tests, applications should generally call `generate()` instead of this function. | 34 | /// Except in tests, applications should generally call `generate()` instead of this function. |
| 35 | pub fn generateDeterministic(seed: [seed_length]u8) IdentityElementError!KeyPair { | 35 | pub fn generateDeterministic(seed: [seed_length]u8) KeyPair { |
| 36 | const kp = KeyPair{ | 36 | return .{ |
| 37 | .public_key = try X25519.recoverPublicKey(seed), | 37 | .public_key = X25519.recoverPublicKey(seed), |
| 38 | .secret_key = seed, | 38 | .secret_key = seed, |
| 39 | }; | 39 | }; |
| 40 | return kp; | | |
| 41 | } | 40 | } |
| 42 | | 41 | |
| 43 | /// Generate a new, random key pair. | 42 | /// Generate a new, random key pair. |
| 44 | pub fn generate(io: std.Io) KeyPair { | 43 | pub fn generate(io: std.Io) KeyPair { |
| 45 | var random_seed: [seed_length]u8 = undefined; | 44 | var random_seed: [seed_length]u8 = undefined; |
| 46 | while (true) { | 45 | io.random(&random_seed); |
| 47 | io.random(&random_seed); | 46 | return generateDeterministic(random_seed); |
| 48 | return generateDeterministic(random_seed) catch { | | |
| 49 | @branchHint(.unlikely); | | |
| 50 | continue; | | |
| 51 | }; | | |
| 52 | } | | |
| 53 | } | 47 | } |
| 54 | | 48 | |
| 55 | /// Create a key pair from an Ed25519 key pair | 49 | /// Create a key pair from an Ed25519 key pair |
| ... | @@ -68,8 +62,8 @@ pub const X25519 = struct { | ... | @@ -68,8 +62,8 @@ pub const X25519 = struct { |
| 68 | }; | 62 | }; |
| 69 | | 63 | |
| 70 | /// Compute the public key for a given private key. | 64 | /// Compute the public key for a given private key. |
| 71 | pub fn recoverPublicKey(secret_key: [secret_length]u8) IdentityElementError![public_length]u8 { | 65 | pub fn recoverPublicKey(secret_key: [secret_length]u8) [public_length]u8 { |
| 72 | const q = try Curve.basePoint.clampedMul(secret_key); | 66 | const q = Curve.basePoint.clampedMul(secret_key) catch unreachable; |
| 73 | return q.toBytes(); | 67 | return q.toBytes(); |
| 74 | } | 68 | } |
| 75 | | 69 | |
| ... | @@ -96,7 +90,7 @@ test "public key calculation from secret key" { | ... | @@ -96,7 +90,7 @@ test "public key calculation from secret key" { |
| 96 | var pk_expected: [32]u8 = undefined; | 90 | var pk_expected: [32]u8 = undefined; |
| 97 | _ = try fmt.hexToBytes(sk[0..], "8052030376d47112be7f73ed7a019293dd12ad910b654455798b4667d73de166"); | 91 | _ = try fmt.hexToBytes(sk[0..], "8052030376d47112be7f73ed7a019293dd12ad910b654455798b4667d73de166"); |
| 98 | _ = try fmt.hexToBytes(pk_expected[0..], "f1814f0e8ff1043d8a44d25babff3cedcae6c22c3edaa48f857ae70de2baae50"); | 92 | _ = try fmt.hexToBytes(pk_expected[0..], "f1814f0e8ff1043d8a44d25babff3cedcae6c22c3edaa48f857ae70de2baae50"); |
| 99 | const pk_calculated = try X25519.recoverPublicKey(sk); | 93 | const pk_calculated = X25519.recoverPublicKey(sk); |
| 100 | try std.testing.expectEqual(pk_calculated, pk_expected); | 94 | try std.testing.expectEqual(pk_calculated, pk_expected); |
| 101 | } | 95 | } |
| 102 | | 96 | |