mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-06 07:43:10 +00:00
Working
This commit is contained in:
parent
f81e32f8b4
commit
fae471f9d8
@ -9,7 +9,9 @@ use crate::gates::packed_util::PackedEvaluableBase;
|
||||
use crate::gates::util::StridedConstraintConsumer;
|
||||
use crate::hash::hash_types::RichField;
|
||||
use crate::iop::ext_target::ExtensionTarget;
|
||||
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
|
||||
use crate::iop::generator::{
|
||||
ConstantGenerator, GeneratedValues, SimpleGenerator, WitnessGenerator,
|
||||
};
|
||||
use crate::iop::target::Target;
|
||||
use crate::iop::wire::Wire;
|
||||
use crate::iop::witness::PartitionWitness;
|
||||
@ -82,20 +84,21 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for ConstantGate {
|
||||
gate_index: usize,
|
||||
local_constants: &[F],
|
||||
) -> Vec<Box<dyn WitnessGenerator<F>>> {
|
||||
(0..self.num_consts)
|
||||
.map(|i| {
|
||||
let g: Box<dyn WitnessGenerator<F>> = Box::new(
|
||||
ConstantGenerator {
|
||||
gate_index,
|
||||
gate: *self,
|
||||
i,
|
||||
constant: local_constants[self.const_input(i)],
|
||||
}
|
||||
.adapter(),
|
||||
);
|
||||
g
|
||||
})
|
||||
.collect()
|
||||
// (0..self.num_consts)
|
||||
// .map(|i| {
|
||||
// let g: Box<dyn WitnessGenerator<F>> = Box::new(
|
||||
// ConstantGenerator {
|
||||
// gate_index,
|
||||
// gate: *self,
|
||||
// i,
|
||||
// constant: local_constants[self.const_input(i)],
|
||||
// }
|
||||
// .adapter(),
|
||||
// );
|
||||
// g
|
||||
// })
|
||||
// .collect()
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn num_wires(&self) -> usize {
|
||||
@ -114,9 +117,16 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for ConstantGate {
|
||||
self.num_consts
|
||||
}
|
||||
|
||||
fn extra_constants(&self) -> Vec<(usize, usize)> {
|
||||
fn extra_constants(&self, gate_index: usize) -> Vec<ConstantGenerator<F>> {
|
||||
(0..self.num_consts)
|
||||
.map(|i| (self.const_input(i), self.wire_output(i)))
|
||||
.map(|i| {
|
||||
ConstantGenerator::new(
|
||||
gate_index,
|
||||
self.const_input(i),
|
||||
self.wire_output(i),
|
||||
F::ZERO,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
@ -133,27 +143,27 @@ impl<F: RichField + Extendable<D>, const D: usize> PackedEvaluableBase<F, D> for
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ConstantGenerator<F: Field> {
|
||||
gate_index: usize,
|
||||
gate: ConstantGate,
|
||||
i: usize,
|
||||
constant: F,
|
||||
}
|
||||
// #[derive(Debug)]
|
||||
// struct ConstantGenerator<F: Field> {
|
||||
// gate_index: usize,
|
||||
// gate: ConstantGate,
|
||||
// i: usize,
|
||||
// constant: F,
|
||||
// }
|
||||
|
||||
impl<F: Field> SimpleGenerator<F> for ConstantGenerator<F> {
|
||||
fn dependencies(&self) -> Vec<Target> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn run_once(&self, _witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
||||
let wire = Wire {
|
||||
gate: self.gate_index,
|
||||
input: self.gate.wire_output(self.i),
|
||||
};
|
||||
out_buffer.set_wire(wire, self.constant);
|
||||
}
|
||||
}
|
||||
// impl<F: Field> SimpleGenerator<F> for ConstantGenerator<F> {
|
||||
// fn dependencies(&self) -> Vec<Target> {
|
||||
// Vec::new()
|
||||
// }
|
||||
//
|
||||
// fn run_once(&self, _witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
||||
// let wire = Wire {
|
||||
// gate: self.gate_index,
|
||||
// input: self.gate.wire_output(self.i),
|
||||
// };
|
||||
// out_buffer.set_wire(wire, self.constant);
|
||||
// }
|
||||
// }
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -12,7 +12,7 @@ use crate::gates::selectors::UNUSED_SELECTOR;
|
||||
use crate::gates::util::StridedConstraintConsumer;
|
||||
use crate::hash::hash_types::RichField;
|
||||
use crate::iop::ext_target::ExtensionTarget;
|
||||
use crate::iop::generator::WitnessGenerator;
|
||||
use crate::iop::generator::{ConstantGenerator, WitnessGenerator};
|
||||
use crate::plonk::circuit_builder::CircuitBuilder;
|
||||
use crate::plonk::vars::{
|
||||
EvaluationTargets, EvaluationVars, EvaluationVarsBase, EvaluationVarsBaseBatch,
|
||||
@ -181,7 +181,7 @@ pub trait Gate<F: RichField + Extendable<D>, const D: usize>: 'static + Send + S
|
||||
.len()
|
||||
}
|
||||
|
||||
fn extra_constants(&self) -> Vec<(usize, usize)> {
|
||||
fn extra_constants(&self, _gate_index: usize) -> Vec<ConstantGenerator<F>> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,7 +11,9 @@ use crate::gates::packed_util::PackedEvaluableBase;
|
||||
use crate::gates::util::StridedConstraintConsumer;
|
||||
use crate::hash::hash_types::RichField;
|
||||
use crate::iop::ext_target::ExtensionTarget;
|
||||
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
|
||||
use crate::iop::generator::{
|
||||
ConstantGenerator, GeneratedValues, SimpleGenerator, WitnessGenerator,
|
||||
};
|
||||
use crate::iop::target::Target;
|
||||
use crate::iop::wire::Wire;
|
||||
use crate::iop::witness::{PartitionWitness, Witness};
|
||||
@ -230,18 +232,18 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGa
|
||||
);
|
||||
g
|
||||
})
|
||||
.chain((0..self.num_extra_constants).map(|i| {
|
||||
let g: Box<dyn WitnessGenerator<F>> = Box::new(
|
||||
RandomAccessExtraConstantsGenerator {
|
||||
gate_index,
|
||||
gate: *self,
|
||||
i,
|
||||
constant: local_constants[i],
|
||||
}
|
||||
.adapter(),
|
||||
);
|
||||
g
|
||||
}))
|
||||
// .chain((0..self.num_extra_constants).map(|i| {
|
||||
// let g: Box<dyn WitnessGenerator<F>> = Box::new(
|
||||
// RandomAccessExtraConstantsGenerator {
|
||||
// gate_index,
|
||||
// gate: *self,
|
||||
// i,
|
||||
// constant: local_constants[i],
|
||||
// }
|
||||
// .adapter(),
|
||||
// );
|
||||
// g
|
||||
// }))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@ -266,9 +268,9 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGa
|
||||
self.num_copies
|
||||
}
|
||||
|
||||
fn extra_constants(&self) -> Vec<(usize, usize)> {
|
||||
fn extra_constants(&self, gate_index: usize) -> Vec<ConstantGenerator<F>> {
|
||||
(0..self.num_extra_constants)
|
||||
.map(|i| (i, self.wire_extra_constant(i)))
|
||||
.map(|i| ConstantGenerator::new(gate_index, i, self.wire_extra_constant(i), F::ZERO))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
@ -440,6 +442,7 @@ mod tests {
|
||||
lists: Vec<Vec<F>>,
|
||||
access_indices: Vec<usize>,
|
||||
claimed_elements: Vec<F>,
|
||||
constants: &[F],
|
||||
) -> Vec<FF> {
|
||||
let num_copies = lists.len();
|
||||
let vec_size = lists[0].len();
|
||||
@ -458,6 +461,7 @@ mod tests {
|
||||
bit_vals.push(F::from_bool(((access_index >> i) & 1) != 0));
|
||||
}
|
||||
}
|
||||
v.extend(constants);
|
||||
v.extend(bit_vals);
|
||||
|
||||
v.iter().map(|&x| x.into()).collect()
|
||||
@ -478,6 +482,7 @@ mod tests {
|
||||
num_extra_constants: 1,
|
||||
_phantom: PhantomData,
|
||||
};
|
||||
let constants = F::rand_vec(gate.num_constants());
|
||||
|
||||
let good_claimed_elements = lists
|
||||
.iter()
|
||||
@ -485,19 +490,26 @@ mod tests {
|
||||
.map(|(l, &i)| l[i])
|
||||
.collect();
|
||||
let good_vars = EvaluationVars {
|
||||
local_constants: &[],
|
||||
local_constants: &constants.iter().map(|&x| x.into()).collect::<Vec<_>>(),
|
||||
local_wires: &get_wires(
|
||||
bits,
|
||||
lists.clone(),
|
||||
access_indices.clone(),
|
||||
good_claimed_elements,
|
||||
&constants,
|
||||
),
|
||||
public_inputs_hash: &HashOut::rand(),
|
||||
};
|
||||
let bad_claimed_elements = F::rand_vec(4);
|
||||
let bad_vars = EvaluationVars {
|
||||
local_constants: &[],
|
||||
local_wires: &get_wires(bits, lists, access_indices, bad_claimed_elements),
|
||||
local_constants: &constants.iter().map(|&x| x.into()).collect::<Vec<_>>(),
|
||||
local_wires: &get_wires(
|
||||
bits,
|
||||
lists,
|
||||
access_indices,
|
||||
bad_claimed_elements,
|
||||
&constants,
|
||||
),
|
||||
public_inputs_hash: &HashOut::rand(),
|
||||
};
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ use plonky2_field::field_types::{Field, PrimeField};
|
||||
use crate::gadgets::arithmetic_u32::U32Target;
|
||||
use crate::gadgets::biguint::BigUintTarget;
|
||||
use crate::gadgets::nonnative::NonNativeTarget;
|
||||
use crate::gates::gate::Gate;
|
||||
use crate::hash::hash_types::{HashOut, HashOutTarget, RichField};
|
||||
use crate::iop::ext_target::ExtensionTarget;
|
||||
use crate::iop::target::{BoolTarget, Target};
|
||||
@ -325,3 +326,38 @@ impl<F: Field> SimpleGenerator<F> for NonzeroTestGenerator {
|
||||
out_buffer.set_target(self.dummy, dummy_value);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ConstantGenerator<F: Field> {
|
||||
pub gate_index: usize,
|
||||
pub constant_index: usize,
|
||||
pub target_index: usize,
|
||||
pub constant: F,
|
||||
}
|
||||
|
||||
impl<F: Field> ConstantGenerator<F> {
|
||||
pub fn new(gate_index: usize, constant_index: usize, target_index: usize, constant: F) -> Self {
|
||||
ConstantGenerator {
|
||||
gate_index,
|
||||
constant_index,
|
||||
target_index,
|
||||
constant,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_constant(&mut self, c: F) {
|
||||
self.constant = c;
|
||||
}
|
||||
}
|
||||
impl<F: Field> SimpleGenerator<F> for ConstantGenerator<F> {
|
||||
fn dependencies(&self) -> Vec<Target> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn run_once(&self, _witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
||||
out_buffer.set_target(
|
||||
Target::wire(self.gate_index, self.target_index),
|
||||
self.constant,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField};
|
||||
use crate::hash::merkle_proofs::MerkleProofTarget;
|
||||
use crate::iop::ext_target::ExtensionTarget;
|
||||
use crate::iop::generator::{
|
||||
CopyGenerator, RandomValueGenerator, SimpleGenerator, WitnessGenerator,
|
||||
ConstantGenerator, CopyGenerator, RandomValueGenerator, SimpleGenerator, WitnessGenerator,
|
||||
};
|
||||
use crate::iop::target::{BoolTarget, Target};
|
||||
use crate::iop::wire::Wire;
|
||||
@ -85,8 +85,8 @@ pub struct CircuitBuilder<F: RichField + Extendable<D>, const D: usize> {
|
||||
/// Map between gate type and the current gate of this type with available slots.
|
||||
current_slots: HashMap<GateRef<F, D>, CurrentSlot<F, D>>,
|
||||
|
||||
/// gate_index, constant_index, target_index
|
||||
constants_slots: Vec<(usize, usize, usize)>,
|
||||
/// gate_index, constant_index, target_index, ConstantGen
|
||||
constants_slots: Vec<ConstantGenerator<F>>,
|
||||
}
|
||||
|
||||
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
@ -224,9 +224,11 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
|
||||
let index = self.gate_instances.len();
|
||||
|
||||
for (ci, ti) in gate_type.extra_constants() {
|
||||
self.constants_slots.push((index, ci, ti));
|
||||
}
|
||||
self.constants_slots
|
||||
.extend(gate_type.extra_constants(index));
|
||||
// for const_gen in gate_type.extra_constants() {
|
||||
// self.constants_slots.push((index, ci, ti, gen));
|
||||
// }
|
||||
|
||||
// Note that we can't immediately add this gate's generators, because the list of constants
|
||||
// could be modified later, i.e. in the case of `ConstantGate`. We will add them later in
|
||||
@ -664,7 +666,6 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
|
||||
// Fill constants
|
||||
while self.constants_to_targets.len() > self.constants_slots.len() {
|
||||
dbg!("lfg");
|
||||
self.add_gate(
|
||||
ConstantGate {
|
||||
num_consts: self.config.num_constants,
|
||||
@ -674,14 +675,19 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
}
|
||||
dbg!(self.constants_to_targets.len(), self.constants_slots.len());
|
||||
|
||||
for ((c, t), (gate_index, const_wire, target_wire)) in self
|
||||
for ((c, t), mut const_gen) in self
|
||||
.constants_to_targets
|
||||
.clone()
|
||||
.into_iter()
|
||||
.zip(self.constants_slots.clone())
|
||||
{
|
||||
self.gate_instances[gate_index].constants[const_wire] = c;
|
||||
self.generate_copy(Target::wire(gate_index, target_wire), t);
|
||||
self.gate_instances[const_gen.gate_index].constants[const_gen.constant_index] = c;
|
||||
self.generate_copy(
|
||||
Target::wire(const_gen.gate_index, const_gen.target_index),
|
||||
t,
|
||||
);
|
||||
const_gen.set_constant(c);
|
||||
self.add_simple_generator(const_gen);
|
||||
}
|
||||
|
||||
info!(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user