Skip to content

Commit

Permalink
Merge pull request #2014 from AleoHQ/rename_MM
Browse files Browse the repository at this point in the history
Rename MM to SM, from MarlinMode to SnarkMode
  • Loading branch information
howardwu authored Sep 25, 2023
2 parents ac027cd + 9efae9d commit fa509f0
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 145 deletions.
14 changes: 7 additions & 7 deletions algorithms/src/snark/varuna/ahp/ahp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ use std::collections::BTreeMap;
/// The algebraic holographic proof defined in [CHMMVW19](https://eprint.iacr.org/2019/1047).
/// Currently, this AHP only supports inputs of size one
/// less than a power of 2 (i.e., of the form 2^n - 1).
pub struct AHPForR1CS<F: Field, MM: SNARKMode> {
pub struct AHPForR1CS<F: Field, SM: SNARKMode> {
field: PhantomData<F>,
mode: PhantomData<MM>,
mode: PhantomData<SM>,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -57,13 +57,13 @@ pub(crate) struct NonZeroDomains<F: PrimeField> {
pub(crate) domain_c: EvaluationDomain<F>,
}

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// The linear combinations that are statically known to evaluate to zero.
/// These correspond to the virtual commitments as noted in the Aleo varuna protocol docs
pub const LC_WITH_ZERO_EVAL: [&'static str; 3] = ["matrix_sumcheck", "lineval_sumcheck", "rowcheck_zerocheck"];

pub fn zk_bound() -> Option<usize> {
MM::ZK.then_some(1)
SM::ZK.then_some(1)
}

/// Check that the (formatted) public input is of the form 2^n for some integer n.
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
Ok(*[
2 * constraint_domain_size + 2 * zk_bound - 2,
2 * variable_domain_size + 2 * zk_bound - 2,
if MM::ZK { variable_domain_size + 3 } else { 0 }, // mask_poly
if SM::ZK { variable_domain_size + 3 } else { 0 }, // mask_poly
variable_domain_size,
constraint_domain_size,
non_zero_domain_size - 1, // non-zero polynomials
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
evals: &E,
prover_third_message: &prover::ThirdMessage<F>,
prover_fourth_message: &prover::FourthMessage<F>,
state: &verifier::State<F, MM>,
state: &verifier::State<F, SM>,
) -> Result<BTreeMap<String, LinearCombination<F>>, AHPError> {
assert!(!public_inputs.is_empty());
let max_constraint_domain = state.max_constraint_domain;
Expand Down Expand Up @@ -286,7 +286,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
// We're now going to calculate the lineval_sumcheck
let lineval_sumcheck = {
let mut lineval_sumcheck = LinearCombination::empty("lineval_sumcheck");
if MM::ZK {
if SM::ZK {
lineval_sumcheck.add(F::one(), "mask_poly");
}
for (i, (id, c)) in batch_combiners.iter().enumerate() {
Expand Down
24 changes: 12 additions & 12 deletions algorithms/src/snark/varuna/ahp/indexer/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl CircuitId {
/// 2) `{a,b,c}` are the matrices defining the R1CS instance
/// 3) `{a,b,c}_arith` are structs containing information about the arithmetized matrices
#[derive(Clone, Debug)]
pub struct Circuit<F: PrimeField, MM: SNARKMode> {
pub struct Circuit<F: PrimeField, SM: SNARKMode> {
/// Information about the indexed circuit.
pub index_info: CircuitInfo,

Expand All @@ -80,30 +80,30 @@ pub struct Circuit<F: PrimeField, MM: SNARKMode> {

pub fft_precomputation: FFTPrecomputation<F>,
pub ifft_precomputation: IFFTPrecomputation<F>,
pub(crate) _mode: PhantomData<MM>,
pub(crate) _mode: PhantomData<SM>,
pub(crate) id: CircuitId,
}

impl<F: PrimeField, MM: SNARKMode> Eq for Circuit<F, MM> {}
impl<F: PrimeField, MM: SNARKMode> PartialEq for Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> Eq for Circuit<F, SM> {}
impl<F: PrimeField, SM: SNARKMode> PartialEq for Circuit<F, SM> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl<F: PrimeField, MM: SNARKMode> Ord for Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> Ord for Circuit<F, SM> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.id.cmp(&other.id)
}
}

impl<F: PrimeField, MM: SNARKMode> PartialOrd for Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> PartialOrd for Circuit<F, SM> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<F: PrimeField, MM: SNARKMode> Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> Circuit<F, SM> {
pub fn hash(
index_info: &CircuitInfo,
a: &Matrix<F>,
Expand All @@ -120,7 +120,7 @@ impl<F: PrimeField, MM: SNARKMode> Circuit<F, MM> {

/// The maximum degree required to represent polynomials of this index.
pub fn max_degree(&self) -> usize {
self.index_info.max_degree::<F, MM>()
self.index_info.max_degree::<F, SM>()
}

/// The size of the constraint domain in this R1CS instance.
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<F: PrimeField, MM: SNARKMode> Circuit<F, MM> {
}
}

impl<F: PrimeField, MM: SNARKMode> CanonicalSerialize for Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> CanonicalSerialize for Circuit<F, SM> {
fn serialize_with_mode<W: Write>(&self, mut writer: W, compress: Compress) -> Result<(), SerializationError> {
self.index_info.serialize_with_mode(&mut writer, compress)?;
self.a.serialize_with_mode(&mut writer, compress)?;
Expand All @@ -176,7 +176,7 @@ impl<F: PrimeField, MM: SNARKMode> CanonicalSerialize for Circuit<F, MM> {
}
}

impl<F: PrimeField, MM: SNARKMode> snarkvm_utilities::Valid for Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> snarkvm_utilities::Valid for Circuit<F, SM> {
fn check(&self) -> Result<(), SerializationError> {
Ok(())
}
Expand All @@ -186,7 +186,7 @@ impl<F: PrimeField, MM: SNARKMode> snarkvm_utilities::Valid for Circuit<F, MM> {
}
}

impl<F: PrimeField, MM: SNARKMode> CanonicalDeserialize for Circuit<F, MM> {
impl<F: PrimeField, SM: SNARKMode> CanonicalDeserialize for Circuit<F, SM> {
fn deserialize_with_mode<R: Read>(
mut reader: R,
compress: Compress,
Expand All @@ -204,7 +204,7 @@ impl<F: PrimeField, MM: SNARKMode> CanonicalDeserialize for Circuit<F, MM> {
let non_zero_c_domain_size = EvaluationDomain::<F>::compute_size_of_domain(index_info.num_non_zero_c)
.ok_or(SerializationError::InvalidData)?;

let (fft_precomputation, ifft_precomputation) = AHPForR1CS::<F, MM>::fft_precomputation(
let (fft_precomputation, ifft_precomputation) = AHPForR1CS::<F, SM>::fft_precomputation(
variable_domain_size,
constraint_domain_size,
non_zero_a_domain_size,
Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/snark/varuna/ahp/indexer/circuit_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub struct CircuitInfo {

impl CircuitInfo {
/// The maximum degree of polynomial required to represent this index in the AHP.
pub fn max_degree<F: PrimeField, MM: SNARKMode>(&self) -> usize {
pub fn max_degree<F: PrimeField, SM: SNARKMode>(&self) -> usize {
let max_non_zero = self.num_non_zero_a.max(self.num_non_zero_b).max(self.num_non_zero_c);
AHPForR1CS::<F, MM>::max_degree(self.num_constraints, self.num_variables, max_non_zero).unwrap()
AHPForR1CS::<F, SM>::max_degree(self.num_constraints, self.num_variables, max_non_zero).unwrap()
}
}

Expand Down
8 changes: 4 additions & 4 deletions algorithms/src/snark/varuna/ahp/indexer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ use snarkvm_utilities::println;

use super::Matrix;

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// Generate the index polynomials for this constraint system.
pub fn index<C: ConstraintSynthesizer<F>>(c: &C) -> Result<Circuit<F, MM>> {
pub fn index<C: ConstraintSynthesizer<F>>(c: &C) -> Result<Circuit<F, SM>> {
let IndexerState {
constraint_domain,
variable_domain,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
let padding_time = start_timer!(|| "Padding matrices");
// Given that we only use the matrix for indexing, the values we choose for assignments don't matter
let random_assignments = None;
MM::ZK.then(|| {
SM::ZK.then(|| {
crate::snark::varuna::ahp::matrices::add_randomizing_variables::<_, _>(&mut ics, random_assignments)
});

Expand Down Expand Up @@ -206,7 +206,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
.try_into()
.unwrap();

let id = Circuit::<F, MM>::hash(&index_info, &a, &b, &c).unwrap();
let id = Circuit::<F, SM>::hash(&index_info, &a, &b, &c).unwrap();

let result = Ok(IndexerState {
constraint_domain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use snarkvm_utilities::{cfg_par_bridge, cfg_reduce};
#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// Output the number of oracles sent by the prover in this round.
pub const fn num_fifth_round_oracles() -> usize {
1
Expand All @@ -41,7 +41,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
/// Output the fifth round message and the next state.
pub fn prover_fifth_round<R: RngCore>(
verifier_message: verifier::FourthMessage<F>,
state: prover::State<'_, F, MM>,
state: prover::State<'_, F, SM>,
_r: &mut R,
) -> Result<prover::FifthOracles<F>, AHPError> {
let lhs_sum: DensePolynomial<F> = cfg_reduce!(
Expand Down
16 changes: 8 additions & 8 deletions algorithms/src/snark/varuna/ahp/prover/round_functions/first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ use snarkvm_utilities::cfg_into_iter;
#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// Output the number of oracles sent by the prover in the first round.
pub fn num_first_round_oracles(total_batch_size: usize) -> usize {
total_batch_size + (MM::ZK as usize)
total_batch_size + (SM::ZK as usize)
}

/// Output the degree bounds of oracles in the first round.
Expand All @@ -50,17 +50,17 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
.flat_map(move |i| [PolynomialInfo::new(witness_label(circuit_id, "w", i), None, Self::zk_bound())])
})
.collect::<Vec<_>>();
if MM::ZK {
if SM::ZK {
polynomials.push(PolynomialInfo::new("mask_poly".to_string(), None, None));
}
polynomials.into_iter().map(|info| (info.label().into(), info)).collect()
}

/// Output the first round message and the next state.
pub fn prover_first_round<'a, R: RngCore>(
mut state: prover::State<'a, F, MM>,
mut state: prover::State<'a, F, SM>,
rng: &mut R,
) -> Result<prover::State<'a, F, MM>, AHPError> {
) -> Result<prover::State<'a, F, SM>, AHPError> {
let round_time = start_timer!(|| "AHP::Prover::FirstRound");
let mut job_pool = snarkvm_utilities::ExecutionPool::with_capacity(state.total_instances);
for (circuit, circuit_state) in state.circuit_specific_states.iter_mut() {
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
circuit_specific_batches.insert(circuit.id, batches);
end_timer!(round_time);
}
let mask_poly = MM::ZK.then(|| Self::calculate_mask_poly(state.max_variable_domain, rng));
let mask_poly = SM::ZK.then(|| Self::calculate_mask_poly(state.max_variable_domain, rng));
let oracles = prover::FirstOracles { batches: circuit_specific_batches, mask_poly };
assert!(oracles.matches_info(&Self::first_round_polynomial_info(
state.circuit_specific_states.iter().map(|(c, s)| (&c.id, &s.batch_size))
Expand All @@ -99,7 +99,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
}

fn calculate_mask_poly<R: RngCore>(variable_domain: EvaluationDomain<F>, rng: &mut R) -> LabeledPolynomial<F> {
assert!(MM::ZK);
assert!(SM::ZK);
let mask_poly_time = start_timer!(|| "Computing mask polynomial");
// We'll use the masking technique from Lunar (https://eprint.iacr.org/2020/1069.pdf, pgs 20-22).
let h_1_mask = DensePolynomial::rand(3, rng).coeffs; // selected arbitrarily.
Expand Down Expand Up @@ -129,7 +129,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
x_poly: DensePolynomial<F>,
variable_domain: EvaluationDomain<F>,
input_domain: EvaluationDomain<F>,
circuit: &Circuit<F, MM>,
circuit: &Circuit<F, SM>,
) -> Witness<F> {
let mut w_extended = private_variables;
let ratio = variable_domain.size() / input_domain.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Apoly<F> = LabeledPolynomial<F>;
type Bpoly<F> = LabeledPolynomial<F>;
type Gpoly<F> = LabeledPolynomial<F>;

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// Output the number of oracles sent by the prover in the fourth round.
pub const fn num_fourth_round_oracles(circuits: usize) -> usize {
circuits * 3
Expand Down Expand Up @@ -77,9 +77,9 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
pub fn prover_fourth_round<'a, R: RngCore>(
second_message: &verifier::SecondMessage<F>,
third_message: &verifier::ThirdMessage<F>,
mut state: prover::State<'a, F, MM>,
mut state: prover::State<'a, F, SM>,
_r: &mut R,
) -> Result<(prover::FourthMessage<F>, prover::FourthOracles<F>, prover::State<'a, F, MM>), AHPError> {
) -> Result<(prover::FourthMessage<F>, prover::FourthOracles<F>, prover::State<'a, F, SM>), AHPError> {
let round_time = start_timer!(|| "AHP::Prover::FourthRound");

let verifier::SecondMessage { alpha, .. } = second_message;
Expand Down
12 changes: 6 additions & 6 deletions algorithms/src/snark/varuna/ahp/prover/round_functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ mod fourth;
mod second;
mod third;

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// Initialize the AHP prover.
pub fn init_prover<'a, C: ConstraintSynthesizer<F>, R: Rng + CryptoRng>(
circuits_to_constraints: &BTreeMap<&'a Circuit<F, MM>, &[C]>,
circuits_to_constraints: &BTreeMap<&'a Circuit<F, SM>, &[C]>,
rng: &mut R,
) -> Result<prover::State<'a, F, MM>, AHPError> {
) -> Result<prover::State<'a, F, SM>, AHPError> {
let init_time = start_timer!(|| "AHP::Prover::Init");

let mut randomizing_assignments = Vec::with_capacity(circuits_to_constraints.len());
for constraints in circuits_to_constraints.values() {
let mut circuit_assignments = Vec::with_capacity(constraints.len());
for _ in 0..constraints.len() {
if MM::ZK {
if SM::ZK {
let a = F::rand(rng);
let b = F::rand(rng);
let c = a * b;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
let padding_time =
start_timer!(|| format!("Padding matrices for {:?} and index {_i}", circuit.id));

MM::ZK.then(|| {
SM::ZK.then(|| {
crate::snark::varuna::ahp::matrices::add_randomizing_variables::<_, _>(
&mut pcs,
rand_assignments,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
.collect::<Result<Vec<prover::Assignments<F>>, AHPError>>()?;
Ok((*circuit, assignments))
})
.collect::<Result<BTreeMap<&'a Circuit<F, MM>, Vec<prover::Assignments<F>>>, AHPError>>()?;
.collect::<Result<BTreeMap<&'a Circuit<F, SM>, Vec<prover::Assignments<F>>>, AHPError>>()?;

let state = prover::State::initialize(indices_and_assignments)?;

Expand Down
10 changes: 5 additions & 5 deletions algorithms/src/snark/varuna/ahp/prover/round_functions/second.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use snarkvm_utilities::{cfg_into_iter, cfg_iter_mut, cfg_reduce, ExecutionPool};
#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {
/// Output the number of oracles sent by the prover in the second round.
pub const fn num_second_round_oracles() -> usize {
1
Expand All @@ -48,9 +48,9 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
/// Output the second round message and the next state.
pub fn prover_second_round<'a, R: RngCore>(
verifier_message: &verifier::FirstMessage<F>,
mut state: prover::State<'a, F, MM>,
mut state: prover::State<'a, F, SM>,
_r: &mut R,
) -> Result<(prover::SecondOracles<F>, prover::State<'a, F, MM>)> {
) -> Result<(prover::SecondOracles<F>, prover::State<'a, F, SM>)> {
let round_time = start_timer!(|| "AHP::Prover::SecondRound");

let zk_bound = Self::zk_bound();
Expand All @@ -72,7 +72,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
}

fn calculate_rowcheck_witness(
state: &mut prover::State<F, MM>,
state: &mut prover::State<F, SM>,
batch_combiners: &BTreeMap<CircuitId, verifier::BatchCombiners<F>>,
) -> Result<DensePolynomial<F>> {
let mut job_pool = ExecutionPool::with_capacity(state.circuit_specific_states.len());
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<F: PrimeField, MM: SNARKMode> AHPForR1CS<F, MM> {
label: impl ToString,
evaluations: Vec<F>,
constraint_domain: EvaluationDomain<F>,
circuit: &Circuit<F, MM>,
circuit: &Circuit<F, SM>,
) -> DensePolynomial<F> {
let label = label.to_string();
let poly_time = start_timer!(|| format!("Computing {label}"));
Expand Down
Loading

0 comments on commit fa509f0

Please sign in to comment.