1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::*;
impl<N: Network> Stack<N> {
pub fn sample_value<R: Rng + CryptoRng>(
&self,
burner_address: &Address<N>,
value_type: &ValueType<N>,
rng: &mut R,
) -> Result<Value<N>> {
match value_type {
ValueType::Constant(plaintext_type)
| ValueType::Public(plaintext_type)
| ValueType::Private(plaintext_type) => Ok(Value::Plaintext(self.sample_plaintext(plaintext_type, rng)?)),
ValueType::Record(record_name) => {
Ok(Value::Record(self.sample_record(burner_address, record_name, rng)?))
}
ValueType::ExternalRecord(locator) => {
bail!("Illegal operation: Cannot sample external records (for '{locator}.record').")
}
}
}
pub fn sample_record<R: Rng + CryptoRng>(
&self,
burner_address: &Address<N>,
record_name: &Identifier<N>,
rng: &mut R,
) -> Result<Record<N, Plaintext<N>>> {
let record = self.sample_record_internal(burner_address, record_name, 0, rng)?;
self.matches_record(&record, record_name)?;
Ok(record)
}
pub fn sample_plaintext<R: Rng + CryptoRng>(
&self,
plaintext_type: &PlaintextType<N>,
rng: &mut R,
) -> Result<Plaintext<N>> {
let plaintext = self.sample_plaintext_internal(plaintext_type, 0, rng)?;
self.matches_plaintext(&plaintext, plaintext_type)?;
Ok(plaintext)
}
}
impl<N: Network> Stack<N> {
fn sample_record_internal<R: Rng + CryptoRng>(
&self,
burner_address: &Address<N>,
record_name: &Identifier<N>,
depth: usize,
rng: &mut R,
) -> Result<Record<N, Plaintext<N>>> {
ensure!(depth <= N::MAX_DATA_DEPTH, "Plaintext exceeded maximum depth of {}", N::MAX_DATA_DEPTH);
let record_type = self.program.get_record(record_name)?;
let owner = match record_type.owner().is_public() {
true => Owner::Public(*burner_address),
false => Owner::Private(Plaintext::Literal(Literal::Address(*burner_address), Default::default())),
};
let amount = U64::new(rng.gen_range(0..(1 << 52)));
let gates = match record_type.gates().is_public() {
true => Balance::Public(amount),
false => Balance::Private(Plaintext::Literal(Literal::U64(amount), Default::default())),
};
let data = record_type
.entries()
.iter()
.map(|(entry_name, entry_type)| {
let entry = self.sample_entry_internal(entry_type, depth + 1, rng)?;
Ok((*entry_name, entry))
})
.collect::<Result<IndexMap<_, _>>>()?;
let nonce = Group::rand(rng);
Record::<N, Plaintext<N>>::from_plaintext(owner, gates, data, nonce)
}
fn sample_entry_internal<R: Rng + CryptoRng>(
&self,
entry_type: &EntryType<N>,
depth: usize,
rng: &mut R,
) -> Result<Entry<N, Plaintext<N>>> {
ensure!(depth <= N::MAX_DATA_DEPTH, "Entry exceeded maximum depth of {}", N::MAX_DATA_DEPTH);
match entry_type {
EntryType::Constant(plaintext_type)
| EntryType::Public(plaintext_type)
| EntryType::Private(plaintext_type) => {
let plaintext = self.sample_plaintext_internal(plaintext_type, depth, rng)?;
match entry_type {
EntryType::Constant(..) => Ok(Entry::Constant(plaintext)),
EntryType::Public(..) => Ok(Entry::Public(plaintext)),
EntryType::Private(..) => Ok(Entry::Private(plaintext)),
}
}
}
}
fn sample_plaintext_internal<R: Rng + CryptoRng>(
&self,
plaintext_type: &PlaintextType<N>,
depth: usize,
rng: &mut R,
) -> Result<Plaintext<N>> {
ensure!(depth <= N::MAX_DATA_DEPTH, "Plaintext exceeded maximum depth of {}", N::MAX_DATA_DEPTH);
let plaintext = match plaintext_type {
PlaintextType::Literal(literal_type) => {
Plaintext::Literal(Literal::sample(*literal_type, rng), Default::default())
}
PlaintextType::Interface(interface_name) => {
let interface = self.program.get_interface(interface_name)?;
let members = interface
.members()
.iter()
.map(|(member_name, member_type)| {
let member = self.sample_plaintext_internal(member_type, depth + 1, rng)?;
Ok((*member_name, member))
})
.collect::<Result<IndexMap<_, _>>>()?;
Plaintext::Interface(members, Default::default())
}
};
Ok(plaintext)
}
}