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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use super::*;
impl<E: Environment, I: IntegerType> Add<Integer<E, I>> for Integer<E, I> {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
self + &other
}
}
impl<E: Environment, I: IntegerType> Add<Integer<E, I>> for &Integer<E, I> {
type Output = Integer<E, I>;
fn add(self, other: Integer<E, I>) -> Self::Output {
self + &other
}
}
impl<E: Environment, I: IntegerType> Add<&Integer<E, I>> for Integer<E, I> {
type Output = Self;
fn add(self, other: &Self) -> Self::Output {
&self + other
}
}
impl<E: Environment, I: IntegerType> Add<&Integer<E, I>> for &Integer<E, I> {
type Output = Integer<E, I>;
fn add(self, other: &Integer<E, I>) -> Self::Output {
let mut output = self.clone();
output += other;
output
}
}
impl<E: Environment, I: IntegerType> AddAssign<Integer<E, I>> for Integer<E, I> {
fn add_assign(&mut self, other: Integer<E, I>) {
*self += &other;
}
}
impl<E: Environment, I: IntegerType> AddAssign<&Integer<E, I>> for Integer<E, I> {
fn add_assign(&mut self, other: &Integer<E, I>) {
*self = self.add_checked(other);
}
}
impl<E: Environment, I: IntegerType> AddChecked<Self> for Integer<E, I> {
type Output = Self;
#[inline]
fn add_checked(&self, other: &Integer<E, I>) -> Self::Output {
if self.is_constant() && other.is_constant() {
match self.eject_value().checked_add(&other.eject_value()) {
Some(value) => Integer::constant(console::Integer::new(value)),
None => E::halt("Integer overflow on addition of two constants"),
}
} else {
let sum = self.to_field() + other.to_field();
let (sum, carry) = match sum.to_lower_bits_le(I::BITS as usize + 1).split_last() {
Some((carry, bits_le)) => (Integer::from_bits_le(bits_le), carry.clone()),
None => E::halt("Malformed sum detected during integer addition"),
};
match I::is_signed() {
true => {
let is_same_sign = self.msb().is_equal(other.msb());
let is_overflow = is_same_sign & sum.msb().is_not_equal(self.msb());
E::assert_eq(is_overflow, E::zero());
}
false => E::assert_eq(carry, E::zero()),
}
sum
}
}
}
impl<E: Environment, I: IntegerType> Metrics<dyn Add<Integer<E, I>, Output = Integer<E, I>>> for Integer<E, I> {
type Case = (Mode, Mode);
fn count(case: &Self::Case) -> Count {
<Self as Metrics<dyn AddChecked<Integer<E, I>, Output = Integer<E, I>>>>::count(case)
}
}
impl<E: Environment, I: IntegerType> OutputMode<dyn Add<Integer<E, I>, Output = Integer<E, I>>> for Integer<E, I> {
type Case = (Mode, Mode);
fn output_mode(case: &Self::Case) -> Mode {
<Self as OutputMode<dyn AddChecked<Integer<E, I>, Output = Integer<E, I>>>>::output_mode(case)
}
}
impl<E: Environment, I: IntegerType> Metrics<dyn AddChecked<Integer<E, I>, Output = Integer<E, I>>> for Integer<E, I> {
type Case = (Mode, Mode);
fn count(case: &Self::Case) -> Count {
match I::is_signed() {
true => match (case.0, case.1) {
(Mode::Constant, Mode::Constant) => Count::is(I::BITS, 0, 0, 0),
(Mode::Constant, _) => Count::is(0, 0, I::BITS + 2, I::BITS + 4),
(_, Mode::Constant) => Count::is(0, 0, I::BITS + 3, I::BITS + 5),
(_, _) => Count::is(0, 0, I::BITS + 4, I::BITS + 6),
},
false => match (case.0, case.1) {
(Mode::Constant, Mode::Constant) => Count::is(I::BITS, 0, 0, 0),
(_, _) => Count::is(0, 0, I::BITS + 1, I::BITS + 3),
},
}
}
}
impl<E: Environment, I: IntegerType> OutputMode<dyn AddChecked<Integer<E, I>, 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::Private,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_integer_binary;
use snarkvm_circuit_environment::Circuit;
use test_utilities::*;
use core::{ops::RangeInclusive, panic::RefUnwindSafe};
const ITERATIONS: u64 = 128;
fn check_add<I: IntegerType + RefUnwindSafe>(
name: &str,
first: console::Integer<<Circuit as Environment>::Network, I>,
second: console::Integer<<Circuit as Environment>::Network, I>,
mode_a: Mode,
mode_b: Mode,
) {
let a = Integer::<Circuit, I>::new(mode_a, first);
let b = Integer::new(mode_b, second);
match first.checked_add(&second) {
Some(expected) => Circuit::scope(name, || {
let candidate = a.add_checked(&b);
assert_eq!(expected, *candidate.eject_value());
assert_eq!(console::Integer::new(expected), candidate.eject_value());
assert_count!(Add(Integer<I>, Integer<I>) => Integer<I>, &(mode_a, mode_b));
assert_output_mode!(Add(Integer<I>, Integer<I>) => Integer<I>, &(mode_a, mode_b), candidate);
}),
None => match mode_a.is_constant() && mode_b.is_constant() {
true => check_operation_halts(&a, &b, Integer::add_checked),
false => Circuit::scope(name, || {
let _candidate = a.add_checked(&b);
assert_count_fails!(Add(Integer<I>, Integer<I>) => Integer<I>, &(mode_a, mode_b));
}),
},
}
Circuit::reset();
}
fn run_test<I: IntegerType + RefUnwindSafe>(mode_a: Mode, mode_b: Mode) {
let mut rng = TestRng::default();
for i in 0..ITERATIONS {
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let name = format!("Add: {} + {} {}", mode_a, mode_b, i);
check_add::<I>(&name, first, second, mode_a, mode_b);
check_add::<I>(&name, second, first, mode_a, mode_b); }
check_add::<I>("MAX + 1", console::Integer::MAX, console::Integer::one(), mode_a, mode_b);
check_add::<I>("1 + MAX", console::Integer::one(), console::Integer::MAX, mode_a, mode_b);
if I::is_signed() {
check_add::<I>("MIN + (-1)", console::Integer::MIN, -console::Integer::one(), mode_a, mode_b);
check_add::<I>("-1 + MIN", -console::Integer::one(), console::Integer::MIN, mode_a, mode_b);
}
}
fn run_exhaustive_test<I: IntegerType + RefUnwindSafe>(mode_a: Mode, mode_b: Mode)
where
RangeInclusive<I>: Iterator<Item = I>,
{
for first in I::MIN..=I::MAX {
for second in I::MIN..=I::MAX {
let first = console::Integer::<_, I>::new(first);
let second = console::Integer::<_, I>::new(second);
let name = format!("Add: ({} + {})", first, second);
check_add::<I>(&name, first, second, mode_a, mode_b);
}
}
}
test_integer_binary!(run_test, i8, plus);
test_integer_binary!(run_test, i16, plus);
test_integer_binary!(run_test, i32, plus);
test_integer_binary!(run_test, i64, plus);
test_integer_binary!(run_test, i128, plus);
test_integer_binary!(run_test, u8, plus);
test_integer_binary!(run_test, u16, plus);
test_integer_binary!(run_test, u32, plus);
test_integer_binary!(run_test, u64, plus);
test_integer_binary!(run_test, u128, plus);
test_integer_binary!(#[ignore], run_exhaustive_test, u8, plus, exhaustive);
test_integer_binary!(#[ignore], run_exhaustive_test, i8, plus, exhaustive);
}