Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created June 29, 2018 03:35
Show Gist options
  • Save Varriount/026c85b8f6bb99e1c280466b09a22edc to your computer and use it in GitHub Desktop.
Save Varriount/026c85b8f6bb99e1c280466b09a22edc to your computer and use it in GitHub Desktop.
type
RawSecp256k1Context* = distinct pointer
RawSecp256k1ScratchSpace* = distinct pointer
Secp256k1Context* = ref object
raw*: RawSecp256k1Context
Secp256k1ScratchSpace* = ref object
raw*: RawSecp256k1ScratchSpace
Secp256k1Pubkey* = object
data*: array[64, cuchar]
Secp256k1EcdsaSignature* = object
data*: array[64, cuchar]
Secp256k1NonceFunction* = proc (
nonce32: ptr cuchar;
msg32: ptr cuchar;
key32: ptr cuchar;
algo16: ptr cuchar;
data: pointer;
attempt: cuint): cint {.cdecl.}
const
secp256k1FlagsTypeMask* = (1 shl 8) - 1
secp256k1FlagsTypeContext* = (1 shl 0)
secp256k1FlagsTypeCompression* = (1 shl 1)
const
secp256k1FlagsBitContextVerify* = (1 shl 8)
secp256k1FlagsBitContextSign* = (1 shl 9)
secp256k1FlagsBitCompression* = (1 shl 8)
const
secp256k1ContextVerify* = (
secp256k1FlagsTypeContext or secp256k1FlagsBitContextVerify
)
secp256k1ContextSign* = (
secp256k1FlagsTypeContext or secp256k1FlagsBitContextSign
)
secp256k1ContextNone* = secp256k1FlagsTypeContext
const
secp256k1EcCompressed* = (
secp256k1FlagsTypeCompression or secp256k1FlagsBitCompression
)
secp256k1EcUncompressed* = secp256k1FlagsTypeCompression
const
secp256k1TagPubkeyEven* = 0x00000002
secp256k1TagPubkeyOdd* = 0x00000003
secp256k1TagPubkeyUncompressed* = 0x00000004
secp256k1TagPubkeyHybridEven* = 0x00000006
secp256k1TagPubkeyHybridOdd* = 0x00000007
# ## Create/Clone/Copy Procedures
# Context
proc secp256k1ContextCreate*(flags: cuint): RawSecp256k1Context {.
cdecl,
importc: "secp256k1_context_create",
header: "secp256k1.h"
.}
proc secp256k1ContextClone*(ctx: RawSecp256k1Context): RawSecp256k1Context {.
cdecl,
importc: "secp256k1_context_clone",
header: "secp256k1.h"
.}
proc secp256k1ContextDestroy*(ctx: RawSecp256k1Context) {.
cdecl,
importc: "secp256k1_context_destroy",
header: "secp256k1.h"
.}
# Scratch Space
proc secp256k1ScratchSpaceCreate*(ctx: RawSecp256k1Context; maxSize: csize): RawSecp256k1ScratchSpace {.
cdecl,
importc: "secp256k1_scratch_space_create",
header: "secp256k1.h"
.}
proc secp256k1ScratchSpaceDestroy*(scratch: RawSecp256k1ScratchSpace) {.
cdecl,
importc: "secp256k1_scratch_space_destroy",
header: "secp256k1.h"
.}
# Safe Versions
proc destroySecp256k1Context*(ctx: Secp256k1Context) =
secp256k1ContextDestroy(ctx.raw)
proc newSecp256k1Context*(flags: cuint): Secp256k1Context =
new(result, destroySecp256k1Context)
result.raw = secp256k1ContextCreate(flags)
proc destroySecp256k1ScratchSpace*(scratch: Secp256k1Context) =
secp256k1ScratchSpaceDestroy(scratch.raw)
proc newSecp256k1ScratchSpace*(ctx: Secp256k1Context; maxSize: csize): Secp256k1ScratchSpace =
new(result, destroySecp256k1ScratchSpace)
result.raw = secp256k1ScratchSpaceCreate(ctx.raw, maxSize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment