tor_hscrypto/pow/v1/
verify.rs
1use crate::pow::err::Error;
4use crate::pow::v1::challenge::Challenge;
5use crate::pow::v1::{err::RuntimeErrorV1, err::SolutionErrorV1, types::Instance, types::Solution};
6use equix::{EquiXBuilder, HashError, RuntimeOption};
7
8use super::Seed;
9
10pub struct Verifier {
15 instance: Instance,
17 equix: EquiXBuilder,
19}
20
21impl Verifier {
22 pub fn new(instance: Instance) -> Self {
24 Self {
25 instance,
26 equix: Default::default(),
27 }
28 }
29
30 pub fn runtime(&mut self, option: RuntimeOption) -> &mut Self {
34 self.equix.runtime(option);
35 self
36 }
37
38 pub fn check(&self, solution: &Solution) -> Result<(), Error> {
42 match self.check_seed(solution) {
43 Err(e) => Err(Error::BadSolution(e.into())),
44 Ok(()) => {
45 let challenge = Challenge::new(&self.instance, solution.effort(), solution.nonce());
46 match challenge.check_effort(&solution.proof_to_bytes()) {
47 Err(e) => Err(Error::BadSolution(e.into())),
48 Ok(()) => match self.equix.verify(challenge.as_ref(), solution.proof()) {
49 Ok(()) => Ok(()),
50 Err(equix::Error::HashSum) => {
51 Err(Error::BadSolution(SolutionErrorV1::HashSum.into()))
52 }
53 Err(equix::Error::Hash(HashError::ProgramConstraints)) => Err(
54 Error::BadSolution(SolutionErrorV1::ChallengeConstraints.into()),
55 ),
56 Err(e) => Err(Error::VerifyRuntime(RuntimeErrorV1::EquiX(e).into())),
57 },
58 }
59 }
60 }
61 }
62
63 fn check_seed(&self, solution: &Solution) -> Result<(), SolutionErrorV1> {
68 if solution.seed_head() == self.instance.seed().head() {
69 Ok(())
70 } else {
71 Err(SolutionErrorV1::Seed)
72 }
73 }
74
75 pub fn seed(&self) -> &Seed {
77 self.instance.seed()
78 }
79}