2021-01-20 12:30:35 -08:00
|
|
|
//! Structs related to bundles of Orchard actions.
|
|
|
|
|
2021-02-24 12:07:49 -08:00
|
|
|
use nonempty::NonEmpty;
|
|
|
|
|
2021-01-20 12:30:35 -08:00
|
|
|
use crate::{
|
|
|
|
circuit::Proof,
|
2021-04-19 15:26:58 -07:00
|
|
|
note::{EncryptedNote, ExtractedNoteCommitment, Nullifier},
|
2021-01-20 12:35:54 -08:00
|
|
|
primitives::redpallas::{self, Binding, SpendAuth},
|
2021-01-20 12:30:35 -08:00
|
|
|
tree::Anchor,
|
|
|
|
value::{ValueCommitment, ValueSum},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An action applied to the global ledger.
|
|
|
|
///
|
|
|
|
/// Externally, this both creates a note (adding a commitment to the global ledger),
|
|
|
|
/// and consumes some note created prior to this action (adding a nullifier to the
|
|
|
|
/// global ledger).
|
|
|
|
///
|
|
|
|
/// Internally, this may both consume a note and create a note, or it may do only one of
|
|
|
|
/// the two. TODO: Determine which is more efficient (circuit size vs bundle size).
|
|
|
|
#[derive(Debug)]
|
2021-03-03 09:35:25 -08:00
|
|
|
pub struct Action<T> {
|
2021-01-20 12:30:35 -08:00
|
|
|
/// The nullifier of the note being spent.
|
2021-04-21 20:33:37 -07:00
|
|
|
nf: Nullifier,
|
2021-01-20 12:30:35 -08:00
|
|
|
/// The randomized verification key for the note being spent.
|
2021-01-20 12:35:54 -08:00
|
|
|
rk: redpallas::VerificationKey<SpendAuth>,
|
2021-01-20 12:30:35 -08:00
|
|
|
/// A commitment to the new note being created.
|
2021-04-21 20:33:37 -07:00
|
|
|
cmx: ExtractedNoteCommitment,
|
2021-01-20 12:30:35 -08:00
|
|
|
/// The encrypted output note.
|
|
|
|
encrypted_note: EncryptedNote,
|
|
|
|
/// A commitment to the net value created or consumed by this action.
|
|
|
|
cv_net: ValueCommitment,
|
2021-03-03 09:35:25 -08:00
|
|
|
/// The authorization for this action.
|
|
|
|
authorization: T,
|
|
|
|
}
|
|
|
|
|
2021-04-14 21:14:34 -07:00
|
|
|
impl<T> Action<T> {
|
2021-04-21 20:33:37 -07:00
|
|
|
/// Constructs an `Action` from its constituent parts.
|
|
|
|
pub fn from_parts(
|
|
|
|
nf: Nullifier,
|
|
|
|
rk: redpallas::VerificationKey<SpendAuth>,
|
|
|
|
cmx: ExtractedNoteCommitment,
|
|
|
|
encrypted_note: EncryptedNote,
|
|
|
|
cv_net: ValueCommitment,
|
|
|
|
authorization: T,
|
|
|
|
) -> Self {
|
|
|
|
Action {
|
|
|
|
nf,
|
|
|
|
rk,
|
|
|
|
cmx,
|
|
|
|
encrypted_note,
|
|
|
|
cv_net,
|
|
|
|
authorization,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:24:01 -07:00
|
|
|
/// Returns the nullifier of the note being spent.
|
|
|
|
pub fn nullifier(&self) -> &Nullifier {
|
|
|
|
&self.nf
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the randomized verification key for the note being spent.
|
|
|
|
pub fn rk(&self) -> &redpallas::VerificationKey<SpendAuth> {
|
|
|
|
&self.rk
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the commitment to the new note being created.
|
|
|
|
pub fn cmx(&self) -> &ExtractedNoteCommitment {
|
|
|
|
&self.cmx
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the encrypted note ciphertext.
|
|
|
|
pub fn encrypted_note(&self) -> &EncryptedNote {
|
|
|
|
&self.encrypted_note
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the commitment to the net value created or consumed by this action.
|
|
|
|
pub fn cv_net(&self) -> &ValueCommitment {
|
|
|
|
&self.cv_net
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the authorization for this action.
|
|
|
|
pub fn authorization(&self) -> &T {
|
|
|
|
&self.authorization
|
|
|
|
}
|
|
|
|
|
2021-04-14 21:14:34 -07:00
|
|
|
/// Transitions this action from one authorization state to another.
|
|
|
|
pub fn map<U>(self, step: impl FnOnce(T) -> U) -> Action<U> {
|
|
|
|
Action {
|
2021-04-21 20:33:37 -07:00
|
|
|
nf: self.nf,
|
2021-04-14 21:14:34 -07:00
|
|
|
rk: self.rk,
|
2021-04-21 20:33:37 -07:00
|
|
|
cmx: self.cmx,
|
2021-04-14 21:14:34 -07:00
|
|
|
encrypted_note: self.encrypted_note,
|
|
|
|
cv_net: self.cv_net,
|
|
|
|
authorization: step(self.authorization),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transitions this action from one authorization state to another.
|
|
|
|
pub fn try_map<U, E>(self, step: impl FnOnce(T) -> Result<U, E>) -> Result<Action<U>, E> {
|
|
|
|
Ok(Action {
|
2021-04-21 20:33:37 -07:00
|
|
|
nf: self.nf,
|
2021-04-14 21:14:34 -07:00
|
|
|
rk: self.rk,
|
2021-04-21 20:33:37 -07:00
|
|
|
cmx: self.cmx,
|
2021-04-14 21:14:34 -07:00
|
|
|
encrypted_note: self.encrypted_note,
|
|
|
|
cv_net: self.cv_net,
|
|
|
|
authorization: step(self.authorization)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:35:25 -08:00
|
|
|
/// Orchard-specific flags.
|
2021-04-14 21:14:34 -07:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2021-03-03 09:35:25 -08:00
|
|
|
pub struct Flags {
|
2021-04-21 21:35:47 -07:00
|
|
|
/// Flag denoting whether Orchard spends are enabled in the transaction.
|
|
|
|
///
|
2021-04-26 14:10:32 -07:00
|
|
|
/// If `false`, spent notes within [`Action`]s in the transaction's [`Bundle`] are
|
|
|
|
/// guaranteed to be dummy notes. If `true`, the spent notes may be either real or
|
2021-04-21 21:35:47 -07:00
|
|
|
/// dummy notes.
|
2021-03-03 09:35:25 -08:00
|
|
|
spends_enabled: bool,
|
2021-04-21 21:35:47 -07:00
|
|
|
/// Flag denoting whether Orchard outputs are enabled in the transaction.
|
|
|
|
///
|
2021-04-26 14:10:32 -07:00
|
|
|
/// If `false`, created notes within [`Action`]s in the transaction's [`Bundle`] are
|
|
|
|
/// guaranteed to be dummy notes. If `true`, the created notes may be either real or
|
2021-04-21 21:35:47 -07:00
|
|
|
/// dummy notes.
|
2021-03-03 09:35:25 -08:00
|
|
|
outputs_enabled: bool,
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:04:45 -07:00
|
|
|
impl Flags {
|
|
|
|
/// Construct a set of flags from its constituent parts
|
|
|
|
pub fn from_parts(spends_enabled: bool, outputs_enabled: bool) -> Self {
|
2021-04-26 17:28:42 -07:00
|
|
|
Flags {
|
|
|
|
spends_enabled,
|
|
|
|
outputs_enabled,
|
|
|
|
}
|
2021-04-23 12:04:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Flag denoting whether Orchard spends are enabled in the transaction.
|
|
|
|
///
|
2021-04-26 14:10:32 -07:00
|
|
|
/// If `false`, spent notes within [`Action`]s in the transaction's [`Bundle`] are
|
|
|
|
/// guaranteed to be dummy notes. If `true`, the spent notes may be either real or
|
2021-04-23 12:04:45 -07:00
|
|
|
/// dummy notes.
|
|
|
|
pub fn spends_enabled(&self) -> bool {
|
|
|
|
self.spends_enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flag denoting whether Orchard outputs are enabled in the transaction.
|
|
|
|
///
|
2021-04-26 14:10:32 -07:00
|
|
|
/// If `false`, created notes within [`Action`]s in the transaction's [`Bundle`] are
|
|
|
|
/// guaranteed to be dummy notes. If `true`, the created notes may be either real or
|
2021-04-23 12:04:45 -07:00
|
|
|
/// dummy notes.
|
|
|
|
pub fn outputs_enabled(&self) -> bool {
|
|
|
|
self.outputs_enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:35:25 -08:00
|
|
|
/// Defines the authorization type of an Orchard bundle.
|
|
|
|
pub trait Authorization {
|
|
|
|
/// The authorization type of an Orchard action.
|
|
|
|
type SpendAuth;
|
2021-01-20 12:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A bundle of actions to be applied to the ledger.
|
|
|
|
#[derive(Debug)]
|
2021-03-03 09:35:25 -08:00
|
|
|
pub struct Bundle<T: Authorization> {
|
2021-04-21 21:35:47 -07:00
|
|
|
/// The list of actions that make up this bundle.
|
2021-03-03 09:35:25 -08:00
|
|
|
actions: NonEmpty<Action<T::SpendAuth>>,
|
2021-04-21 21:35:47 -07:00
|
|
|
/// Orchard-specific transaction-level flags for this bundle.
|
2021-04-21 20:33:37 -07:00
|
|
|
flags: Flags,
|
2021-04-22 06:32:20 -07:00
|
|
|
/// The net value moved out of the Orchard shielded pool.
|
2021-04-21 21:35:47 -07:00
|
|
|
///
|
2021-04-22 06:32:20 -07:00
|
|
|
/// This is the sum of Orchard spends minus the sum of Orchard outputs.
|
2021-01-21 04:16:50 -08:00
|
|
|
value_balance: ValueSum,
|
2021-04-21 21:35:47 -07:00
|
|
|
/// The root of the Orchard commitment tree that this bundle commits to.
|
2021-03-03 09:35:25 -08:00
|
|
|
anchor: Anchor,
|
2021-04-21 21:35:47 -07:00
|
|
|
/// The authorization for this bundle.
|
2021-03-03 09:35:25 -08:00
|
|
|
authorization: T,
|
2021-01-20 12:30:35 -08:00
|
|
|
}
|
|
|
|
|
2021-03-03 09:35:25 -08:00
|
|
|
impl<T: Authorization> Bundle<T> {
|
2021-04-21 20:33:37 -07:00
|
|
|
/// Constructs a `Bundle` from its constituent parts.
|
|
|
|
pub fn from_parts(
|
|
|
|
actions: NonEmpty<Action<T::SpendAuth>>,
|
|
|
|
flags: Flags,
|
|
|
|
value_balance: ValueSum,
|
|
|
|
anchor: Anchor,
|
|
|
|
authorization: T,
|
|
|
|
) -> Self {
|
|
|
|
Bundle {
|
|
|
|
actions,
|
|
|
|
flags,
|
|
|
|
value_balance,
|
|
|
|
anchor,
|
|
|
|
authorization,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:24:01 -07:00
|
|
|
/// Returns the list of actions that make up this bundle.
|
|
|
|
pub fn actions(&self) -> &NonEmpty<Action<T::SpendAuth>> {
|
|
|
|
&self.actions
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Orchard-specific transaction-level flags for this bundle.
|
|
|
|
pub fn flags(&self) -> &Flags {
|
|
|
|
&self.flags
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the net value moved into or out of the Orchard shielded pool.
|
|
|
|
///
|
|
|
|
/// This is the sum of Orchard spends minus the sum Orchard outputs.
|
|
|
|
pub fn value_balance(&self) -> &ValueSum {
|
|
|
|
&self.value_balance
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the root of the Orchard commitment tree that this bundle commits to.
|
|
|
|
pub fn anchor(&self) -> &Anchor {
|
|
|
|
&self.anchor
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the authorization for this bundle.
|
|
|
|
///
|
|
|
|
/// In the case of a `Bundle<Authorized>`, this is the proof and binding signature.
|
|
|
|
pub fn authorization(&self) -> &T {
|
|
|
|
&self.authorization
|
|
|
|
}
|
|
|
|
|
2021-01-20 12:30:35 -08:00
|
|
|
/// Computes a commitment to the effects of this bundle, suitable for inclusion within
|
|
|
|
/// a transaction ID.
|
|
|
|
pub fn commitment(&self) -> BundleCommitment {
|
|
|
|
todo!()
|
|
|
|
}
|
2021-04-14 21:14:34 -07:00
|
|
|
|
|
|
|
/// Transitions this bundle from one authorization state to another.
|
2021-04-26 17:22:04 -07:00
|
|
|
pub fn authorize<R, U: Authorization>(
|
2021-04-14 21:14:34 -07:00
|
|
|
self,
|
2021-04-26 17:22:04 -07:00
|
|
|
context: &mut R,
|
|
|
|
mut spend_auth: impl FnMut(&mut R, &T, T::SpendAuth) -> U::SpendAuth,
|
|
|
|
step: impl FnOnce(&mut R, T) -> U,
|
2021-04-14 21:14:34 -07:00
|
|
|
) -> Bundle<U> {
|
|
|
|
let authorization = self.authorization;
|
|
|
|
Bundle {
|
|
|
|
actions: self
|
|
|
|
.actions
|
2021-04-26 17:22:04 -07:00
|
|
|
.map(|a| a.map(|a_auth| spend_auth(context, &authorization, a_auth))),
|
2021-04-21 20:33:37 -07:00
|
|
|
flags: self.flags,
|
2021-04-14 21:14:34 -07:00
|
|
|
value_balance: self.value_balance,
|
|
|
|
anchor: self.anchor,
|
2021-04-26 17:22:04 -07:00
|
|
|
authorization: step(context, authorization),
|
2021-04-14 21:14:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transitions this bundle from one authorization state to another.
|
2021-04-26 17:22:04 -07:00
|
|
|
pub fn try_authorize<R, U: Authorization, E>(
|
2021-04-14 21:14:34 -07:00
|
|
|
self,
|
2021-04-26 17:22:04 -07:00
|
|
|
context: &mut R,
|
|
|
|
mut spend_auth: impl FnMut(&mut R, &T, T::SpendAuth) -> Result<U::SpendAuth, E>,
|
|
|
|
step: impl FnOnce(&mut R, T) -> Result<U, E>,
|
2021-04-14 21:14:34 -07:00
|
|
|
) -> Result<Bundle<U>, E> {
|
|
|
|
let authorization = self.authorization;
|
|
|
|
let new_actions = self
|
|
|
|
.actions
|
|
|
|
.into_iter()
|
2021-04-26 17:22:04 -07:00
|
|
|
.map(|a| a.try_map(|a_auth| spend_auth(context, &authorization, a_auth)))
|
2021-04-14 21:14:34 -07:00
|
|
|
.collect::<Result<Vec<_>, E>>()?;
|
|
|
|
|
|
|
|
Ok(Bundle {
|
|
|
|
actions: NonEmpty::from_vec(new_actions).unwrap(),
|
2021-04-21 20:33:37 -07:00
|
|
|
flags: self.flags,
|
2021-04-14 21:14:34 -07:00
|
|
|
value_balance: self.value_balance,
|
|
|
|
anchor: self.anchor,
|
2021-04-26 17:22:04 -07:00
|
|
|
authorization: step(context, authorization)?,
|
2021-04-14 21:14:34 -07:00
|
|
|
})
|
|
|
|
}
|
2021-01-20 12:30:35 -08:00
|
|
|
}
|
|
|
|
|
2021-03-03 09:35:25 -08:00
|
|
|
/// Authorizing data for a bundle of actions, ready to be committed to the ledger.
|
2021-01-20 12:30:35 -08:00
|
|
|
#[derive(Debug)]
|
2021-03-03 09:35:25 -08:00
|
|
|
pub struct Authorized {
|
2021-02-24 12:01:16 -08:00
|
|
|
proof: Proof,
|
2021-01-20 12:35:54 -08:00
|
|
|
binding_signature: redpallas::Signature<Binding>,
|
2021-01-20 12:30:35 -08:00
|
|
|
}
|
|
|
|
|
2021-03-03 09:35:25 -08:00
|
|
|
impl Authorization for Authorized {
|
|
|
|
type SpendAuth = redpallas::Signature<SpendAuth>;
|
|
|
|
}
|
2021-01-20 12:30:35 -08:00
|
|
|
|
2021-04-21 20:33:37 -07:00
|
|
|
impl Authorized {
|
|
|
|
/// Constructs the authorizing data for a bundle of actions from its constituent parts.
|
|
|
|
pub fn from_parts(proof: Proof, binding_signature: redpallas::Signature<Binding>) -> Self {
|
|
|
|
Authorized {
|
|
|
|
proof,
|
|
|
|
binding_signature,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:35:25 -08:00
|
|
|
impl Bundle<Authorized> {
|
2021-01-20 12:30:35 -08:00
|
|
|
/// Computes a commitment to the authorizing data within for this bundle.
|
|
|
|
///
|
2021-03-03 09:35:25 -08:00
|
|
|
/// This together with `Bundle::commitment` bind the entire bundle.
|
2021-01-20 12:30:35 -08:00
|
|
|
pub fn authorizing_commitment(&self) -> BundleAuthorizingCommitment {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A commitment to a bundle of actions.
|
|
|
|
///
|
|
|
|
/// This commitment is non-malleable, in the sense that a bundle's commitment will only
|
|
|
|
/// change if the effects of the bundle are altered.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BundleCommitment;
|
|
|
|
|
|
|
|
/// A commitment to the authorizing data within a bundle of actions.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BundleAuthorizingCommitment;
|