Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for trimmable SRS #284

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions algorithms/src/traits/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ pub trait SNARK {
unimplemented!()
}

fn trim_universal_setup_parameters(
_long: &Self::UniversalSetupParameters,
_new_config: &Self::UniversalSetupConfig,
) -> Result<Self::UniversalSetupParameters, SNARKError> {
unimplemented!()
}

fn index<C: ConstraintSynthesizer<Self::ScalarField>>(
_circuit: &C,
_srs: &Self::UniversalSetupParameters,
Expand Down
10 changes: 10 additions & 0 deletions marlin/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ use std::{
},
};

pub struct UniversalSetupConfig {
pub supported_degree : usize,
pub supported_hiding_bound : usize,
}

pub struct UniversalSetupParameters<E: PairingEngine> {
pub powers_of_g : Vec<E::G1Affine>,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}

#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
#[derive(Debug, CanonicalSerialize, CanonicalDeserialize)]
Expand Down
7 changes: 7 additions & 0 deletions marlin/src/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ where
Ok(srs)
}

fn trim_universal_setup_parameters(
_long: &Self::UniversalSetupParameters,
_new_config: &Self::UniversalSetupConfig,
) -> Result<Self::UniversalSetupParameters, SNARKError> {
unimplemented!()
}

fn index<C: ConstraintSynthesizer<E::Fr>>(
circuit: &C,
srs: &Self::UniversalSetupParameters,
Expand Down
86 changes: 82 additions & 4 deletions polycommit/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use crate::{Arc, String, Vec};
pub use snarkvm_algorithms::fft::DensePolynomial as Polynomial;
use snarkvm_fields::{ConstraintFieldError, Field, ToConstraintField};
use snarkvm_utilities::{error as error_fn, errors::SerializationError, serialize::*, FromBytes, ToBytes};
use snarkvm_utilities::{error as error_fn, ops::Range, errors::SerializationError, serialize::*, FromBytes, ToBytes};

use core::{
borrow::Borrow,
Expand All @@ -29,11 +29,89 @@ use rand_core::RngCore;
/// Labels a `LabeledPolynomial` or a `LabeledCommitment`.
pub type PolynomialLabel = String;

/// Defines the minimal interface for public params for any polynomial
/// commitment scheme.
pub trait PCUniversalParams: CanonicalSerialize + CanonicalDeserialize + Clone + Debug + ToBytes + FromBytes {
/// An enum defining the supported degree bounds
#[derive(Clone, Debug)]
pub enum PCSupportedBounds {
Empty, // Does not support this type of bounds (e.g., no hiding)
Range(Range<usize>), // Support a..b bound (b not included)
Subset(Vec<usize>) // Support any bound in the vector
}

impl CanonicalSerialize for PCSupportedBounds {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), SerializationError> {
let which_type = match self {
PCSupportedBounds::Empty => 0u8,
PCSupportedBounds::Range(_) => 1u8,
PCSupportedBounds::Subset(_) => 2u8
};
which_type.serialize(writer)?;

match self {
PCSupportedBounds::Empty => {
Ok(())
}
PCSupportedBounds::Range(range) => {
range.serialize(writer)
}
PCSupportedBounds::Subset(set) => {
set.serialize(writer)
}
}
}

fn serialized_size(&self) -> usize {
let which_type = match self {
PCSupportedBounds::Empty => 0u8,
PCSupportedBounds::Range(_) => 1u8,
PCSupportedBounds::Subset(_) => 2u8
};

match self {
PCSupportedBounds::Empty => which_type.serialized_size(),
PCSupportedBounds::Range(range) => {
range.serialized_size() + which_type.serialized_size()
}
PCSupportedBounds::Subset(set) => {
set.serialized_size() + which_type.serialized_size()
}
}
}
}

impl CanonicalDeserialize for PCSupportedBounds {
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, SerializationError> {
let which_type = u8::deserialize(reader)?;
if which_type > 2u8 {
return Err(SerializationError::InvalidData);
}

if which_type == 0 {
Ok(PCSupportedBounds::Empty)
} else if which_type == 1 {
let range = Range::<usize>::deserialize(reader)?;
Ok(PCSupportedBounds::Range(range))
} else if which_type == 2 {
let set = Vec::<usize>::deserialize(reader)?;
Ok(PCSupportedBounds::Subset(set))
} else {
unreachable!()
}
}
}

/// Defines some common interface for the universal setup config.
pub trait PCUniversalSetupConfig: Clone + Debug {
/// Outputs the maximum degree supported by the committer key.
fn max_degree(&self) -> usize;
/// Outputs the supported hiding bounds.
fn supported_hiding_bounds(&self) -> PCSupportedBounds;
/// Outputs the supported degree bounds that the can be enforced.
fn supported_degree_bounds(&self) -> PCSupportedBounds;
}

/// Defines the minimal interface for public params for any polynomial
/// commitment scheme.
pub trait PCUniversalParams: PCUniversalSetupConfig + CanonicalSerialize + CanonicalDeserialize + Clone + Debug + ToBytes + FromBytes {
}

/// Defines the minimal interface of committer keys for any polynomial
Expand Down
148 changes: 140 additions & 8 deletions polycommit/src/kzg10/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,141 @@ use snarkvm_utilities::{

use core::ops::Mul;

/// `UniversalSetupConfig` define what parameters are needed during the setup phase.
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct UniversalSetupConfig {
pub supported_degree : usize,
pub supported_hiding_bounds : PCSupportedBounds,
pub supported_degree_bounds : PCSupportedBounds,
pub supported_g2_powers: PCSupportedBounds,
}

impl PCUniversalSetupConfig for UniversalSetupConfig {
fn max_degree(&self) -> usize {
self.supported_degree
}

fn supported_hiding_bounds(&self) -> PCSupportedBounds {
self.supported_hiding_bounds.clone()
}

fn supported_degree_bounds(&self) -> PCSupportedBounds {
self.supported_degree_bounds.clone()
}
}

#[derive(Clone, Debug)]
pub enum PowersProvider<T: CanonicalSerialize + CanonicalDeserialize> {
Empty,
Range(Vec<T>),
Subset(BTreeMap<usize, T>)
}

impl<T: CanonicalSerialize + CanonicalDeserialize> Default for PowersProvider<T> {
fn default() -> Self {
PowersProvider::Empty
}
}

impl<T: CanonicalSerialize + CanonicalDeserialize> CanonicalSerialize for PowersProvider<T>{
fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), SerializationError> {
let which_type = match self {
PowersProvider::Empty => 0u8,
PowersProvider::Range(_) => 1u8,
PowersProvider::Subset(_) => 2u8
};
which_type.serialize(writer)?;

match self {
PowersProvider::Empty => {
Ok(())
}
PowersProvider::Range(vec) => {
vec.serialize(writer)
}
PowersProvider::Subset(map) => {
map.serialize(writer)
}
}
}

fn serialized_size(&self) -> usize {
let which_type = match self {
PowersProvider::Empty => 0u8,
PowersProvider::Range(_) => 1u8,
PowersProvider::Subset(_) => 2u8
};

match self {
PowersProvider::Empty => {
which_type.serialized_size()
}
PowersProvider::Range(vec) => {
vec.serialized_size() + which_type.serialized_size()
}
PowersProvider::Subset(map) => {
map.serialized_size() + which_type.serialized_size()
}
}
}
}

impl<T: CanonicalSerialize + CanonicalDeserialize> CanonicalDeserialize for PowersProvider<T>{
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, SerializationError> {
let which_type = u8::deserialize(reader)?;
if which_type > 2u8 {
return Err(SerializationError::InvalidData);
}

if which_type == 0 {
Ok(PowersProvider::Empty)
} else if which_type == 1 {
let vec = Vec::<T>::deserialize(reader)?;
Ok(PowersProvider::Range(vec))
} else if which_type == 2 {
let map = BTreeMap::<usize, T>::deserialize(reader)?;
Ok(PowersProvider::Subset(map))
} else {
unreachable!()
}
}
}

/// `UniversalParams` are the universal parameters for the KZG10 scheme.
#[derive(Derivative)]
#[derivative(Default(bound = ""), Clone(bound = ""), Debug(bound = ""))]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct UniversalParams<E: PairingEngine> {
/// Group elements of the form `{ \beta^i G }`, where `i` ranges from 0 to `degree`.
pub supported_degree : usize,
pub supported_hiding_bounds : PCSupportedBounds,
pub supported_degree_bounds : PCSupportedBounds,
pub supported_g2_powers : PCSupportedBounds,

/// An array of group elements of the form `{ \beta^i G }`,
/// where `i` ranges from 0 to `supported_degree`.
pub powers_of_g: Vec<E::G1Affine>,
/// Group elements of the form `{ \beta^i \gamma G }`, where `i` ranges from 0 to `degree`.
pub powers_of_gamma_g: BTreeMap<usize, E::G1Affine>,

/// An array of group elements of the form `{ \beta^i \gamma G }`,
/// where `i` is in the set of `supported_hiding_bounds`.
pub powers_of_gamma_g: PowersProvider<E::G1Affine>,

/// An array of group elements of the form `{ \beta^{max_degree - i} G }`,
/// where `i` is in the set of `supported_degree_bounds`.
///
/// Initially, it supports the entire range of `powers_of_g`,
/// in which case it will be stored as `None`, and the KZG10
/// implementation will use `powers_of_g`.
///
pub reverse_powers_of_g: Option<PowersProvider<E::G1Affine>>,

/// The generator of G2.
pub h: E::G2Affine,
/// \beta times the above generator of G2.
pub beta_h: E::G2Affine,
/// Group elements of the form `{ \beta^i G2 }`, where `i` ranges from `0` to `-degree`.
pub prepared_neg_powers_of_h: BTreeMap<usize, <E::G2Affine as PairingCurve>::Prepared>,

/// Group elements of the form `{ \beta^i G2 }`, where `i` ranges from `0` to `-supported_degree`.
pub prepared_neg_powers_of_h: PowersProvider<<E::G2Affine as PairingCurve>::Prepared>,

/// The generator of G2, prepared for use in pairings.
#[derivative(Debug = "ignore")]
pub prepared_h: <E::G2Affine as PairingCurve>::Prepared,
Expand All @@ -53,12 +173,24 @@ pub struct UniversalParams<E: PairingEngine> {
pub prepared_beta_h: <E::G2Affine as PairingCurve>::Prepared,
}

impl<E: PairingEngine> PCUniversalSetupConfig for UniversalParams<E> {
fn max_degree(&self) -> usize {
self.supported_degree
// which equals the len of `powers_of_g` - 1
}

fn supported_hiding_bounds(&self) -> PCSupportedBounds {
self.supported_hiding_bounds.clone()
}

fn supported_degree_bounds(&self) -> PCSupportedBounds {
self.supported_degree_bounds.clone()
}
}

impl_bytes!(UniversalParams);

impl<E: PairingEngine> PCUniversalParams for UniversalParams<E> {
fn max_degree(&self) -> usize {
self.powers_of_g.len() - 1
}
}

/// `Powers` is used to commit to and create evaluation proofs for a given
Expand Down
Loading