simplify commitmentengine (#149)

This commit is contained in:
Srinath Setty
2023-03-09 14:33:33 -08:00
committed by GitHub
parent 6ba204401f
commit 0e86f9acc3
4 changed files with 24 additions and 52 deletions

View File

@@ -5,7 +5,7 @@ use crate::{
provider::pedersen::CommitmentKeyExtTrait,
spartan::polynomial::EqPolynomial,
traits::{
commitment::{CommitmentEngineTrait, CommitmentKeyTrait, CommitmentTrait},
commitment::{CommitmentEngineTrait, CommitmentTrait},
evaluation::EvaluationEngineTrait,
Group, TranscriptEngineTrait, TranscriptReprTrait,
},
@@ -59,12 +59,12 @@ where
ck: &<Self::CE as CommitmentEngineTrait<G>>::CommitmentKey,
) -> (Self::ProverKey, Self::VerifierKey) {
let pk = ProverKey {
ck_s: CommitmentKey::<G>::new(b"ipa", 1),
ck_s: G::CE::setup(b"ipa", 1),
};
let vk = VerifierKey {
ck_v: ck.clone(),
ck_s: CommitmentKey::<G>::new(b"ipa", 1),
ck_s: G::CE::setup(b"ipa", 1),
};
(pk, vk)

View File

@@ -2,7 +2,7 @@
use crate::{
errors::NovaError,
traits::{
commitment::{CommitmentEngineTrait, CommitmentKeyTrait, CommitmentTrait},
commitment::{CommitmentEngineTrait, CommitmentTrait},
AbsorbInROTrait, CompressedGroup, Group, ROTrait, TranscriptReprTrait,
},
};
@@ -36,28 +36,6 @@ pub struct CompressedCommitment<G: Group> {
comm: G::CompressedGroupElement,
}
impl<G: Group> CommitmentKeyTrait<G> for CommitmentKey<G> {
type Commitment = Commitment<G>;
fn new(label: &'static [u8], n: usize) -> Self {
CommitmentKey {
ck: G::from_label(label, n.next_power_of_two()),
_p: Default::default(),
}
}
fn len(&self) -> usize {
self.ck.len()
}
fn commit(&self, v: &[G::Scalar]) -> Self::Commitment {
assert!(self.ck.len() >= v.len());
Commitment {
comm: G::vartime_multiscalar_mul(v, &self.ck[..v.len()]),
}
}
}
impl<G: Group> CommitmentTrait<G> for Commitment<G> {
type CompressedCommitment = CompressedCommitment<G>;
@@ -210,12 +188,22 @@ impl<G: Group> CommitmentEngineTrait<G> for CommitmentEngine<G> {
type CommitmentKey = CommitmentKey<G>;
type Commitment = Commitment<G>;
fn setup(label: &'static [u8], n: usize) -> Self::CommitmentKey {
Self::CommitmentKey {
ck: G::from_label(label, n.next_power_of_two()),
_p: Default::default(),
}
}
fn commit(ck: &Self::CommitmentKey, v: &[G::Scalar]) -> Self::Commitment {
ck.commit(v)
assert!(ck.ck.len() >= v.len());
Commitment {
comm: G::vartime_multiscalar_mul(v, &ck.ck[..v.len()]),
}
}
}
pub(crate) trait CommitmentKeyExtTrait<G: Group>: CommitmentKeyTrait<G> {
pub(crate) trait CommitmentKeyExtTrait<G: Group> {
type CE: CommitmentEngineTrait<G>;
/// Splits the commitment key into two pieces at a specified point
@@ -271,9 +259,9 @@ impl<G: Group> CommitmentKeyExtTrait<G> for CommitmentKey<G> {
// combines the left and right halves of `self` using `w1` and `w2` as the weights
fn fold(&self, w1: &G::Scalar, w2: &G::Scalar) -> CommitmentKey<G> {
let w = vec![*w1, *w2];
let (L, R) = self.split_at(self.len() / 2);
let (L, R) = self.split_at(self.ck.len() / 2);
let ck = (0..self.len() / 2)
let ck = (0..self.ck.len() / 2)
.into_par_iter()
.map(|i| {
let bases = [L.ck[i].clone(), R.ck[i].clone()].to_vec();

View File

@@ -8,8 +8,7 @@ use crate::{
utils::scalar_as_base,
},
traits::{
commitment::{CommitmentEngineTrait, CommitmentKeyTrait},
AbsorbInROTrait, Group, ROTrait, TranscriptReprTrait,
commitment::CommitmentEngineTrait, AbsorbInROTrait, Group, ROTrait, TranscriptReprTrait,
},
Commitment, CommitmentKey, CE,
};
@@ -74,7 +73,7 @@ pub struct RelaxedR1CSInstance<G: Group> {
impl<G: Group> R1CS<G> {
/// Samples public parameters for the specified number of constraints and variables in an R1CS
pub fn commitment_key(num_cons: usize, num_vars: usize) -> CommitmentKey<G> {
CommitmentKey::<G>::new(b"ck", max(num_vars, num_cons))
G::CE::setup(b"ck", max(num_vars, num_cons))
}
}

View File

@@ -10,24 +10,6 @@ use core::{
};
use serde::{Deserialize, Serialize};
/// This trait defines the behavior of commitment key
#[allow(clippy::len_without_is_empty)]
pub trait CommitmentKeyTrait<G: Group>:
Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>
{
/// Holds the type of the commitment that can be produced
type Commitment;
/// Samples a new commitment key of a specified size
fn new(label: &'static [u8], n: usize) -> Self;
/// Returns the vector length that can be committed
fn len(&self) -> usize;
/// Commits to a vector using the commitment key
fn commit(&self, v: &[G::Scalar]) -> Self::Commitment;
}
/// Defines basic operations on commitments
pub trait CommitmentOps<Rhs = Self, Output = Self>:
Add<Rhs, Output = Output> + AddAssign<Rhs>
@@ -99,11 +81,14 @@ pub trait CommitmentEngineTrait<G: Group>:
Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
{
/// Holds the type of the commitment key
type CommitmentKey: CommitmentKeyTrait<G, Commitment = Self::Commitment>;
type CommitmentKey: Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>;
/// Holds the type of the commitment
type Commitment: CommitmentTrait<G>;
/// Samples a new commitment key of a specified size
fn setup(label: &'static [u8], n: usize) -> Self::CommitmentKey;
/// Commits to the provided vector using the provided generators
fn commit(ck: &Self::CommitmentKey, v: &[G::Scalar]) -> Self::Commitment;
}