Rename fields of Wire

This commit is contained in:
wborgeaud 2022-05-17 11:31:45 +02:00
parent e10e103933
commit 4cd37ca8d4
17 changed files with 80 additions and 57 deletions

View File

@ -264,8 +264,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F> for Insert
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -372,8 +372,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -251,8 +251,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -216,8 +216,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn dependencies(&self) -> Vec<Target> { fn dependencies(&self) -> Vec<Target> {
let local_target = |input| { let local_target = |input| {
Target::Wire(Wire { Target::Wire(Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}) })
}; };
@ -236,8 +236,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -307,8 +307,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn dependencies(&self) -> Vec<Target> { fn dependencies(&self) -> Vec<Target> {
let local_target = |input| { let local_target = |input| {
Target::Wire(Wire { Target::Wire(Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}) })
}; };
@ -327,8 +327,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -426,8 +426,8 @@ impl<F: RichField + Extendable<D> + Poseidon, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let mut state = (0..SPONGE_WIDTH) let mut state = (0..SPONGE_WIDTH)
@ -569,16 +569,16 @@ mod tests {
let mut inputs = PartialWitness::new(); let mut inputs = PartialWitness::new();
inputs.set_wire( inputs.set_wire(
Wire { Wire {
gate: gate_index, row: gate_index,
input: Gate::WIRE_SWAP, column: Gate::WIRE_SWAP,
}, },
F::ZERO, F::ZERO,
); );
for i in 0..SPONGE_WIDTH { for i in 0..SPONGE_WIDTH {
inputs.set_wire( inputs.set_wire(
Wire { Wire {
gate: gate_index, row: gate_index,
input: Gate::wire_input(i), column: Gate::wire_input(i),
}, },
permutation_inputs[i], permutation_inputs[i],
); );
@ -590,8 +590,8 @@ mod tests {
F::poseidon(permutation_inputs.try_into().unwrap()); F::poseidon(permutation_inputs.try_into().unwrap());
for i in 0..SPONGE_WIDTH { for i in 0..SPONGE_WIDTH {
let out = witness.get_wire(Wire { let out = witness.get_wire(Wire {
gate: 0, row: 0,
input: Gate::wire_output(i), column: Gate::wire_output(i),
}); });
assert_eq!(out, expected_outputs[i]); assert_eq!(out, expected_outputs[i]);
} }

View File

@ -329,8 +329,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -17,7 +17,10 @@ pub enum Target {
impl Target { impl Target {
pub fn wire(gate: usize, input: usize) -> Self { pub fn wire(gate: usize, input: usize) -> Self {
Self::Wire(Wire { gate, input }) Self::Wire(Wire {
row: gate,
column: input,
})
} }
pub fn is_routable(&self, config: &CircuitConfig) -> bool { pub fn is_routable(&self, config: &CircuitConfig) -> bool {
@ -33,7 +36,10 @@ impl Target {
pub fn index(&self, num_wires: usize, degree: usize) -> usize { pub fn index(&self, num_wires: usize, degree: usize) -> usize {
match self { match self {
Target::Wire(Wire { gate, input }) => gate * num_wires + input, Target::Wire(Wire {
row: gate,
column: input,
}) => gate * num_wires + input,
Target::VirtualTarget { index } => degree * num_wires + index, Target::VirtualTarget { index } => degree * num_wires + index,
} }
} }

View File

@ -2,21 +2,26 @@ use std::ops::Range;
use crate::plonk::circuit_data::CircuitConfig; use crate::plonk::circuit_data::CircuitConfig;
/// Represents a wire in the circuit. /// Represents a wire in the circuit, seen as a `degree x num_wires` table.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct Wire { pub struct Wire {
/// The index of the associated gate. /// Row index of the wire.
pub gate: usize, pub row: usize,
/// The index of the gate input wherein this wire is inserted. /// Column index of the wire.
pub input: usize, pub column: usize,
} }
impl Wire { impl Wire {
pub fn is_routable(&self, config: &CircuitConfig) -> bool { pub fn is_routable(&self, config: &CircuitConfig) -> bool {
self.input < config.num_routed_wires self.column < config.num_routed_wires
} }
pub fn from_range(gate: usize, range: Range<usize>) -> Vec<Self> { pub fn from_range(gate: usize, range: Range<usize>) -> Vec<Self> {
range.map(|i| Wire { gate, input: i }).collect() range
.map(|i| Wire {
row: gate,
column: i,
})
.collect()
} }
} }

View File

@ -315,7 +315,7 @@ impl<'a, F: Field> PartitionWitness<'a, F> {
let mut wire_values = vec![vec![F::ZERO; self.degree]; self.num_wires]; let mut wire_values = vec![vec![F::ZERO; self.degree]; self.num_wires];
for i in 0..self.degree { for i in 0..self.degree {
for j in 0..self.num_wires { for j in 0..self.num_wires {
let t = Target::Wire(Wire { gate: i, input: j }); let t = Target::Wire(Wire { row: i, column: j });
if let Some(x) = self.try_get_target(t) { if let Some(x) = self.try_get_target(t) {
wire_values[j][i] = x; wire_values[j][i] = x;
} }

View File

@ -527,7 +527,10 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let gate = self.add_gate(NoopGate, vec![]); let gate = self.add_gate(NoopGate, vec![]);
for w in 0..num_wires { for w in 0..num_wires {
self.add_simple_generator(RandomValueGenerator { self.add_simple_generator(RandomValueGenerator {
target: Target::Wire(Wire { gate, input: w }), target: Target::Wire(Wire {
row: gate,
column: w,
}),
}); });
} }
} }
@ -542,18 +545,18 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
for w in 0..num_routed_wires { for w in 0..num_routed_wires {
self.add_simple_generator(RandomValueGenerator { self.add_simple_generator(RandomValueGenerator {
target: Target::Wire(Wire { target: Target::Wire(Wire {
gate: gate_1, row: gate_1,
input: w, column: w,
}), }),
}); });
self.generate_copy( self.generate_copy(
Target::Wire(Wire { Target::Wire(Wire {
gate: gate_1, row: gate_1,
input: w, column: w,
}), }),
Target::Wire(Wire { Target::Wire(Wire {
gate: gate_2, row: gate_2,
input: w, column: w,
}), }),
); );
} }
@ -596,7 +599,10 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
for gate in 0..degree { for gate in 0..degree {
for input in 0..config.num_wires { for input in 0..config.num_wires {
forest.add(Target::Wire(Wire { gate, input })); forest.add(Target::Wire(Wire {
row: gate,
column: input,
}));
} }
} }

View File

@ -91,7 +91,10 @@ impl Forest {
// Here we keep just the Wire targets, filtering out everything else. // Here we keep just the Wire targets, filtering out everything else.
for gate in 0..self.degree { for gate in 0..self.degree {
for input in 0..self.num_routed_wires { for input in 0..self.num_routed_wires {
let w = Wire { gate, input }; let w = Wire {
row: gate,
column: input,
};
let t = Target::Wire(w); let t = Target::Wire(w);
let x_parent = self.parents[self.target_index(t)]; let x_parent = self.parents[self.target_index(t)];
partition.entry(x_parent).or_default().push(w); partition.entry(x_parent).or_default().push(w);
@ -146,9 +149,12 @@ impl WirePartition {
let mut sigma = Vec::new(); let mut sigma = Vec::new();
for input in 0..num_routed_wires { for input in 0..num_routed_wires {
for gate in 0..degree { for gate in 0..degree {
let wire = Wire { gate, input }; let wire = Wire {
row: gate,
column: input,
};
let neighbor = neighbors[&wire]; let neighbor = neighbors[&wire];
sigma.push(neighbor.input * degree + neighbor.gate); sigma.push(neighbor.column * degree + neighbor.row);
} }
} }
sigma sigma

View File

@ -292,8 +292,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -301,8 +301,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -415,8 +415,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -285,8 +285,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));

View File

@ -250,8 +250,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SwitchGenerator<F, D> {
fn run_in_out(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_in_out(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));
@ -280,8 +280,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SwitchGenerator<F, D> {
fn run_in_switch(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) { fn run_in_switch(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire { let local_wire = |input| Wire {
gate: self.gate_index, row: self.gate_index,
input, column: input,
}; };
let get_local_wire = |input| witness.get_wire(local_wire(input)); let get_local_wire = |input| witness.get_wire(local_wire(input));