2021-10-01 14:20:21 -07:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
2021-10-04 14:17:28 -07:00
|
|
|
use num::bigint::BigUint;
|
|
|
|
|
|
2021-10-01 14:20:21 -07:00
|
|
|
use crate::field::field_types::RichField;
|
|
|
|
|
use crate::field::{extension_field::Extendable, field_types::Field};
|
2021-11-09 18:10:47 -08:00
|
|
|
use crate::gadgets::arithmetic_u32::U32Target;
|
|
|
|
|
use crate::gates::arithmetic_u32::U32ArithmeticGate;
|
2021-10-01 14:20:21 -07:00
|
|
|
use crate::gates::switch::SwitchGate;
|
|
|
|
|
use crate::iop::generator::{GeneratedValues, SimpleGenerator};
|
|
|
|
|
use crate::iop::target::Target;
|
|
|
|
|
use crate::iop::witness::{PartitionWitness, Witness};
|
|
|
|
|
use crate::plonk::circuit_builder::CircuitBuilder;
|
|
|
|
|
use crate::util::bimap::bimap_from_lists;
|
2021-11-10 09:53:27 -08:00
|
|
|
|
|
|
|
|
pub struct ForeignFieldTarget<FF: Field> {
|
2021-10-01 14:20:21 -07:00
|
|
|
/// These F elements are assumed to contain 32-bit values.
|
|
|
|
|
limbs: Vec<U32Target>,
|
2021-11-10 09:53:27 -08:00
|
|
|
_phantom: PhantomData<FF>,
|
2021-10-01 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
2021-10-12 11:41:34 -07:00
|
|
|
pub fn order_u32_limbs<FF: Field>(&mut self) -> Vec<U32Target> {
|
2021-11-10 09:53:27 -08:00
|
|
|
let modulus = FF::order();
|
|
|
|
|
let limbs = modulus.to_u32_digits();
|
|
|
|
|
limbs.iter().map(|&limb| self.constant_u32(F::from_canonical_u32(limb))).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add two `ForeignFieldTarget`s, which we assume are both normalized.
|
|
|
|
|
pub fn add_nonnative<FF: Field>(&mut self, a: ForeignFieldTarget<FF>, b: ForeignFieldTarget<FF>) -> ForeignFieldTarget<FF> {
|
2021-10-01 14:20:21 -07:00
|
|
|
let num_limbs = a.limbs.len();
|
|
|
|
|
debug_assert!(b.limbs.len() == num_limbs);
|
|
|
|
|
|
2021-10-04 14:17:19 -07:00
|
|
|
let mut combined_limbs = self.add_virtual_u32_targets(num_limbs + 1);
|
|
|
|
|
let mut carry = self.zero_u32();
|
2021-10-01 14:20:21 -07:00
|
|
|
for i in 0..num_limbs {
|
2021-10-12 11:41:34 -07:00
|
|
|
let (new_limb, carry) = self.add_three_u32(carry.clone(), a.limbs[i].clone(), b.limbs[i].clone());
|
2021-10-04 14:17:19 -07:00
|
|
|
combined_limbs[i] = new_limb;
|
|
|
|
|
}
|
|
|
|
|
combined_limbs[num_limbs] = carry;
|
2021-10-04 14:17:28 -07:00
|
|
|
|
2021-11-10 09:53:27 -08:00
|
|
|
let reduced_limbs = self.reduce_add_result::<FF>(combined_limbs);
|
|
|
|
|
ForeignFieldTarget {
|
2021-10-04 14:18:32 -07:00
|
|
|
limbs: reduced_limbs,
|
2021-11-10 09:53:27 -08:00
|
|
|
_phantom: PhantomData,
|
2021-10-01 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 09:53:27 -08:00
|
|
|
pub fn reduce_add_result<FF: Field>(&mut self, limbs: Vec<U32Target>) -> Vec<U32Target> {
|
2021-10-01 14:20:21 -07:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 09:53:27 -08:00
|
|
|
pub fn mul_nonnative<FF: Field>(&mut self, a: ForeignFieldTarget<FF>, b: ForeignFieldTarget<FF>) -> ForeignFieldTarget<FF> {
|
2021-10-01 14:20:21 -07:00
|
|
|
let num_limbs = a.limbs.len();
|
|
|
|
|
debug_assert!(b.limbs.len() == num_limbs);
|
|
|
|
|
|
2021-10-12 11:41:34 -07:00
|
|
|
/*let mut combined_limbs = self.add_virtual_u32_targets(2 * num_limbs - 1);
|
2021-10-01 14:20:21 -07:00
|
|
|
for i in 0..num_limbs {
|
|
|
|
|
for j in 0..num_limbs {
|
2021-10-04 16:23:21 -07:00
|
|
|
let sum = self.add_u32(a.limbs[i], b.limbs[j]);
|
2021-10-12 11:41:34 -07:00
|
|
|
combined_limbs[i + j] = self.add_u32(combined_limbs[i + j], sum);
|
2021-10-01 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 09:53:27 -08:00
|
|
|
let reduced_limbs = self.reduce_mul_result::<FF>(combined_limbs);
|
2021-11-09 18:10:47 -08:00
|
|
|
|
2021-11-10 09:53:27 -08:00
|
|
|
ForeignFieldTarget {
|
2021-10-01 14:20:21 -07:00
|
|
|
limbs: reduced_limbs,
|
2021-11-10 09:53:27 -08:00
|
|
|
_phantom: PhantomData,
|
2021-10-12 11:41:34 -07:00
|
|
|
}*/
|
|
|
|
|
todo!()
|
2021-10-01 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 09:53:27 -08:00
|
|
|
pub fn reduce_mul_result<FF: Field>(&mut self, limbs: Vec<U32Target>) -> Vec<U32Target> {
|
2021-10-01 14:20:21 -07:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|