2023-05-18 08:24:17 +00:00
|
|
|
{.used.}
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
# Nim-Libp2p
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2023-05-18 08:24:17 +00:00
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
import unittest2
|
2020-07-07 11:14:11 +00:00
|
|
|
import ../libp2p/crypto/[crypto, secp]
|
2019-10-29 18:51:48 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
2019-09-02 18:55:42 +00:00
|
|
|
suite "Secp256k1 testing suite":
|
|
|
|
const TestsCount = 20
|
2020-07-07 11:14:11 +00:00
|
|
|
|
2019-09-02 18:55:42 +00:00
|
|
|
test "Private key serialize/deserialize test":
|
|
|
|
for i in 0 ..< TestsCount:
|
|
|
|
var rkey1, rkey2: SkPrivateKey
|
|
|
|
var skey2 = newSeq[byte](256)
|
2020-07-07 11:14:11 +00:00
|
|
|
var key = SkPrivateKey.random(rng[])
|
2019-09-02 18:55:42 +00:00
|
|
|
var skey1 = key.getBytes()
|
|
|
|
check:
|
2020-05-18 05:25:55 +00:00
|
|
|
key.toBytes(skey2).expect("bytes len") > 0
|
2019-09-02 18:55:42 +00:00
|
|
|
check:
|
2020-05-18 05:25:55 +00:00
|
|
|
rkey1.init(skey1).isOk == true
|
|
|
|
rkey2.init(skey2).isOk == true
|
|
|
|
var rkey3 = SkPrivateKey.init(skey1).expect("private key")
|
|
|
|
var rkey4 = SkPrivateKey.init(skey2).expect("private key")
|
2019-09-02 18:55:42 +00:00
|
|
|
check:
|
|
|
|
rkey1 == key
|
|
|
|
rkey2 == key
|
|
|
|
rkey3 == key
|
|
|
|
rkey4 == key
|
|
|
|
test "Public key serialize/deserialize test":
|
|
|
|
for i in 0 ..< TestsCount:
|
|
|
|
var rkey1, rkey2: SkPublicKey
|
|
|
|
var skey2 = newSeq[byte](256)
|
2020-07-07 11:14:11 +00:00
|
|
|
var pair = SkKeyPair.random(rng[])
|
2019-09-02 18:55:42 +00:00
|
|
|
var skey1 = pair.pubkey.getBytes()
|
|
|
|
check:
|
2020-05-18 05:25:55 +00:00
|
|
|
pair.pubkey.toBytes(skey2).expect("bytes len") > 0
|
|
|
|
rkey1.init(skey1).isOk == true
|
|
|
|
rkey2.init(skey2).isOk == true
|
|
|
|
var rkey3 = SkPublicKey.init(skey1).expect("public key")
|
|
|
|
var rkey4 = SkPublicKey.init(skey2).expect("public key")
|
2019-09-02 18:55:42 +00:00
|
|
|
check:
|
|
|
|
rkey1 == pair.pubkey
|
|
|
|
rkey2 == pair.pubkey
|
|
|
|
rkey3 == pair.pubkey
|
|
|
|
rkey4 == pair.pubkey
|
|
|
|
test "Generate/Sign/Serialize/Deserialize/Verify test":
|
|
|
|
var message = "message to sign"
|
|
|
|
for i in 0 ..< TestsCount:
|
2020-07-07 11:14:11 +00:00
|
|
|
var kp = SkKeyPair.random(rng[])
|
2020-06-22 17:03:15 +00:00
|
|
|
var sig = kp.seckey.sign(message)
|
2019-09-02 18:55:42 +00:00
|
|
|
var sersk = kp.seckey.getBytes()
|
|
|
|
var serpk = kp.pubkey.getBytes()
|
|
|
|
var sersig = sig.getBytes()
|
2020-05-18 05:25:55 +00:00
|
|
|
discard SkPrivateKey.init(sersk).expect("private key")
|
|
|
|
var pubkey = SkPublicKey.init(serpk).expect("public key")
|
|
|
|
var csig = SkSignature.init(sersig).expect("signature")
|
2019-09-02 18:55:42 +00:00
|
|
|
check csig.verify(message, pubkey) == true
|
2020-04-17 10:51:13 +00:00
|
|
|
var error = csig.getBytes()
|
|
|
|
error[^1] = not error[^1]
|
2020-05-18 05:25:55 +00:00
|
|
|
csig = SkSignature.init(error).expect("signature")
|
2019-09-02 18:55:42 +00:00
|
|
|
check csig.verify(message, pubkey) == false
|