mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-11 02:03:07 +00:00
PartialWitness back to HashMap
This commit is contained in:
parent
e81001b9ba
commit
a90ea6ec79
@ -35,7 +35,7 @@ fn bench_prove<F: Field + Extendable<D>, const D: usize>() -> Result<()> {
|
||||
},
|
||||
};
|
||||
|
||||
let inputs = PartialWitness::new(config.num_wires);
|
||||
let inputs = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let zero = builder.zero();
|
||||
|
||||
@ -517,7 +517,7 @@ mod tests {
|
||||
|
||||
let config = CircuitConfig::large_config();
|
||||
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let vs = FF::rand_vec(3);
|
||||
@ -552,7 +552,7 @@ mod tests {
|
||||
|
||||
let config = CircuitConfig::large_zk_config();
|
||||
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let x = FF::rand();
|
||||
@ -580,7 +580,7 @@ mod tests {
|
||||
|
||||
let config = CircuitConfig::large_config();
|
||||
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let x = FF::rand_vec(4);
|
||||
|
||||
@ -64,7 +64,7 @@ mod tests {
|
||||
type FF = QuarticCrandallField;
|
||||
let len = 1 << len_log;
|
||||
let config = CircuitConfig::large_config();
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
let v = (0..len - 1)
|
||||
.map(|_| builder.constant_extension(FF::rand()))
|
||||
|
||||
@ -49,7 +49,7 @@ mod tests {
|
||||
type F = CrandallField;
|
||||
type FF = QuarticCrandallField;
|
||||
let config = CircuitConfig::large_config();
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
|
||||
let len = 4;
|
||||
|
||||
@ -66,7 +66,7 @@ mod tests {
|
||||
type FF = QuarticCrandallField;
|
||||
let len = 1 << len_log;
|
||||
let config = CircuitConfig::large_config();
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
let vec = FF::rand_vec(len);
|
||||
let v: Vec<_> = vec.iter().map(|x| builder.constant_extension(*x)).collect();
|
||||
|
||||
@ -53,7 +53,7 @@ mod tests {
|
||||
type F = CrandallField;
|
||||
type FF = QuarticCrandallField;
|
||||
let config = CircuitConfig::large_config();
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
|
||||
let (x, y) = (FF::rand(), FF::rand());
|
||||
|
||||
@ -101,7 +101,7 @@ mod tests {
|
||||
fn test_split_base() -> Result<()> {
|
||||
type F = CrandallField;
|
||||
let config = CircuitConfig::large_config();
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
let x = F::from_canonical_usize(0b110100000); // 416 = 1532 in base 6.
|
||||
let xt = builder.constant(x);
|
||||
@ -127,7 +127,7 @@ mod tests {
|
||||
fn test_base_sum() -> Result<()> {
|
||||
type F = CrandallField;
|
||||
let config = CircuitConfig::large_config();
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
|
||||
let n = thread_rng().gen_range(0..(1 << 10));
|
||||
|
||||
@ -124,7 +124,7 @@ pub(crate) fn test_eval_fns<F: Extendable<D>, G: Gate<F, D>, const D: usize>(
|
||||
let constants = F::Extension::rand_vec(gate.num_constants());
|
||||
|
||||
let config = CircuitConfig::large_config();
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let wires_t = builder.add_virtual_extension_targets(wires.len());
|
||||
|
||||
@ -234,7 +234,7 @@ mod tests {
|
||||
fn test_recursive_merkle_proof() -> Result<()> {
|
||||
type F = CrandallField;
|
||||
let config = CircuitConfig::large_config();
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||
|
||||
let log_n = 8;
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::field::extension_field::target::ExtensionTarget;
|
||||
@ -154,62 +155,33 @@ impl<F: Field> MatrixWitness<F> {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PartialWitness<F: Field> {
|
||||
pub(crate) wire_values: Vec<Vec<Option<F>>>,
|
||||
pub(crate) virtual_target_values: Vec<Option<F>>,
|
||||
pub(crate) set_targets: Vec<(Target, F)>,
|
||||
// pub(crate) wire_values: Vec<Vec<Option<F>>>,
|
||||
// pub(crate) virtual_target_values: Vec<Option<F>>,
|
||||
pub(crate) target_values: HashMap<Target, F>,
|
||||
}
|
||||
|
||||
impl<F: Field> PartialWitness<F> {
|
||||
pub fn new(num_wires: usize) -> Self {
|
||||
pub fn new() -> Self {
|
||||
PartialWitness {
|
||||
wire_values: vec![vec![None; num_wires]],
|
||||
virtual_target_values: vec![],
|
||||
set_targets: vec![],
|
||||
target_values: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> Witness<F> for PartialWitness<F> {
|
||||
fn try_get_target(&self, target: Target) -> Option<F> {
|
||||
match target {
|
||||
Target::Wire(Wire { gate, input }) => *self.wire_values.get(gate)?.get(input)?,
|
||||
Target::VirtualTarget { index } => *self.virtual_target_values.get(index)?,
|
||||
}
|
||||
self.target_values.get(&target).copied()
|
||||
}
|
||||
|
||||
fn set_target(&mut self, target: Target, value: F) {
|
||||
match target {
|
||||
Target::Wire(Wire { gate, input }) => {
|
||||
if gate >= self.wire_values.len() {
|
||||
self.wire_values
|
||||
.resize(gate + 1, vec![None; self.wire_values[0].len()]);
|
||||
}
|
||||
if let Some(old_value) = self.wire_values[gate][input] {
|
||||
assert_eq!(
|
||||
old_value, value,
|
||||
"Target was set twice with different values: {:?}",
|
||||
target
|
||||
);
|
||||
} else {
|
||||
self.wire_values[gate][input] = Some(value);
|
||||
}
|
||||
}
|
||||
Target::VirtualTarget { index } => {
|
||||
if index >= self.virtual_target_values.len() {
|
||||
self.virtual_target_values.resize(index + 1, None);
|
||||
}
|
||||
if let Some(old_value) = self.virtual_target_values[index] {
|
||||
assert_eq!(
|
||||
old_value, value,
|
||||
"Target was set twice with different values: {:?}",
|
||||
target
|
||||
);
|
||||
} else {
|
||||
self.virtual_target_values[index] = Some(value);
|
||||
}
|
||||
}
|
||||
let opt_old_value = self.target_values.insert(target, value);
|
||||
if let Some(old_value) = opt_old_value {
|
||||
assert_eq!(
|
||||
old_value, value,
|
||||
"Target {:?} was set twice with different values",
|
||||
target
|
||||
);
|
||||
}
|
||||
self.set_targets.push((target, value));
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,19 +213,13 @@ impl<F: Field> PartitionWitness<F> {
|
||||
}
|
||||
|
||||
pub fn full_witness(self) -> MatrixWitness<F> {
|
||||
let mut wire_values = vec![vec![]; self.num_wires];
|
||||
for j in 0..self.num_wires {
|
||||
wire_values[j].reserve_exact(self.degree);
|
||||
unsafe {
|
||||
// After .reserve_exact(l), wire_values[i] will have capacity at least l. Hence, set_len
|
||||
// will not cause the buffer to overrun.
|
||||
wire_values[j].set_len(self.degree);
|
||||
}
|
||||
}
|
||||
let mut wire_values = vec![vec![F::ZERO; self.degree]; self.num_wires];
|
||||
for i in 0..self.degree {
|
||||
for j in 0..self.num_wires {
|
||||
let t = Target::Wire(Wire { gate: i, input: j });
|
||||
wire_values[j][i] = self.try_get_target(t).unwrap_or(F::ZERO);
|
||||
if let Some(x) = self.try_get_target(t) {
|
||||
wire_values[j][i] = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -12,7 +12,6 @@ use crate::hash::merkle_tree::MerkleCap;
|
||||
use crate::iop::generator::WitnessGenerator;
|
||||
use crate::iop::target::Target;
|
||||
use crate::iop::witness::{PartialWitness, PartitionWitness};
|
||||
use crate::plonk::copy_constraint::CopyConstraint;
|
||||
use crate::plonk::proof::ProofWithPublicInputs;
|
||||
use crate::plonk::prover::prove;
|
||||
use crate::plonk::verifier::verify;
|
||||
|
||||
@ -36,7 +36,7 @@ pub(crate) fn prove<F: Extendable<D>, const D: usize>(
|
||||
timed!(
|
||||
timing,
|
||||
"fill partition",
|
||||
for &(t, v) in &inputs.set_targets {
|
||||
for (t, v) in inputs.target_values.into_iter() {
|
||||
partition_witness.set_target(t, v);
|
||||
}
|
||||
);
|
||||
|
||||
@ -386,7 +386,7 @@ mod tests {
|
||||
}
|
||||
let data = builder.build();
|
||||
(
|
||||
data.prove(PartialWitness::new(config.num_wires))?,
|
||||
data.prove(PartialWitness::new())?,
|
||||
data.verifier_only,
|
||||
data.common,
|
||||
)
|
||||
@ -394,7 +394,7 @@ mod tests {
|
||||
verify(proof_with_pis.clone(), &vd, &cd)?;
|
||||
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let pt = proof_to_proof_target(&proof_with_pis, &mut builder);
|
||||
set_proof_target(&proof_with_pis, &pt, &mut pw);
|
||||
|
||||
@ -442,7 +442,7 @@ mod tests {
|
||||
}
|
||||
let data = builder.build();
|
||||
(
|
||||
data.prove(PartialWitness::new(config.num_wires))?,
|
||||
data.prove(PartialWitness::new())?,
|
||||
data.verifier_only,
|
||||
data.common,
|
||||
)
|
||||
@ -450,7 +450,7 @@ mod tests {
|
||||
verify(proof_with_pis.clone(), &vd, &cd)?;
|
||||
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let pt = proof_to_proof_target(&proof_with_pis, &mut builder);
|
||||
set_proof_target(&proof_with_pis, &pt, &mut pw);
|
||||
|
||||
@ -468,7 +468,7 @@ mod tests {
|
||||
|
||||
verify(proof_with_pis.clone(), &vd, &cd)?;
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
|
||||
let mut pw = PartialWitness::new(config.num_wires);
|
||||
let mut pw = PartialWitness::new();
|
||||
let pt = proof_to_proof_target(&proof_with_pis, &mut builder);
|
||||
set_proof_target(&proof_with_pis, &pt, &mut pw);
|
||||
|
||||
|
||||
@ -220,7 +220,7 @@ mod tests {
|
||||
|
||||
let config = CircuitConfig::large_config();
|
||||
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let alpha = FF::rand();
|
||||
@ -248,7 +248,7 @@ mod tests {
|
||||
|
||||
let config = CircuitConfig::large_config();
|
||||
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let pw = PartialWitness::new();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let alpha = FF::rand();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user