mirror of
https://github.com/codex-storage/nim-codex-dht.git
synced 2025-01-30 21:57:07 +00:00
acd03ff831
Project has been updated to use nimble as a build system. All required dependencies have been added. All nim-eth mirrored files were added to a private folder in the libp2pdht module. A libp2pdht/discv5 module was added to alias the nim-eth modules (which will change over time). Test have been updated to use status-im/asynctest. This PR uses a branch of asynctest that supports async suite before/after. This seemed like the only the tests would work without throwing gcsafe errors. All tests working.
23 lines
718 B
Nim
23 lines
718 B
Nim
import bearssl
|
|
|
|
## Random helpers: similar as in stdlib, but with BrHmacDrbgContext rng
|
|
# TODO: Move these somewhere else?
|
|
const randMax = 18_446_744_073_709_551_615'u64
|
|
|
|
proc rand*(rng: var BrHmacDrbgContext, max: Natural): int =
|
|
if max == 0: return 0
|
|
|
|
var x: uint64
|
|
while true:
|
|
brHmacDrbgGenerate(addr rng, addr x, csize_t(sizeof(x)))
|
|
if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias
|
|
return int(x mod (uint64(max) + 1'u64))
|
|
|
|
proc sample*[T](rng: var BrHmacDrbgContext, a: openArray[T]): T =
|
|
result = a[rng.rand(a.high)]
|
|
|
|
proc shuffle*[T](rng: var BrHmacDrbgContext, a: var openArray[T]) =
|
|
for i in countdown(a.high, 1):
|
|
let j = rng.rand(i)
|
|
swap(a[i], a[j])
|