Skip to content
Merged
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
173 changes: 31 additions & 142 deletions src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ const fn piop_domain_size_from_pcs_domain_size(pcs_domain_size: usize) -> usize
}

/// Ring suite.
pub trait RingSuite: PedersenSuite
where
BaseField<Self>: ark_ff::PrimeField,
CurveConfig<Self>: TECurveConfig,
AffinePoint<Self>: TEMapping<CurveConfig<Self>>,
///
/// This trait provides the cryptographic primitives needed for ring VRF signatures.
/// All required bounds are expressed directly on the associated type for better ergonomics.
pub trait RingSuite:
PedersenSuite<
Affine: AffineRepr<BaseField: ark_ff::PrimeField, Config: TECurveConfig + Clone>
+ TEMapping<<Self::Affine as AffineRepr>::Config>,
>
{
/// Pairing type.
type Pairing: ark_ec::pairing::Pairing<ScalarField = BaseField<Self>>;
Expand Down Expand Up @@ -156,12 +159,7 @@ pub type RingBareProof<S> = ring_proof::RingProof<BaseField<S>, Pcs<S>>;
/// - `pedersen_proof`: Key commitment and VRF correctness proof
/// - `ring_proof`: Membership proof binding the commitment to the ring
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct Proof<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub struct Proof<S: RingSuite> {
pub pedersen_proof: PedersenProof<S>,
pub ring_proof: RingBareProof<S>,
}
Expand All @@ -170,12 +168,7 @@ where
///
/// Implementors can create anonymous proofs that a VRF output
/// is correctly derived using a secret key from a ring of public keys.
pub trait Prover<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub trait Prover<S: RingSuite> {
/// Generate a proof for the given input/output and additional data.
///
/// Creates a zero-knowledge proof that:
Expand All @@ -199,12 +192,7 @@ where
///
/// Implementors can verify anonymous proofs that a VRF output
/// was derived using a secret key from a ring of public keys.
pub trait Verifier<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub trait Verifier<S: RingSuite> {
/// Verify a proof for the given input/output and additional data.
///
/// Verifies that:
Expand All @@ -228,12 +216,7 @@ where
) -> Result<(), Error>;
}

impl<S: RingSuite> Prover<S> for Secret<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> Prover<S> for Secret<S> {
fn prove(
&self,
input: Input<S>,
Expand All @@ -252,12 +235,7 @@ where
}
}

impl<S: RingSuite> Verifier<S> for Public<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> Verifier<S> for Public<S> {
fn verify(
input: Input<S>,
output: Output<S>,
Expand All @@ -281,24 +259,14 @@ where
/// - `pcs`: Polynomial Commitment Scheme parameters (KZG setup)
/// - `piop`: Polynomial Interactive Oracle Proof parameters
#[derive(Clone)]
pub struct RingProofParams<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub struct RingProofParams<S: RingSuite> {
/// PCS parameters.
pub pcs: PcsParams<S>,
/// PIOP parameters.
pub piop: PiopParams<S>,
}

pub(crate) fn piop_params<S: RingSuite>(domain_size: usize) -> PiopParams<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub(crate) fn piop_params<S: RingSuite>(domain_size: usize) -> PiopParams<S> {
PiopParams::<S>::setup(
ring_proof::Domain::new(domain_size, true),
S::BLINDING_BASE.into_te(),
Expand All @@ -307,12 +275,7 @@ where
)
}

impl<S: RingSuite> RingProofParams<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> RingProofParams<S> {
/// Construct deterministic ring proof params for the given ring size.
///
/// Creates parameters using a deterministic `ChaCha20Rng` seeded with `seed`.
Expand Down Expand Up @@ -463,12 +426,7 @@ where
}
}

impl<S: RingSuite> CanonicalSerialize for RingProofParams<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> CanonicalSerialize for RingProofParams<S> {
fn serialize_with_mode<W: ark_serialize::Write>(
&self,
mut writer: W,
Expand All @@ -482,12 +440,7 @@ where
}
}

impl<S: RingSuite> CanonicalDeserialize for RingProofParams<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> CanonicalDeserialize for RingProofParams<S> {
fn deserialize_with_mode<R: ark_serialize::Read>(
mut reader: R,
compress: ark_serialize::Compress,
Expand All @@ -506,12 +459,7 @@ where
}
}

impl<S: RingSuite> ark_serialize::Valid for RingProofParams<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> ark_serialize::Valid for RingProofParams<S> {
fn check(&self) -> Result<(), ark_serialize::SerializationError> {
self.pcs.check()
}
Expand All @@ -522,11 +470,7 @@ where
/// Basically the SRS in Lagrangian form.
/// Can be constructed via the `PcsParams::ck_with_lagrangian()` method.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct RingBuilderPcsParams<S: RingSuite>(pub Vec<G1Affine<S>>)
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>;
pub struct RingBuilderPcsParams<S: RingSuite>(pub Vec<G1Affine<S>>);

// Under construction ring commitment.
type PartialRingCommitment<S> =
Expand All @@ -539,12 +483,7 @@ type RawVerifierKey<S> = <PcsParams<S> as ring_proof::pcs::PcsParams>::RVK;
/// Allows constructing a verifier key by adding public keys in batches,
/// which is useful for large rings or memory-constrained environments.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct RingVerifierKeyBuilder<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub struct RingVerifierKeyBuilder<S: RingSuite> {
partial: PartialRingCommitment<S>,
raw_vk: RawVerifierKey<S>,
}
Expand All @@ -555,33 +494,20 @@ pub type G2Affine<S> = <<S as RingSuite>::Pairing as Pairing>::G2Affine;
/// Trait for accessing Structured Reference String entries in Lagrangian basis.
///
/// Provides access to precomputed SRS elements needed for efficient ring operations.
pub trait SrsLookup<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub trait SrsLookup<S: RingSuite> {
fn lookup(&self, range: Range<usize>) -> Option<Vec<G1Affine<S>>>;
}

impl<S: RingSuite, F> SrsLookup<S> for F
where
F: Fn(Range<usize>) -> Option<Vec<G1Affine<S>>>,
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
fn lookup(&self, range: Range<usize>) -> Option<Vec<G1Affine<S>>> {
self(range)
}
}

impl<S: RingSuite> SrsLookup<S> for &RingBuilderPcsParams<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> SrsLookup<S> for &RingBuilderPcsParams<S> {
fn lookup(&self, range: Range<usize>) -> Option<Vec<G1Affine<S>>> {
if range.end > self.0.len() {
return None;
Expand All @@ -590,12 +516,7 @@ where
}
}

impl<S: RingSuite> RingVerifierKeyBuilder<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> RingVerifierKeyBuilder<S> {
/// Create a new empty ring verifier key builder.
///
/// * `params` - Ring proof parameters
Expand Down Expand Up @@ -740,12 +661,7 @@ pub(crate) mod testing {
}

#[allow(unused)]
pub fn prove_verify<S: RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub fn prove_verify<S: RingSuite>() {
let rng = &mut ark_std::test_rng();
let params = RingProofParams::<S>::from_rand(TEST_RING_SIZE, rng);

Expand Down Expand Up @@ -781,9 +697,7 @@ pub(crate) mod testing {
#[allow(unused)]
pub fn padding_check<S: RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>> + CheckPoint,
AffinePoint<S>: CheckPoint,
{
// Check that point has been computed using the magic spell.
assert_eq!(S::PADDING, S::data_to_point(PADDING_SEED).unwrap());
Expand All @@ -795,9 +709,7 @@ pub(crate) mod testing {
#[allow(unused)]
pub fn accumulator_base_check<S: RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>> + FindAccumulatorBase<S> + CheckPoint,
AffinePoint<S>: FindAccumulatorBase<S> + CheckPoint,
{
// Check that point has been computed using the magic spell.
assert_eq!(
Expand All @@ -812,12 +724,7 @@ pub(crate) mod testing {
}

#[allow(unused)]
pub fn verifier_key_builder<S: RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub fn verifier_key_builder<S: RingSuite>() {
use crate::testing::{random_val, random_vec};

let rng = &mut ark_std::test_rng();
Expand Down Expand Up @@ -893,12 +800,7 @@ pub(crate) mod testing {
};
}

pub trait RingSuiteExt: RingSuite + crate::testing::SuiteExt
where
BaseField<Self>: ark_ff::PrimeField,
CurveConfig<Self>: TECurveConfig + Clone,
AffinePoint<Self>: TEMapping<CurveConfig<Self>>,
{
pub trait RingSuiteExt: RingSuite + crate::testing::SuiteExt {
const SRS_FILE: &str;

fn params() -> &'static RingProofParams<Self>;
Expand Down Expand Up @@ -927,24 +829,14 @@ pub(crate) mod testing {
}
}

pub struct TestVector<S: RingSuite>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
pub struct TestVector<S: RingSuite> {
pub pedersen: pedersen::testing::TestVector<S>,
pub ring_pks: [AffinePoint<S>; TEST_RING_SIZE],
pub ring_pks_com: RingCommitment<S>,
pub ring_proof: RingBareProof<S>,
}

impl<S: RingSuite> core::fmt::Debug for TestVector<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
impl<S: RingSuite> core::fmt::Debug for TestVector<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TestVector")
.field("pedersen", &self.pedersen)
Expand All @@ -956,9 +848,6 @@ pub(crate) mod testing {
impl<S> common::TestVectorTrait for TestVector<S>
where
S: RingSuiteExt + std::fmt::Debug + 'static,
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: TECurveConfig + Clone,
AffinePoint<S>: TEMapping<CurveConfig<S>>,
{
fn name() -> String {
S::suite_name() + "_ring"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/te_sw_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//! allowing operations to be performed in the most convenient form for a given task.

use ark_ec::{
CurveConfig,
short_weierstrass::{Affine as SWAffine, SWCurveConfig},
twisted_edwards::{Affine as TEAffine, MontCurveConfig, TECurveConfig},
CurveConfig,
};
use ark_ff::{Field, One};
use ark_std::borrow::Cow;
Expand Down
Loading