2021-02-09 21:25:21 -08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use crate::field::field::Field;
|
2021-02-26 13:18:41 -08:00
|
|
|
use crate::target::Target;
|
2021-02-09 21:25:21 -08:00
|
|
|
use crate::wire::Wire;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-02-24 12:25:13 -08:00
|
|
|
pub struct PartialWitness<F: Field> {
|
2021-02-26 13:18:41 -08:00
|
|
|
target_values: HashMap<Target, F>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-02-24 12:25:13 -08:00
|
|
|
impl<F: Field> PartialWitness<F> {
|
2021-02-09 21:25:21 -08:00
|
|
|
pub fn new() -> Self {
|
2021-02-24 12:25:13 -08:00
|
|
|
PartialWitness {
|
2021-02-09 21:25:21 -08:00
|
|
|
target_values: HashMap::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
pub fn singleton(target: Target, value: F) -> Self {
|
2021-02-24 12:25:13 -08:00
|
|
|
let mut witness = PartialWitness::new();
|
2021-02-09 21:25:21 -08:00
|
|
|
witness.set_target(target, value);
|
|
|
|
|
witness
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.target_values.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
pub fn get_target(&self, target: Target) -> F {
|
2021-02-09 21:25:21 -08:00
|
|
|
self.target_values[&target]
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
pub fn try_get_target(&self, target: Target) -> Option<F> {
|
2021-02-09 21:25:21 -08:00
|
|
|
self.target_values.get(&target).cloned()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_wire(&self, wire: Wire) -> F {
|
2021-02-26 13:18:41 -08:00
|
|
|
self.get_target(Target::Wire(wire))
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
pub fn contains(&self, target: Target) -> bool {
|
2021-02-09 21:25:21 -08:00
|
|
|
self.target_values.contains_key(&target)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
pub fn contains_all(&self, targets: &[Target]) -> bool {
|
2021-02-09 21:25:21 -08:00
|
|
|
targets.iter().all(|&t| self.contains(t))
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
pub fn set_target(&mut self, target: Target, value: F) {
|
2021-02-09 21:25:21 -08:00
|
|
|
self.target_values.insert(target, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_wire(&mut self, wire: Wire, value: F) {
|
2021-02-26 13:18:41 -08:00
|
|
|
self.set_target(Target::Wire(wire), value)
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
}
|