From f436c142421498b0c7c8224031df27394ac056f5 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 20 Jan 2022 16:09:31 -0800 Subject: [PATCH] ECDSA merge --- plonky2/src/gadgets/arithmetic_u32.rs | 2 -- plonky2/src/gadgets/nonnative.rs | 13 -------- plonky2/src/iop/witness.rs | 2 +- plonky2/src/plonk/circuit_builder.rs | 45 +++++++++++++++++++++------ 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/plonky2/src/gadgets/arithmetic_u32.rs b/plonky2/src/gadgets/arithmetic_u32.rs index 0ba50c85..af1682f6 100644 --- a/plonky2/src/gadgets/arithmetic_u32.rs +++ b/plonky2/src/gadgets/arithmetic_u32.rs @@ -193,8 +193,6 @@ impl, const D: usize> SimpleGenerator let x_u64 = x.to_canonical_u64(); let low = x_u64 as u32; let high: u32 = (x_u64 >> 32).try_into().unwrap(); - println!("LOW: {}", low); - println!("HIGH: {}", high); out_buffer.set_u32_target(self.low.clone(), low); out_buffer.set_u32_target(self.high.clone(), high); diff --git a/plonky2/src/gadgets/nonnative.rs b/plonky2/src/gadgets/nonnative.rs index 12709300..20ecd382 100644 --- a/plonky2/src/gadgets/nonnative.rs +++ b/plonky2/src/gadgets/nonnative.rs @@ -178,16 +178,10 @@ impl, const D: usize> CircuitBuilder { /// Returns `x % |FF|` as a `NonNativeTarget`. fn reduce(&mut self, x: &BigUintTarget) -> NonNativeTarget { - println!("NUM LIMBS: {}", x.limbs.len()); - let before = self.num_gates(); - let modulus = FF::order(); let order_target = self.constant_biguint(&modulus); let value = self.rem_biguint(x, &order_target); - println!("NUMBER OF GATES: {}", self.num_gates() - before); - println!("OUTPUT LIMBS: {}", value.limbs.len()); - NonNativeTarget { value, _phantom: PhantomData, @@ -196,7 +190,6 @@ impl, const D: usize> CircuitBuilder { /// Returns `x % |FF|` as a `NonNativeTarget`. /*fn reduce_by_bits(&mut self, x: &BigUintTarget) -> NonNativeTarget { - println!("NUM LIMBS: {}", x.limbs.len()); let before = self.num_gates(); let mut powers_of_two = Vec::new(); @@ -430,7 +423,6 @@ mod tests { let x_ff = FF::rand(); let y_ff = FF::rand(); let product_ff = x_ff * y_ff; - println!("PRODUCT FF: {:?}", product_ff); let config = CircuitConfig::standard_recursion_config(); let pw = PartialWitness::new(); @@ -456,8 +448,6 @@ mod tests { let mut unop_builder = CircuitBuilder::::new(config.clone()); let mut op_builder = CircuitBuilder::::new(config); - println!("NUM: {}", num); - let ffs: Vec<_> = (0..num).map(|_| FF::rand()).collect(); let op_targets: Vec<_> = ffs @@ -465,7 +455,6 @@ mod tests { .map(|&x| op_builder.constant_nonnative(x)) .collect(); op_builder.mul_many_nonnative(&op_targets); - println!("OPTIMIZED GATE COUNT: {}", op_builder.num_gates()); let unop_targets: Vec<_> = ffs .iter() @@ -475,8 +464,6 @@ mod tests { for i in 1..unop_targets.len() { result = unop_builder.mul_nonnative(&result, &unop_targets[i]); } - - println!("UNOPTIMIZED GATE COUNT: {}", unop_builder.num_gates()); } #[test] diff --git a/plonky2/src/iop/witness.rs b/plonky2/src/iop/witness.rs index efe4d911..558832d6 100644 --- a/plonky2/src/iop/witness.rs +++ b/plonky2/src/iop/witness.rs @@ -65,7 +65,7 @@ pub trait Witness { fn get_biguint_target(&self, target: BigUintTarget) -> BigUint { let mut result = BigUint::zero(); - let limb_base = BigUint::from_u64(1 << 32u64).unwrap(); + let limb_base = BigUint::from_u64(1 << 30u64).unwrap(); for i in (0..target.num_limbs()).rev() { let limb = target.get_limb(i); result *= &limb_base; diff --git a/plonky2/src/plonk/circuit_builder.rs b/plonky2/src/plonk/circuit_builder.rs index f9704e0e..ae65a53b 100644 --- a/plonky2/src/plonk/circuit_builder.rs +++ b/plonky2/src/plonk/circuit_builder.rs @@ -224,11 +224,6 @@ impl, const D: usize> CircuitBuilder { let gate_ref = GateRef::new(gate_type); self.gates.insert(gate_ref.clone()); - /*println!("ADDING GATE {}: {:?}", index, gate_ref); - if index == 145 { - panic!(); - }*/ - self.gate_instances.push(GateInstance { gate_ref, constants, @@ -1070,10 +1065,10 @@ impl, const D: usize> CircuitBuilder { // Update `free_binary_arithmetic` with new values. if i + 1 < BinaryArithmeticGate::::new_from_config(&self.config).num_ops { self.batched_gates - .free_random_access + .free_binary_arithmetic_gate .insert(BITS, (gate, i + 1)); } else { - self.batched_gates.free_random_access.remove(&BITS); + self.batched_gates.free_binary_arithmetic_gate.remove(&BITS); } (gate, i) @@ -1099,10 +1094,10 @@ impl, const D: usize> CircuitBuilder { // Update `free_binary_subtraction` with new values. if i + 1 < BinarySubtractionGate::::new_from_config(&self.config).num_ops { self.batched_gates - .free_random_access + .free_binary_subtraction_gate .insert(BITS, (gate, i + 1)); } else { - self.batched_gates.free_random_access.remove(&BITS); + self.batched_gates.free_binary_subtraction_gate.remove(&BITS); } (gate, i) @@ -1242,6 +1237,36 @@ impl, const D: usize> CircuitBuilder { } } + /// Fill the remaining unused binary arithmetic operations with zeros, so that all + /// `BinaryArithmeticGenerator`s are run. + fn fill_binary_arithmetic_gates(&mut self) { + let zero = self.zero_binary::<30>(); + if let Some(&(_, i)) = self.batched_gates.free_binary_arithmetic_gate.get(&30) { + let max_copies = + BinaryArithmeticGate::::new_from_config(&self.config).num_ops; + for _ in i..max_copies { + let dummy = self.add_virtual_binary_target(); + self.mul_add_binary(dummy, dummy, dummy); + self.connect_binary(dummy, zero); + } + } + } + + /// Fill the remaining unused binary subtraction operations with zeros, so that all + /// `BinarySubtractionGenerator`s are run. + fn fill_binary_subtraction_gates(&mut self) { + let zero = self.zero_binary::<30>(); + if let Some(&(_, i)) = self.batched_gates.free_binary_subtraction_gate.get(&30) { + let max_copies = + BinarySubtractionGate::::new_from_config(&self.config).num_ops; + for _ in i..max_copies { + let dummy = self.add_virtual_binary_target(); + self.sub_binary(dummy, dummy, dummy); + self.connect_binary(dummy, zero); + } + } + } + fn fill_batched_gates(&mut self) { self.fill_arithmetic_gates(); self.fill_base_arithmetic_gates(); @@ -1250,5 +1275,7 @@ impl, const D: usize> CircuitBuilder { self.fill_switch_gates(); self.fill_u32_arithmetic_gates(); self.fill_u32_subtraction_gates(); + self.fill_binary_arithmetic_gates(); + self.fill_binary_subtraction_gates(); } }