mirror of https://github.com/vacp2p/nim-libp2p.git
parent
b99fd88deb
commit
6331b04cb4
|
@ -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.} =
|
||||
|
|
|
@ -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.}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue