halo2_ecc/bigint/
scalar_mul_no_carry.rs

1use super::{CRTInteger, OverflowInteger};
2use halo2_base::{
3    gates::GateInstructions,
4    utils::{log2_ceil, ScalarField},
5    Context,
6    QuantumCell::Constant,
7};
8
9pub fn assign<F: ScalarField>(
10    gate: &impl GateInstructions<F>,
11    ctx: &mut Context<F>,
12    a: OverflowInteger<F>,
13    c_f: F,
14    c_log2_ceil: usize,
15) -> OverflowInteger<F> {
16    let out_limbs = a.limbs.into_iter().map(|limb| gate.mul(ctx, limb, Constant(c_f))).collect();
17    OverflowInteger::new(out_limbs, a.max_limb_bits + c_log2_ceil)
18}
19
20pub fn crt<F: ScalarField>(
21    gate: &impl GateInstructions<F>,
22    ctx: &mut Context<F>,
23    a: CRTInteger<F>,
24    c: i64,
25) -> CRTInteger<F> {
26    let (c_f, c_abs) = if c >= 0 {
27        let c_abs = u64::try_from(c).unwrap();
28        (F::from(c_abs), c_abs)
29    } else {
30        let c_abs = u64::try_from(-c).unwrap();
31        (-F::from(c_abs), c_abs)
32    };
33
34    let out_overflow = assign(gate, ctx, a.truncation, c_f, log2_ceil(c_abs));
35    let out_native = gate.mul(ctx, a.native, Constant(c_f));
36    let out_val = a.value * c;
37
38    CRTInteger::new(out_overflow, out_native, out_val)
39}