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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
impl<N: Network> ToBits for Record<N, Plaintext<N>> {
/// Returns this data as a list of **little-endian** bits.
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// Construct the owner visibility bit.
vec.push(self.owner.is_private());
// Construct the owner bits.
match &self.owner {
Owner::Public(public) => public.write_bits_le(vec),
Owner::Private(Plaintext::Literal(Literal::Address(address), ..)) => address.write_bits_le(vec),
_ => N::halt("Internal error: plaintext to_bits_le corrupted in record owner"),
};
// Compute the data bits.
let mut data_bits_le = vec![];
for (identifier, entry) in &self.data {
identifier.write_bits_le(&mut data_bits_le);
entry.write_bits_le(&mut data_bits_le);
}
// Ensure the data length is less than 2^31 bits.
if data_bits_le.len() >= (1 << 31) {
N::halt("Record data exceeds (1 << 31) bits")
}
// Write the first 31 bits of the data length (as we know it is less than 2^31).
// Note: In order to introduce a hiding bitflag, we repurpose the last bit as the hiding bit.
let data_bits_len = u32::try_from(data_bits_le.len()).or_halt_with::<N>("Record data exceeds u32::MAX bits");
vec.extend_from_slice(&data_bits_len.to_bits_le()[..31]);
// Construct the hiding bit.
// Note: While this bitflag is redundant, it is necessary for backwards compatibility.
vec.push(self.is_hiding());
// Construct the data bits.
vec.extend_from_slice(&data_bits_le);
// Construct the nonce bits.
self.nonce.write_bits_le(vec);
// Construct the version bits.
self.version.write_bits_le(vec);
}
/// Returns this data as a list of **big-endian** bits.
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// Construct the owner visibility bit.
vec.push(self.owner.is_private());
// Construct the owner bits.
match &self.owner {
Owner::Public(public) => public.write_bits_be(vec),
Owner::Private(Plaintext::Literal(Literal::Address(address), ..)) => address.write_bits_be(vec),
_ => N::halt("Internal error: plaintext to_bits_be corrupted in record owner"),
};
// Compute the data bits.
let mut data_bits_be = vec![];
for (identifier, entry) in &self.data {
identifier.write_bits_be(&mut data_bits_be);
entry.write_bits_be(&mut data_bits_be);
}
// Ensure the data length is less than 2^31 bits.
if data_bits_be.len() >= (1 << 31) {
N::halt("Record data exceeds (1 << 31) bits")
}
// Construct the hiding bit.
// Note: While this bitflag is redundant, it is necessary for backwards compatibility.
vec.push(self.is_hiding());
// Write the last 31 bits of the data length (as we know it is less than 2^31).
// Note: In order to introduce a hiding bitflag, we repurpose the first bit as the hiding bit.
let data_bits_len = u32::try_from(data_bits_be.len()).or_halt_with::<N>("Record data exceeds u32::MAX bits");
vec.extend_from_slice(&data_bits_len.to_bits_be()[1..]);
// Construct the data bits.
vec.extend_from_slice(&data_bits_be);
// Construct the nonce bits.
self.nonce.write_bits_be(vec);
// Construct the version bits.
self.version.write_bits_be(vec);
}
}
impl<N: Network> ToBits for Record<N, Ciphertext<N>> {
/// Returns this data as a list of **little-endian** bits.
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// Construct the owner visibility bit.
vec.push(self.owner.is_private());
// Construct the owner bits.
match &self.owner {
Owner::Public(public) => public.write_bits_le(vec),
Owner::Private(ciphertext) => {
// Ensure there is exactly one field element in the ciphertext.
match ciphertext.len() == 1 {
true => ciphertext[0].write_bits_le(vec),
false => N::halt("Internal error: ciphertext to_bits_le corrupted in record owner"),
}
}
};
// Compute the data bits.
let mut data_bits_le = vec![];
for (identifier, entry) in &self.data {
identifier.write_bits_le(&mut data_bits_le);
entry.write_bits_le(&mut data_bits_le);
}
// Ensure the data length is less than 2^31 bits.
if data_bits_le.len() >= (1 << 31) {
N::halt("Record data exceeds (1 << 31) bits")
}
// Write the first 31 bits of the data length (as we know it is less than 2^31).
// Note: In order to introduce a hiding bitflag, we repurpose the last bit as the hiding bit.
let data_bits_len = u32::try_from(data_bits_le.len()).or_halt_with::<N>("Record data exceeds u32::MAX bits");
vec.extend_from_slice(&data_bits_len.to_bits_le()[..31]);
// Construct the hiding bit.
// Note: While this bitflag is redundant, it is necessary for backwards compatibility.
vec.push(self.is_hiding());
// Construct the data bits.
vec.extend_from_slice(&data_bits_le);
// Construct the nonce bits.
self.nonce.write_bits_le(vec);
// Construct the version bits.
self.version.write_bits_le(vec);
}
/// Returns this data as a list of **big-endian** bits.
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// Construct the owner visibility bit.
vec.push(self.owner.is_private());
// Construct the owner bits.
match &self.owner {
Owner::Public(public) => public.write_bits_be(vec),
Owner::Private(ciphertext) => {
// Ensure there is exactly one field element in the ciphertext.
match ciphertext.len() == 1 {
true => ciphertext[0].write_bits_be(vec),
false => N::halt("Internal error: ciphertext to_bits_be corrupted in record owner"),
}
}
};
// Compute the data bits.
let mut data_bits_be = vec![];
for (identifier, entry) in &self.data {
identifier.write_bits_be(&mut data_bits_be);
entry.write_bits_be(&mut data_bits_be);
}
// Ensure the data length is less than 2^31 bits.
if data_bits_be.len() >= (1 << 31) {
N::halt("Record data exceeds (1 << 31) bits")
}
// Construct the hiding bit.
// Note: While this bitflag is redundant, it is necessary for backwards compatibility.
vec.push(self.is_hiding());
// Write the last 31 bits of the data length (as we know it is less than 2^31).
// Note: In order to introduce a hiding bitflag, we repurpose the first bit as the hiding bit.
let data_bits_len = u32::try_from(data_bits_be.len()).or_halt_with::<N>("Record data exceeds u32::MAX bits");
vec.extend_from_slice(&data_bits_len.to_bits_be()[1..]);
// Construct the data bits.
vec.extend_from_slice(&data_bits_be);
// Construct the nonce bits.
self.nonce.write_bits_be(vec);
// Construct the version bits.
self.version.write_bits_be(vec);
}
}