plonky2/src/generator.rs

55 lines
1.9 KiB
Rust
Raw Normal View History

2021-02-09 21:25:21 -08:00
use crate::field::field::Field;
2021-02-26 13:18:41 -08:00
use crate::target::Target;
2021-02-24 12:25:13 -08:00
use crate::witness::PartialWitness;
2021-02-09 21:25:21 -08:00
/// A generator participates in the generation of the witness.
pub trait WitnessGenerator2<F: Field>: 'static {
/// Targets to be "watched" by this generator. Whenever a target in the watch list is populated,
/// the generator will be queued to run.
2021-02-26 13:18:41 -08:00
fn watch_list(&self) -> Vec<Target>;
2021-02-09 21:25:21 -08:00
/// Run this generator, returning a `PartialWitness` containing any new witness elements, and a
/// flag indicating whether the generator is finished. If the flag is true, the generator will
/// never be run again, otherwise it will be queued for another run next time a target in its
/// watch list is populated.
2021-02-24 12:25:13 -08:00
fn run(&mut self, witness: &PartialWitness<F>) -> (PartialWitness<F>, bool);
2021-02-09 21:25:21 -08:00
}
/// A generator which runs once after a list of dependencies is present in the witness.
pub trait SimpleGenerator<F: Field>: 'static {
2021-02-26 13:18:41 -08:00
fn dependencies(&self) -> Vec<Target>;
2021-02-09 21:25:21 -08:00
2021-02-24 12:25:13 -08:00
fn run_once(&mut self, witness: &PartialWitness<F>) -> PartialWitness<F>;
2021-02-09 21:25:21 -08:00
}
impl<F: Field, SG: SimpleGenerator<F>> WitnessGenerator2<F> for SG {
2021-02-26 13:18:41 -08:00
fn watch_list(&self) -> Vec<Target> {
2021-02-09 21:25:21 -08:00
self.dependencies()
}
2021-02-24 12:25:13 -08:00
fn run(&mut self, witness: &PartialWitness<F>) -> (PartialWitness<F>, bool) {
2021-02-09 21:25:21 -08:00
if witness.contains_all(&self.dependencies()) {
(self.run_once(witness), true)
} else {
2021-02-24 12:25:13 -08:00
(PartialWitness::new(), false)
2021-02-09 21:25:21 -08:00
}
}
}
/// A generator which copies one wire to another.
pub(crate) struct CopyGenerator {
2021-02-26 13:18:41 -08:00
pub(crate) src: Target,
pub(crate) dst: Target,
2021-02-09 21:25:21 -08:00
}
impl<F: Field> SimpleGenerator<F> for CopyGenerator {
2021-02-26 13:18:41 -08:00
fn dependencies(&self) -> Vec<Target> {
2021-02-09 21:25:21 -08:00
vec![self.src]
}
2021-02-24 12:25:13 -08:00
fn run_once(&mut self, witness: &PartialWitness<F>) -> PartialWitness<F> {
2021-02-09 21:25:21 -08:00
let value = witness.get_target(self.src);
2021-02-24 12:25:13 -08:00
PartialWitness::singleton(self.dst, value)
2021-02-09 21:25:21 -08:00
}
}