From 858d6ca3d633610f9c1612e62577e29d33f8dcdf Mon Sep 17 00:00:00 2001 From: Gruruya Date: Mon, 3 Apr 2023 18:25:51 -0400 Subject: [PATCH] Make SkKeyPair a wrapper around secp256k1_keypair --- secp256k1.nim | 31 +++++++++++++++++++++++++------ tests/test_secp256k1.nim | 4 ++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/secp256k1.nim b/secp256k1.nim index b1e4bb5..2edcafa 100644 --- a/secp256k1.nim +++ b/secp256k1.nim @@ -80,8 +80,7 @@ type SkKeyPair* = object ## Representation of private/public keys pair. - seckey*: SkSecretKey - pubkey*: SkPublicKey + data*: secp256k1_keypair SkSignature* {.requiresInit.} = object ## Representation of non-recoverable signature. @@ -399,20 +398,40 @@ func toRaw*(sig: SkRecoverableSignature): array[SkRawRecoverableSignatureSize, b func toHex*(sig: SkRecoverableSignature): string = toHex(toRaw(sig)) +func pubkey*(kp: SkKeyPair): SkPublicKey = + var key {.noinit.}: secp256k1_pubkey + let res = secp256k1_keypair_pub(secp256k1_context_no_precomp, addr key, addr kp.data) + doAssert res == 1, "Can't fail, per documentation" + SkPublicKey(data: key) + +func seckey*(kp: SkKeyPair): SkSecretKey = + var key {.noinit.}: array[SkRawSecretKeySize, byte] + let res = secp256k1_keypair_sec(secp256k1_context_no_precomp, key.baseAddr, addr kp.data) + doAssert res == 1, "Can't fail, per documentation" + SkSecretKey(data: key) + proc random*(T: type SkKeyPair, rng: Rng): SkResult[T] = ## Generates new random key pair. let seckey = ? SkSecretKey.random(rng) + + var keypair {.noinit.}: secp256k1_keypair + let res = secp256k1_keypair_create(getContext(), addr keypair, addr seckey.data[0]) + doAssert res == 1, "Can't fail, only fails if secret key is invalid but it was freshly generated." + ok(T( - seckey: seckey, - pubkey: seckey.toPublicKey() + data: keypair )) proc random*(T: type SkKeyPair, rng: FoolproofRng): T = ## Generates new random key pair. let seckey = SkSecretKey.random(rng) + + var keypair {.noinit.}: secp256k1_keypair + let res = secp256k1_keypair_create(getContext(), addr keypair, addr seckey.data[0]) + doAssert res == 1, "Can't fail, only fails if secret key is invalid but it was freshly generated." + T( - seckey: seckey, - pubkey: seckey.toPublicKey() + data: keypair ) func `==`*(lhs, rhs: SkPublicKey): bool = diff --git a/tests/test_secp256k1.nim b/tests/test_secp256k1.nim index bc86660..d569df5 100644 --- a/tests/test_secp256k1.nim +++ b/tests/test_secp256k1.nim @@ -57,3 +57,7 @@ suite "secp256k1": SkMessage.fromBytes([]).isErr() SkMessage.fromBytes([0'u8]).isErr() SkMessage.fromBytes(array[32, byte](msg0)).isOk() + + test "Keypairs": + check: + SkKeyPair.random(workingRng).isOk()