From 0e86f9acc373746ebe8a3545dc0f54d6fb038c7b Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Thu, 9 Mar 2023 14:33:33 -0800 Subject: [PATCH] simplify commitmentengine (#149) --- src/provider/ipa_pc.rs | 6 +++--- src/provider/pedersen.rs | 42 ++++++++++++++-------------------------- src/r1cs.rs | 5 ++--- src/traits/commitment.rs | 23 ++++------------------ 4 files changed, 24 insertions(+), 52 deletions(-) diff --git a/src/provider/ipa_pc.rs b/src/provider/ipa_pc.rs index 09383e4..7aa4f0d 100644 --- a/src/provider/ipa_pc.rs +++ b/src/provider/ipa_pc.rs @@ -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: &>::CommitmentKey, ) -> (Self::ProverKey, Self::VerifierKey) { let pk = ProverKey { - ck_s: CommitmentKey::::new(b"ipa", 1), + ck_s: G::CE::setup(b"ipa", 1), }; let vk = VerifierKey { ck_v: ck.clone(), - ck_s: CommitmentKey::::new(b"ipa", 1), + ck_s: G::CE::setup(b"ipa", 1), }; (pk, vk) diff --git a/src/provider/pedersen.rs b/src/provider/pedersen.rs index 5813941..33a8526 100644 --- a/src/provider/pedersen.rs +++ b/src/provider/pedersen.rs @@ -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 { comm: G::CompressedGroupElement, } -impl CommitmentKeyTrait for CommitmentKey { - type Commitment = Commitment; - - 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 CommitmentTrait for Commitment { type CompressedCommitment = CompressedCommitment; @@ -210,12 +188,22 @@ impl CommitmentEngineTrait for CommitmentEngine { type CommitmentKey = CommitmentKey; type Commitment = Commitment; + 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: CommitmentKeyTrait { +pub(crate) trait CommitmentKeyExtTrait { type CE: CommitmentEngineTrait; /// Splits the commitment key into two pieces at a specified point @@ -271,9 +259,9 @@ impl CommitmentKeyExtTrait for CommitmentKey { // 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 { 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(); diff --git a/src/r1cs.rs b/src/r1cs.rs index 6655674..9691360 100644 --- a/src/r1cs.rs +++ b/src/r1cs.rs @@ -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 { impl R1CS { /// 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 { - CommitmentKey::::new(b"ck", max(num_vars, num_cons)) + G::CE::setup(b"ck", max(num_vars, num_cons)) } } diff --git a/src/traits/commitment.rs b/src/traits/commitment.rs index a1e2b96..9b4725f 100644 --- a/src/traits/commitment.rs +++ b/src/traits/commitment.rs @@ -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: - 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: Add + AddAssign @@ -99,11 +81,14 @@ pub trait CommitmentEngineTrait: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> { /// Holds the type of the commitment key - type CommitmentKey: CommitmentKeyTrait; + type CommitmentKey: Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>; /// Holds the type of the commitment type Commitment: CommitmentTrait; + /// 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; }