mirror of
https://github.com/arnaucube/Nova.git
synced 2026-01-11 08:31:29 +01:00
simplify commitmentengine (#149)
This commit is contained in:
@@ -5,7 +5,7 @@ use crate::{
|
|||||||
provider::pedersen::CommitmentKeyExtTrait,
|
provider::pedersen::CommitmentKeyExtTrait,
|
||||||
spartan::polynomial::EqPolynomial,
|
spartan::polynomial::EqPolynomial,
|
||||||
traits::{
|
traits::{
|
||||||
commitment::{CommitmentEngineTrait, CommitmentKeyTrait, CommitmentTrait},
|
commitment::{CommitmentEngineTrait, CommitmentTrait},
|
||||||
evaluation::EvaluationEngineTrait,
|
evaluation::EvaluationEngineTrait,
|
||||||
Group, TranscriptEngineTrait, TranscriptReprTrait,
|
Group, TranscriptEngineTrait, TranscriptReprTrait,
|
||||||
},
|
},
|
||||||
@@ -59,12 +59,12 @@ where
|
|||||||
ck: &<Self::CE as CommitmentEngineTrait<G>>::CommitmentKey,
|
ck: &<Self::CE as CommitmentEngineTrait<G>>::CommitmentKey,
|
||||||
) -> (Self::ProverKey, Self::VerifierKey) {
|
) -> (Self::ProverKey, Self::VerifierKey) {
|
||||||
let pk = ProverKey {
|
let pk = ProverKey {
|
||||||
ck_s: CommitmentKey::<G>::new(b"ipa", 1),
|
ck_s: G::CE::setup(b"ipa", 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
let vk = VerifierKey {
|
let vk = VerifierKey {
|
||||||
ck_v: ck.clone(),
|
ck_v: ck.clone(),
|
||||||
ck_s: CommitmentKey::<G>::new(b"ipa", 1),
|
ck_s: G::CE::setup(b"ipa", 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
(pk, vk)
|
(pk, vk)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
errors::NovaError,
|
errors::NovaError,
|
||||||
traits::{
|
traits::{
|
||||||
commitment::{CommitmentEngineTrait, CommitmentKeyTrait, CommitmentTrait},
|
commitment::{CommitmentEngineTrait, CommitmentTrait},
|
||||||
AbsorbInROTrait, CompressedGroup, Group, ROTrait, TranscriptReprTrait,
|
AbsorbInROTrait, CompressedGroup, Group, ROTrait, TranscriptReprTrait,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -36,28 +36,6 @@ pub struct CompressedCommitment<G: Group> {
|
|||||||
comm: G::CompressedGroupElement,
|
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> {
|
impl<G: Group> CommitmentTrait<G> for Commitment<G> {
|
||||||
type CompressedCommitment = CompressedCommitment<G>;
|
type CompressedCommitment = CompressedCommitment<G>;
|
||||||
|
|
||||||
@@ -210,12 +188,22 @@ impl<G: Group> CommitmentEngineTrait<G> for CommitmentEngine<G> {
|
|||||||
type CommitmentKey = CommitmentKey<G>;
|
type CommitmentKey = CommitmentKey<G>;
|
||||||
type Commitment = Commitment<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 {
|
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>;
|
type CE: CommitmentEngineTrait<G>;
|
||||||
|
|
||||||
/// Splits the commitment key into two pieces at a specified point
|
/// 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
|
// 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> {
|
fn fold(&self, w1: &G::Scalar, w2: &G::Scalar) -> CommitmentKey<G> {
|
||||||
let w = vec![*w1, *w2];
|
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()
|
.into_par_iter()
|
||||||
.map(|i| {
|
.map(|i| {
|
||||||
let bases = [L.ck[i].clone(), R.ck[i].clone()].to_vec();
|
let bases = [L.ck[i].clone(), R.ck[i].clone()].to_vec();
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ use crate::{
|
|||||||
utils::scalar_as_base,
|
utils::scalar_as_base,
|
||||||
},
|
},
|
||||||
traits::{
|
traits::{
|
||||||
commitment::{CommitmentEngineTrait, CommitmentKeyTrait},
|
commitment::CommitmentEngineTrait, AbsorbInROTrait, Group, ROTrait, TranscriptReprTrait,
|
||||||
AbsorbInROTrait, Group, ROTrait, TranscriptReprTrait,
|
|
||||||
},
|
},
|
||||||
Commitment, CommitmentKey, CE,
|
Commitment, CommitmentKey, CE,
|
||||||
};
|
};
|
||||||
@@ -74,7 +73,7 @@ pub struct RelaxedR1CSInstance<G: Group> {
|
|||||||
impl<G: Group> R1CS<G> {
|
impl<G: Group> R1CS<G> {
|
||||||
/// Samples public parameters for the specified number of constraints and variables in an 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<G> {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,24 +10,6 @@ use core::{
|
|||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
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
|
/// Defines basic operations on commitments
|
||||||
pub trait CommitmentOps<Rhs = Self, Output = Self>:
|
pub trait CommitmentOps<Rhs = Self, Output = Self>:
|
||||||
Add<Rhs, Output = Output> + AddAssign<Rhs>
|
Add<Rhs, Output = Output> + AddAssign<Rhs>
|
||||||
@@ -99,11 +81,14 @@ pub trait CommitmentEngineTrait<G: Group>:
|
|||||||
Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
|
Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
|
||||||
{
|
{
|
||||||
/// Holds the type of the commitment key
|
/// 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
|
/// Holds the type of the commitment
|
||||||
type Commitment: CommitmentTrait<G>;
|
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
|
/// Commits to the provided vector using the provided generators
|
||||||
fn commit(ck: &Self::CommitmentKey, v: &[G::Scalar]) -> Self::Commitment;
|
fn commit(ck: &Self::CommitmentKey, v: &[G::Scalar]) -> Self::Commitment;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user