arnaucube
arnaucube/NovaPublic Self-hosted repository
simplify commitmentengine (#149)

Srinath Setty srinath@microsoft.com
Thu, 09 Mar 2023 14:33:33 -0800
commit

0e86f9acc373746ebe8a3545dc0f54d6fb038c7b

parent

6ba204401f165dab3ed560ef47ea468fd1a8f5ed

4 files changed, 24 insertions(+), 52 deletions(-)

jump to
M src/provider/ipa_pc.rs → src/provider/ipa_pc.rs

@@ -5,7 +5,7 @@ errors::NovaError,

provider::pedersen::CommitmentKeyExtTrait, spartan::polynomial::EqPolynomial, traits::{ - commitment::{CommitmentEngineTrait, CommitmentKeyTrait, CommitmentTrait}, + commitment::{CommitmentEngineTrait, CommitmentTrait}, evaluation::EvaluationEngineTrait, Group, TranscriptEngineTrait, TranscriptReprTrait, },

@@ -59,12 +59,12 @@ fn setup(

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)
M src/provider/pedersen.rs → src/provider/pedersen.rs

@@ -2,7 +2,7 @@ //! This module provides an implementation of a commitment engine

use crate::{ errors::NovaError, traits::{ - commitment::{CommitmentEngineTrait, CommitmentKeyTrait, CommitmentTrait}, + commitment::{CommitmentEngineTrait, CommitmentTrait}, AbsorbInROTrait, CompressedGroup, Group, ROTrait, TranscriptReprTrait, }, };

@@ -34,28 +34,6 @@ #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]

#[serde(bound = "")] 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> {

@@ -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 @@

// 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();
M src/r1cs.rs → src/r1cs.rs

@@ -8,8 +8,7 @@ nonnative::{bignat::nat_to_limbs, util::f_to_nat},

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 @@

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)) } }
M src/traits/commitment.rs → src/traits/commitment.rs

@@ -10,24 +10,6 @@ ops::{Add, AddAssign, Mul, MulAssign},

}; 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,10 +81,13 @@ 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;