mirror of
https://github.com/status-im/nim-ethash.git
synced 2025-02-19 15:34:21 +00:00
Remove ttmath dependency for mining
This commit is contained in:
parent
0e1a1169e4
commit
4d1cc3a359
@ -7,7 +7,7 @@ srcDir = "src"
|
||||
|
||||
### Dependencies
|
||||
|
||||
requires "nim >= 0.17.2", "keccak_tiny >= 0.1.0", "ttmath > 0.2.0" # ttmath with exposed table field is required for mining only
|
||||
requires "nim >= 0.17.2", "keccak_tiny >= 0.1.0"
|
||||
|
||||
proc test(name: string, lang: string = "c") =
|
||||
if not dirExists "build":
|
||||
@ -25,5 +25,4 @@ task test, "Run Proof-of-Work tests (without mining)":
|
||||
task test_mining, "Run Proof-of-Work and mining tests (test in release mode)":
|
||||
switch("define", "release")
|
||||
switch("define", "ethash_mining")
|
||||
test "all_tests", "cpp"
|
||||
|
||||
test "all_tests"
|
||||
|
@ -2,15 +2,7 @@
|
||||
# Distributed under the Apache v2 License (license terms are at http://www.apache.org/licenses/LICENSE-2.0).
|
||||
|
||||
import ./proof_of_work, ./private/casting
|
||||
import ttmath, random, math
|
||||
# TODO we don't really need ttmath here
|
||||
|
||||
|
||||
proc readUint256BE*(ba: ByteArrayBE[32]): UInt256 {.noSideEffect.}=
|
||||
## Convert a big-endian array of Bytes to an UInt256 (in native host endianness)
|
||||
const N = 32
|
||||
for i in 0 ..< N:
|
||||
result = result shl 8 or ba[i].u256
|
||||
import endians, random, math
|
||||
|
||||
proc willMulOverflow(a, b: uint64): bool {.noSideEffect.}=
|
||||
# Returns true if a * b overflows
|
||||
@ -77,9 +69,7 @@ proc isValid(nonce: uint64,
|
||||
# i.e we only need to test that hashimoto * difficulty doesn't overflow uint256
|
||||
|
||||
# First run the hashimoto with the candidate nonce
|
||||
let candidate = readUint256BE(cast[ByteArrayBE[32]](
|
||||
hashimoto_full(full_size, dataset, header, nonce).value
|
||||
))
|
||||
let candidate_hash = hashimoto_full(full_size, dataset, header, nonce)
|
||||
|
||||
# Now check if the multiplication of both would overflow
|
||||
|
||||
@ -91,14 +81,18 @@ proc isValid(nonce: uint64,
|
||||
#
|
||||
# Overflow occurs only if "1 * 5" overflows 2^64
|
||||
|
||||
# First we convert the Hash[256] to an array of 4 uint64 and then
|
||||
# only consider the most significant
|
||||
let hash_qwords = cast[array[4, uint64]](candidate_hash.value)
|
||||
var hi_hash: uint64
|
||||
|
||||
when system.cpuEndian == littleEndian:
|
||||
let hi_hash = candidate.table[3]
|
||||
littleEndian64(hi_hash.addr, hash_qwords[3].unsafeAddr)
|
||||
else:
|
||||
let hi_hash = candidate.table[0]
|
||||
littleEndian64(hi_hash.addr, hash_qwords[0].unsafeAddr)
|
||||
|
||||
result = not willMulOverflow(hi_hash, difficulty)
|
||||
|
||||
|
||||
proc mine*(full_size: Natural, dataset: seq[Hash[512]], header: Hash[256], difficulty: uint64): uint64 =
|
||||
# Returns a valid nonce
|
||||
|
||||
@ -107,4 +101,4 @@ proc mine*(full_size: Natural, dataset: seq[Hash[512]], header: Hash[256], diffi
|
||||
# Also random is deprecate and do not include the end of the range.
|
||||
|
||||
while not result.isValid(difficulty, full_size, dataset, header):
|
||||
inc(result) # we rely on uin overflow (mod 2^64) here.
|
||||
inc(result) # we rely on uin overflow (mod 2^64) here.
|
||||
|
Loading…
x
Reference in New Issue
Block a user