2019-07-23 02:43:00 +05:30
# PoW_SHA256
2019-05-11 02:58:06 +05:30
2019-07-23 00:59:16 +05:30
Rust crate which generates SHA256 Proofs of Work on serializable datatypes.
2019-07-23 02:43:00 +05:30
Whether for blockchain-related projects or Hashcash-like schemes, this crate can be used to prove work was done on a given serializable input. The input merely needs to implement `serde::Deserialize` to be used.
2019-05-11 02:58:06 +05:30
2019-07-23 02:43:00 +05:30
This is a fork of the [`pow` library ](https://github.com/bddap/pow ) by bddap with some new additions. Primary of these being:
2019-07-23 00:34:45 +05:30
- PoW datatype now saves the calculation result to be used for checking proof validity given input
- `is_valid_proof` method to do the above mentioned
Other small changes have also been included of various importance but mostly just stylistic/ease of use improvements.
2019-05-13 22:40:19 +05:30
# Examples
2019-07-23 02:43:00 +05:30
Prove work was done, specifically targeting a phrase.
2019-05-13 22:40:19 +05:30
2019-05-13 22:53:00 +05:30
```rust
2019-07-23 02:43:00 +05:30
use PoW_SHA256::PoW;
2019-05-13 22:40:19 +05:30
2019-07-23 02:43:00 +05:30
// Very easy difficulty
2019-05-13 22:40:19 +05:30
let difficulty = u128::max_value() - u128::max_value() / 2;
2019-07-23 02:43:00 +05:30
let phrase = b"Phrase to be used.".to_vec();
2019-07-22 23:24:13 +05:30
let pw = PoW::prove_work(& phrase, difficulty).unwrap();
2019-07-23 02:43:00 +05:30
// Asserting that the result is of sufficient difficulty
assert!(pw.result >= difficulty);
// Asserting that the PoW was generated from the provided phrase
assert!(pw.is_valid_proof(& phrase))
2019-05-13 22:40:19 +05:30
```
Prove more difficult work. This time targeting a time.
2019-05-13 22:53:00 +05:30
```rust
2019-07-23 02:43:00 +05:30
// Greater diffculty this time around. Takes around 100,000 hashes to find a nonce of the correct difficulty.
2019-05-13 22:40:19 +05:30
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-17 21:47:27 +05:30
2019-07-23 02:43:00 +05:30
assert!(pw.result >= difficulty);
assert!(pw.is_valid_proof(& phrase))
2019-05-17 21:47:27 +05:30
```
2019-07-23 02:43:00 +05:30
2019-07-23 00:59:16 +05:30
# Hashing Scheme
2019-05-13 22:40:19 +05:30
2019-07-23 00:59:16 +05:30
SHA256 is calculated over the concatenation of the:
- SALT
- Serialized Input `T`
- Nonce
2019-05-13 22:40:19 +05:30
2019-07-23 00:59:16 +05:30
The first 16 bytes of the resulting hash are interpreted as a 128 bit unsigned integer.
2019-05-13 22:40:19 +05:30
2019-07-23 00:59:16 +05:30
A randomly generated constant, `SALT` , is used as prefix to prevent PoW reuse from other systems such as proof of work blockchains.
2019-05-13 22:40:19 +05:30
# Choosing a difficulty setting.
2019-07-23 02:43:00 +05:30
Depending on your use case, difficulty settings often are best set dynamically a la bitcoin.
2019-05-13 22:40:19 +05:30
2019-07-23 02:43:00 +05:30
However if your use case requires manual setting then it is trivial to set one yourself. One way to manually select a difficulty is to choose the average Hnumber of hashes desired with a function like this:
2019-05-13 22:40:19 +05:30
2019-05-13 22:53:00 +05:30
```rust
2019-07-23 02:43:00 +05:30
fn get_difficulty(average: u128) -> u128 {
2019-05-13 22:40:19 +05:30
debug_assert_ne!(average, 0, "It is impossible to prove work in zero attempts.");
let m = u128::max_value();
m - m / average
}
```
2019-07-23 02:43:00 +05:30
Conversely we can use the same equation to calculate the probable number of hashes required to satisfy a given difficulty:
2019-05-13 22:40:19 +05:30
2019-05-13 22:53:00 +05:30
```rust
2019-07-23 02:43:00 +05:30
fn est_average(difficulty: u128) -> u128 {
2019-05-13 22:40:19 +05:30
let m = u128::max_value();
if difficulty == m {
return m;
}
m / (m - difficulty)
}
```
2019-07-20 03:06:57 +05:30
# License
2019-07-23 00:59:16 +05:30
This project is dual-licensed under `Apache License Version 2.0` & `MIT license` .