diff --git a/src/gadgets/arithmetic_u32.rs b/src/gadgets/arithmetic_u32.rs index 0d88d52c..7cb5b7c5 100644 --- a/src/gadgets/arithmetic_u32.rs +++ b/src/gadgets/arithmetic_u32.rs @@ -36,6 +36,12 @@ impl, const D: usize> CircuitBuilder { self.assert_zero(x.0) } + fn get_u32_target(&self, target: U32Target) -> F { + let result = self.get_target(target.0); + debug_assert!(result.to_canonical_u64() < 1 << 32u64); + result + } + // Returns x * y + z. pub fn mul_add_u32( &mut self, diff --git a/src/gadgets/biguint.rs b/src/gadgets/biguint.rs index b30b4680..54589f54 100644 --- a/src/gadgets/biguint.rs +++ b/src/gadgets/biguint.rs @@ -41,7 +41,22 @@ impl, const D: usize> CircuitBuilder { } } - fn pad_biguints(&mut self, a: BigUintTarget, b: BigUintTarget) -> (BigUintTarget, BigUintTarget) { + fn get_biguint_target(&self, target: BigUintTarget) -> BigUint { + let mut result = BigUint::zero(); + let base = BigUint::from_u64(1 << 32u64); + for &limb in target.limbs.iter().rev() { + let limb_value = self.get_target(limb.0); + result += BigUint::from_u64(limb_value.to_canonical_u64()); + result *= base; + } + result + } + + fn pad_biguints( + &mut self, + a: BigUintTarget, + b: BigUintTarget, + ) -> (BigUintTarget, BigUintTarget) { if a.num_limbs() > b.num_limbs() { let mut padded_b_limbs = b.limbs.clone(); padded_b_limbs.extend(self.add_virtual_u32_targets(a.num_limbs() - b.num_limbs())); @@ -69,11 +84,11 @@ impl, const D: usize> CircuitBuilder { } fn add_virtual_biguint_target(&mut self, num_limbs: usize) -> BigUintTarget { - let limbs = (0..num_limbs).map(|_| self.add_virtual_u32_target()).collect(); + let limbs = (0..num_limbs) + .map(|_| self.add_virtual_u32_target()) + .collect(); - BigUintTarget { - limbs, - } + BigUintTarget { limbs } } // Add two `BigUintTarget`s. @@ -144,7 +159,11 @@ impl, const D: usize> CircuitBuilder { } } - pub fn div_rem_biguint(&mut self, a: BigUintTarget, b: BigUintTarget) -> (BigUintTarget, BigUintTarget) { + pub fn div_rem_biguint( + &mut self, + a: BigUintTarget, + b: BigUintTarget, + ) -> (BigUintTarget, BigUintTarget) { let num_limbs = a.limbs.len(); let div = self.add_virtual_biguint_target(num_limbs); let rem = self.add_virtual_biguint_target(num_limbs); @@ -159,11 +178,10 @@ impl, const D: usize> CircuitBuilder { let div_b = self.mul_biguint(div, b); let div_b_plus_rem = self.add_biguint(div_b, rem); - self.connect_biguint(x, div_b_plus_rem); + self.connect_biguint(a, div_b_plus_rem); - let - - self.assert_one() + let cmp_rem_b = self.cmp_biguint(rem, b); + self.assert_one(cmp_rem_b.target); (div, rem) } @@ -182,10 +200,13 @@ impl, const D: usize> SimpleGenerator for BigUintDivRemGenerator { fn dependencies(&self) -> Vec { - self.a.limbs.iter().map(|&l| l.0).chain(self.b.limbs.iter().map(|&l| l.0)).collect() + self.a + .limbs + .iter() + .map(|&l| l.0) + .chain(self.b.limbs.iter().map(|&l| l.0)) + .collect() } - fn run_once(&self, witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { - - } -} \ No newline at end of file + fn run_once(&self, witness: &PartitionWitness, out_buffer: &mut GeneratedValues) {} +} diff --git a/src/iop/witness.rs b/src/iop/witness.rs index 12186fb1..13a374e2 100644 --- a/src/iop/witness.rs +++ b/src/iop/witness.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::convert::TryInto; -use num::BigUint; +use num::{BigUint, FromPrimitive, Zero}; use crate::field::extension_field::target::ExtensionTarget; use crate::field::extension_field::{Extendable, FieldExtension}; @@ -57,24 +57,12 @@ pub trait Witness { panic!("not a bool") } - fn get_u32_target(&self, target: U32Target) -> F { - let result = self.get_target(target.0); - debug_assert!(result.to_canonical_u64() < 1 << 32u64); - result - } - fn get_hash_target(&self, ht: HashOutTarget) -> HashOut { HashOut { elements: self.get_targets(&ht.elements).try_into().unwrap(), } } - fn get_biguint_target(&self, target: BigUintTarget) -> BigUint { - let mut result = BigUint::zero(); - for (i, &limb) in target - result - } - fn get_wire(&self, wire: Wire) -> F { self.get_target(Target::Wire(wire)) }