Skip to main content

zebra_chain/orchard/
note.rs

1//! Orchard notes
2
3use group::ff::PrimeField;
4use halo2::pasta::pallas;
5use rand_core::{CryptoRng, Rng};
6
7use crate::{
8    amount::{Amount, NonNegative},
9    error::RandError,
10};
11
12use super::address::Address;
13
14mod ciphertexts;
15mod nullifiers;
16
17pub use ciphertexts::{EncryptedNote, WrappedNoteKey};
18pub use nullifiers::Nullifier;
19
20#[cfg(any(test, feature = "proptest-impl"))]
21mod arbitrary;
22
23/// A random seed (rseed) used in the Orchard note creation.
24#[derive(Clone, Copy, Debug)]
25// At the moment this field is never read.
26//
27// TODO: consider replacing this code with the equivalent `orchard` crate code,
28//       which is better tested.
29#[allow(dead_code)]
30pub struct SeedRandomness(pub(crate) [u8; 32]);
31
32impl SeedRandomness {
33    pub fn new<T>(csprng: &mut T) -> Result<Self, RandError>
34    where
35        T: Rng + CryptoRng,
36    {
37        let mut bytes = [0u8; 32];
38        csprng
39            .try_fill_bytes(&mut bytes)
40            .map_err(|_| RandError::FillBytes)?;
41        Ok(Self(bytes))
42    }
43}
44
45/// Used as input to PRF^nf as part of deriving the _nullifier_ of the _note_.
46///
47/// When creating a new note from spending an old note, the new note's _rho_ is
48/// the _nullifier_ of the previous note. If creating a note from scratch (like
49/// a miner reward), a dummy note is constructed, and its nullifier as the _rho_
50/// for the actual output note. When creating a dummy note, its _rho_ is chosen
51/// as a random Pallas point's x-coordinate.
52///
53/// <https://zips.z.cash/protocol/nu5.pdf#orcharddummynotes>
54#[derive(Clone, Debug)]
55pub struct Rho(pub(crate) pallas::Base);
56
57impl From<Rho> for [u8; 32] {
58    fn from(rho: Rho) -> Self {
59        rho.0.to_repr()
60    }
61}
62
63impl From<Nullifier> for Rho {
64    fn from(nf: Nullifier) -> Self {
65        Self(nf.0)
66    }
67}
68
69/// Additional randomness used in deriving the _nullifier_.
70///
71/// <https://zips.z.cash/protocol/nu5.pdf#orchardsend>
72#[derive(Clone, Debug)]
73pub struct Psi(pub(crate) pallas::Base);
74
75impl From<Psi> for [u8; 32] {
76    fn from(psi: Psi) -> Self {
77        psi.0.to_repr()
78    }
79}
80
81/// A Note represents that a value is spendable by the recipient who holds the
82/// spending key corresponding to a given shielded payment address.
83///
84/// <https://zips.z.cash/protocol/protocol.pdf#notes>
85#[derive(Clone, Debug)]
86pub struct Note {
87    /// The recipient's shielded payment address.
88    pub address: Address,
89    /// An integer representing the value of the _note_ in zatoshi.
90    pub value: Amount<NonNegative>,
91    /// Used as input to PRF^nfOrchard_nk as part of deriving the _nullifier_ of
92    /// the _note_.
93    pub rho: Rho,
94    /// 32 random bytes from which _rcm_, _psi_, and the _ephemeral private key_
95    /// are derived.
96    pub rseed: SeedRandomness,
97}
98
99impl Note {
100    /// Create an Orchard _note_, by choosing 32 uniformly random bytes for
101    /// rseed.
102    ///
103    /// <https://zips.z.cash/protocol/protocol.pdf#notes>
104    pub fn new<T>(
105        csprng: &mut T,
106        address: Address,
107        value: Amount<NonNegative>,
108        nf_old: Nullifier,
109    ) -> Result<Self, RandError>
110    where
111        T: Rng + CryptoRng,
112    {
113        Ok(Self {
114            address,
115            value,
116            rho: nf_old.into(),
117            rseed: SeedRandomness::new(csprng)?,
118        })
119    }
120}