1//! Orchard notes
23use group::{ff::PrimeField, GroupEncoding};
4use halo2::pasta::pallas;
5use rand_core::{CryptoRng, RngCore};
67use crate::{
8 amount::{Amount, NonNegative},
9 error::{NoteError, RandError},
10};
1112use super::{address::Address, sinsemilla::extract_p};
1314mod ciphertexts;
15mod nullifiers;
1617pub use ciphertexts::{EncryptedNote, WrappedNoteKey};
18pub use nullifiers::Nullifier;
1920#[cfg(any(test, feature = "proptest-impl"))]
21mod arbitrary;
2223/// 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]);
3132impl SeedRandomness {
33pub fn new<T>(csprng: &mut T) -> Result<Self, RandError>
34where
35T: RngCore + CryptoRng,
36 {
37let mut bytes = [0u8; 32];
38 csprng
39 .try_fill_bytes(&mut bytes)
40 .map_err(|_| RandError::FillBytes)?;
41Ok(Self(bytes))
42 }
43}
4445/// 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);
5657impl From<Rho> for [u8; 32] {
58fn from(rho: Rho) -> Self {
59 rho.0.to_repr()
60 }
61}
6263impl From<Nullifier> for Rho {
64fn from(nf: Nullifier) -> Self {
65Self(nf.0)
66 }
67}
6869impl Rho {
70pub fn new<T>(csprng: &mut T) -> Result<Self, NoteError>
71where
72T: RngCore + CryptoRng,
73 {
74let mut bytes = [0u8; 32];
75 csprng
76 .try_fill_bytes(&mut bytes)
77 .map_err(|_| NoteError::from(RandError::FillBytes))?;
7879let possible_point = pallas::Point::from_bytes(&bytes);
8081if possible_point.is_some().into() {
82Ok(Self(extract_p(possible_point.unwrap())))
83 } else {
84Err(NoteError::InvalidRho)
85 }
86 }
87}
8889/// Additional randomness used in deriving the _nullifier_.
90///
91/// <https://zips.z.cash/protocol/nu5.pdf#orchardsend>
92#[derive(Clone, Debug)]
93pub struct Psi(pub(crate) pallas::Base);
9495impl From<Psi> for [u8; 32] {
96fn from(psi: Psi) -> Self {
97 psi.0.to_repr()
98 }
99}
100101/// A Note represents that a value is spendable by the recipient who holds the
102/// spending key corresponding to a given shielded payment address.
103///
104/// <https://zips.z.cash/protocol/protocol.pdf#notes>
105#[derive(Clone, Debug)]
106pub struct Note {
107/// The recipient's shielded payment address.
108pub address: Address,
109/// An integer representing the value of the _note_ in zatoshi.
110pub value: Amount<NonNegative>,
111/// Used as input to PRF^nfOrchard_nk as part of deriving the _nullifier_ of
112 /// the _note_.
113pub rho: Rho,
114/// 32 random bytes from which _rcm_, _psi_, and the _ephemeral private key_
115 /// are derived.
116pub rseed: SeedRandomness,
117}
118119impl Note {
120/// Create an Orchard _note_, by choosing 32 uniformly random bytes for
121 /// rseed.
122 ///
123 /// <https://zips.z.cash/protocol/protocol.pdf#notes>
124pub fn new<T>(
125 csprng: &mut T,
126 address: Address,
127 value: Amount<NonNegative>,
128 nf_old: Nullifier,
129 ) -> Result<Self, RandError>
130where
131T: RngCore + CryptoRng,
132 {
133Ok(Self {
134 address,
135 value,
136 rho: nf_old.into(),
137 rseed: SeedRandomness::new(csprng)?,
138 })
139 }
140}