Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanups #160

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions blscurve/bls_batch_verifier.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,21 @@ type

# Per-batch contexts for multithreaded batch verification
batchContexts: seq[ContextMultiAggregateVerify[DST]]
when compileOption("threads"):
flows: seq[Flowvar[bool]]

# Serial Batch Verifier
# ----------------------------------------------------------------------

func init*(T: type BatchedBLSVerifierCache): T =
## Initialise the cache for single-threaded usage
when compileOption("threads"):
BatchedBLSVerifierCache(
batchContexts: newSeq[ContextMultiAggregateVerify[DST]](1),
flows: @[]
)
else:
BatchedBLSVerifierCache(
batchContexts: newSeq[ContextMultiAggregateVerify[DST]](1),
)

BatchedBLSVerifierCache(
batchContexts: newSeq[ContextMultiAggregateVerify[DST]](1),
)

when compileOption("threads"):
func init*(T: type BatchedBLSVerifierCache, tp: Taskpool): T =
## Initialise the cache for multi-threaded usage
BatchedBLSVerifierCache(
batchContexts: newSeq[ContextMultiAggregateVerify[DST]](tp.numThreads),
flows: newSeq[Flowvar[bool]](tp.numThreads)
)

func batchVerifySerial*(
Expand Down
13 changes: 9 additions & 4 deletions blscurve/eth2_keygen/hkdf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@

import nimcrypto/hmac

func hkdfExtract*[T;S,I: char|byte](ctx: var HMAC[T],
prk: var MDigest[T.bits],
func hkdfExtract*[T;M;S,I: char|byte](ctx: var HMAC[T],
prk: var M, # MDigest[T.bits]
salt: openArray[S],
ikm: openArray[I],
append: static openArray[I]
Expand All @@ -108,6 +108,9 @@ func hkdfExtract*[T;S,I: char|byte](ctx: var HMAC[T],
## - ctx: a HMAC["cryptographic-hash"] context, for example HMAC[sha256].

mixin init, update, finish

# TODO https://github.com/nim-lang/Nim/issues/22494
static: doAssert M.bits == T.bits
ctx.init(salt)
ctx.update(ikm)
when append.len > 0:
Expand All @@ -116,8 +119,8 @@ func hkdfExtract*[T;S,I: char|byte](ctx: var HMAC[T],

# ctx.clear() - TODO: very expensive

func hkdfExpand*[T;I,A: char|byte](ctx: var HMAC[T],
prk: MDigest[T.bits],
func hkdfExpand*[T;M;I,A: char|byte](ctx: var HMAC[T],
prk: M, # MDigest[T.bits]
info: openArray[I],
append: static openArray[A],
output: var openArray[byte]
Expand All @@ -141,6 +144,8 @@ func hkdfExpand*[T;I,A: char|byte](ctx: var HMAC[T],
## - ctx: a HMAC["cryptographic-hash"] context, for example HMAC[sha256].
mixin init, update, finish

# TODO https://github.com/nim-lang/Nim/issues/22494
static: doAssert M.bits == T.bits
const HashLen = T.bits div 8

static: doAssert T.bits >= 0
Expand Down
10 changes: 5 additions & 5 deletions blscurve/miracl/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func fromBytes*(res: var BIG_384, a: openArray[byte]): bool =
let length = if len(a) > MODBYTES_384: MODBYTES_384 else: len(a)
for i in 0..<length:
discard BIG_384_fshl(res, 8)
res[0] = res[0] + cast[Chunk](a[i])
res[0] = res[0] + Chunk(a[i])
true

func fromBytes*(res: var DBIG_384, a: openArray[byte]): bool =
Expand All @@ -547,7 +547,7 @@ func fromBytes*(res: var DBIG_384, a: openArray[byte]): bool =
zeroMem(res.addr, sizeof(res))
for rawByte in a:
BIG_384_dshl(res, 8)
res[0] = res[0] + cast[Chunk](rawByte)
res[0] = res[0] + Chunk(rawByte)
true

func fromHex*(res: var BIG_384, a: string): bool {.inline.} =
Expand All @@ -556,7 +556,7 @@ func fromHex*(res: var BIG_384, a: string): bool {.inline.} =
## Returns ``true`` if conversion was successful.
try:
fromBytes(res, hexToSeqByte(a))
except ValueError, IndexError:
except ValueError:
# TODO: change to exception-free
# https://github.com/status-im/nim-blscurve/issues/57
false
Expand Down Expand Up @@ -651,7 +651,7 @@ func fromHex*(res: var ECP2_BLS12381, a: string): bool {.inline.} =
## Returns ``true`` if conversion was successfull.
try:
fromBytes(res, hexToSeqByte(a))
except ValueError, IndexError:
except ValueError:
# TODO: change to exception-free
# https://github.com/status-im/nim-blscurve/issues/57
false
Expand Down Expand Up @@ -737,7 +737,7 @@ func fromHex*(res: var ECP_BLS12381, a: string): bool {.inline.} =
## Returns ``true`` if conversion was successfull.
try:
fromBytes(res, hexToSeqByte(a))
except ValueError, IndexError:
except ValueError:
# TODO: change to exception-free
# https://github.com/status-im/nim-blscurve/issues/57
false
Expand Down