Add bigEndian/littleEndian distinction

This commit is contained in:
mratsim 2018-02-19 13:42:52 +01:00
parent 6fb91f346f
commit f6f2725cf2
2 changed files with 9 additions and 3 deletions

View File

@ -105,10 +105,13 @@ proc calc_dataset_item(cache: seq[Hash[512]], i: Natural): Hash[512] {.noSideEff
# Initialize the mix, it's a reference to a cache item that we will modify in-place
var mix = cast[ptr U512](unsafeAddr cache[i mod n])
mix[0] = mix[0] xor i.uint64 # This is probably broken on big endian
when system.cpuEndian == littleEndian:
mix[0] = mix[0] xor i.uint64
else:
mix[high(mix)] = mix[high(0)] xor i.uint64
mix[] = toU512 sha3_512 mix[]
# FNV with a lots of random ache node based on i
# FNV with a lots of random cache nodes based on i
for j in 0'u64 ..< DATASET_PARENTS:
let cache_index = fnv(i.uint64 xor j, mix[j mod r])
mix[] = zipMap(mix[], cache[cache_index.int mod n].toU512, fnv(x, y))

View File

@ -8,7 +8,10 @@ type U512* = array[8, uint64]
## and be able to do sha3_512 which only accepts arrays
proc toU512*(x: Natural): U512 {.inline, noSideEffect.}=
result[result.high] = x.uint64
when system.cpuEndian == littleEndian:
result[0] = x.uint64
else:
result[result.high] = x.uint64
proc toU512*(x: Hash[512]): U512 {.inline, noSideEffect, noInit.}=
cast[type result](x)