use crate::field::field::Field; use crate::target::Target; use crate::witness::PartialWitness; /// A generator participates in the generation of the witness. pub trait WitnessGenerator2: 'static { /// Targets to be "watched" by this generator. Whenever a target in the watch list is populated, /// the generator will be queued to run. fn watch_list(&self) -> Vec; /// 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. fn run(&mut self, witness: &PartialWitness) -> (PartialWitness, bool); } /// A generator which runs once after a list of dependencies is present in the witness. pub trait SimpleGenerator: 'static { fn dependencies(&self) -> Vec; fn run_once(&mut self, witness: &PartialWitness) -> PartialWitness; } impl> WitnessGenerator2 for SG { fn watch_list(&self) -> Vec { self.dependencies() } fn run(&mut self, witness: &PartialWitness) -> (PartialWitness, bool) { if witness.contains_all(&self.dependencies()) { (self.run_once(witness), true) } else { (PartialWitness::new(), false) } } } /// A generator which copies one wire to another. pub(crate) struct CopyGenerator { pub(crate) src: Target, pub(crate) dst: Target, } impl SimpleGenerator for CopyGenerator { fn dependencies(&self) -> Vec { vec![self.src] } fn run_once(&mut self, witness: &PartialWitness) -> PartialWitness { let value = witness.get_target(self.src); PartialWitness::singleton(self.dst, value) } }