nim-abc/tests/abc/testKeys.nim

63 lines
2.1 KiB
Nim
Raw Normal View History

2021-06-28 08:10:15 +00:00
import pkg/stew/byteutils
2021-06-29 13:24:11 +00:00
import ./basics
2021-06-24 12:43:17 +00:00
suite "Keys":
test "generates random private keys":
check PrivateKey.random != PrivateKey.random
test "erases memory associated with a private key":
var key = PrivateKey.example
2021-06-24 12:43:17 +00:00
let bytes = cast[ptr[uint64]](addr key)
check bytes[] != 0
erase key
check bytes[] == 0
test "derives public key from private key":
let key1, key2 = PrivateKey.example
2021-06-24 12:43:17 +00:00
check key1.toPublicKey == key1.toPublicKey
check key2.toPublicKey == key2.toPublicKey
check key1.toPublicKey != key2.toPublicKey
2021-06-28 08:10:15 +00:00
test "can be used to sign messages":
const message = "hello".toBytes
let key = PrivateKey.example
2021-06-28 08:10:15 +00:00
let signature = key.sign(message)
check signature != Signature.default
test "can be used to verify signatures":
let message1 = "hello".toBytes
let message2 = "hallo".toBytes
let private = PrivateKey.example
2021-06-28 08:10:15 +00:00
let public = private.toPublicKey
let signature = private.sign(message1)
check public.verify(message1, signature)
check not public.verify(message2, signature)
2021-06-28 10:55:12 +00:00
test "public key can be converted to bytes":
let key = PublicKey.example
2021-06-28 10:55:12 +00:00
let bytes = key.toBytes
check PublicKey.fromBytes(bytes) == key.some
test "conversion from bytes to public key can fail":
let key = PublicKey.example
2021-06-28 10:55:12 +00:00
let bytes = key.toBytes
let invalid = bytes[1..^1]
check PublicKey.fromBytes(invalid) == PublicKey.none
test "public keys can be aggregated":
let key1, key2, key3 = PublicKey.example
check aggregate(key1, key2) != aggregate(key1, key3)
check aggregate(key1, key2) == aggregate(key2, key1)
check aggregate(PublicKey.default, key1) == key1
check aggregate(aggregate(key1, key2), key3) == aggregate(key1, key2, key3)
test "signatures can be aggregated":
let key1, key2 = PrivateKey.example
let message = "hello".toBytes
let sig1 = key1.sign(message)
let sig2 = key2.sign(message)
let aggregateKey = aggregate(key1.toPublicKey, key2.toPublicKey)
let aggregateSig = aggregate(sig1, sig2)
check aggregateKey.verify(message, aggregateSig)