pow_sha256/README.md

162 lines
4.1 KiB
Markdown
Raw Permalink Normal View History

2021-03-06 09:48:58 +05:30
<div align="center">
2021-03-06 17:07:28 +05:30
<h1>PoW-SHA256</h1>
2021-03-06 09:48:58 +05:30
<p>
2021-03-06 17:07:28 +05:30
<strong>PoW-SHA256 - SHA256 based Proof-of-Work</strong>
2021-03-06 09:48:58 +05:30
</p>
[![status-badge](https://ci.batsense.net/api/badges/99/status.svg)](https://ci.batsense.net/repos/99)
2021-03-06 09:48:58 +05:30
</div>
2022-08-09 02:18:42 +05:30
**NOTE: All versions prior to 0.3.1 produce broken PoWs, upgrade to
2021-04-01 22:52:26 +05:30
latest version**
> mcaptcha_pow_sha256 is a copy of `pow_sha256` by
> [robkorn](https://github.com/robkorn/mcaptcha_pow_sha256)
2021-03-06 09:48:58 +05:30
> which is a modified version of [`pow` library](https://github.com/bddap/pow).
> All copyrights belong to the original authors.
2020-09-02 11:52:31 +05:30
2021-03-06 17:07:28 +05:30
Rust crate which generates SHA256 Proofs of Work on serializable datatypes.
2019-07-23 00:59:16 +05:30
2021-03-06 09:48:58 +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
2021-03-06 09:48:58 +05:30
This is a fork of the [`pow` library](https://github.com/bddap/pow) by
[@robkorn](https://github.com/robkorn/mcaptcha_pow_sha256)) with some new
2021-03-06 17:07:28 +05:30
additions. Primary of these being:
2021-03-08 18:27:18 +05:30
- PoW datatype now offers a constructor
2021-03-06 17:07:28 +05:30
- Salt is no longer hard coded into the library, users can provide
unique salts.
2021-03-06 09:48:58 +05:30
Other small changes have also been included of various importance but
mostly just stylistic/ease of use improvements.
2021-03-11 12:09:19 +05:30
## Documentation
https://docs.rs/mcaptcha_pow_sha256
2021-03-11 12:09:19 +05:30
2021-03-06 17:07:28 +05:30
## Examples
2021-03-06 17:07:28 +05:30
Prove work specifically targeting a phrase.
2019-05-13 22:53:00 +05:30
```rust
use mcaptcha_pow_sha256::{ConfigBuilder, PoW};
2021-03-06 17:07:28 +05:30
fn main() {
let config = ConfigBuilder::default()
.salt("myrandomsaltisnotlongenoug".into())
.build()
.unwrap();
2021-03-06 17:07:28 +05:30
let phrase = "ironmansucks";
2021-03-08 18:27:18 +05:30
const DIFFICULTY: u32 = 1000;
2021-03-06 17:07:28 +05:30
let work = config.prove_work(&phrase, DIFFICULTY).unwrap();
assert!(config.calculate(&work, &phrase).unwrap() >= DIFFICULTY);
assert!(config.is_valid_proof(&work, &phrase));
assert!(config.is_sufficient_difficulty(&work, DIFFICULTY));
}
```
Prove more difficult work. This time targeting a time.
2019-05-13 22:53:00 +05:30
```rust
2021-03-06 09:48:58 +05:30
// Greater difficulty this time around. Takes around 100,000 hashes
// to find a nonce of the correct difficulty.
use mcaptcha_pow_sha256::{ConfigBuilder, PoW};
2021-03-06 17:07:28 +05:30
fn main() {
let config = ConfigBuilder::default()
.salt("myrandomsaltisnotlongenoug".into())
.build()
.unwrap();
let phrase = "ironmansucks";
2021-03-08 18:27:18 +05:30
const DIFFICULTY: u32 = 100_000;
2021-03-06 17:07:28 +05:30
let work = config.prove_work(&phrase, DIFFICULTY).unwrap();
2021-03-06 17:07:28 +05:30
assert!(config.calculate(&work, &phrase).unwrap() >= DIFFICULTY);
assert!(config.is_valid_proof(&work, &phrase));
assert!(config.is_sufficient_difficulty(&work, DIFFICULTY));
}
```
## Hashing Scheme
`SALT` is used as prefix to prevent PoW reuse from other systems such as
proof of work blockchains.
2019-07-23 00:59:16 +05:30
SHA256 is calculated over the concatenation of the:
2021-03-06 17:07:28 +05:30
2019-07-23 00:59:16 +05:30
- SALT
2021-03-06 17:07:28 +05:30
- Serialized Input `T`
2019-07-23 00:59:16 +05:30
- Nonce
2021-03-06 09:48:58 +05:30
The first 16 bytes of the resulting hash are interpreted as a 128 bit
unsigned integer and saved as the final result.
2021-03-06 17:07:28 +05:30
## Choosing a difficulty setting.
2021-03-06 09:48:58 +05:30
Depending on your use case, difficulty settings often are best set
dynamically a la bitcoin.
2021-03-06 09:48:58 +05:30
However if your use case requires manual setting then it is trivial to
set one yourself. One way to do so is to choose the average number of
hashes desired with a function like this:
2019-05-13 22:53:00 +05:30
```rust
fn get_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
}
```
2021-03-06 17:07:28 +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:53:00 +05:30
```rust
fn est_average(difficulty: u128) -> u128 {
let m = u128::max_value();
if difficulty == m {
return m;
2021-03-06 17:07:28 +05:30
}
m / (m - difficulty)
}
```
2021-03-08 18:27:18 +05:30
## Changelog
See [CHANGELOG.md](./CHANGELOG.md)
## License
2019-07-25 00:11:28 +05:30
This project is dual-licensed under `Apache License Version 2.0` or `MIT license`.
2023-03-08 17:21:54 +05:30
## Funding
### NLnet
<div align="center">
<img
height="150px"
alt="NLnet NGIZero logo"
src="./docs/third-party/NGIZero-green.hex.svg"
/>
</div>
<br />
2023 development is funded through the [NGI0 Entrust
Fund](https://nlnet.nl/entrust), via [NLnet](https://nlnet.nl/). Please
see [here](https://nlnet.nl/project/mCaptcha/) for more details.