diff --git a/src/gadgets/biguint.rs b/src/gadgets/biguint.rs index 3aa5c8c1..80c72a1d 100644 --- a/src/gadgets/biguint.rs +++ b/src/gadgets/biguint.rs @@ -121,7 +121,7 @@ impl, const D: usize> CircuitBuilder { let mut borrow = self.zero_u32(); for i in 0..num_limbs { let (result, new_borrow) = self.sub_u32(a.limbs[i], b.limbs[i], borrow); - result_limbs[i] = result; + result_limbs.push(result); borrow = new_borrow; } // Borrow should be zero here. @@ -252,4 +252,28 @@ mod tests { verify(proof, &data.verifier_only, &data.common) } + + #[test] + fn test_biguint_sub() -> Result<()> { + let x_value = BigUint::from_u128(33333333333333333333333333333333333333).unwrap(); + let y_value = BigUint::from_u128(22222222222222222222222222222222222222).unwrap(); + let expected_z_value = &x_value - &y_value; + + type F = CrandallField; + let config = CircuitConfig::large_config(); + let pw = PartialWitness::new(); + let mut builder = CircuitBuilder::::new(config); + + let x = builder.constant_biguint(x_value); + let y = builder.constant_biguint(y_value); + let z = builder.sub_biguint(x, y); + let expected_z = builder.constant_biguint(expected_z_value); + + builder.connect_biguint(z, expected_z); + + let data = builder.build(); + let proof = data.prove(pw).unwrap(); + + verify(proof, &data.verifier_only, &data.common) + } } diff --git a/src/plonk/circuit_builder.rs b/src/plonk/circuit_builder.rs index 804e7be2..dbc8f3df 100644 --- a/src/plonk/circuit_builder.rs +++ b/src/plonk/circuit_builder.rs @@ -565,9 +565,7 @@ impl, const D: usize> CircuitBuilder { let mut timing = TimingTree::new("preprocess", Level::Trace); let start = Instant::now(); - self.fill_arithmetic_gates(); - self.fill_random_access_gates(); - self.fill_switch_gates(); + self.fill_batched_gates(); // Hash the public inputs, and route them to a `PublicInputGate` which will enforce that // those hash wires match the claimed public inputs. @@ -1007,4 +1005,11 @@ impl, const D: usize> CircuitBuilder { } } } + + fn fill_batched_gates(&mut self) { + self.fill_arithmetic_gates(); + self.fill_switch_gates(); + self.fill_u32_arithmetic_gates(); + self.fill_u32_subtraction_gates(); + } }