blake2b_pow/
lib.rs

1#![no_std]
2
3use blake2_rfc::blake2b::blake2b;
4
5mod params;
6
7/// # Blake2B PoW
8/// This rust library follows Nano's Proof of Work concept for their cryptocurrency. It uses the hash function Blake2B, and the input address, and keys it with a u64 nonce that is ever-increasing.
9/// 
10/// It takes as input an address, or previous block hash, as a u8 byte slice, and it also takes the threshold u64 value, with a higher value indicating greater difficulty.
11/// 
12/// It outputs the nonce that was either equal to or more than the threshold after the hashing process.
13/// 
14/// ## Default Value
15/// * Threshold: 0xffffffc000000000
16pub fn mine (address: &[u8], threshold: u64) -> u64 {
17    let mut nonce: u64 = 0u64;
18    
19    loop {
20        let w = blake2b(8, &nonce.to_be_bytes(), &address);
21        
22        if w.as_bytes() < &threshold.to_be_bytes() {
23            nonce += 1u64;
24        }
25        else {
26            return nonce
27        }
28    }
29}
30
31/// # Verify The Nonce
32/// A Function to validate the nonce is correct and the output is above the threshold.
33pub fn verify_nonce (address: &[u8], threshold: u64, nonce: u64) -> bool {
34    let w = blake2b(8, &nonce.to_be_bytes(), &address);
35    if w.as_bytes() >= &threshold.to_be_bytes() {
36        return true
37    }
38    else {
39        return false
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    #[test]
46    fn it_works() {
47        assert_eq!(2 + 2, 4);
48    }
49}