This commit is contained in:
Nicholas Ward 2021-11-10 09:58:14 -08:00
parent 0c182c4621
commit 1402791139
2 changed files with 33 additions and 4 deletions

View File

@ -121,7 +121,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
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::<F, 4>::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)
}
}

View File

@ -565,9 +565,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
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<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}
}
fn fill_batched_gates(&mut self) {
self.fill_arithmetic_gates();
self.fill_switch_gates();
self.fill_u32_arithmetic_gates();
self.fill_u32_subtraction_gates();
}
}