| ... | @@ -289,18 +289,18 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -289,18 +289,18 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 289 | /// Secret scalar. | 289 | /// Secret scalar. |
| 290 | secret_key: SecretKey, | 290 | secret_key: SecretKey, |
| 291 | | 291 | |
| | 292 | /// Create a new random key pair. `crypto.random.bytes` must be supported for the target. |
| | 293 | pub fn generate() IdentityElementError!KeyPair { |
| | 294 | var random_seed: [seed_length]u8 = undefined; |
| | 295 | crypto.random.bytes(&random_seed); |
| | 296 | return create(random_seed); |
| | 297 | } |
| | 298 | |
| 292 | /// Create a new key pair. The seed must be secret and indistinguishable from random. | 299 | /// Create a new key pair. The seed must be secret and indistinguishable from random. |
| 293 | /// The seed can also be left to null in order to generate a random key pair. | 300 | pub fn create(seed: [seed_length]u8) IdentityElementError!KeyPair { |
| 294 | pub fn create(seed: ?[seed_length]u8) IdentityElementError!KeyPair { | | |
| 295 | var seed_ = seed; | | |
| 296 | if (seed_ == null) { | | |
| 297 | var random_seed: [seed_length]u8 = undefined; | | |
| 298 | crypto.random.bytes(&random_seed); | | |
| 299 | seed_ = random_seed; | | |
| 300 | } | | |
| 301 | const h = [_]u8{0x00} ** Hash.digest_length; | 301 | const h = [_]u8{0x00} ** Hash.digest_length; |
| 302 | const k0 = [_]u8{0x01} ** SecretKey.encoded_length; | 302 | const k0 = [_]u8{0x01} ** SecretKey.encoded_length; |
| 303 | const secret_key = deterministicScalar(h, k0, seed_).toBytes(.big); | 303 | const secret_key = deterministicScalar(h, k0, seed).toBytes(.big); |
| 304 | return fromSecretKey(SecretKey{ .bytes = secret_key }); | 304 | return fromSecretKey(SecretKey{ .bytes = secret_key }); |
| 305 | } | 305 | } |
| 306 | | 306 | |
| ... | @@ -380,7 +380,7 @@ test "Basic operations over EcdsaP384Sha384" { | ... | @@ -380,7 +380,7 @@ test "Basic operations over EcdsaP384Sha384" { |
| 380 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | 380 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 381 | | 381 | |
| 382 | const Scheme = EcdsaP384Sha384; | 382 | const Scheme = EcdsaP384Sha384; |
| 383 | const kp = try Scheme.KeyPair.create(null); | 383 | const kp = try Scheme.KeyPair.generate(); |
| 384 | const msg = "test"; | 384 | const msg = "test"; |
| 385 | | 385 | |
| 386 | var noise: [Scheme.noise_length]u8 = undefined; | 386 | var noise: [Scheme.noise_length]u8 = undefined; |
| ... | @@ -396,7 +396,7 @@ test "Basic operations over Secp256k1" { | ... | @@ -396,7 +396,7 @@ test "Basic operations over Secp256k1" { |
| 396 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | 396 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 397 | | 397 | |
| 398 | const Scheme = EcdsaSecp256k1Sha256oSha256; | 398 | const Scheme = EcdsaSecp256k1Sha256oSha256; |
| 399 | const kp = try Scheme.KeyPair.create(null); | 399 | const kp = try Scheme.KeyPair.generate(); |
| 400 | const msg = "test"; | 400 | const msg = "test"; |
| 401 | | 401 | |
| 402 | var noise: [Scheme.noise_length]u8 = undefined; | 402 | var noise: [Scheme.noise_length]u8 = undefined; |
| ... | @@ -412,7 +412,7 @@ test "Basic operations over EcdsaP384Sha256" { | ... | @@ -412,7 +412,7 @@ test "Basic operations over EcdsaP384Sha256" { |
| 412 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | 412 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 413 | | 413 | |
| 414 | const Scheme = Ecdsa(crypto.ecc.P384, crypto.hash.sha2.Sha256); | 414 | const Scheme = Ecdsa(crypto.ecc.P384, crypto.hash.sha2.Sha256); |
| 415 | const kp = try Scheme.KeyPair.create(null); | 415 | const kp = try Scheme.KeyPair.generate(); |
| 416 | const msg = "test"; | 416 | const msg = "test"; |
| 417 | | 417 | |
| 418 | var noise: [Scheme.noise_length]u8 = undefined; | 418 | var noise: [Scheme.noise_length]u8 = undefined; |
| ... | @@ -886,7 +886,7 @@ test "Sec1 encoding/decoding" { | ... | @@ -886,7 +886,7 @@ test "Sec1 encoding/decoding" { |
| 886 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | 886 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 887 | | 887 | |
| 888 | const Scheme = EcdsaP384Sha384; | 888 | const Scheme = EcdsaP384Sha384; |
| 889 | const kp = try Scheme.KeyPair.create(null); | 889 | const kp = try Scheme.KeyPair.generate(); |
| 890 | const pk = kp.public_key; | 890 | const pk = kp.public_key; |
| 891 | const pk_compressed_sec1 = pk.toCompressedSec1(); | 891 | const pk_compressed_sec1 = pk.toCompressedSec1(); |
| 892 | const pk_recovered1 = try Scheme.PublicKey.fromSec1(&pk_compressed_sec1); | 892 | const pk_recovered1 = try Scheme.PublicKey.fromSec1(&pk_compressed_sec1); |