ECDSA merge

This commit is contained in:
Nicholas Ward 2022-01-20 16:09:31 -08:00
parent c561333c22
commit f436c14242
4 changed files with 37 additions and 25 deletions

View File

@ -193,8 +193,6 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
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);

View File

@ -178,16 +178,10 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Returns `x % |FF|` as a `NonNativeTarget`.
fn reduce<FF: Field>(&mut self, x: &BigUintTarget) -> NonNativeTarget<FF> {
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<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Returns `x % |FF|` as a `NonNativeTarget`.
/*fn reduce_by_bits<FF: Field>(&mut self, x: &BigUintTarget) -> NonNativeTarget<FF> {
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::<F, 4>::new(config.clone());
let mut op_builder = CircuitBuilder::<F, 4>::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]

View File

@ -65,7 +65,7 @@ pub trait Witness<F: Field> {
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;

View File

@ -224,11 +224,6 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
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<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
// Update `free_binary_arithmetic` with new values.
if i + 1 < BinaryArithmeticGate::<F, D, BITS>::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<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
// Update `free_binary_subtraction` with new values.
if i + 1 < BinarySubtractionGate::<F, D, BITS>::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<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}
/// 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::<F, D, 30>::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::<F, D, 30>::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<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
self.fill_switch_gates();
self.fill_u32_arithmetic_gates();
self.fill_u32_subtraction_gates();
self.fill_binary_arithmetic_gates();
self.fill_binary_subtraction_gates();
}
}