1//! Support for the `v1` proof of work scheme's intro payload extension
23use crate::relaycell::hs::pow::ProofOfWorkType;
4use tor_bytes::{EncodeResult, Reader, Result, Writeable, Writer};
5use tor_hscrypto::pow::v1::{Effort, Nonce, SeedHead, SolutionByteArray};
67/// Proof of work using the `v1` scheme
8///
9/// Specified as part of <https://spec.torproject.org/rend-spec/introduction-protocol.html#INTRO1_POW_EXT>
10#[derive(derive_more::Constructor, amplify::Getters, Debug, Clone, PartialEq)]
11pub struct ProofOfWorkV1 {
12/// Nonce value chosen by the client
13#[getter(as_ref)]
14nonce: Nonce,
15/// Effort chosen by the client
16#[getter(as_copy)]
17effort: Effort,
18/// Header with which to identify the applicable service-provided seed
19#[getter(as_copy)]
20seed_head: SeedHead,
21/// Proposed solution proof, not validated
22 ///
23 /// This byte array still needs to be validated first as a well-formed
24 /// Equix solution, and then as a proof for a particular puzzle.
25#[getter(as_ref)]
26solution: SolutionByteArray,
27}
2829impl ProofOfWorkV1 {
30/// Construct by reading the scheme-specific data, if the scheme ID is correct for [`ProofOfWorkV1`]
31pub(super) fn try_take_body_from(scheme: u8, b: &mut Reader<'_>) -> Result<Option<Self>> {
32if scheme == ProofOfWorkType::V1.get() {
33Ok(Some(Self {
34 nonce: b.extract()?,
35 effort: b.extract()?,
36 seed_head: b.extract()?,
37 solution: b.extract()?,
38 }))
39 } else {
40Ok(None)
41 }
42 }
4344/// Write the scheme and scheme-specific portion the v1 Proof Of Work
45pub(super) fn write_onto<B: Writer + ?Sized>(&self, b: &mut B) -> EncodeResult<()> {
46 b.write_u8(ProofOfWorkType::V1.get());
47self.nonce.write_onto(b)?;
48self.effort.write_onto(b)?;
49self.seed_head.write_onto(b)?;
50self.solution.write_onto(b)?;
51Ok(())
52 }
53}