Add computed and LUT sizes tests

This commit is contained in:
mratsim 2018-02-21 10:48:44 +01:00
parent 403d954637
commit b8af327118
3 changed files with 77 additions and 14 deletions

View File

@ -3,6 +3,23 @@
# ETHASH Data sizes lookup tables
## 2048 Epochs (~20 years) worth of tabulated DAG sizes
##
## Generated with the following Mathematica Code:
##
## GetCacheSizes[n_] := Module[{
## CacheSizeBytesInit = 2^24,
## CacheGrowth = 2^17,
## HashBytes = 64,
## j = 0},
## Reap[
## While[j < n,
## Module[{i =
## Floor[(CacheSizeBytesInit + CacheGrowth * j) / HashBytes]},
## While[! PrimeQ[i], i--];
## Sow[i*HashBytes]; j++]]]][[2]][[1]]
const
data_sizes* = [
1073739904'u, 1082130304'u, 1090514816'u, 1098906752'u, 1107293056'u,
@ -416,6 +433,22 @@ const
18186498944'u, 18194886784'u, 18203275648'u, 18211666048'u, 18220048768'u,
18228444544'u, 18236833408'u, 18245220736'u]
## Generated with the following Mathematica Code:
##
## GetCacheSizes[n_] := Module[{
## DataSetSizeBytesInit = 2^30,
## MixBytes = 128,
## DataSetGrowth = 2^23,
## HashBytes = 64,
## CacheMultiplier = 1024,
## j = 0},
## Reap[
## While[j < n,
## Module[{i = Floor[(DataSetSizeBytesInit + DataSetGrowth * j) / (CacheMultiplier * HashBytes)]},
## While[! PrimeQ[i], i--];
## Sow[i*HashBytes]; j++]]]][[2]][[1]]
cache_sizes* = [uint 16776896, 16907456, 17039296, 17170112, 17301056, 17432512, 17563072,
17693888, 17824192, 17955904, 18087488, 18218176, 18349504, 18481088,
18611392, 18742336, 18874304, 19004224, 19135936, 19267264, 19398208,

View File

@ -5,7 +5,6 @@ import math, sequtils, algorithm,
keccak_tiny
import ./private/[primes, casting, functional, intmath, concat]
import ./data_sizes
export toHex, hexToSeqBytesBE
# TODO: Switching from default int to uint64
@ -18,10 +17,10 @@ export toHex, hexToSeqBytesBE
const
REVISION* = 23 # Based on spec revision 23
WORD_BYTES = 4 # bytes in word - in Nim we use 64 bits words # TODO check that
DATASET_BYTES_INIT* = 2^30 # bytes in dataset at genesis
DATASET_BYTES_GROWTH* = 2^23 # dataset growth per epoch
CACHE_BYTES_INIT* = 2^24 # bytes in cache at genesis
CACHE_BYTES_GROWTH* = 2^17 # cache growth per epoch
DATASET_BYTES_INIT* = 2'u^30 # bytes in dataset at genesis
DATASET_BYTES_GROWTH* = 2'u^23 # dataset growth per epoch
CACHE_BYTES_INIT* = 2'u^24 # bytes in cache at genesis
CACHE_BYTES_GROWTH* = 2'u^17 # cache growth per epoch
CACHE_MULTIPLIER=1024 # Size of the DAG relative to the cache
EPOCH_LENGTH* = 30000 # blocks per epoch
MIX_BYTES* = 128 # width of mix
@ -36,29 +35,30 @@ const
# ###############################################################################
# Parameters
proc get_cache_size(block_number: Natural): Natural {.noSideEffect.}=
proc get_cache_size*(block_number: uint): uint {.noSideEffect.}=
result = CACHE_BYTES_INIT + CACHE_BYTES_GROWTH * (block_number div EPOCH_LENGTH)
result -= HASH_BYTES
while (let dm = divmod(result, HASH_BYTES);
dm.rem == 0 and dm.quot.isPrime):
dm.rem == 0 and not dm.quot.isPrime):
# In a static lang, checking that the result of a division is prime
# Means checking that reminder == 0 and quotient is prime
result -= 2 * HASH_BYTES
proc get_full_size(block_number: Natural): Natural {.noSideEffect.}=
proc get_data_size*(block_number: uint): uint {.noSideEffect.}=
result = DATASET_BYTES_INIT + DATASET_BYTES_GROWTH * (block_number div EPOCH_LENGTH)
result -= MIX_BYTES
while (let dm = divmod(result, MIX_BYTES);
dm.rem == 0 and dm.quot.isPrime):
dm.rem == 0 and not dm.quot.isPrime):
result -= 2 * MIX_BYTES
# ###############################################################################
# Fetch from lookup tables of 2048 epochs of data sizes and cache sizes
import ./data_sizes
proc get_datasize*(block_number: Natural): uint64 {.noSideEffect, inline.} =
proc get_datasize_lut*(block_number: Natural): uint64 {.noSideEffect, inline.} =
data_sizes[block_number div EPOCH_LENGTH]
proc get_cachesize*(block_number: Natural): uint64 {.noSideEffect, inline.} =
proc get_cachesize_lut*(block_number: Natural): uint64 {.noSideEffect, inline.} =
cache_sizes[block_number div EPOCH_LENGTH]
# ###############################################################################
@ -204,7 +204,7 @@ proc hashimoto_light(full_size:Natural, cache: seq[Hash[512]],
full_size,
light)
proc hashimoto_light(full_size:Natural, dataset: seq[Hash[512]],
proc hashimoto_full(full_size:Natural, dataset: seq[Hash[512]],
header: Hash[256], nonce: uint64): HashimotoHash {.noSideEffect, inline.} =
let full: DatasetLookup = proc(x: Natural): Hash[512] = dataset[x]

View File

@ -44,8 +44,8 @@ suite "Endianness (not implemented)":
suite "Genesis parameters":
let
full_size = get_datasize(0).int
cache_size = get_cachesize(0).int
full_size = get_datasize(0)
cache_size = get_cachesize(0)
test "Full dataset size should be less or equal DATASET_BYTES_INIT":
check: full_size <= DATASET_BYTES_INIT
@ -61,3 +61,33 @@ suite "Genesis parameters":
test "Cache size == 16776896":
check: cache_size == 16776896
test "Full dataset size at the change of epochs":
check: get_data_size(EPOCH_LENGTH - 1) == 1073739904'u
check: get_data_size(EPOCH_LENGTH) == 1082130304'u
check: get_data_size(EPOCH_LENGTH + 1) == 1082130304'u
check: get_data_size(EPOCH_LENGTH * 2046) == 18236833408'u
check: get_data_size(EPOCH_LENGTH * 2047) == 18245220736'u
test "Cache size at the change of epochs":
check: get_cache_size(EPOCH_LENGTH - 1) == 16776896'u
check: get_cache_size(EPOCH_LENGTH) == 16907456'u
check: get_cache_size(EPOCH_LENGTH + 1) == 16907456'u
check: get_cache_size(EPOCH_LENGTH * 2046) == 284950208'u
check: get_cache_size(EPOCH_LENGTH * 2047) == 285081536'u
check: get_cache_size(EPOCH_LENGTH * 2048 - 1) == 285081536'u
test "Full dataset size at the change of epochs - Look-up tables":
check: get_data_size_lut(EPOCH_LENGTH - 1) == 1073739904'u
check: get_data_size_lut(EPOCH_LENGTH) == 1082130304'u
check: get_data_size_lut(EPOCH_LENGTH + 1) == 1082130304'u
check: get_data_size_lut(EPOCH_LENGTH * 2046) == 18236833408'u
check: get_data_size_lut(EPOCH_LENGTH * 2047) == 18245220736'u
test "Cache size at the change of epochs - Look-up tables":
check: get_cache_size_lut(EPOCH_LENGTH - 1) == 16776896'u
check: get_cache_size_lut(EPOCH_LENGTH) == 16907456'u
check: get_cache_size_lut(EPOCH_LENGTH + 1) == 16907456'u
check: get_cache_size_lut(EPOCH_LENGTH * 2046) == 284950208'u
check: get_cache_size_lut(EPOCH_LENGTH * 2047) == 285081536'u
check: get_cache_size_lut(EPOCH_LENGTH * 2048 - 1) == 285081536'u