Pass the tests 🔥 ⚠⚠ Endianness in PoW spec seems wrong

This commit is contained in:
mratsim 2018-02-25 12:34:33 +01:00
parent 3d219299aa
commit 6aedd32111
3 changed files with 13 additions and 8 deletions

View File

@ -170,7 +170,11 @@ proc hashimoto(header: Hash[256],
let s_words = cast[ptr array[16, uint32]](addr s) # Alias for to interpret s as an uint32 array let s_words = cast[ptr array[16, uint32]](addr s) # Alias for to interpret s as an uint32 array
s_bytes[0..<32] = header.toByteArrayBE # We first populate the first 40 bytes of s with the concatenation s_bytes[0..<32] = header.toByteArrayBE # We first populate the first 40 bytes of s with the concatenation
s_bytes[32..<40] = nonce.toByteArrayBE
when system.cpuEndian == littleEndian: # ⚠⚠ Warning ⚠⚠, the spec is WRONG compared to tests here
s_bytes[32..<40] = cast[array[8,byte]](nonce) # the nonce should be concatenated with its LITTLE ENDIAN representation
else:
raise newException(ValueError, "Big endian system not supported yet")
s = keccak_512 s_bytes[0..<40] # TODO: Does this allocate a seq? s = keccak_512 s_bytes[0..<40] # TODO: Does this allocate a seq?
@ -196,6 +200,7 @@ proc hashimoto(header: Hash[256],
for i in countup(0, mix.len - 1, 4): for i in countup(0, mix.len - 1, 4):
cmix[i div 4] = mix[i].fnv(mix[i+1]).fnv(mix[i+2]).fnv(mix[i+3]) cmix[i div 4] = mix[i].fnv(mix[i+1]).fnv(mix[i+2]).fnv(mix[i+3])
# ⚠⚠ Warning ⚠⚠: Another big endian little endian issue?
# result.mix_digest = cast[Hash[256]]( # result.mix_digest = cast[Hash[256]](
# mapArray(cmix, x.toByteArrayBE) # Each uint32 must be changed to Big endian # mapArray(cmix, x.toByteArrayBE) # Each uint32 must be changed to Big endian
# ) # )

View File

@ -94,7 +94,7 @@ proc toHex*(ba: seq[byte]): string {.noSideEffect, noInit.}=
result[2*i+1] = hexChars[int ba[i] and 0xF] result[2*i+1] = hexChars[int ba[i] and 0xF]
proc toByteArrayBE*[T: SomeInteger](num: T): ByteArrayBE[T.sizeof] {.noSideEffect, noInit, inline.}= proc toByteArrayBE*[T: SomeInteger](num: T): ByteArrayBE[T.sizeof] {.noSideEffect, noInit, inline.}=
## Convert an UInt256 (in native host endianness) to a big-endian byte array ## Convert an int (in native host endianness) to a big-endian byte array
# Note: only works on devel # Note: only works on devel
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:

View File

@ -166,12 +166,12 @@ suite "Dagger hashimoto computation":
check: $calc_dataset_item(cache, 0) == expected check: $calc_dataset_item(cache, 0) == expected
# test "Real dataset and recomputation from cache matches": test "Real dataset and recomputation from cache matches":
# # https://github.com/ethereum/ethash/blob/f5f0a8b1962544d2b6f40df8e4b0d9a32faf8f8e/test/c/test.cpp#L360-L374 # https://github.com/ethereum/ethash/blob/f5f0a8b1962544d2b6f40df8e4b0d9a32faf8f8e/test/c/test.cpp#L360-L374
# for i in 0 ..< full_size div sizeof(Hash[512]): for i in 0 ..< full_size div sizeof(Hash[512]):
# for j in 0 ..< 32: for j in 0 ..< 32:
# let expected = calc_dataset_item(cache, j) let expected = calc_dataset_item(cache, j)
# check: full[j] == expected check: full[j] == expected
test "Light and full Hashimoto agree": test "Light and full Hashimoto agree":
# https://github.com/ethereum/ethash/blob/f5f0a8b1962544d2b6f40df8e4b0d9a32faf8f8e/test/python/test_pyethash.py#L44-L58 # https://github.com/ethereum/ethash/blob/f5f0a8b1962544d2b6f40df8e4b0d9a32faf8f8e/test/python/test_pyethash.py#L44-L58