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
use snarkvm_fields::PrimeField;
use snarkvm_r1cs::{ConstraintSystem, SynthesisError};
use crate::{
bits::boolean::{AllocatedBit, Boolean},
errors::UnsignedIntegerError,
integers::uint::*,
traits::{
alloc::AllocGadget,
integers::{Div, Integer},
select::CondSelectGadget,
},
};
macro_rules! div_int_impl {
($($gadget:ident),*) => ($(
impl<F: PrimeField> Div<F> for $gadget {
type ErrorType = UnsignedIntegerError;
fn div<CS: ConstraintSystem<F>>(
&self,
mut cs: CS,
other: &Self,
) -> Result<Self, Self::ErrorType> {
if other.eq(&Self::constant(0 as <$gadget as Integer>::IntegerType)) {
return Err(SynthesisError::DivisionByZero.into());
}
let is_constant = Boolean::constant(Self::result_is_constant(&self, &other));
let allocated_true = Boolean::from(AllocatedBit::alloc(&mut cs.ns(|| "true"), || Ok(true)).unwrap());
let true_bit = Boolean::conditionally_select(
&mut cs.ns(|| "constant_or_allocated_true"),
&is_constant,
&Boolean::constant(true),
&allocated_true,
)?;
let allocated_one = Self::alloc(&mut cs.ns(|| "one"), || Ok(1 as <$gadget as Integer>::IntegerType))?;
let one = Self::conditionally_select(
&mut cs.ns(|| "constant_or_allocated_1"),
&is_constant,
&Self::constant(1 as <$gadget as Integer>::IntegerType),
&allocated_one,
)?;
let allocated_zero = Self::alloc(&mut cs.ns(|| "zero"), || Ok(0 as <$gadget as Integer>::IntegerType))?;
let zero = Self::conditionally_select(
&mut cs.ns(|| "constant_or_allocated_0"),
&is_constant,
&Self::constant(0 as <$gadget as Integer>::IntegerType),
&allocated_zero,
)?;
let self_is_zero = Boolean::Constant(self.eq(&Self::constant(0 as <$gadget as Integer>::IntegerType)));
let mut quotient = zero.clone();
let mut remainder = zero.clone();
for (i, bit) in self.bits.iter().rev().enumerate() {
remainder = Self::addmany(&mut cs.ns(|| format!("shift_left_{}", i)), &[
remainder.clone(),
remainder.clone(),
])?;
let bit_is_true = Boolean::constant(bit.eq(&Boolean::constant(true)));
let new_remainder = Self::addmany(&mut cs.ns(|| format!("set_remainder_bit_{}", i)), &[
remainder.clone(),
one.clone(),
])?;
remainder = Self::conditionally_select(
&mut cs.ns(|| format!("increment_or_remainder_{}", i)),
&bit_is_true,
&new_remainder,
&remainder,
)?;
let no_remainder = Boolean::constant(remainder.eq(&other));
let subtraction = remainder.sub_unsafe(&mut cs.ns(|| format!("subtract_divisor_{}", i)), &other)?;
let sub_is_zero = Boolean::constant(subtraction.eq(&Self::constant(0 as <$gadget as Integer>::IntegerType)));
let cond1 = Boolean::and(
&mut cs.ns(|| format!("cond_1_{}", i)),
&no_remainder.not(),
&sub_is_zero.not(),
)?;
let cond2 = Boolean::or(&mut cs.ns(|| format!("cond_2_{}", i)), &no_remainder, &cond1)?;
remainder = Self::conditionally_select(
&mut cs.ns(|| format!("subtract_or_same_{}", i)),
&cond2,
&subtraction,
&remainder,
)?;
let index = <$gadget as Integer>::SIZE - 1 - i as usize;
let bit_value = (1 as <$gadget as Integer>::IntegerType) << (index as <$gadget as Integer>::IntegerType);
let mut new_quotient = quotient.clone();
new_quotient.bits[index] = true_bit;
new_quotient.value = Some(new_quotient.value.unwrap() + bit_value);
quotient = Self::conditionally_select(
&mut cs.ns(|| format!("set_bit_or_same_{}", i)),
&cond2,
&new_quotient,
"ient,
)?;
}
Self::conditionally_select(&mut cs.ns(|| "self_or_quotient"), &self_is_zero, self, "ient).map_err(UnsignedIntegerError::SynthesisError)
}
}
)*)
}
div_int_impl!(UInt8, UInt16, UInt32, UInt64, UInt128);