snarkvm_ledger_coinbase/helpers/prover_solution/
bytes.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
15use super::*;
16
17impl<N: Network> FromBytes for ProverSolution<N> {
18    /// Reads the prover solution from the buffer.
19    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
20        let partial_solution: PartialSolution<N> = FromBytes::read_le(&mut reader)?;
21        let proof = KZGProof::read_le(&mut reader)?;
22
23        Ok(Self::new(partial_solution, proof))
24    }
25}
26
27impl<N: Network> ToBytes for ProverSolution<N> {
28    /// Writes the prover solution to the buffer.
29    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
30        self.partial_solution.write_le(&mut writer)?;
31        self.proof.write_le(&mut writer)
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use console::{account::PrivateKey, network::Testnet3};
39
40    type CurrentNetwork = Testnet3;
41
42    #[test]
43    fn test_bytes() -> Result<()> {
44        let mut rng = TestRng::default();
45        let private_key = PrivateKey::<CurrentNetwork>::new(&mut rng)?;
46        let address = Address::try_from(private_key)?;
47
48        // Sample a new prover solution.
49        let partial_solution = PartialSolution::new(address, u64::rand(&mut rng), KZGCommitment(rng.gen()));
50        let expected = ProverSolution::new(partial_solution, KZGProof { w: rng.gen(), random_v: None });
51
52        // Check the byte representation.
53        let expected_bytes = expected.to_bytes_le()?;
54        assert_eq!(expected, ProverSolution::read_le(&expected_bytes[..])?);
55        assert!(ProverSolution::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
56
57        Ok(())
58    }
59}