2019-07-22 23:24:13 +05:30
|
|
|
# PoW
|
2019-05-11 02:58:06 +05:30
|
|
|
|
2019-05-13 22:40:19 +05:30
|
|
|
Sha256 based proof of work over a typed piece of data.
|
2019-05-11 02:58:06 +05:30
|
|
|
|
2019-05-13 22:40:19 +05:30
|
|
|
Any type that implementes serde::Deserialize can be tagged with a proof of work.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
Prove we did work targeting a phrase.
|
|
|
|
|
2019-05-13 22:53:00 +05:30
|
|
|
```rust
|
2019-07-22 23:24:13 +05:30
|
|
|
use PoW::PoW;
|
2019-05-13 22:40:19 +05:30
|
|
|
|
|
|
|
// very easy mode
|
|
|
|
let difficulty = u128::max_value() - u128::max_value() / 2;
|
|
|
|
|
|
|
|
let phrase = b"Phrase to tag.".to_vec();
|
2019-07-22 23:24:13 +05:30
|
|
|
let pw = PoW::prove_work(&phrase, difficulty).unwrap();
|
2019-05-13 22:40:19 +05:30
|
|
|
assert!(pw.score(&phrase).unwrap() >= difficulty);
|
|
|
|
```
|
|
|
|
|
|
|
|
Prove more difficult work. This time targeting a time.
|
|
|
|
|
2019-05-13 22:53:00 +05:30
|
|
|
```rust
|
2019-05-13 22:40:19 +05:30
|
|
|
// more diffcult, takes around 100_000 hashes to generate proof
|
|
|
|
let difficulty = u128::max_value() - u128::max_value() / 100_000;
|
|
|
|
|
|
|
|
let now: u64 = get_unix_time_seconds();
|
2019-07-22 23:24:13 +05:30
|
|
|
let pw = PoW::prove_work(&now, difficulty).unwrap();
|
2019-05-13 22:40:19 +05:30
|
|
|
assert!(pw.score(&now).unwrap() >= difficulty);
|
|
|
|
```
|
|
|
|
|
2019-05-17 21:47:27 +05:30
|
|
|
Define a blockchain block.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
struct Block<T> {
|
|
|
|
prev: [u8; 32], // hash of last block
|
|
|
|
payload: T, // generic data
|
2019-07-22 23:24:13 +05:30
|
|
|
proof_of_work: PoW<([u8; 32], T)>,
|
2019-05-17 21:47:27 +05:30
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-05-13 22:40:19 +05:30
|
|
|
# Score scheme
|
|
|
|
|
2019-07-22 23:24:13 +05:30
|
|
|
To score a proof of work for a given (target, PoW) pair:
|
|
|
|
Sha256 is calculated over the concatenation SALT + target + PoW.
|
2019-05-13 22:40:19 +05:30
|
|
|
The first 16 bytes of the hash are interpreted as a 128 bit unsigned integer.
|
|
|
|
That integer is the score.
|
2019-07-22 23:24:13 +05:30
|
|
|
A constant, SALT, is used as prefix to prevent PoW reuse from other systems such as proof
|
2019-05-13 22:40:19 +05:30
|
|
|
of work blockchains.
|
|
|
|
|
|
|
|
In other words:
|
|
|
|
|
2019-05-13 22:53:00 +05:30
|
|
|
```rust
|
2019-07-22 23:24:13 +05:30
|
|
|
fn score<T: Serialize>(target: &T, PoW_tag: &PoW<T>) -> u128 {
|
|
|
|
let bytes = serialize(&SALT) + serialize(target) + serialize(PoW_tag);
|
2019-05-13 22:40:19 +05:30
|
|
|
let hash = sha256(&bytes);
|
|
|
|
deserialize(&hash[..16])
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Serialization encoding.
|
|
|
|
|
|
|
|
It shouldn't matter to users of this library, but the bincode crate is used for cheap
|
|
|
|
deterministic serialization. All values are serialized using network byte order.
|
|
|
|
|
|
|
|
# Threshold scheme
|
|
|
|
|
2019-07-22 23:24:13 +05:30
|
|
|
Given a minimum score m. A PoW p satisfies the minimum score for target t iff score(t, p) >= m.
|
2019-05-13 22:40:19 +05:30
|
|
|
|
|
|
|
# Choosing a difficulty setting.
|
|
|
|
|
|
|
|
Difficulty settings are usually best adjusted dynamically a la bitcoin.
|
|
|
|
|
|
|
|
To manually select a difficulty, choose the average number of hashes required.
|
|
|
|
|
2019-05-13 22:53:00 +05:30
|
|
|
```rust
|
2019-05-13 22:40:19 +05:30
|
|
|
fn difficulty(average: u128) -> u128 {
|
|
|
|
debug_assert_ne!(average, 0, "It is impossible to prove work in zero attempts.");
|
|
|
|
let m = u128::max_value();
|
|
|
|
m - m / average
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Conversely, to calculate probable number of hashes required to satisfy a given minimum
|
|
|
|
difficulty.
|
|
|
|
|
2019-05-13 22:53:00 +05:30
|
|
|
```rust
|
2019-05-13 22:40:19 +05:30
|
|
|
fn average(difficulty: u128) -> u128 {
|
|
|
|
let m = u128::max_value();
|
|
|
|
if difficulty == m {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
m / (m - difficulty)
|
|
|
|
}
|
|
|
|
```
|
2019-07-20 03:06:57 +05:30
|
|
|
|
|
|
|
# License
|
|
|
|
|
|
|
|
This project is licensed under either of Apache License, Version 2.0 or MIT license, at your option.
|