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
use super::*;
impl<E: Environment> ToLowerBits for Field<E> {
type Boolean = Boolean<E>;
fn to_lower_bits_le(&self, k: usize) -> Vec<Self::Boolean> {
if k > E::BaseField::size_in_bits() {
E::halt(format!(
"Attempted to extract {k} bits from a {}-bit base field element",
E::BaseField::size_in_bits()
))
}
let bits = witness!(|self| self.to_bits_le().into_iter().take(k).collect::<Vec<_>>());
let mut accumulator = Field::zero();
let mut coefficient = Field::one();
for bit in &bits {
accumulator += Field::from_boolean(bit) * &coefficient;
coefficient = coefficient.double();
}
E::assert_eq(self, accumulator);
bits
}
fn to_lower_bits_be(&self, k: usize) -> Vec<Self::Boolean> {
let mut bits_be = self.to_lower_bits_le(k);
bits_be.reverse();
bits_be
}
}
impl<E: Environment> Metrics<dyn ToLowerBits<Boolean = Boolean<E>>> for Field<E> {
type Case = (Mode, u64);
fn count(case: &Self::Case) -> Count {
match case {
(Mode::Constant, k) => Count::is(*k, 0, 0, 0),
(_, k) => Count::is(0, 0, *k, k + 1),
}
}
}
impl<E: Environment> OutputMode<dyn ToLowerBits<Boolean = Boolean<E>>> for Field<E> {
type Case = Mode;
fn output_mode(case: &Self::Case) -> Mode {
match case {
Mode::Constant => Mode::Constant,
_ => Mode::Private,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
const ITERATIONS: u64 = 100;
#[rustfmt::skip]
fn check_to_lower_k_bits_le<I: IntegerType + Unsigned>(
mode: Mode,
) {
for i in 0..ITERATIONS {
let value: I = Uniform::rand(&mut test_rng());
let expected = value.to_bits_le();
let candidate = Field::<Circuit>::new(mode, console::Field::from_bits_le(&expected).unwrap());
Circuit::scope(&format!("{} {}", mode, i), || {
let candidate = candidate.to_lower_bits_le(I::BITS as usize);
assert_eq!(I::BITS, candidate.len() as u64);
for (i, (expected_bit, candidate_bit)) in expected.iter().zip_eq(candidate.iter()).enumerate() {
assert_eq!(*expected_bit, candidate_bit.eject_value(), "LSB+{}", i);
}
assert_count!(ToLowerBits<Boolean>() => Field, &(mode, I::BITS));
assert_output_mode!(ToLowerBits<Boolean>() => Field, &mode, candidate);
});
}
}
#[test]
fn test_to_8_bits_constant() {
check_to_lower_k_bits_le::<u8>(Mode::Constant);
}
#[test]
fn test_to_8_bits_public() {
check_to_lower_k_bits_le::<u8>(Mode::Public);
}
#[test]
fn test_to_8_bits_private() {
check_to_lower_k_bits_le::<u8>(Mode::Private);
}
#[test]
fn test_to_16_bits_constant() {
check_to_lower_k_bits_le::<u16>(Mode::Constant);
}
#[test]
fn test_to_16_bits_public() {
check_to_lower_k_bits_le::<u16>(Mode::Public);
}
#[test]
fn test_to_16_bits_private() {
check_to_lower_k_bits_le::<u16>(Mode::Private);
}
#[test]
fn test_to_32_bits_constant() {
check_to_lower_k_bits_le::<u32>(Mode::Constant);
}
#[test]
fn test_to_32_bits_public() {
check_to_lower_k_bits_le::<u32>(Mode::Public);
}
#[test]
fn test_to_32_bits_private() {
check_to_lower_k_bits_le::<u32>(Mode::Private);
}
#[test]
fn test_to_64_bits_constant() {
check_to_lower_k_bits_le::<u64>(Mode::Constant);
}
#[test]
fn test_to_64_bits_public() {
check_to_lower_k_bits_le::<u64>(Mode::Public);
}
#[test]
fn test_to_64_bits_private() {
check_to_lower_k_bits_le::<u64>(Mode::Private);
}
#[test]
fn test_to_128_bits_constant() {
check_to_lower_k_bits_le::<u128>(Mode::Constant);
}
#[test]
fn test_to_128_bits_public() {
check_to_lower_k_bits_le::<u128>(Mode::Public);
}
#[test]
fn test_to_128_bits_private() {
check_to_lower_k_bits_le::<u128>(Mode::Private);
}
}