# Dynamic Proof of Stake

## Overview

The DarkFi blockchain is based off proof of stake privacy focused Ouroboros Crypsinous, tunned with a discrete controller to achieve a stable supply.

## Blockchain

Blockchain $C_{loc}$ is a series of epochs: it's a tree of chains, $C_{1}$, $C_{2}$, $…$, $C_{n}$, the chain ending in a single leader per slot single finalization.

Crypsinous Blockchain is built on top of Zerocash sapling scheme, and Ouroboros Genesis blockchain. Each participant $U_{p}$ stores its own local view of the Blockchain $C_{loc}$. $C_{loc}$ is a sequence of blocks $B_{i}$ (i>0), where each $B∈C_{loc}$ $B=(tx_{lead},st)$ $tx_{lead}=(LEAD,header,txs,stx_{proof})$ LEAD is a magic word, header is a metadata, and txs is a vector of transaction hash (see appendix). $stx_{proof}=(cm_{′c},sn_{c},ep,sl,ρ,h,π)$ the Block's st is the block data, and h is the hash of that data. the commitment of the newly created coin is: $(cm_{c_{2}},r_{c_{2}})=COMM(pk_{COIN}∣∣τ∣∣v_{c}∣∣ρ_{c_{2}})$, $τ$ is slot timestamp, or index. $sn_{c}$ is the coin's serial number revealed to spend the coin. $sn_{c}=PRF_{root_{sk}}(ρ_{c})$ $ρ=η_{sk_{sl}}$ $η$ is randomness from random oracle implemented as hash of previous epoch, $ρ$ id derived randomness from $η$. $π$ is the NIZK proof of the LEAD statement.

### st transactions

the blockchain view is a chain of blocks, each block $B_{j}=(tx_{lead},st)$, while $st$ being the merkle tree structure of the validated transactions received through the network, that include transfer, and public transactions.

### LEAD statement

for $x=(cm_{c_{2}},sn_{c_{1}},η,sl,ρ,h,ptr,μ_{ρ},μ_{y},root)$, and $w=(path,root_{sk_{COIN}},path_{sk_{COIN}},τ_{c},ρ_{c},r_{c_{1}},v,r_{c_{2}})$ for tuple $(x,w)∈L_{lead}$ iff:

- $pk_{COIN}=PRF_{root_{sk}}(τ_{c})$.
- $ρ_{c_{2}}=PRF_{root_{sk}}(ρ_{c_{1}})$. note here the nonce of the new coin is deterministically driven from the nonce of the old coin, this works as resistance mechanism to allow the same coin to be eligible for leadership more than once in the same epoch.
- $∀i∈{1,2}:DeComm(cm_{c_{i}},pk_{COIN}∣∣v∣∣ρ_{c_{i}},r_{c_{i}})=T$.
- path is a valid Merkle tree path to $cm_{c_{1}}$ in the tree with the root root.
- $path_{sk_{COIN}}$ is a valid path to a leaf at position $sl−τ_{c}$ in a tree with a root $root_{sk}$.
- $sn_{c_{1}}=PRF_{root_{sk}}(ρ_{c_{1}})$
- $y=μ_{y}$
- $ρ=μ_{ρ}$
- $y<T(v)$ note that this process involves burning old coin $c_{1}$, minting new $c_{2}$ of the same value + reward.

#### validation rules

validation of proposed lead proof as follows:

- slot index is less than current slot index
- proposal extend from valid fork chain
- transactions doesn't exceed max limit
- signature is valid based off producer public key
- verify block hash
- verify block header hash
- public inputs $μ_{y}$, $μ_{rho}$ are hash of current consensus $η$, and current slot
- public inputs of target 2-term approximations $σ_{1}$, $σ_{2}$ are valid given total network stake and controller parameters
- the competing coin nullifier isn't published before to protect against double spending, before burning the coin.
- verify block transactions

## Epoch

An epoch is a vector of blocks. Some of the blocks might be empty if there is no winning leader. tokens in stake are constant during the epoch.

## Leader selection

At the onset of each slot each stakeholder needs to verify if it's the weighted random leader for this slot.

$y<T_{i}$

^{ check if the random y output is less than some threshold }

This statement might hold true for zero or more stakeholders, thus we might end up with multiple leaders for a slot, and other times no leader. Also note that no node would know the leader identity or how many leaders are there for the slot, until it receives a signed block with a proof claiming to be a leader.

^{$η$ is random nonce generated from the blockchain, $sid$ is block id}

$ϕ_{f}=1−(1−f)_{α_{i}}$ $T_{i}=Lϕ_{f}(α_{i})$

Note that $ϕ_{f}(1)=f$, $f$: the active slot coefficient is the probability that a party holding all the stake will be selected to be a leader. Stakeholder is selected as leader for slot j with probability $ϕ_{f}(α_{i})$, $α_{i}$ is $U_{i}$ relative stake.

see the appendix for absolute stake aggregation dependent leader selection family of functions.

### automating f tuning

the stable consensus token supply is maintained by the help of discrete PID controller, that maintain stabilized occurrence of single leader per slot.

#### control lottery f tunning parameter

$f[k]=f[k−1]+K_{1}e[k]+K_{2}e[k−1]+K_{3}e[k−2]$

with $k_{1}=k_{p}+K_{i}+K_{d}$, $k_{2}=−K_{p}−2K_{d}$, $k_{3}=K_{d}$, and e is the error function.

### target T n-term approximation

target function is approximated to avoid use of power, and division in zk, since no function in the family of functions that have independent aggregation property achieve avoid it (see appendix).

#### target function

target fuction T: $T=L∗ϕ(σ)=L∗(1−(1−f)_{σ})$ $σ$ is relative stake. f is tuning parameter, or the probability of winning have all the stake L is field length

#### $ϕ(σ)$ approximation

$ϕ(σ)=1−(1−f)_{σ}$ $=1−e_{σln(1−f)}$ $=1−(1+n=1∑∞ n!(σln(1−f))_{n} )$ $σ=Σs $ s is stake, and $Σ$ is total stake.

#### target T n term approximation

$k=Lln(1−f)_{1}$ $k_{_{′}n}=Lln(1−f)_{n}$ $T=−[kσ+2!k_{_{′′}} σ_{2}+⋯+n!k_{_{′}n} σ_{n}]$ $=−[Σk s+Σ_{2}2!k_{_{′′}} s_{2}+⋯+Σ_{n}n!k_{_{′}n} s_{n}]$

#### comparison of original target to approximation

# Appendix

This section gives further details about the structures that will be used by the protocol.

## Blockchain

Field | Type | Description |
---|---|---|

`blocks` | `Vec<Block>` | Series of blocks consisting the Blockchain |

## Header

Field | Type | Description |
---|---|---|

`version` | `u8` | Version |

`previous` | `blake3Hash` | Previous block hash |

`epoch` | `u64` | Epoch |

`slot` | `u64` | Slot UID |

`timestamp` | `Timestamp` | Block creation timestamp |

`root` | `MerkleRoot` | Root of the transaction hashes merkle tree |

## Block

Field | Type | Description |
---|---|---|

`magic` | `u8` | Magic bytes |

`header` | `blake3Hash` | Header hash |

`txs` | `Vec<blake3Hash>` | Transaction hashes |

`lead_info` | `LeadInfo` | Block leader information |

## LeadInfo

Field | Type | Description |
---|---|---|

`signature` | `Signature` | Block owner signature |

`public_inputs` | `Vec<pallas::Base>` | Nizk proof public inputs |

`serial_number` | `pallas::Base` | competing coin's nullifier |

`eta` | `[u8; 32]` | randomness from the previous epoch |

`proof` | `Vec<u8>` | Nizk $π$ Proof the stakeholder is the block owner |

`offset` | `u64` | Slot offset block producer used |

`leaders` | `u64` | Block producer leaders count |

## Public Inputs

Field | Type | Description |
---|---|---|

`pk` | `pallas::Base` | burnt coin public key |

`c1_cm_x` | `pallas::Base` | burnt coin commitment x coordinate |

`c1_cm_y` | `pallas::Base` | burnt coin commitment y coordinate |

`c2_cm_x` | `pallas::Base` | minted coin commitment x coordinate |

`c2_cm_y` | `pallas::Base` | minted coin commitment y coordinate |

`cm1_root` | `pallas::Base` | root of burnt coin commitment in burnt merkle tree |

`c1_sk_root` | `pallas::Base` | burn coin secret key |

`sn` | `pallas::Base` | burnt coin spending nullifier |

`y_mu` | `pallas::Base` | random seed base from blockchain |

`y` | `pallas::Base` | hash of random seed, and `y_mu` , used in lottery |

`rho_mu` | `pallas::Base` | random seed base from blockchain |

`rho` | `pallas::Base` | hash of random seed and `rho_mu` to constrain lottery |

`sigma1` | `pallas::Base` | first term in 2-terms target approximation. |

`sigma2` | `pallas::Base` | second term in 2-terms target approximation. |

### Linear family functions

In the previous leader selection function, it has the unique property of independent aggregation of the stakes, meaning the property of a leader winning leadership with stakes $σ$ is independent of whether the stakeholder would act as a pool of stakes, or distributed stakes on competing coins. "one minus the probability" of winning leadership with aggregated stakes is $1−ϕ(∑_{i}σ_{i})=1−(1+(1−f)_{σ_{i}})=−(1−f)_{∑_{i}σ_{i}}$, the joint "one minus probability" of all the stakes (each with probability $ϕ(σ_{i}))$ winning aggregated winning the leadership $∏_{i}(1−ϕ(σ_{i}))=−(1−f)_{∑_{i}(σ_{i})}$ thus: $1−ϕ(i∑ σ_{i})=i∏n (1−ϕ(σ_{i}))$

A non-exponential linear leader selection can be:

$y<T$ $y=2_{l}k∣0≤k≤1$ $T=2_{l}ϕ(v)$ $ϕ(v)=v_{max+}+c1 v∣c∈Z$

#### Dependent aggregation

Linear leader selection has the dependent aggregation property, meaning it's favorable to compete in pools with sum of the stakes over aggregated stakes of distributed stakes:

$ϕ(i∑ σ_{i})>i∏n σ_{i}$ $i∑ σ_{i}>(v_{max}+c1 )_{n−1}v_{1}v_{2}…v_{n}$ let's assume the stakes are divided to stakes of value $σ_{i}=1$ for $Σ>1∈Z$, $∑_{i}σ_{i}=V$ $V>(v_{max}+c1 )_{n−1}$ note that $(v_{max}+c1 )_{n−1}<1,V>1$, thus competing with single coin of the sum of stakes held by the stakeholder is favorable.

#### Scalar linear aggregation dependent leader selection

A target function T with scalar coefficients can be formalized as $T=2_{l}kϕ(Σ)=2_{l}(v_{max}+c1 )Σ$ let's assume $v_{max}=2_{v}$, and $c=0$ then: $T=2_{l}kϕ(Σ)=2_{l−v}Σ$ then the lead statement is $y<2_{l−v}Σ$ for example for a group order or l= 24 bits, and maximum value of $v_{max}=2_{10}$, then lead statement: $y<2_{14}Σ$

#### Competing max value coins

For a stakeholder with $nv_{max}$ absolute stake, $∣n∈Z$ it's advantageous for the stakeholder to distribute stakes on $n$ competing coins.

#### Inverse functions

Inverse lead selection functions doesn't require maximum stake, most suitable for absolute stake, it has the disadvantage that it's inflating with increasing rate as time goes on, but it can be function of the inverse of the slot to control the increasing frequency of winning leadership.

#### Leader selection without maximum stake upper limit

The inverse leader selection without maximum stake value can be $ϕ(v)=v+cv ∣c>1$ and inversely proportional with probability of winning leadership, let it be called leadership coefficient.

#### Decaying linear leader selection

As the time goes one, and stakes increase, this means the combined stakes of all stakeholders increases the probability of winning leadership in next slots leading to more leaders at a single slot, to maintain, or to be more general to control this frequency of leaders per slot, c (the leadership coefficient) need to be function of the slot $sl$, i.e $c(sl)=Rsl $ where $R$ is epoch size (number of slots in epoch).

#### Pairing leader selection independent aggregation function

The only family of functions $ϕ(α)$ that are isomorphic to summation on multiplication $ϕ(α_{1}+α_{2})=ϕ(α_{1})ϕ(α_{2})$(having the independent aggregation property) is the exponential function, and since it's impossible to implement in plonk, a re-formalization of the lead statement using pairing that is isomorphic to summation on multiplication is an option.

Let's assume $ϕ$ is isomorphic function between multiplication and addition, $ϕ(α)=ϕ(2α )ϕ(2α )=ϕ(2α )_{2}$, thus: $ϕ(α)=αϕ(1)…ϕ(1) =ϕ(1)_{α}$ then the only family of functions $ϕ:R→R$ satisfying this is the exponential function $ϕ(α)=c_{α}∣c∈R$

#### no solution for the lead statement parameters, and constants $S,f,α$ defined over group of integers.

assume there is a solution for the lead statement parameters and constants $S,f,α$ defined over group of integers. for the statement $y<T$, $T=Lϕ_{max}ϕ(α)=Sϕ(α)$ $S=ord(G)ϕ_{max}ϕ(α)$ such that S $inZ$ $ϕ_{max}=ϕ(α_{max})$ where $α_{max}$ is the maximum stake value being $2_{64}$, following from the previous proof that the family of function having independent aggregation property is the exponential function $f_{α}$, and $f∈Z∣f>1$, the smallest value satisfying f is $f=2$, then $ϕ_{max}=2_{2_{64}}$ note that since $ord(G)<<ϕ_{max}$ thus $S<<1$, contradiction.

### Leaky non-resettable beacon

Built on top of globally synchronized clock, that leaks the nonce $η$ of the next epoch a head of time (thus called leaky), non-resettable in the sense that the random nonce is deterministic at slot s, while assuring security against adversary controlling some stakeholders.

For an epoch j, the nonce $η_{j}$ is calculated by hash function H, as:

$η_{j}=H(η_{j−1}∣∣j∣∣v)$

v is the concatenation of the value $ρ$ in all blocks from the beginning of epoch $e_{i−1}$ to the slot with timestamp up to $(j−2)R+1+ϵ16k $, note that k is a persistence security parameter, R is the epoch length in terms of slots.

### toward better decentralization in ouroboros

the randomization of the leader selection at each slot is hinged on the random $y$, $μ_{y}$, $ρ_{c}$, those three values are derived from $η$, and root of the secret keys, the root of the secret keys for each stakeholder can be sampled, and derived beforehand, but $η$ is a response to global random oracle query, so it's security is hinged on $centralized global random node$.

#### solution

to break this centralization, a decentralized emulation of $G_{ro}$ functionality for calculation of: $η_{i}=PRF_{η_{i−1}}(ψ)$ $ψ=hash(tx_{0})$ $η_{0}=hash("lettherebedark!")$ note that first transaction in the block, is the proof transaction.