Skip to content

Commit

Permalink
cleanups (#160)
Browse files Browse the repository at this point in the history
* remove unused variables
* avoid enlarging casts
* don't try to catch defects
* work around nim bug
  • Loading branch information
arnetheduck authored Aug 17, 2023
1 parent 3956f63 commit 93df4f2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
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

0 comments on commit 93df4f2

Please sign in to comment.