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
use super::{OverflowInteger, ProperCrtUint, ProperUint};
use halo2_base::{gates::GateInstructions, utils::ScalarField, AssignedValue, Context};
pub fn positive<F: ScalarField>(
gate: &impl GateInstructions<F>,
ctx: &mut Context<F>,
a: OverflowInteger<F>,
) -> AssignedValue<F> {
let k = a.limbs.len();
assert_ne!(k, 0);
assert!(a.max_limb_bits as u32 + k.ilog2() < F::CAPACITY);
let sum = gate.sum(ctx, a.limbs);
gate.is_zero(ctx, sum)
}
pub fn assign<F: ScalarField>(
gate: &impl GateInstructions<F>,
ctx: &mut Context<F>,
a: ProperUint<F>,
) -> AssignedValue<F> {
assert!(!a.0.is_empty());
let mut a_limbs = a.0.into_iter();
let mut partial = gate.is_zero(ctx, a_limbs.next().unwrap());
for a_limb in a_limbs {
let limb_is_zero = gate.is_zero(ctx, a_limb);
partial = gate.and(ctx, limb_is_zero, partial);
}
partial
}
pub fn crt<F: ScalarField>(
gate: &impl GateInstructions<F>,
ctx: &mut Context<F>,
a: ProperCrtUint<F>,
) -> AssignedValue<F> {
assign(gate, ctx, ProperUint(a.0.truncation.limbs))
}