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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use super::*;
impl<E: Environment, I: IntegerType, M: Magnitude> ShlWrapped<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shl_wrapped(&self, rhs: &Integer<E, M>) -> Self::Output {
if self.is_constant() && rhs.is_constant() {
witness!(|self, rhs| console::Integer::new(self.wrapping_shl(rhs.to_u32().unwrap())))
} else {
let first_upper_bit_index = I::BITS.trailing_zeros() as usize;
let mut bits_le = rhs.bits_le[..first_upper_bit_index].to_vec();
bits_le.extend(core::iter::repeat(Boolean::constant(false)).take(8));
let rhs_as_u8 = U8 { bits_le, phantom: Default::default() };
if rhs_as_u8.is_constant() {
let shift_amount = rhs_as_u8.eject_value();
let mut bits_le = vec![Boolean::constant(false); *shift_amount as usize];
bits_le.extend_from_slice(&self.bits_le);
bits_le.truncate(I::BITS as usize);
Self { bits_le, phantom: Default::default() }
} else {
let two = Field::one() + Field::one();
let mut shift_in_field = Field::one();
for bit in rhs.bits_le[..first_upper_bit_index].iter().rev() {
shift_in_field = shift_in_field.square();
shift_in_field = Field::ternary(bit, &(&shift_in_field * &two), &shift_in_field);
}
let shift_as_multiplicand =
Self { bits_le: shift_in_field.to_lower_bits_le(I::BITS as usize), phantom: Default::default() };
self.mul_wrapped(&shift_as_multiplicand)
}
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> Metrics<dyn ShlWrapped<Integer<E, M>, Output = Integer<E, I>>>
for Integer<E, I>
{
type Case = (Mode, Mode);
fn count(case: &Self::Case) -> Count {
let index = |num_bits: u64| match [8, 16, 32, 64, 128].iter().position(|&bits| bits == num_bits) {
Some(index) => index as u64,
None => E::halt(format!("Integer of {num_bits} bits is not supported")),
};
match (case.0, case.1) {
(Mode::Constant, Mode::Constant) => Count::is(I::BITS, 0, 0, 0),
(_, Mode::Constant) => Count::is(0, 0, 0, 0),
(Mode::Constant, _) => Count::is(
0,
0,
(2 * I::BITS) + (I::BITS / 2) + (2 * index(I::BITS)) + 5,
(2 * I::BITS) + (I::BITS / 2) + (2 * index(I::BITS)) + 7,
),
(_, _) => Count::is(
0,
0,
(2 * I::BITS) + (I::BITS / 2) + (2 * index(I::BITS)) + 8,
(2 * I::BITS) + (I::BITS / 2) + (2 * index(I::BITS)) + 10,
),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> OutputMode<dyn ShlWrapped<Integer<E, M>, Output = Integer<E, I>>>
for Integer<E, I>
{
type Case = (Mode, Mode);
fn output_mode(case: &Self::Case) -> Mode {
match (case.0, case.1) {
(Mode::Constant, Mode::Constant) => Mode::Constant,
(mode_a, Mode::Constant) => mode_a,
(_, _) => Mode::Private,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
use core::{ops::RangeInclusive, panic::RefUnwindSafe};
const ITERATIONS: u64 = 32;
fn check_shl<I: IntegerType + RefUnwindSafe, M: Magnitude + RefUnwindSafe>(
name: &str,
first: console::Integer<<Circuit as Environment>::Network, I>,
second: console::Integer<<Circuit as Environment>::Network, M>,
mode_a: Mode,
mode_b: Mode,
) {
let expected = first.wrapping_shl(second.to_u32().unwrap());
let a = Integer::<Circuit, I>::new(mode_a, first);
let b = Integer::<Circuit, M>::new(mode_b, second);
Circuit::scope(name, || {
let candidate = a.shl_wrapped(&b);
assert_eq!(expected, *candidate.eject_value());
assert_eq!(console::Integer::new(expected), candidate.eject_value());
assert_count!(ShlWrapped(Integer<I>, Integer<M>) => Integer<I>, &(mode_a, mode_b));
assert_output_mode!(ShlWrapped(Integer<I>, Integer<M>) => Integer<I>, &(mode_a, mode_b), candidate);
});
Circuit::reset();
}
fn run_test<I: IntegerType + RefUnwindSafe, M: Magnitude + RefUnwindSafe>(mode_a: Mode, mode_b: Mode) {
for i in 0..ITERATIONS {
let first = Uniform::rand(&mut test_rng());
let second = Uniform::rand(&mut test_rng());
let name = format!("Shl: {} << {} {}", mode_a, mode_b, i);
check_shl::<I, M>(&name, first, second, mode_a, mode_b);
let name = format!("Double: {} << {} {}", mode_a, mode_b, i);
check_shl::<I, M>(&name, first, console::Integer::one(), mode_a, mode_b);
let name = format!("Quadruple: {} << {} {}", mode_a, mode_b, i);
check_shl::<I, M>(&name, first, console::Integer::one() + console::Integer::one(), mode_a, mode_b);
}
}
fn run_exhaustive_test<I: IntegerType + RefUnwindSafe, M: Magnitude + RefUnwindSafe>(mode_a: Mode, mode_b: Mode)
where
RangeInclusive<I>: Iterator<Item = I>,
RangeInclusive<M>: Iterator<Item = M>,
{
for first in I::MIN..=I::MAX {
for second in M::MIN..=M::MAX {
let first = console::Integer::<_, I>::new(first);
let second = console::Integer::<_, M>::new(second);
let name = format!("Shl: ({} << {})", first, second);
check_shl::<I, M>(&name, first, second, mode_a, mode_b);
}
}
}
test_integer_binary!(run_test, i8, u8, shl);
test_integer_binary!(run_test, i8, u16, shl);
test_integer_binary!(run_test, i8, u32, shl);
test_integer_binary!(run_test, i16, u8, shl);
test_integer_binary!(run_test, i16, u16, shl);
test_integer_binary!(run_test, i16, u32, shl);
test_integer_binary!(run_test, i32, u8, shl);
test_integer_binary!(run_test, i32, u16, shl);
test_integer_binary!(run_test, i32, u32, shl);
test_integer_binary!(run_test, i64, u8, shl);
test_integer_binary!(run_test, i64, u16, shl);
test_integer_binary!(run_test, i64, u32, shl);
test_integer_binary!(run_test, i128, u8, shl);
test_integer_binary!(run_test, i128, u16, shl);
test_integer_binary!(run_test, i128, u32, shl);
test_integer_binary!(run_test, u8, u8, shl);
test_integer_binary!(run_test, u8, u16, shl);
test_integer_binary!(run_test, u8, u32, shl);
test_integer_binary!(run_test, u16, u8, shl);
test_integer_binary!(run_test, u16, u16, shl);
test_integer_binary!(run_test, u16, u32, shl);
test_integer_binary!(run_test, u32, u8, shl);
test_integer_binary!(run_test, u32, u16, shl);
test_integer_binary!(run_test, u32, u32, shl);
test_integer_binary!(run_test, u64, u8, shl);
test_integer_binary!(run_test, u64, u16, shl);
test_integer_binary!(run_test, u64, u32, shl);
test_integer_binary!(run_test, u128, u8, shl);
test_integer_binary!(run_test, u128, u16, shl);
test_integer_binary!(run_test, u128, u32, shl);
test_integer_binary!(#[ignore], run_exhaustive_test, u8, u8, shl, exhaustive);
test_integer_binary!(#[ignore], run_exhaustive_test, i8, u8, shl, exhaustive);
}