Skip to content

Commit 1354c29

Browse files
authored
Improve ergonomics (#65)
1 parent c210f4c commit 1354c29

File tree

2 files changed

+32
-143
lines changed

2 files changed

+32
-143
lines changed

src/ring.rs

Lines changed: 31 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ const fn piop_domain_size_from_pcs_domain_size(pcs_domain_size: usize) -> usize
9191
}
9292

9393
/// Ring suite.
94-
pub trait RingSuite: PedersenSuite
95-
where
96-
BaseField<Self>: ark_ff::PrimeField,
97-
CurveConfig<Self>: TECurveConfig,
98-
AffinePoint<Self>: TEMapping<CurveConfig<Self>>,
94+
///
95+
/// This trait provides the cryptographic primitives needed for ring VRF signatures.
96+
/// All required bounds are expressed directly on the associated type for better ergonomics.
97+
pub trait RingSuite:
98+
PedersenSuite<
99+
Affine: AffineRepr<BaseField: ark_ff::PrimeField, Config: TECurveConfig + Clone>
100+
+ TEMapping<<Self::Affine as AffineRepr>::Config>,
101+
>
99102
{
100103
/// Pairing type.
101104
type Pairing: ark_ec::pairing::Pairing<ScalarField = BaseField<Self>>;
@@ -156,12 +159,7 @@ pub type RingBareProof<S> = ring_proof::RingProof<BaseField<S>, Pcs<S>>;
156159
/// - `pedersen_proof`: Key commitment and VRF correctness proof
157160
/// - `ring_proof`: Membership proof binding the commitment to the ring
158161
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
159-
pub struct Proof<S: RingSuite>
160-
where
161-
BaseField<S>: ark_ff::PrimeField,
162-
CurveConfig<S>: TECurveConfig,
163-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
164-
{
162+
pub struct Proof<S: RingSuite> {
165163
pub pedersen_proof: PedersenProof<S>,
166164
pub ring_proof: RingBareProof<S>,
167165
}
@@ -170,12 +168,7 @@ where
170168
///
171169
/// Implementors can create anonymous proofs that a VRF output
172170
/// is correctly derived using a secret key from a ring of public keys.
173-
pub trait Prover<S: RingSuite>
174-
where
175-
BaseField<S>: ark_ff::PrimeField,
176-
CurveConfig<S>: TECurveConfig,
177-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
178-
{
171+
pub trait Prover<S: RingSuite> {
179172
/// Generate a proof for the given input/output and additional data.
180173
///
181174
/// Creates a zero-knowledge proof that:
@@ -199,12 +192,7 @@ where
199192
///
200193
/// Implementors can verify anonymous proofs that a VRF output
201194
/// was derived using a secret key from a ring of public keys.
202-
pub trait Verifier<S: RingSuite>
203-
where
204-
BaseField<S>: ark_ff::PrimeField,
205-
CurveConfig<S>: TECurveConfig,
206-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
207-
{
195+
pub trait Verifier<S: RingSuite> {
208196
/// Verify a proof for the given input/output and additional data.
209197
///
210198
/// Verifies that:
@@ -228,12 +216,7 @@ where
228216
) -> Result<(), Error>;
229217
}
230218

231-
impl<S: RingSuite> Prover<S> for Secret<S>
232-
where
233-
BaseField<S>: ark_ff::PrimeField,
234-
CurveConfig<S>: TECurveConfig,
235-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
236-
{
219+
impl<S: RingSuite> Prover<S> for Secret<S> {
237220
fn prove(
238221
&self,
239222
input: Input<S>,
@@ -252,12 +235,7 @@ where
252235
}
253236
}
254237

255-
impl<S: RingSuite> Verifier<S> for Public<S>
256-
where
257-
BaseField<S>: ark_ff::PrimeField,
258-
CurveConfig<S>: TECurveConfig,
259-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
260-
{
238+
impl<S: RingSuite> Verifier<S> for Public<S> {
261239
fn verify(
262240
input: Input<S>,
263241
output: Output<S>,
@@ -281,24 +259,14 @@ where
281259
/// - `pcs`: Polynomial Commitment Scheme parameters (KZG setup)
282260
/// - `piop`: Polynomial Interactive Oracle Proof parameters
283261
#[derive(Clone)]
284-
pub struct RingProofParams<S: RingSuite>
285-
where
286-
BaseField<S>: ark_ff::PrimeField,
287-
CurveConfig<S>: TECurveConfig + Clone,
288-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
289-
{
262+
pub struct RingProofParams<S: RingSuite> {
290263
/// PCS parameters.
291264
pub pcs: PcsParams<S>,
292265
/// PIOP parameters.
293266
pub piop: PiopParams<S>,
294267
}
295268

296-
pub(crate) fn piop_params<S: RingSuite>(domain_size: usize) -> PiopParams<S>
297-
where
298-
BaseField<S>: ark_ff::PrimeField,
299-
CurveConfig<S>: TECurveConfig + Clone,
300-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
301-
{
269+
pub(crate) fn piop_params<S: RingSuite>(domain_size: usize) -> PiopParams<S> {
302270
PiopParams::<S>::setup(
303271
ring_proof::Domain::new(domain_size, true),
304272
S::BLINDING_BASE.into_te(),
@@ -307,12 +275,7 @@ where
307275
)
308276
}
309277

310-
impl<S: RingSuite> RingProofParams<S>
311-
where
312-
BaseField<S>: ark_ff::PrimeField,
313-
CurveConfig<S>: TECurveConfig + Clone,
314-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
315-
{
278+
impl<S: RingSuite> RingProofParams<S> {
316279
/// Construct deterministic ring proof params for the given ring size.
317280
///
318281
/// Creates parameters using a deterministic `ChaCha20Rng` seeded with `seed`.
@@ -463,12 +426,7 @@ where
463426
}
464427
}
465428

466-
impl<S: RingSuite> CanonicalSerialize for RingProofParams<S>
467-
where
468-
BaseField<S>: ark_ff::PrimeField,
469-
CurveConfig<S>: TECurveConfig + Clone,
470-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
471-
{
429+
impl<S: RingSuite> CanonicalSerialize for RingProofParams<S> {
472430
fn serialize_with_mode<W: ark_serialize::Write>(
473431
&self,
474432
mut writer: W,
@@ -482,12 +440,7 @@ where
482440
}
483441
}
484442

485-
impl<S: RingSuite> CanonicalDeserialize for RingProofParams<S>
486-
where
487-
BaseField<S>: ark_ff::PrimeField,
488-
CurveConfig<S>: TECurveConfig + Clone,
489-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
490-
{
443+
impl<S: RingSuite> CanonicalDeserialize for RingProofParams<S> {
491444
fn deserialize_with_mode<R: ark_serialize::Read>(
492445
mut reader: R,
493446
compress: ark_serialize::Compress,
@@ -506,12 +459,7 @@ where
506459
}
507460
}
508461

509-
impl<S: RingSuite> ark_serialize::Valid for RingProofParams<S>
510-
where
511-
BaseField<S>: ark_ff::PrimeField,
512-
CurveConfig<S>: TECurveConfig + Clone,
513-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
514-
{
462+
impl<S: RingSuite> ark_serialize::Valid for RingProofParams<S> {
515463
fn check(&self) -> Result<(), ark_serialize::SerializationError> {
516464
self.pcs.check()
517465
}
@@ -522,11 +470,7 @@ where
522470
/// Basically the SRS in Lagrangian form.
523471
/// Can be constructed via the `PcsParams::ck_with_lagrangian()` method.
524472
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
525-
pub struct RingBuilderPcsParams<S: RingSuite>(pub Vec<G1Affine<S>>)
526-
where
527-
BaseField<S>: ark_ff::PrimeField,
528-
CurveConfig<S>: TECurveConfig + Clone,
529-
AffinePoint<S>: TEMapping<CurveConfig<S>>;
473+
pub struct RingBuilderPcsParams<S: RingSuite>(pub Vec<G1Affine<S>>);
530474

531475
// Under construction ring commitment.
532476
type PartialRingCommitment<S> =
@@ -539,12 +483,7 @@ type RawVerifierKey<S> = <PcsParams<S> as ring_proof::pcs::PcsParams>::RVK;
539483
/// Allows constructing a verifier key by adding public keys in batches,
540484
/// which is useful for large rings or memory-constrained environments.
541485
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
542-
pub struct RingVerifierKeyBuilder<S: RingSuite>
543-
where
544-
BaseField<S>: ark_ff::PrimeField,
545-
CurveConfig<S>: TECurveConfig + Clone,
546-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
547-
{
486+
pub struct RingVerifierKeyBuilder<S: RingSuite> {
548487
partial: PartialRingCommitment<S>,
549488
raw_vk: RawVerifierKey<S>,
550489
}
@@ -555,33 +494,20 @@ pub type G2Affine<S> = <<S as RingSuite>::Pairing as Pairing>::G2Affine;
555494
/// Trait for accessing Structured Reference String entries in Lagrangian basis.
556495
///
557496
/// Provides access to precomputed SRS elements needed for efficient ring operations.
558-
pub trait SrsLookup<S: RingSuite>
559-
where
560-
BaseField<S>: ark_ff::PrimeField,
561-
CurveConfig<S>: TECurveConfig + Clone,
562-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
563-
{
497+
pub trait SrsLookup<S: RingSuite> {
564498
fn lookup(&self, range: Range<usize>) -> Option<Vec<G1Affine<S>>>;
565499
}
566500

567501
impl<S: RingSuite, F> SrsLookup<S> for F
568502
where
569503
F: Fn(Range<usize>) -> Option<Vec<G1Affine<S>>>,
570-
BaseField<S>: ark_ff::PrimeField,
571-
CurveConfig<S>: TECurveConfig + Clone,
572-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
573504
{
574505
fn lookup(&self, range: Range<usize>) -> Option<Vec<G1Affine<S>>> {
575506
self(range)
576507
}
577508
}
578509

579-
impl<S: RingSuite> SrsLookup<S> for &RingBuilderPcsParams<S>
580-
where
581-
BaseField<S>: ark_ff::PrimeField,
582-
CurveConfig<S>: TECurveConfig + Clone,
583-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
584-
{
510+
impl<S: RingSuite> SrsLookup<S> for &RingBuilderPcsParams<S> {
585511
fn lookup(&self, range: Range<usize>) -> Option<Vec<G1Affine<S>>> {
586512
if range.end > self.0.len() {
587513
return None;
@@ -590,12 +516,7 @@ where
590516
}
591517
}
592518

593-
impl<S: RingSuite> RingVerifierKeyBuilder<S>
594-
where
595-
BaseField<S>: ark_ff::PrimeField,
596-
CurveConfig<S>: TECurveConfig + Clone,
597-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
598-
{
519+
impl<S: RingSuite> RingVerifierKeyBuilder<S> {
599520
/// Create a new empty ring verifier key builder.
600521
///
601522
/// * `params` - Ring proof parameters
@@ -740,12 +661,7 @@ pub(crate) mod testing {
740661
}
741662

742663
#[allow(unused)]
743-
pub fn prove_verify<S: RingSuite>()
744-
where
745-
BaseField<S>: ark_ff::PrimeField,
746-
CurveConfig<S>: TECurveConfig + Clone,
747-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
748-
{
664+
pub fn prove_verify<S: RingSuite>() {
749665
let rng = &mut ark_std::test_rng();
750666
let params = RingProofParams::<S>::from_rand(TEST_RING_SIZE, rng);
751667

@@ -781,9 +697,7 @@ pub(crate) mod testing {
781697
#[allow(unused)]
782698
pub fn padding_check<S: RingSuite>()
783699
where
784-
BaseField<S>: ark_ff::PrimeField,
785-
CurveConfig<S>: TECurveConfig + Clone,
786-
AffinePoint<S>: TEMapping<CurveConfig<S>> + CheckPoint,
700+
AffinePoint<S>: CheckPoint,
787701
{
788702
// Check that point has been computed using the magic spell.
789703
assert_eq!(S::PADDING, S::data_to_point(PADDING_SEED).unwrap());
@@ -795,9 +709,7 @@ pub(crate) mod testing {
795709
#[allow(unused)]
796710
pub fn accumulator_base_check<S: RingSuite>()
797711
where
798-
BaseField<S>: ark_ff::PrimeField,
799-
CurveConfig<S>: TECurveConfig + Clone,
800-
AffinePoint<S>: TEMapping<CurveConfig<S>> + FindAccumulatorBase<S> + CheckPoint,
712+
AffinePoint<S>: FindAccumulatorBase<S> + CheckPoint,
801713
{
802714
// Check that point has been computed using the magic spell.
803715
assert_eq!(
@@ -812,12 +724,7 @@ pub(crate) mod testing {
812724
}
813725

814726
#[allow(unused)]
815-
pub fn verifier_key_builder<S: RingSuite>()
816-
where
817-
BaseField<S>: ark_ff::PrimeField,
818-
CurveConfig<S>: TECurveConfig + Clone,
819-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
820-
{
727+
pub fn verifier_key_builder<S: RingSuite>() {
821728
use crate::testing::{random_val, random_vec};
822729

823730
let rng = &mut ark_std::test_rng();
@@ -893,12 +800,7 @@ pub(crate) mod testing {
893800
};
894801
}
895802

896-
pub trait RingSuiteExt: RingSuite + crate::testing::SuiteExt
897-
where
898-
BaseField<Self>: ark_ff::PrimeField,
899-
CurveConfig<Self>: TECurveConfig + Clone,
900-
AffinePoint<Self>: TEMapping<CurveConfig<Self>>,
901-
{
803+
pub trait RingSuiteExt: RingSuite + crate::testing::SuiteExt {
902804
const SRS_FILE: &str;
903805

904806
fn params() -> &'static RingProofParams<Self>;
@@ -927,24 +829,14 @@ pub(crate) mod testing {
927829
}
928830
}
929831

930-
pub struct TestVector<S: RingSuite>
931-
where
932-
BaseField<S>: ark_ff::PrimeField,
933-
CurveConfig<S>: TECurveConfig + Clone,
934-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
935-
{
832+
pub struct TestVector<S: RingSuite> {
936833
pub pedersen: pedersen::testing::TestVector<S>,
937834
pub ring_pks: [AffinePoint<S>; TEST_RING_SIZE],
938835
pub ring_pks_com: RingCommitment<S>,
939836
pub ring_proof: RingBareProof<S>,
940837
}
941838

942-
impl<S: RingSuite> core::fmt::Debug for TestVector<S>
943-
where
944-
BaseField<S>: ark_ff::PrimeField,
945-
CurveConfig<S>: TECurveConfig + Clone,
946-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
947-
{
839+
impl<S: RingSuite> core::fmt::Debug for TestVector<S> {
948840
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
949841
f.debug_struct("TestVector")
950842
.field("pedersen", &self.pedersen)
@@ -956,9 +848,6 @@ pub(crate) mod testing {
956848
impl<S> common::TestVectorTrait for TestVector<S>
957849
where
958850
S: RingSuiteExt + std::fmt::Debug + 'static,
959-
BaseField<S>: ark_ff::PrimeField,
960-
CurveConfig<S>: TECurveConfig + Clone,
961-
AffinePoint<S>: TEMapping<CurveConfig<S>>,
962851
{
963852
fn name() -> String {
964853
S::suite_name() + "_ring"

src/utils/te_sw_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//! allowing operations to be performed in the most convenient form for a given task.
55
66
use ark_ec::{
7+
CurveConfig,
78
short_weierstrass::{Affine as SWAffine, SWCurveConfig},
89
twisted_edwards::{Affine as TEAffine, MontCurveConfig, TECurveConfig},
9-
CurveConfig,
1010
};
1111
use ark_ff::{Field, One};
1212
use ark_std::borrow::Cow;

0 commit comments

Comments
 (0)