nim-secp256k1/tests/test_secp256k1_abi.nim

47 lines
2.0 KiB
Nim
Raw Normal View History

import ../secp256k1/abi, unittest
2018-03-03 09:13:09 +00:00
2020-04-17 05:43:30 +00:00
{.used.}
suite "ABI tests":
2018-03-16 09:28:15 +00:00
test "Context should be created and destroyed":
let ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN or SECP256K1_CONTEXT_VERIFY)
check ctx != nil
secp256k1_context_destroy(ctx)
test "ECDHE data should be equal":
var aSecretKey: array[32, uint8]
var bSecretKey: array[32, uint8]
var aPublicKey: secp256k1_pubkey
var bPublicKey: secp256k1_pubkey
var data1: array[32, byte]
var data2: array[32, byte]
2018-03-16 09:28:15 +00:00
aSecretKey[31] = 1'u8
bSecretKey[31] = 2'u8
let ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN)
check ctx != nil
check secp256k1_ec_pubkey_create(ctx, addr aPublicKey,
cast[ptr byte](addr aSecretKey[0])) == 1
2018-03-16 09:28:15 +00:00
check secp256k1_ec_pubkey_create(ctx, addr bPublicKey,
cast[ptr byte](addr bSecretKey[0])) == 1
2018-03-16 09:28:15 +00:00
check secp256k1_ecdh(ctx, addr data1[0],
addr bPublicKey,
cast[ptr byte](addr aSecretKey[0])) == 1
2018-03-16 09:28:15 +00:00
check secp256k1_ecdh(ctx, addr data2[0],
addr aPublicKey,
cast[ptr byte](addr bSecretKey[0])) == 1
2018-03-16 09:28:15 +00:00
check(data1 == data2)
Enable and expose Schnorrsig (#44) * Enable Schnorrsig module in wrapper The extrakeys module is a dependency for Schnorrsig, so that's enabled as well. * Add {.bycopy.} pragma * Add Schnorrsig interface to `abi.nim` Multikey interface is a dependency the for schnorrsig, so it was added as well. * Add tests for Schnorr signing * Fix schnorr magic const declaration on 1.6 and below * Remove unnecessary {.bycopy.} pragmas Done under the impression that {.bycopy.} is not necessary when only passing the object to C via ptr. * Make SkKeyPair a wrapper around secp256k1_keypair * Add more helper procs for new SkKeyPair * Small fixes * Re-order * Rework patch. Implement Schnorr signing and undo breaking changes. * Reduce code duplication * Fix type * Remove accidental extra indentation * Add `default` {.error.} proc for SkSchnorrSignature * Remove extra test * Add from/to raw/hex * Comments * Add low-level test for `secp256k1_keypair` * Fix errors on Nim 1.2 * Comment * Allow passing a `Rng`/`FoolproofRng` to `signSchnorr` for improved security * Comments * Correct `noncefp` to be a pointer in `extraparams` object * Remove unneeded {.bycopy.} Co-authored-by: Jacek Sieka <arnetheduck@gmail.com> * Don't check the RNG for Schnorr sig using private key requirements. * Add comment detailing that `signSchnorr` without an `rng` is discouraged * Remove non-`rng` signSchnorr variant from tests * Rename `signSchnorr` without `rng` to `signSchnorrUnsafe` * Unify `schnorrSig` implementations and add `array[32, bytes]` variant * Fix on Nim 1.2 * Make `signSchnorr` accept `Opt[array[32, byte]]` rather than `[array[32,byte]]` * Remove unused template param * Inline `signSchnorr Rng` procs * Remove `nimble.lock`, was breaking tests on Nim >1.6 Was causing `Error: cannot open file: stew/byteutils` * Correct template parameter naming * Consistently apply {.noinit.} pragma * `{.noinit.}` random byte array * Revert "`{.noinit.}` random byte array" This reverts commit a3f99817d9627880974be1ae81014fa17d14f2db. * Correct template pragmas * Explicitly declare `noncefp` as `nil` * Create and export `xonly_pubkey` wrapping type * Complete implementation of `SkXOnlyPublicKey` * Correct comment * Add tests for 'SkXOnlyPublicKey` * Correct conversion proc name * Correct conversion proc name cont. --------- Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
2023-04-11 08:17:03 +00:00
test "C-side keypairs should be unchanged when serialized":
var keypair: secp256k1_keypair
var secretKey: array[32, uint8]
var publicKey: secp256k1_xonly_pubkey
var parsed: array[32, byte]
var reflectedPublicKey: secp256k1_xonly_pubkey
secretKey[31] = 1'u8
let ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN or SECP256K1_CONTEXT_VERIFY)
check secp256k1_keypair_create(ctx, addr keypair, cast[ptr byte](addr secretKey[0])) == 1
check secp256k1_keypair_xonly_pub(ctx, addr publicKey, nil, addr keypair) == 1
check secp256k1_xonly_pubkey_serialize(ctx, addr parsed[0], addr publicKey) == 1
check secp256k1_xonly_pubkey_parse(ctx, addr reflectedPublicKey, addr parsed[0]) == 1
check publicKey == reflectedPublicKey