secp: requiresInit updates (#237)

* secp: requiresInit updates

* fix
This commit is contained in:
Jacek Sieka 2020-06-22 19:03:15 +02:00 committed by GitHub
parent b99fd88deb
commit 6331b04cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 22 deletions

View File

@ -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:

View File

@ -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.}

View File

@ -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..<TestsCount:
var rkey1, rkey2: SkPublicKey
@ -54,13 +49,11 @@ suite "Secp256k1 testing suite":
rkey2 == pair.pubkey
rkey3 == pair.pubkey
rkey4 == pair.pubkey
rkey1.clear()
rkey2.clear()
test "Generate/Sign/Serialize/Deserialize/Verify test":
var message = "message to sign"
for i in 0..<TestsCount:
var kp = SkKeyPair.random().expect("random key pair")
var sig = kp.seckey.sign(message).expect("signature")
var sig = kp.seckey.sign(message)
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()