Prerequisites
To better understand this page, we recommend you first read up on proof-of-work consensus, mining, and mining algorithms.Dagger-Hashimoto
Dagger-Hashimoto aims to satisfy two goals:- ASIC-resistance: the benefit from creating specialized hardware for the algorithm should be as small as possible
- Light client verifiability: a block should be efficiently verifiable by a light client.
DAG Generation
The code for the algorithm will be defined in Python below. First, we giveencode_int
for marshaling unsigned ints of specified precision to strings. Its inverse is also given:
sha3
is a function that takes an integer and outputs an integer, and dbl_sha3
is a double-sha3 function; if converting this reference code into an implementation use:
Parameters
The parameters used for the algorithm are:P
in this case is a prime chosen such that log₂(P)
is just slightly less than 512, which corresponds to the 512 bits we have been using to represent our numbers. Note that only the latter half of the DAG actually needs to be stored, so the de-facto RAM requirement starts at 1 GB and grows by 441 MB per year.
Dagger graph building
The dagger graph building primitive is defined as follows:sha3(seed)
, and from there starts sequentially adding on other nodes based on random previous nodes. When a new node is created, a modular power of the seed is computed to randomly select some indices less than i
(using x % i
above), and the values of the nodes at those indices are used in a calculation to generate a new a value for x
, which is then fed into a small proof of work function (based on XOR) to ultimately generate the value of the graph at index i
. The rationale behind this particular design is to force sequential access of the DAG; the next value of the DAG that will be accessed cannot be determined until the current value is known. Finally, modular exponentiation hashes the result further.
This algorithm relies on several results from number theory. See the appendix below for a discussion.
Light client evaluation
The above graph construction intends to allow each node in the graph to be reconstructed by computing a subtree of only a small number of nodes and requiring only a small amount of auxiliary memory. Note that with k=1, the subtree is only a chain of values going up to the first element in the DAG. The light client computing function for the DAG works as follows:k=1
the cache is unnecessary, although a further optimization actually precomputes the first few thousand values of the DAG and keeps that as a static cache for computations; see the appendix for a code implementation of this.
Double buffer of DAGs
In a full client, a double buffer of 2 DAGs produced by the above formula is used. The idea is that DAGs are produced everyepochtime
number of blocks according to the params above. Instead of the client using the latest DAG produced, it uses the one previous. The benefit of this is that it allows the DAGs to be replaced over time without needing to incorporate a step where miners must suddenly recompute all of the data. Otherwise, there is the potential for an abrupt temporary slowdown in chain processing at regular intervals and dramatically increasing centralization. Thus 51% attack risks within those few minutes before all data are recomputed.
The algorithm used to generate the set of DAGs used to compute the work for a block is as follows:
Hashimoto
The idea behind the original Hashimoto is to use the blockchain as a dataset, performing a computation that selects N indices from the blockchain, gathers the transactions at those indices, performs an XOR of this data, and returns the hash of the result. Thaddeus Dryja’s original algorithm, translated to Python for consistency, is as follows:Mining and verifying
Now, let us put it all together into the mining algorithm:- For two-layer verification to work, a block header must have both the nonce and the middle value pre-sha3
- Somewhere, a block header must store the sha3 of the current seedset
Appendix
As noted above, the RNG used for DAG generation relies on some results from number theory. First, we provide assurance that the Lehmer RNG that is the basis for thepicker
variable has a wide period. Second, we show that pow(x,3,P)
will not map x
to 1
or P-1
provided x ∈ [2,P-2]
to start. Finally, we show that pow(x,3,P)
has a low collision rate when treated as a hashing function.
Lehmer random number generator
While theproduce_dag
function does not need to produce unbiased random numbers, a potential threat is that seed**i % P
only takes on a handful of values. This could provide an advantage to miners recognizing the pattern over those that do not.
To avoid this, a result from number theory is appealed to. A Safe Prime is defined to be a prime P
such that (P-1)/2
is also prime. The order of a member x
of the multiplicative group ℤ/nℤ
is defined to be the minimal m
such that
Observation 1. LetProof. Sincex
be a member of the multiplicative groupℤ/Pℤ
for a safe primeP
. Ifx mod P ≠ 1 mod P
andx mod P ≠ P-1 mod P
, then the order ofx
is eitherP-1
or(P-1)/2
.
P
is a safe prime, then by [Lagrange’s Theorem][lagrange] we have that the order of x
is either 1
, 2
, (P-1)/2
, or P-1
.
The order of x
cannot be 1
, since by Fermat’s Little Theorem we have:
x
must be a multiplicative identity of ℤ/nℤ
, which is unique. Since we assumed that x ≠ 1
by assumption, this is not possible.
The order of x
cannot be 2
unless x = P-1
, since this would violate that P
is prime.
From the above proposition, we can recognize that iterating (picker * init) % P
will have a cycle length of at least (P-1)/2
. This is because we selected P
to be a safe prime approximately equal to be a higher power of two, and init
is in the interval [2,2**256+1]
. Given the magnitude of P
, we should never expect a cycle from modular exponentiation.
When we are assigning the first cell in the DAG (the variable labeled init
), we compute pow(sha3(seed) + 2, 3, P)
. At first glance, this does not guarantee that the result is neither 1
nor P-1
. However, since P-1
is a safe prime, we have the following additional assurance, which is a corollary of Observation 1:
Observation 2. Letx
be a member of the multiplicative groupℤ/Pℤ
for a safe primeP
, and letw
be a natural number. Ifx mod P ≠ 1 mod P
andx mod P ≠ P-1 mod P
, as well asw mod P ≠ P-1 mod P
andw mod P ≠ 0 mod P
, thenxʷ mod P ≠ 1 mod P
andxʷ mod P ≠ P-1 mod P
Modular exponentiation as a hash function
For certain values ofP
and w
, the function pow(x, w, P)
may have many collisions. For instance, pow(x,9,19)
only takes on values {1,18}
.
Given that P
is prime, then an appropriate w
for a modular exponentiation hashing function can be chosen using the following result:
Observation 3. LetThus, given thatP
be a prime;w
andP-1
are relatively prime if and only if for alla
andb
inℤ/Pℤ
:aʷ mod P ≡ bʷ mod P
if and only ifa mod P ≡ b mod P
P
is prime and w
is relatively prime to P-1
, we have that |{pow(x, w, P) : x ∈ ℤ}| = P
, implying that the hashing function has the minimal collision rate possible.
In the special case that P
is a safe prime as we have selected, then P-1
only has factors 1, 2, (P-1)/2
and P-1
. Since P
> 7, we know that 3 is relatively prime to P-1
, hence w=3
satisfies the above proposition.