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
use snarkvm_fields::PrimeField;
use snarkvm_r1cs::ConstraintSystem;
use crate::{
bits::boolean::Boolean,
errors::{SignedIntegerError, UnsignedIntegerError},
integers::int::*,
traits::{
eq::{EqGadget, EvaluateEqGadget},
integers::{Div, Integer, Neg},
select::CondSelectGadget,
},
};
macro_rules! div_int_impl {
($($gadget:ident),*) => ($(
impl<F: PrimeField> Div<F> for $gadget {
type ErrorType = SignedIntegerError;
fn div<CS: ConstraintSystem<F>>(
&self,
mut cs: CS,
other: &Self
) -> Result<Self, Self::ErrorType> {
if let Some(value) = other.value {
if value == 0 as <$gadget as Integer>::IntegerType {
return Err(SignedIntegerError::DivisionByZero.into());
}
}
if self.is_constant() && other.is_constant() {
return Ok(Self::constant(self.value.unwrap().wrapping_div(other.value.unwrap())));
}
if self.value.is_some() && other.value.is_some() {
if self.value.unwrap() == <$gadget as Integer>::IntegerType::MIN && other.value.unwrap() == -1 {
return Err(SignedIntegerError::Overflow.into());
}
}
let self_is_min = self.evaluate_equal(cs.ns(||"is_self_min"), &Self::constant(<$gadget as Integer>::IntegerType::MIN))?;
let other_is_minus_one = other.evaluate_equal(cs.ns(||"is_other_minus_one"), &Self::constant(-1 as <$gadget as Integer>::IntegerType))?;
Boolean::and(cs.ns(||"is_result_overflowing"), &self_is_min, &other_is_minus_one)?.enforce_equal(cs.ns(||"result_cannot_be_overflowing"), &Boolean::constant(false))?;
let is_a_negative = self.bits.last().unwrap();
let is_b_negative = other.bits.last().unwrap();
let positive_result = is_a_negative.evaluate_equal(cs.ns(|| "compare_msb"), &is_b_negative)?;
let a_absolute : <$gadget as Integer>::UnsignedGadget = {
let negated_bits = self.bits.neg(cs.ns(||"neg_self_bits"))?;
let mut absolute_value_bits = Vec::with_capacity(self.bits.len());
for i in 0..self.bits.len() {
absolute_value_bits.push(Boolean::conditionally_select(
cs.ns(|| format!("select_the_self_absolute_value_bit_{}", i)),
&is_a_negative,
&negated_bits[i],
&self.bits[i],
)?);
}
<$gadget as Integer>::UnsignedGadget::from_bits_le(&absolute_value_bits)
};
let b_absolute : <$gadget as Integer>::UnsignedGadget = {
let negated_bits = other.bits.neg(cs.ns(||"neg_other_bits"))?;
let mut absolute_value_bits = Vec::with_capacity(self.bits.len());
for i in 0..self.bits.len() {
absolute_value_bits.push(Boolean::conditionally_select(
cs.ns(|| format!("select_the_other_absolute_value_bit_{}", i)),
&is_b_negative,
&negated_bits[i],
&other.bits[i],
)?);
}
<$gadget as Integer>::UnsignedGadget::from_bits_le(&absolute_value_bits)
};
let quotient = a_absolute.div(cs.ns(||"div_absolute_value"), &b_absolute).map_err(
|err|
match err {
UnsignedIntegerError::Overflow => SignedIntegerError::Overflow,
UnsignedIntegerError::DivisionByZero => SignedIntegerError::DivisionByZero,
UnsignedIntegerError::SynthesisError(e) => SignedIntegerError::SynthesisError(e),
}
)?;
let negated_quotient_bits = quotient.bits.neg(cs.ns(||"neg_quotient_bits"))?;
let mut result_bits = Vec::<Boolean>::with_capacity(quotient.bits.len());
for i in 0..quotient.bits.len() {
result_bits.push(Boolean::conditionally_select(
cs.ns(|| format!("select_final_bits_{}", i)),
&positive_result,
"ient.bits[i],
&negated_quotient_bits[i],
)?);
}
let quotient = Self::from_bits_le(&result_bits);
Ok(quotient)
}
}
)*)
}
div_int_impl!(Int8, Int16, Int32, Int64, Int128);