midnight_circuits/verifier/
mod.rs1use std::collections::BTreeMap;
17
18use group::Group;
19use midnight_proofs::{
20 circuit::Value,
21 plonk,
22 plonk::ConstraintSystem,
23 poly::{kzg::KZGCommitmentScheme, EvaluationDomain},
24};
25
26use crate::{
27 field::AssignedNative,
28 types::{InnerValue, Instantiable},
29};
30
31mod accumulator;
32mod expressions;
33mod kzg;
34mod lookup;
35mod msm;
36mod permutation;
37mod traces;
38mod transcript_gadget;
39mod trash;
40mod types;
41mod utils;
42mod vanishing;
43mod verifier_gadget;
44
45pub use accumulator::{Accumulator, AssignedAccumulator};
46pub use msm::{AssignedMsm, Msm};
47#[cfg(feature = "dev-curves")]
48pub use types::BnEmulation;
49pub use types::{BlstrsEmulation, SelfEmulation};
50pub use verifier_gadget::VerifierGadget;
51
52type VerifyingKey<S> =
53 plonk::VerifyingKey<<S as SelfEmulation>::F, KZGCommitmentScheme<<S as SelfEmulation>::Engine>>;
54
55#[derive(Clone, Debug)]
66pub struct AssignedVk<S: SelfEmulation> {
67 vk_name: String,
68 domain: EvaluationDomain<S::F>,
69 cs: ConstraintSystem<S::F>,
70 transcript_repr: AssignedNative<S::F>,
71}
72
73impl<S: SelfEmulation> InnerValue for AssignedVk<S> {
74 type Element = VerifyingKey<S>;
75
76 fn value(&self) -> Value<VerifyingKey<S>> {
77 unimplemented!(
78 "It is not possible to get a full verifying key out of an
79 AssignedVk, as the latter does not include fixed commitments."
80 )
81 }
82}
83
84impl<S: SelfEmulation> Instantiable<S::F> for AssignedVk<S> {
85 fn as_public_input(vk: &VerifyingKey<S>) -> Vec<S::F> {
86 AssignedNative::<S::F>::as_public_input(&vk.transcript_repr())
87 }
88
89 fn from_public_input(_fields: &[S::F]) -> Option<VerifyingKey<S>> {
90 unimplemented!("as_public_input encodes the VK as its transcript_repr() — not invertible")
91 }
92}
93
94pub fn fixed_commitment_name(prefix: &str, i: usize) -> String {
96 format!("{prefix}_fixed_com_{i}")
97}
98
99pub fn perm_commitment_name(prefix: &str, i: usize) -> String {
101 format!("{prefix}_perm_com_{i}")
102}
103
104impl<S: SelfEmulation> AssignedVk<S> {
105 fn fixed_commitment_name(&self, i: usize) -> String {
107 fixed_commitment_name(&self.vk_name, i)
108 }
109
110 fn perm_commitment_name(&self, i: usize) -> String {
112 perm_commitment_name(&self.vk_name, i)
113 }
114}
115
116pub fn fixed_bases<S: SelfEmulation>(
119 vk_name: &str,
120 vk: &VerifyingKey<S>,
121) -> BTreeMap<String, S::C> {
122 let mut fixed_bases = BTreeMap::new();
123
124 fixed_bases.insert(String::from("-G"), -S::C::generator());
125
126 let fixed_commitments = vk.fixed_commitments();
127 let perm_commitments = vk.permutation().commitments();
128
129 for (i, com) in fixed_commitments.iter().enumerate() {
130 fixed_bases.insert(fixed_commitment_name(vk_name, i), *com);
131 }
132
133 for (i, com) in perm_commitments.iter().enumerate() {
134 fixed_bases.insert(perm_commitment_name(vk_name, i), *com);
135 }
136
137 fixed_bases
138}
139
140pub fn fixed_base_names<S: SelfEmulation>(
144 vk_name: &str,
145 nb_fixed_commitments: usize,
146 nb_perm_commitments: usize,
147) -> Vec<String> {
148 let mut names = Vec::with_capacity(nb_fixed_commitments + nb_perm_commitments + 1);
149
150 names.push("-G".into());
155
156 for i in 0..nb_fixed_commitments {
157 names.push(fixed_commitment_name(vk_name, i));
158 }
159
160 for i in 0..nb_perm_commitments {
161 names.push(perm_commitment_name(vk_name, i));
162 }
163
164 names
165}