2020-04-17 05:43:30 +00:00
|
|
|
import ../secp256k1, unittest
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
const
|
2020-07-06 13:07:29 +00:00
|
|
|
msg0 = SkMessage([
|
|
|
|
0'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
])
|
|
|
|
msg1 = SkMessage([
|
2020-04-17 05:43:30 +00:00
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
])
|
2023-04-04 03:42:03 +00:00
|
|
|
msg2 = array[40, byte]([
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1'u8, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
])
|
2020-04-17 05:43:30 +00:00
|
|
|
|
2020-07-06 13:07:29 +00:00
|
|
|
proc workingRng(data: var openArray[byte]): bool =
|
|
|
|
data[0] += 1
|
|
|
|
true
|
|
|
|
|
|
|
|
proc brokenRng(data: var openArray[byte]): bool = false
|
|
|
|
|
2020-04-17 05:43:30 +00:00
|
|
|
suite "secp256k1":
|
|
|
|
test "Key ops":
|
|
|
|
let
|
2020-07-06 13:07:29 +00:00
|
|
|
sk = SkSecretKey.random(workingRng).expect("should get a key")
|
2020-06-22 14:08:21 +00:00
|
|
|
pk = sk.toPublicKey()
|
2020-04-17 05:43:30 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
SkSecretKey.fromRaw(sk.toRaw())[].toHex() == sk.toHex()
|
|
|
|
SkSecretKey.fromHex(sk.toHex())[].toHex() == sk.toHex()
|
|
|
|
SkPublicKey.fromRaw(pk.toRaw())[].toHex() == pk.toHex()
|
|
|
|
SkPublicKey.fromRaw(pk.toRawCompressed())[].toHex() == pk.toHex()
|
|
|
|
SkPublicKey.fromHex(pk.toHex())[].toHex() == pk.toHex()
|
2020-07-06 13:07:29 +00:00
|
|
|
SkSecretKey.random(brokenRng).isErr
|
2020-04-17 05:43:30 +00:00
|
|
|
|
2023-04-04 02:43:37 +00:00
|
|
|
test "Keypairs":
|
|
|
|
check:
|
|
|
|
SkKeyPair.random(workingRng).isOk()
|
|
|
|
|
2020-04-17 05:43:30 +00:00
|
|
|
test "Signatures":
|
|
|
|
let
|
2020-07-06 13:07:29 +00:00
|
|
|
sk = SkSecretKey.random(workingRng)[]
|
2020-06-22 14:08:21 +00:00
|
|
|
pk = sk.toPublicKey()
|
2020-07-06 13:07:29 +00:00
|
|
|
otherPk = SkSecretKey.random(workingRng)[].toPublicKey()
|
2020-06-22 14:08:21 +00:00
|
|
|
sig = sign(sk, msg0)
|
|
|
|
sig2 = signRecoverable(sk, msg0)
|
2023-04-04 03:42:03 +00:00
|
|
|
sig3 = signSchnorr(sk, msg0)
|
|
|
|
sig4 = signSchnorr(sk, msg2)
|
2020-04-17 05:43:30 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
verify(sig, msg0, pk)
|
2020-06-22 14:08:21 +00:00
|
|
|
not verify(sig, msg0, otherPk)
|
2020-04-17 05:43:30 +00:00
|
|
|
not verify(sig, msg1, pk)
|
|
|
|
recover(sig2, msg0)[] == pk
|
|
|
|
recover(sig2, msg1)[] != pk
|
|
|
|
SkSignature.fromDer(sig.toDer())[].toHex() == sig.toHex()
|
2023-04-04 03:42:03 +00:00
|
|
|
verify(sig3, msg0, pk)
|
|
|
|
verify(sig4, msg2, pk)
|
2020-04-17 05:43:30 +00:00
|
|
|
|
|
|
|
test "Message":
|
|
|
|
check:
|
|
|
|
SkMessage.fromBytes([]).isErr()
|
|
|
|
SkMessage.fromBytes([0'u8]).isErr()
|
2020-07-06 13:07:29 +00:00
|
|
|
SkMessage.fromBytes(array[32, byte](msg0)).isOk()
|