snarkvm_console_program/data/record/helpers/
owner.rs1use crate::{Ciphertext, Entry, Literal, Plaintext};
17use snarkvm_console_network::prelude::*;
18use snarkvm_console_types::{Address, Boolean, Field};
19
20#[derive(Clone)]
22pub enum Owner<N: Network, Private: Visibility> {
23    Public(Address<N>),
25    Private(Private),
27}
28
29impl<N: Network> Deref for Owner<N, Plaintext<N>> {
30    type Target = Address<N>;
31
32    fn deref(&self) -> &Self::Target {
34        match self {
35            Self::Public(public) => public,
36            Self::Private(Plaintext::Literal(Literal::Address(address), ..)) => address,
37            _ => N::halt("Internal error: plaintext deref corrupted in record owner"),
38        }
39    }
40}
41
42impl<N: Network, Private: Visibility> Owner<N, Private> {
43    pub const fn is_public(&self) -> bool {
45        matches!(self, Self::Public(..))
46    }
47
48    pub const fn is_private(&self) -> bool {
50        matches!(self, Self::Private(..))
51    }
52}
53
54impl<N: Network> Owner<N, Plaintext<N>> {
55    pub fn to_entry(&self) -> Entry<N, Plaintext<N>> {
57        match self {
58            Self::Public(owner) => Entry::Public(Plaintext::from(Literal::Address(*owner))),
59            Self::Private(plaintext) => Entry::Private(plaintext.clone()),
60        }
61    }
62}
63
64impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> Eq for Owner<N, Private> {}
65
66impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> PartialEq for Owner<N, Private> {
67    fn eq(&self, other: &Self) -> bool {
69        *self.is_equal(other)
70    }
71}
72
73impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> Equal<Self> for Owner<N, Private> {
74    type Output = Boolean<N>;
75
76    fn is_equal(&self, other: &Self) -> Self::Output {
78        match (self, other) {
79            (Self::Public(a), Self::Public(b)) => a.is_equal(b),
80            (Self::Private(a), Self::Private(b)) => a.is_equal(b),
81            (Self::Public(_), _) | (Self::Private(_), _) => Boolean::new(false),
82        }
83    }
84
85    fn is_not_equal(&self, other: &Self) -> Self::Output {
87        match (self, other) {
88            (Self::Public(a), Self::Public(b)) => a.is_not_equal(b),
89            (Self::Private(a), Self::Private(b)) => a.is_not_equal(b),
90            (Self::Public(_), _) | (Self::Private(_), _) => Boolean::new(true),
91        }
92    }
93}
94
95impl<N: Network> Owner<N, Plaintext<N>> {
96    pub fn encrypt_with_randomizer(&self, randomizer: &[Field<N>]) -> Result<Owner<N, Ciphertext<N>>> {
98        match self {
99            Self::Public(public) => {
100                ensure!(randomizer.is_empty(), "Expected 0 randomizers, found {}", randomizer.len());
102                Ok(Owner::Public(*public))
104            }
105            Self::Private(Plaintext::Literal(Literal::Address(address), ..)) => {
106                ensure!(randomizer.len() == 1, "Expected 1 randomizer, found {}", randomizer.len());
108                let ciphertext = address.to_field()? + randomizer[0];
110                Ok(Owner::Private(Ciphertext::from_fields(&[ciphertext])?))
112            }
113            _ => bail!("Internal error: plaintext encryption corrupted in record owner"),
114        }
115    }
116}
117
118impl<N: Network> Owner<N, Ciphertext<N>> {
119    pub fn decrypt_with_randomizer(&self, randomizer: &[Field<N>]) -> Result<Owner<N, Plaintext<N>>> {
121        match self {
122            Self::Public(public) => {
123                ensure!(randomizer.is_empty(), "Expected 0 randomizers, found {}", randomizer.len());
125                Ok(Owner::Public(*public))
127            }
128            Self::Private(ciphertext) => {
129                ensure!(randomizer.len() == 1, "Expected 1 randomizer, found {}", randomizer.len());
131                ensure!(ciphertext.len() == 1, "Expected 1 ciphertext, found {}", ciphertext.len());
133                let owner = Address::from_field(&(ciphertext[0] - randomizer[0]))?;
135                Ok(Owner::Private(Plaintext::from(Literal::Address(owner))))
137            }
138        }
139    }
140}
141
142impl<N: Network> Debug for Owner<N, Plaintext<N>> {
143    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
145        Display::fmt(self, f)
146    }
147}
148
149impl<N: Network> Display for Owner<N, Plaintext<N>> {
150    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
152        match self {
153            Self::Public(owner) => write!(f, "{owner}.public"),
154            Self::Private(Plaintext::Literal(Literal::Address(owner), ..)) => write!(f, "{owner}.private"),
155            _ => N::halt("Internal error: plaintext fmt corrupted in record owner"),
156        }
157    }
158}