diem_crypto/
vdf_sha3.rs

1use crate::{HashValue, VerifiableDelayFunction};
2
3/// VDF SHA256.
4pub struct VdfSha3 {}
5
6impl VerifiableDelayFunction for VdfSha3 {
7    fn solve(
8        &self, challenge: &[u8], difficulty: u64,
9    ) -> anyhow::Result<Vec<u8>> {
10        let mut output = HashValue::sha3_256_of(challenge);
11        for _ in 0..difficulty {
12            output = HashValue::sha3_256_of(output.as_ref());
13        }
14        Ok(output.to_vec())
15    }
16
17    fn verify(
18        &self, challenge: &[u8], difficulty: u64, alleged_solution: &[u8],
19    ) -> anyhow::Result<()> {
20        let mut output = HashValue::sha3_256_of(challenge);
21        for _ in 0..difficulty {
22            output = HashValue::sha3_256_of(output.as_ref());
23        }
24        if output.as_ref() == alleged_solution {
25            Ok(())
26        } else {
27            anyhow::bail!("Invalid solution");
28        }
29    }
30}