snarkvm_ledger_coinbase/helpers/partial_solution/mod.rs
1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod bytes;
16mod serialize;
17mod string;
18
19use super::*;
20
21/// The partial solution for the coinbase puzzle from a prover.
22#[derive(Copy, Clone, Eq, PartialEq, Hash)]
23pub struct PartialSolution<N: Network> {
24 /// The address of the prover.
25 address: Address<N>,
26 /// The nonce for the solution.
27 nonce: u64,
28 /// The commitment for the solution.
29 commitment: PuzzleCommitment<N>,
30}
31
32impl<N: Network> PartialSolution<N> {
33 /// Initializes a new instance of the partial solution.
34 pub fn new<C: Into<PuzzleCommitment<N>>>(address: Address<N>, nonce: u64, commitment: C) -> Self {
35 Self { address, nonce, commitment: commitment.into() }
36 }
37
38 /// Returns the address of the prover.
39 pub const fn address(&self) -> Address<N> {
40 self.address
41 }
42
43 /// Returns the nonce for the solution.
44 pub const fn nonce(&self) -> u64 {
45 self.nonce
46 }
47
48 /// Returns the commitment for the solution.
49 pub const fn commitment(&self) -> PuzzleCommitment<N> {
50 self.commitment
51 }
52
53 /// Returns the prover polynomial.
54 pub fn to_prover_polynomial(
55 &self,
56 epoch_challenge: &EpochChallenge<N>,
57 ) -> Result<DensePolynomial<<N::PairingCurve as PairingEngine>::Fr>> {
58 CoinbasePuzzle::prover_polynomial(epoch_challenge, self.address(), self.nonce())
59 }
60
61 /// Returns the target of the solution.
62 pub fn to_target(&self) -> Result<u64> {
63 self.commitment.to_target()
64 }
65}