Rewrite Markable to avoid Arcs

This commit is contained in:
wborgeaud 2021-07-15 09:52:42 +02:00
parent 0ba5916346
commit 7dec6efc6c
9 changed files with 48 additions and 122 deletions

View File

@ -1,6 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::sync::Arc;
use std::time::Instant;
use log::info;
@ -49,7 +48,7 @@ pub struct CircuitBuilder<F: Extendable<D>, const D: usize> {
context: String,
pub marked_targets: Vec<MarkedTargets>,
pub marked_targets: Vec<MarkedTargets<D>>,
/// Generators used to generate the witness.
pub generators: Vec<Box<dyn WitnessGenerator<F>>>,
@ -308,7 +307,7 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
self.context = new_context.to_string();
}
pub fn add_marked(&mut self, targets: Arc<dyn Markable>, name: &str) {
pub fn add_marked(&mut self, targets: Markable<D>, name: &str) {
self.marked_targets.push(MarkedTargets {
targets,
name: name.to_string(),

View File

@ -131,7 +131,7 @@ pub(crate) struct ProverOnlyCircuitData<F: Extendable<D>, const D: usize> {
pub copy_constraints: Vec<CopyConstraint>,
/// The concrete placement of each gate in the circuit.
pub gate_instances: Vec<GateInstance<F, D>>,
pub marked_targets: Vec<MarkedTargets>,
pub marked_targets: Vec<MarkedTargets<D>>,
}
/// Circuit data required by the verifier, but not the prover.

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use env_logger::builder;
use itertools::izip;

View File

@ -1,7 +1,5 @@
//! Concrete instantiation of a hash function.
use std::sync::Arc;
use crate::circuit_builder::CircuitBuilder;
use crate::field::extension_field::Extendable;
use crate::field::field::Field;

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use anyhow::{ensure, Result};
use crate::circuit_builder::CircuitBuilder;
@ -10,7 +8,6 @@ use crate::hash::GMIMC_ROUNDS;
use crate::hash::{compress, hash_or_noop};
use crate::proof::{Hash, HashTarget};
use crate::target::Target;
use crate::util::marking::MarkedTargets;
use crate::wire::Wire;
#[derive(Clone, Debug)]
@ -161,7 +158,6 @@ mod tests {
use crate::field::extension_field::quartic::QuarticCrandallField;
use crate::merkle_proofs::verify_merkle_proof;
use crate::merkle_tree::MerkleTree;
use crate::util::marking::MarkedTargets;
use crate::verifier::verify;
use crate::witness::PartialWitness;
@ -208,64 +204,4 @@ mod tests {
verify(proof, &data.verifier_only, &data.common)
}
#[test]
fn test_recursive_merkle_proof_yo() -> Result<()> {
type F = CrandallField;
type FF = QuarticCrandallField;
let config = CircuitConfig::large_config();
let mut builder = CircuitBuilder::<F, 4>::new(config);
let mut pw = PartialWitness::new();
let eval = vec![
8165005271518921330,
6083226207459673392,
9958534500108693972,
3430614617054831715,
14276647488823198467,
11751680815846448477,
2771303161388554632,
2371046485289351947,
16743918419162514074,
9932615810638040318,
16314448410395528119,
1511019414432045441,
5645123553081661379,
9778873694114674382,
10629301051878288289,
16655634835422730769,
7474748727207643713,
8501202586470516512,
5612524789765317534,
3026252715636633329,
15131263578183166645,
1869341605741303173,
14645831398335944979,
8356334351657818532,
4888183615701827634,
5994174007215505657,
11524125964783895772,
2202081323880269694,
9827048951184368953,
12675978139336549297,
5868550852792001156,
];
let eval = eval
.into_iter()
.map(F::from_canonical_usize)
.collect::<Vec<_>>();
let data = builder.add_virtual_targets(eval.len());
for j in 0..data.len() {
pw.set_target(data[j], eval[j]);
}
dbg!(hash_or_noop(eval.clone()));
let hash = builder.hash_or_noop(data.clone());
builder.add_marked(Arc::new(hash), "hash test");
let data = builder.build();
let proof = data.prove(pw);
verify(proof, &data.verifier_only, &data.common)
}
}

View File

@ -1,7 +1,3 @@
use std::sync::Arc;
use env_logger::builder;
use crate::circuit_builder::CircuitBuilder;
use crate::circuit_data::{CircuitConfig, CommonCircuitData, VerifierCircuitTarget};
use crate::field::extension_field::Extendable;
@ -9,7 +5,6 @@ use crate::field::field::Field;
use crate::gates::gate::{GateRef, PrefixedGate};
use crate::plonk_challenger::RecursiveChallenger;
use crate::proof::{HashTarget, ProofTarget};
use crate::util::marking::MarkedTargets;
use crate::util::scaling::ReducingFactorTarget;
use crate::vanishing_poly::eval_vanishing_poly_recursively;
use crate::vars::EvaluationTargets;
@ -25,7 +20,6 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
inner_config: &CircuitConfig,
inner_verifier_data: &VerifierCircuitTarget,
inner_common_data: &CommonCircuitData<F, D>,
marked: &mut Vec<MarkedTargets>,
) {
assert!(self.config.num_wires >= MIN_WIRES);
assert!(self.config.num_wires >= MIN_ROUTED_WIRES);
@ -76,14 +70,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
&betas,
&gammas,
&alphas,
marked,
);
marked.push(MarkedTargets {
name: "vanishing polys".into(),
targets: Arc::new(vanishing_polys_zeta[0].clone()),
});
self.set_context("Check vanishing and quotient polynomials.");
let quotient_polys_zeta = &proof.openings.quotient_polys;
let zeta_pow_deg = self.exp_u64_extension(zeta, 1 << inner_common_data.degree_bits as u64);
@ -364,7 +352,6 @@ mod tests {
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
let mut pw = PartialWitness::new();
let mut marked = Vec::new();
let pt = proof_to_proof_target(&proof, &mut builder);
set_proof_target(&proof, &pt, &mut pw);
@ -373,7 +360,7 @@ mod tests {
};
pw.set_hash_target(inner_data.constants_sigmas_root, vd.constants_sigmas_root);
builder.add_recursive_verifier(pt, &config, &inner_data, &cd, &mut marked);
builder.add_recursive_verifier(pt, &config, &inner_data, &cd);
let data = builder.build();
let recursive_proof = data.prove(pw);

View File

@ -1,53 +1,62 @@
use std::convert::TryInto;
use std::sync::Arc;
use crate::field::extension_field::target::ExtensionTarget;
use crate::field::extension_field::Extendable;
use crate::field::field::Field;
use crate::proof::HashTarget;
use crate::target::Target;
use crate::witness::{PartialWitness, Witness};
pub trait Markable: 'static + Send + Sync {
fn targets(&self) -> Vec<Target>;
#[derive(Clone)]
pub enum Markable<const D: usize> {
Target(Target),
ExtensionTarget(ExtensionTarget<D>),
HashTarget(HashTarget),
Vec(Vec<Markable<D>>),
}
impl Markable for Target {
fn targets(&self) -> Vec<Target> {
vec![*self]
impl<const D: usize> From<Target> for Markable<D> {
fn from(t: Target) -> Self {
Self::Target(t)
}
}
impl<const D: usize> From<ExtensionTarget<D>> for Markable<D> {
fn from(et: ExtensionTarget<D>) -> Self {
Self::ExtensionTarget(et)
}
}
impl<const D: usize> From<HashTarget> for Markable<D> {
fn from(ht: HashTarget) -> Self {
Self::HashTarget(ht)
}
}
impl<M: Into<Markable<D>>, const D: usize> From<Vec<M>> for Markable<D> {
fn from(v: Vec<M>) -> Self {
Self::Vec(v.into_iter().map(|m| m.into()).collect())
}
}
impl<const D: usize> Markable for ExtensionTarget<D> {
fn targets(&self) -> Vec<Target> {
self.0.try_into().unwrap()
}
}
impl Markable for HashTarget {
fn targets(&self) -> Vec<Target> {
self.elements.try_into().unwrap()
}
}
impl<M: Markable> Markable for Vec<M> {
fn targets(&self) -> Vec<Target> {
self.iter().flat_map(|m| m.targets()).collect()
impl<const D: usize> Markable<D> {
fn print_markable<F: Extendable<D>>(&self, pw: &PartialWitness<F>) {
match self {
Markable::Target(t) => println!("{}", pw.get_target(*t)),
Markable::ExtensionTarget(et) => println!("{}", pw.get_extension_target(*et)),
Markable::HashTarget(ht) => println!("{:?}", pw.get_hash_target(*ht)),
Markable::Vec(v) => v.iter().for_each(|m| m.print_markable(pw)),
}
}
}
#[derive(Clone)]
pub struct MarkedTargets {
pub targets: Arc<dyn Markable>,
pub struct MarkedTargets<const D: usize> {
pub targets: Markable<D>,
pub name: String,
}
impl MarkedTargets {
pub fn display<F: Field>(&self, pw: &PartialWitness<F>) {
let targets = self.targets.targets();
impl<const D: usize> MarkedTargets<D> {
pub fn display<F: Extendable<D>>(&self, pw: &PartialWitness<F>) {
println!("Values for {}:", self.name);
for &t in &targets {
println!("{}", pw.get_target(t));
}
self.targets.print_markable(pw);
println!("End of values for {}", self.name);
}
}

View File

@ -335,7 +335,6 @@ pub fn evaluate_gate_constraints_recursively<F: Extendable<D>, const D: usize>(
gates: &[PrefixedGate<F, D>],
num_gate_constraints: usize,
vars: EvaluationTargets<D>,
marked: &mut Vec<MarkedTargets>,
) -> Vec<ExtensionTarget<D>> {
let mut constraints = vec![builder.zero_extension(); num_gate_constraints];
for gate in gates {
@ -343,10 +342,6 @@ pub fn evaluate_gate_constraints_recursively<F: Extendable<D>, const D: usize>(
.gate
.0
.eval_filtered_recursively(builder, vars, &gate.prefix);
// marked.push(MarkedTargets {
// name: gate.gate.0.id(),
// targets: Box::new(gate_constraints.clone()),
// });
for (i, c) in gate_constraints.into_iter().enumerate() {
constraints[i] = builder.add_extension(constraints[i], c);
}
@ -370,7 +365,6 @@ pub(crate) fn eval_vanishing_poly_recursively<F: Extendable<D>, const D: usize>(
betas: &[Target],
gammas: &[Target],
alphas: &[Target],
marked: &mut Vec<MarkedTargets>,
) -> Vec<ExtensionTarget<D>> {
let max_degree = common_data.quotient_degree_factor;
let (num_prods, final_num_prod) = common_data.num_partial_products;
@ -380,7 +374,6 @@ pub(crate) fn eval_vanishing_poly_recursively<F: Extendable<D>, const D: usize>(
&common_data.gates,
common_data.num_gate_constraints,
vars,
marked,
);
// The L_1(x) (Z(x) - 1) vanishing terms.

View File

@ -78,6 +78,12 @@ impl<F: Field> PartialWitness<F> {
)
}
pub fn get_hash_target(&self, ht: HashTarget) -> Hash<F> {
Hash {
elements: self.get_targets(&ht.elements).try_into().unwrap(),
}
}
pub fn try_get_target(&self, target: Target) -> Option<F> {
self.target_values.get(&target).cloned()
}