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
use crate::{
arithmetic::Mul,
bits::{RippleCarryAdder, SignExtend},
errors::SignedIntegerError,
Int,
Int128,
Int16,
Int32,
Int64,
Int8,
};
use snarkvm_models::{
curves::{FpParameters, PrimeField},
gadgets::{
r1cs::{Assignment, ConstraintSystem, LinearCombination},
utilities::{
alloc::AllocGadget,
boolean::{AllocatedBit, Boolean},
select::CondSelectGadget,
},
},
};
use std::iter;
macro_rules! mul_int_impl {
($($gadget: ident)*) => ($(
impl<F: PrimeField> Mul<F> for $gadget {
type ErrorType = SignedIntegerError;
fn mul<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, Self::ErrorType> {
let is_constant = Boolean::constant(Self::result_is_constant(&self, &other));
let allocated_false = Boolean::from(AllocatedBit::alloc(&mut cs.ns(|| "false"), || Ok(false)).unwrap());
let false_bit = Boolean::conditionally_select(
&mut cs.ns(|| "constant_or_allocated_false"),
&is_constant,
&Boolean::constant(false),
&allocated_false,
)?;
let size = <$gadget as Int>::SIZE * 2;
let a = Boolean::sign_extend(&self.bits, size);
let b = Boolean::sign_extend(&other.bits, size);
let mut bits = vec![false_bit; size];
let mut to_add = Vec::new();
let mut a_shifted = Vec::new();
for (i, b_bit) in b.iter().enumerate() {
a_shifted.extend(iter::repeat(false_bit).take(i));
a_shifted.extend(a.iter());
a_shifted.truncate(size);
to_add.reserve(a_shifted.len());
for (j, a_bit) in a_shifted.iter().enumerate() {
let selected_bit = Boolean::conditionally_select(
&mut cs.ns(|| format!("select product bit {} {}", i, j)),
b_bit,
a_bit,
&false_bit,
)?;
to_add.push(selected_bit);
}
bits = bits.add_bits(
&mut cs.ns(|| format!("add bit {}", i)),
&to_add
)?;
let _carry = bits.pop();
to_add.clear();
a_shifted.clear();
}
drop(to_add);
drop(a_shifted);
let max_bits = <$gadget as Int>::SIZE;
bits.truncate(max_bits);
assert!(F::Parameters::MODULUS_BITS >= max_bits as u32);
let result_value = match (self.value, other.value) {
(Some(a), Some(b)) => {
let val = match a.checked_mul(b) {
Some(val) => val,
None => return Err(SignedIntegerError::Overflow)
};
Some(val)
},
_ => {
None
}
};
let mut lc = LinearCombination::zero();
let mut all_constants = true;
let mut coeff = F::one();
for bit in bits {
match bit {
Boolean::Is(ref bit) => {
all_constants = false;
lc += (coeff, bit.get_variable());
}
Boolean::Not(ref bit) => {
all_constants = false;
lc = lc + (coeff, CS::one()) - (coeff, bit.get_variable());
}
Boolean::Constant(bit) => {
if bit {
lc += (coeff, CS::one());
}
}
}
coeff.double_in_place();
}
let modular_value = result_value.map(|v| v as <$gadget as Int>::IntegerType);
if all_constants && modular_value.is_some() {
return Ok(Self::constant(modular_value.unwrap()));
}
let mut result_bits = Vec::with_capacity(max_bits);
let mut coeff = F::one();
for i in 0..max_bits {
let mask = 1 << i as <$gadget as Int>::IntegerType;
let b = AllocatedBit::alloc(cs.ns(|| format!("result bit_gadget {}", i)), || {
result_value.map(|v| (v & mask) == mask).get()
})?;
lc = lc - (coeff, b.get_variable());
result_bits.push(b.into());
coeff.double_in_place();
}
cs.enforce(|| "modular multiplication", |lc| lc, |lc| lc, |_| lc);
result_bits.truncate(<$gadget as Int>::SIZE);
Ok(Self {
bits: result_bits,
value: modular_value,
})
}
}
)*)
}
mul_int_impl!(Int8 Int16 Int32 Int64 Int128);