From 6331b04cb494d4140ea1b96c84ad23cec96910ef Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 22 Jun 2020 19:03:15 +0200 Subject: [PATCH] secp: requiresInit updates (#237) * secp: requiresInit updates * fix --- libp2p/crypto/crypto.nim | 8 ++++---- libp2p/crypto/secp.nim | 15 +++++---------- tests/testsecp256k1.nim | 9 +-------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/libp2p/crypto/crypto.nim b/libp2p/crypto/crypto.nim index 10a1d8e..9c996f3 100644 --- a/libp2p/crypto/crypto.nim +++ b/libp2p/crypto/crypto.nim @@ -95,7 +95,7 @@ const SupportedSchemesInt* = {int8(RSA), int8(Ed25519), int8(Secp256k1), int8(ECDSA)} -template orError(exp: untyped, err: CryptoError): untyped = +template orError(exp: untyped, err: CryptoError): untyped = (exp.mapErr do (_: auto) -> auto: err) proc random*(t: typedesc[PrivateKey], scheme: PKScheme, @@ -163,7 +163,7 @@ proc getKey*(key: PrivateKey): CryptoResult[PublicKey] = let eckey = ? key.eckey.getKey().orError(KeyError) ok(PublicKey(scheme: ECDSA, eckey: eckey)) of Secp256k1: - let skkey = ? key.skkey.getKey().orError(KeyError) + let skkey = key.skkey.getKey() ok(PublicKey(scheme: Secp256k1, skkey: skkey)) else: err(KeyError) @@ -516,7 +516,7 @@ proc sign*(key: PrivateKey, data: openarray[byte]): CryptoResult[Signature] {.gc res.data = ? sig.getBytes().orError(SigError) ok(res) elif key.scheme == Secp256k1: - let sig = ? key.skkey.sign(data).orError(SigError) + let sig = key.skkey.sign(data) res.data = sig.getBytes() ok(res) else: @@ -639,7 +639,7 @@ proc ephemeral*(scheme: ECDHEScheme): CryptoResult[KeyPair] = elif scheme == Secp521r1: keypair = ? EcKeyPair.random(Secp521r1).orError(KeyError) ok(KeyPair( - seckey: PrivateKey(scheme: ECDSA, eckey: keypair.seckey), + seckey: PrivateKey(scheme: ECDSA, eckey: keypair.seckey), pubkey: PublicKey(scheme: ECDSA, eckey: keypair.pubkey))) proc ephemeral*(scheme: string): CryptoResult[KeyPair] {.inline.} = diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index e7b493a..926e0f6 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -131,9 +131,9 @@ proc init*(t: typedesc[SkSignature], data: string): SkResult[SkSignature] = var sig: SkSignature sig.init(data) and ok(sig) -proc getKey*(key: SkPrivateKey): SkResult[SkPublicKey] = +proc getKey*(key: SkPrivateKey): SkPublicKey = ## Calculate and return Secp256k1 `public key` from `private key` ``key``. - ok(SkPublicKey(? SkSecretKey(key).toPublicKey())) + SkPublicKey(SkSecretKey(key).toPublicKey()) proc toBytes*(key: SkPrivateKey, data: var openarray[byte]): SkResult[int] = ## Serialize Secp256k1 `private key` ``key`` to raw binary form and store it @@ -181,22 +181,17 @@ proc getBytes*(sig: SkSignature): seq[byte] {.inline.} = let length = toBytes(sig, result) result.setLen(length) -proc sign*[T: byte|char](key: SkPrivateKey, msg: openarray[T]): SkResult[SkSignature] = +proc sign*[T: byte|char](key: SkPrivateKey, msg: openarray[T]): SkSignature = ## Sign message `msg` using private key `key` and return signature object. let h = sha256.digest(msg) - ok(SkSignature(? sign(SkSecretKey(key), h))) + SkSignature(sign(SkSecretKey(key), h)) proc verify*[T: byte|char](sig: SkSignature, msg: openarray[T], key: SkPublicKey): bool = let h = sha256.digest(msg) verify(secp256k1.SkSignature(sig), h, secp256k1.SkPublicKey(key)) -proc clear*(key: var SkPrivateKey) {.borrow.} -proc clear*(key: var SkPublicKey) {.borrow.} -proc clear*(key: var SkSignature) {.borrow.} -proc clear*(key: var SkKeyPair) {.borrow.} - -proc verify*(key: SkPrivateKey): bool {.borrow.} +func clear*(key: var SkPrivateKey) {.borrow.} proc `$`*(key: SkPrivateKey): string {.borrow.} proc `$`*(key: SkPublicKey): string {.borrow.} diff --git a/tests/testsecp256k1.nim b/tests/testsecp256k1.nim index a7cc6a7..584ab58 100644 --- a/tests/testsecp256k1.nim +++ b/tests/testsecp256k1.nim @@ -32,11 +32,6 @@ suite "Secp256k1 testing suite": rkey2 == key rkey3 == key rkey4 == key - rkey1.clear() - rkey2.clear() - check: - not rkey1.verify() - not rkey2.verify() test "Public key serialize/deserialize test": for i in 0..