|
| 1 | +use super::{BigPrimeField, FieldChip, Selectable}; |
| 2 | +use halo2_base::gates::RangeChip; |
| 3 | +use halo2_base::QuantumCell::Constant; |
| 4 | +use halo2_base::{ |
| 5 | + gates::GateInstructions, gates::RangeInstructions, utils::modulus, AssignedValue, Context, |
| 6 | +}; |
| 7 | +use num_bigint::BigUint; |
| 8 | +use std::marker::PhantomData; |
| 9 | + |
| 10 | +// native field chip which implements FieldChip, use GateInstructions for basic arithmetic operations |
| 11 | +#[derive(Clone, Debug)] |
| 12 | +pub struct NativeFieldChip<'range, F: BigPrimeField> { |
| 13 | + pub range: &'range RangeChip<F>, |
| 14 | + pub native_modulus: BigUint, |
| 15 | + _marker: PhantomData<F>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<'range, F: BigPrimeField> NativeFieldChip<'range, F> { |
| 19 | + pub fn new(range: &'range RangeChip<F>) -> Self { |
| 20 | + let native_modulus = modulus::<F>(); |
| 21 | + Self { range, native_modulus, _marker: PhantomData } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl<'range, F: BigPrimeField> FieldChip<F> for NativeFieldChip<'range, F> { |
| 26 | + const PRIME_FIELD_NUM_BITS: u32 = F::NUM_BITS; |
| 27 | + type UnsafeFieldPoint = AssignedValue<F>; |
| 28 | + type FieldPoint = AssignedValue<F>; |
| 29 | + type ReducedFieldPoint = AssignedValue<F>; |
| 30 | + type FieldType = F; |
| 31 | + type RangeChip = RangeChip<F>; |
| 32 | + |
| 33 | + fn native_modulus(&self) -> &BigUint { |
| 34 | + &self.native_modulus |
| 35 | + } |
| 36 | + fn range(&self) -> &'range Self::RangeChip { |
| 37 | + self.range |
| 38 | + } |
| 39 | + fn limb_bits(&self) -> usize { |
| 40 | + F::NUM_BITS as usize |
| 41 | + } |
| 42 | + |
| 43 | + fn get_assigned_value(&self, x: &AssignedValue<F>) -> F { |
| 44 | + *x.value() |
| 45 | + } |
| 46 | + |
| 47 | + fn load_private(&self, ctx: &mut Context<F>, a: F) -> AssignedValue<F> { |
| 48 | + ctx.load_witness(a) |
| 49 | + } |
| 50 | + |
| 51 | + fn load_constant(&self, ctx: &mut Context<F>, a: F) -> AssignedValue<F> { |
| 52 | + ctx.load_constant(a) |
| 53 | + } |
| 54 | + |
| 55 | + // signed overflow BigInt functions |
| 56 | + fn add_no_carry( |
| 57 | + &self, |
| 58 | + ctx: &mut Context<F>, |
| 59 | + a: impl Into<AssignedValue<F>>, |
| 60 | + b: impl Into<AssignedValue<F>>, |
| 61 | + ) -> AssignedValue<F> { |
| 62 | + self.gate().add(ctx, a.into(), b.into()) |
| 63 | + } |
| 64 | + |
| 65 | + fn add_constant_no_carry( |
| 66 | + &self, |
| 67 | + ctx: &mut Context<F>, |
| 68 | + a: impl Into<AssignedValue<F>>, |
| 69 | + c: F, |
| 70 | + ) -> AssignedValue<F> { |
| 71 | + self.gate().add(ctx, a.into(), Constant(c)) |
| 72 | + } |
| 73 | + |
| 74 | + fn sub_no_carry( |
| 75 | + &self, |
| 76 | + ctx: &mut Context<F>, |
| 77 | + a: impl Into<AssignedValue<F>>, |
| 78 | + b: impl Into<AssignedValue<F>>, |
| 79 | + ) -> AssignedValue<F> { |
| 80 | + self.gate().sub(ctx, a.into(), b.into()) |
| 81 | + } |
| 82 | + |
| 83 | + // Input: a |
| 84 | + // Output: p - a if a != 0, else a |
| 85 | + fn negate(&self, ctx: &mut Context<F>, a: AssignedValue<F>) -> AssignedValue<F> { |
| 86 | + self.gate().neg(ctx, a) |
| 87 | + } |
| 88 | + |
| 89 | + fn scalar_mul_no_carry( |
| 90 | + &self, |
| 91 | + ctx: &mut Context<F>, |
| 92 | + a: impl Into<AssignedValue<F>>, |
| 93 | + c: i64, |
| 94 | + ) -> AssignedValue<F> { |
| 95 | + let c_f = if c >= 0 { |
| 96 | + let c_abs = u64::try_from(c).unwrap(); |
| 97 | + F::from(c_abs) |
| 98 | + } else { |
| 99 | + let c_abs = u64::try_from(-c).unwrap(); |
| 100 | + -F::from(c_abs) |
| 101 | + }; |
| 102 | + |
| 103 | + self.gate().mul(ctx, a.into(), Constant(c_f)) |
| 104 | + } |
| 105 | + |
| 106 | + fn scalar_mul_and_add_no_carry( |
| 107 | + &self, |
| 108 | + ctx: &mut Context<F>, |
| 109 | + a: impl Into<AssignedValue<F>>, |
| 110 | + b: impl Into<AssignedValue<F>>, |
| 111 | + c: i64, |
| 112 | + ) -> AssignedValue<F> { |
| 113 | + let c_f = if c >= 0 { |
| 114 | + let c_abs = u64::try_from(c).unwrap(); |
| 115 | + F::from(c_abs) |
| 116 | + } else { |
| 117 | + let c_abs = u64::try_from(-c).unwrap(); |
| 118 | + -F::from(c_abs) |
| 119 | + }; |
| 120 | + |
| 121 | + self.gate().mul_add(ctx, a.into(), Constant(c_f), b.into()) |
| 122 | + } |
| 123 | + |
| 124 | + fn mul_no_carry( |
| 125 | + &self, |
| 126 | + ctx: &mut Context<F>, |
| 127 | + a: impl Into<AssignedValue<F>>, |
| 128 | + b: impl Into<AssignedValue<F>>, |
| 129 | + ) -> AssignedValue<F> { |
| 130 | + self.gate().mul(ctx, a.into(), b.into()) |
| 131 | + } |
| 132 | + |
| 133 | + fn check_carry_mod_to_zero(&self, ctx: &mut Context<F>, a: AssignedValue<F>) { |
| 134 | + self.gate().assert_is_const(ctx, &a, &F::ZERO); |
| 135 | + } |
| 136 | + |
| 137 | + // noop |
| 138 | + fn carry_mod(&self, _ctx: &mut Context<F>, a: AssignedValue<F>) -> AssignedValue<F> { |
| 139 | + a |
| 140 | + } |
| 141 | + |
| 142 | + fn range_check( |
| 143 | + &self, |
| 144 | + ctx: &mut Context<F>, |
| 145 | + a: impl Into<AssignedValue<F>>, |
| 146 | + max_bits: usize, // the maximum bits that a.value could take |
| 147 | + ) { |
| 148 | + // skip range chek if max_bits >= F::NUM_BITS |
| 149 | + if max_bits < F::NUM_BITS as usize { |
| 150 | + let a: AssignedValue<F> = a.into(); |
| 151 | + self.range().range_check(ctx, a, max_bits); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + fn enforce_less_than(&self, _ctx: &mut Context<F>, a: AssignedValue<F>) -> AssignedValue<F> { |
| 156 | + a |
| 157 | + } |
| 158 | + |
| 159 | + /// Returns 1 iff `a` is 0 as a BigUint. |
| 160 | + fn is_soft_zero( |
| 161 | + &self, |
| 162 | + ctx: &mut Context<F>, |
| 163 | + a: impl Into<AssignedValue<F>>, |
| 164 | + ) -> AssignedValue<F> { |
| 165 | + let a = a.into(); |
| 166 | + self.gate().is_zero(ctx, a) |
| 167 | + } |
| 168 | + |
| 169 | + fn is_soft_nonzero( |
| 170 | + &self, |
| 171 | + ctx: &mut Context<F>, |
| 172 | + a: impl Into<AssignedValue<F>>, |
| 173 | + ) -> AssignedValue<F> { |
| 174 | + let a = a.into(); |
| 175 | + let is_soft_zero = self.is_soft_zero(ctx, a); |
| 176 | + self.gate().neg(ctx, is_soft_zero) |
| 177 | + } |
| 178 | + |
| 179 | + fn is_zero(&self, ctx: &mut Context<F>, a: impl Into<AssignedValue<F>>) -> AssignedValue<F> { |
| 180 | + self.is_soft_zero(ctx, a) |
| 181 | + } |
| 182 | + |
| 183 | + fn is_equal_unenforced( |
| 184 | + &self, |
| 185 | + ctx: &mut Context<F>, |
| 186 | + a: AssignedValue<F>, |
| 187 | + b: AssignedValue<F>, |
| 188 | + ) -> AssignedValue<F> { |
| 189 | + self.gate().is_equal(ctx, a, b) |
| 190 | + } |
| 191 | + |
| 192 | + fn assert_equal( |
| 193 | + &self, |
| 194 | + ctx: &mut Context<F>, |
| 195 | + a: impl Into<AssignedValue<F>>, |
| 196 | + b: impl Into<AssignedValue<F>>, |
| 197 | + ) { |
| 198 | + ctx.constrain_equal(&a.into(), &b.into()); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +impl<'range, F: BigPrimeField> Selectable<F, AssignedValue<F>> for NativeFieldChip<'range, F> { |
| 203 | + fn select( |
| 204 | + &self, |
| 205 | + ctx: &mut Context<F>, |
| 206 | + a: AssignedValue<F>, |
| 207 | + b: AssignedValue<F>, |
| 208 | + sel: AssignedValue<F>, |
| 209 | + ) -> AssignedValue<F> { |
| 210 | + let gate = self.gate(); |
| 211 | + GateInstructions::select(gate, ctx, a, b, sel) |
| 212 | + } |
| 213 | + |
| 214 | + fn select_by_indicator( |
| 215 | + &self, |
| 216 | + ctx: &mut Context<F>, |
| 217 | + a: &impl AsRef<[AssignedValue<F>]>, |
| 218 | + coeffs: &[AssignedValue<F>], |
| 219 | + ) -> AssignedValue<F> { |
| 220 | + let a = a.as_ref().to_vec(); |
| 221 | + let gate = self.gate(); |
| 222 | + GateInstructions::select_by_indicator(gate, ctx, a, coeffs.to_vec()) |
| 223 | + } |
| 224 | +} |
0 commit comments