Merge pull request #4 from cheatfate/master
Fix crashes, add secp256k1_ecdh definition
This commit is contained in:
commit
81cda1ab6f
@ -24,7 +24,8 @@ type
|
|||||||
|
|
||||||
secp256k1_nonce_function* = proc (nonce32: ptr cuchar; msg32: ptr cuchar;
|
secp256k1_nonce_function* = proc (nonce32: ptr cuchar; msg32: ptr cuchar;
|
||||||
key32: ptr cuchar; algo16: ptr cuchar; data: pointer;
|
key32: ptr cuchar; algo16: ptr cuchar; data: pointer;
|
||||||
attempt: cuint): cint
|
attempt: cuint): cint {.stdcall.}
|
||||||
|
secp256k1_error_function* = proc (message: cstring; data: pointer) {.stdcall.}
|
||||||
|
|
||||||
secp256k1_context* = object
|
secp256k1_context* = object
|
||||||
secp256k1_scratch_space* = object
|
secp256k1_scratch_space* = object
|
||||||
@ -69,12 +70,12 @@ proc secp256k1_context_destroy*(
|
|||||||
|
|
||||||
proc secp256k1_context_set_illegal_callback*(
|
proc secp256k1_context_set_illegal_callback*(
|
||||||
ctx: ptr secp256k1_context;
|
ctx: ptr secp256k1_context;
|
||||||
fun: proc (message: cstring; data: pointer);
|
fun: secp256k1_error_function;
|
||||||
data: pointer) {.secp.}
|
data: pointer) {.secp.}
|
||||||
|
|
||||||
proc secp256k1_context_set_error_callback*(
|
proc secp256k1_context_set_error_callback*(
|
||||||
ctx: ptr secp256k1_context;
|
ctx: ptr secp256k1_context;
|
||||||
fun: proc (message: cstring; data: pointer);
|
fun: secp256k1_error_function;
|
||||||
data: pointer) {.secp.}
|
data: pointer) {.secp.}
|
||||||
|
|
||||||
proc secp256k1_scratch_space_create*(
|
proc secp256k1_scratch_space_create*(
|
||||||
@ -253,3 +254,16 @@ proc secp256k1_ecdsa_recoverable_signature_serialize_compact*(
|
|||||||
## recid: a pointer to an integer to hold the recovery id (can be NULL).
|
## recid: a pointer to an integer to hold the recovery id (can be NULL).
|
||||||
## In: sig: a pointer to an initialized signature object (cannot be NULL)
|
## In: sig: a pointer to an initialized signature object (cannot be NULL)
|
||||||
##
|
##
|
||||||
|
|
||||||
|
proc secp256k1_ecdh*(ctx: ptr secp256k1_context; output32: ptr cuchar;
|
||||||
|
pubkey: ptr secp256k1_pubkey;
|
||||||
|
input32: ptr cuchar): cint {.secp.}
|
||||||
|
## Compute an EC Diffie-Hellman secret in constant time
|
||||||
|
## Returns: 1: exponentiation was successful
|
||||||
|
## 0: scalar was invalid (zero or overflow)
|
||||||
|
## Args: ctx: pointer to a context object (cannot be NULL)
|
||||||
|
## Out: result: a 32-byte array which will be populated by an ECDH
|
||||||
|
## secret computed from the point and scalar
|
||||||
|
## In: pubkey: a pointer to a secp256k1_pubkey containing an
|
||||||
|
## initialized public key
|
||||||
|
## privkey: a 32-byte scalar with which to multiply the point
|
||||||
|
@ -1,7 +1,30 @@
|
|||||||
import secp256k1, unittest
|
import secp256k1, unittest
|
||||||
|
|
||||||
suite "Test1":
|
suite "Test1":
|
||||||
test "Context should be created and destroyed":
|
test "Context should be created and destroyed":
|
||||||
let ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN or SECP256K1_CONTEXT_VERIFY)
|
let ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN or SECP256K1_CONTEXT_VERIFY)
|
||||||
check ctx != nil
|
check ctx != nil
|
||||||
secp256k1_context_destroy(ctx)
|
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, cuchar]
|
||||||
|
var data2: array[32, cuchar]
|
||||||
|
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 cuchar](addr aSecretKey[0])) == 1
|
||||||
|
check secp256k1_ec_pubkey_create(ctx, addr bPublicKey,
|
||||||
|
cast[ptr cuchar](addr bSecretKey[0])) == 1
|
||||||
|
check secp256k1_ecdh(ctx, addr data1[0],
|
||||||
|
addr bPublicKey,
|
||||||
|
cast[ptr cuchar](addr aSecretKey[0])) == 1
|
||||||
|
check secp256k1_ecdh(ctx, addr data2[0],
|
||||||
|
addr aPublicKey,
|
||||||
|
cast[ptr cuchar](addr bSecretKey[0])) == 1
|
||||||
|
check(data1 == data2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user