use std::collections::BTreeMap;
use group::Group;
use midnight_proofs::{
circuit::Value,
plonk,
plonk::ConstraintSystem,
poly::{kzg::KZGCommitmentScheme, EvaluationDomain},
};
use crate::{
field::AssignedNative,
types::{InnerValue, Instantiable},
};
mod accumulator;
mod expressions;
mod kzg;
mod lookup;
mod msm;
mod permutation;
mod traces;
mod transcript_gadget;
mod trash;
mod types;
mod utils;
mod vanishing;
mod verifier_gadget;
pub use accumulator::{Accumulator, AssignedAccumulator};
pub use msm::{AssignedMsm, Msm};
#[cfg(feature = "dev-curves")]
pub use types::BnEmulation;
pub use types::{BlstrsEmulation, SelfEmulation};
pub use verifier_gadget::VerifierGadget;
type VerifyingKey<S> =
plonk::VerifyingKey<<S as SelfEmulation>::F, KZGCommitmentScheme<<S as SelfEmulation>::Engine>>;
#[derive(Clone, Debug)]
pub struct AssignedVk<S: SelfEmulation> {
vk_name: String,
domain: EvaluationDomain<S::F>,
cs: ConstraintSystem<S::F>,
transcript_repr: AssignedNative<S::F>,
}
impl<S: SelfEmulation> InnerValue for AssignedVk<S> {
type Element = VerifyingKey<S>;
fn value(&self) -> Value<VerifyingKey<S>> {
unimplemented!(
"It is not possible to get a full verifying key out of an
AssignedVk, as the latter does not include fixed commitments."
)
}
}
impl<S: SelfEmulation> Instantiable<S::F> for AssignedVk<S> {
fn as_public_input(vk: &VerifyingKey<S>) -> Vec<S::F> {
AssignedNative::<S::F>::as_public_input(&vk.transcript_repr())
}
}
pub fn fixed_commitment_name(prefix: &str, i: usize) -> String {
format!("{prefix}_fixed_com_{i}")
}
pub fn perm_commitment_name(prefix: &str, i: usize) -> String {
format!("{prefix}_perm_com_{i}")
}
impl<S: SelfEmulation> AssignedVk<S> {
fn fixed_commitment_name(&self, i: usize) -> String {
fixed_commitment_name(&self.vk_name, i)
}
fn perm_commitment_name(&self, i: usize) -> String {
perm_commitment_name(&self.vk_name, i)
}
}
pub fn fixed_bases<S: SelfEmulation>(
vk_name: &str,
vk: &VerifyingKey<S>,
) -> BTreeMap<String, S::C> {
let mut fixed_bases = BTreeMap::new();
fixed_bases.insert(String::from("-G"), -S::C::generator());
let fixed_commitments = vk.fixed_commitments();
let perm_commitments = vk.permutation().commitments();
for (i, com) in fixed_commitments.iter().enumerate() {
fixed_bases.insert(fixed_commitment_name(vk_name, i), *com);
}
for (i, com) in perm_commitments.iter().enumerate() {
fixed_bases.insert(perm_commitment_name(vk_name, i), *com);
}
fixed_bases
}
pub fn fixed_base_names<S: SelfEmulation>(
vk_name: &str,
nb_fixed_commitments: usize,
nb_perm_commitments: usize,
) -> Vec<String> {
let mut names = Vec::with_capacity(nb_fixed_commitments + nb_perm_commitments + 1);
names.push("-G".into());
for i in 0..nb_fixed_commitments {
names.push(fixed_commitment_name(vk_name, i));
}
for i in 0..nb_perm_commitments {
names.push(perm_commitment_name(vk_name, i));
}
names
}