From 078b0b7f6b5bb0a1631b55a0895878d652fac071 Mon Sep 17 00:00:00 2001 From: Aaron Feickert <66188213+AaronFeickert@users.noreply.github.com> Date: Thu, 8 Aug 2024 17:37:52 -0500 Subject: [PATCH] Relax `Arc` requirement for input sets (#101) This PR relaxes the `Arc` requirement for input sets. Specifically, it removes the requirement that the caller use an `Arc` wrapper for a `TriptychInputSet` in order to use it to generate a statement. Instead, this wrapping is handled internally. Partially addresses #65. BREAKING CHANGE: Updates the public API. --- benches/parallel.rs | 2 +- benches/triptych.rs | 2 +- examples/ringct.rs | 3 +-- src/lib.rs | 3 +-- src/parallel/mod.rs | 3 +-- src/parallel/statement.rs | 14 +++++++------- src/proof.rs | 4 ++-- src/statement.rs | 13 ++++++++----- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/benches/parallel.rs b/benches/parallel.rs index f2e9181..36dddec 100644 --- a/benches/parallel.rs +++ b/benches/parallel.rs @@ -63,7 +63,7 @@ fn generate_data( offsets.push(r_offset * params.get_G1()); M1[witness.get_l() as usize] = witness.compute_auxiliary_verification_key() + offsets.last().unwrap(); } - let input_set = Arc::new(TriptychInputSet::new(&M, &M1).unwrap()); + let input_set = TriptychInputSet::new(&M, &M1).unwrap(); // Generate statements let mut statements = Vec::with_capacity(b); diff --git a/benches/triptych.rs b/benches/triptych.rs index dca1233..cb2b823 100644 --- a/benches/triptych.rs +++ b/benches/triptych.rs @@ -53,7 +53,7 @@ fn generate_data( for witness in &witnesses { M[witness.get_l() as usize] = witness.compute_verification_key(); } - let input_set = Arc::new(TriptychInputSet::new(&M).unwrap()); + let input_set = TriptychInputSet::new(&M).unwrap(); // Generate statements let mut statements = Vec::with_capacity(b); diff --git a/examples/ringct.rs b/examples/ringct.rs index 502d2bb..00976ff 100644 --- a/examples/ringct.rs +++ b/examples/ringct.rs @@ -66,9 +66,8 @@ mod test { let witness = TriptychWitness::new(¶ms, index, &signing_key, &(commitment_mask - offset_mask)).unwrap(); // We can also set up the input set and statement - // The input set is `Arc`-wrapped since it's likely it could be reused // The linkable ring signature also comes equipped with a linking tag; the library can compute it for us - let input_set = Arc::new(TriptychInputSet::new(&output_keys, &value_commitments).unwrap()); + let input_set = TriptychInputSet::new(&output_keys, &value_commitments).unwrap(); let statement = TriptychStatement::new(¶ms, &input_set, &offset, &witness.compute_linking_tag()).unwrap(); // The proof needs a transcript associated to it diff --git a/src/lib.rs b/src/lib.rs index 8b69136..21012f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,7 +86,6 @@ //! let witness = TriptychWitness::random(¶ms, &mut rng); //! //! // Generate an input set of random verification keys, placing ours at the chosen index -//! // This is `Arc`-wrapped to facilitate efficient reuse! //! let M = (0..params.get_N()) //! .map(|i| { //! if i == witness.get_l() { @@ -96,7 +95,7 @@ //! } //! }) //! .collect::>(); -//! let input_set = Arc::new(TriptychInputSet::new(&M).unwrap()); +//! let input_set = TriptychInputSet::new(&M).unwrap(); //! //! // Generate the statement, which includes the verification key vector and linking tag //! let J = witness.compute_linking_tag(); diff --git a/src/parallel/mod.rs b/src/parallel/mod.rs index a83683d..64df1ad 100644 --- a/src/parallel/mod.rs +++ b/src/parallel/mod.rs @@ -43,7 +43,6 @@ //! let offset = Scalar::random(&mut rng) * params.get_G1(); //! //! // Generate an input set of random verification keys, placing ours at the chosen index -//! // This is `Arc`-wrapped to facilitate efficient reuse! //! let M = (0..params.get_N()) //! .map(|i| { //! if i == witness.get_l() { @@ -63,7 +62,7 @@ //! } //! }) //! .collect::>(); -//! let input_set = Arc::new(TriptychInputSet::new(&M, &M1).unwrap()); +//! let input_set = TriptychInputSet::new(&M, &M1).unwrap(); //! //! // Generate the statement, which includes the verification key vectors and linking tag //! let J = witness.compute_linking_tag(); diff --git a/src/parallel/statement.rs b/src/parallel/statement.rs index 69ceb78..1197128 100644 --- a/src/parallel/statement.rs +++ b/src/parallel/statement.rs @@ -15,8 +15,8 @@ use crate::{parallel::TriptychParameters, Transcript, TRANSCRIPT_HASH_BYTES}; #[allow(non_snake_case)] #[derive(Clone, Debug, Eq, PartialEq)] pub struct TriptychInputSet { - M: Vec, - M1: Vec, + M: Arc>, + M1: Arc>, hash: Vec, } @@ -99,8 +99,8 @@ impl TriptychInputSet { transcript.challenge_bytes(b"hash", &mut hash); Ok(Self { - M: M.to_vec(), - M1: M1.to_vec(), + M: Arc::new(M.to_vec()), + M1: Arc::new(M1.to_vec()), hash, }) } @@ -130,7 +130,7 @@ impl TriptychInputSet { #[derive(Clone, Eq, PartialEq)] pub struct TriptychStatement { params: Arc, - input_set: Arc, + input_set: TriptychInputSet, offset: RistrettoPoint, J: RistrettoPoint, hash: Vec, @@ -162,7 +162,7 @@ impl TriptychStatement { #[allow(non_snake_case)] pub fn new( params: &Arc, - input_set: &Arc, + input_set: &TriptychInputSet, offset: &RistrettoPoint, J: &RistrettoPoint, ) -> Result { @@ -208,7 +208,7 @@ impl TriptychStatement { } /// Get the input set for this [`TriptychStatement`]. - pub fn get_input_set(&self) -> &Arc { + pub fn get_input_set(&self) -> &TriptychInputSet { &self.input_set } diff --git a/src/proof.rs b/src/proof.rs index e7554ae..d4fc35f 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -998,7 +998,7 @@ mod test { for witness in &witnesses { M[witness.get_l() as usize] = witness.compute_verification_key(); } - let input_set = Arc::new(TriptychInputSet::new(&M).unwrap()); + let input_set = TriptychInputSet::new(&M).unwrap(); // Generate statements let mut statements = Vec::with_capacity(b); @@ -1282,7 +1282,7 @@ mod test { let mut M = statements[0].get_input_set().get_keys().to_vec(); let index = ((witnesses[0].get_l() + 1) % witnesses[0].get_params().get_N()) as usize; M[index] = RistrettoPoint::random(&mut rng); - let evil_input_set = Arc::new(TriptychInputSet::new(&M).unwrap()); + let evil_input_set = TriptychInputSet::new(&M).unwrap(); let evil_statement = TriptychStatement::new(statements[0].get_params(), &evil_input_set, statements[0].get_J()).unwrap(); diff --git a/src/statement.rs b/src/statement.rs index 99c9b70..103c519 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -15,7 +15,7 @@ use crate::{Transcript, TriptychParameters, TRANSCRIPT_HASH_BYTES}; #[allow(non_snake_case)] #[derive(Clone, Debug, Eq, PartialEq)] pub struct TriptychInputSet { - M: Vec, + M: Arc>, hash: Vec, } @@ -74,7 +74,10 @@ impl TriptychInputSet { let mut hash = vec![0u8; TRANSCRIPT_HASH_BYTES]; transcript.challenge_bytes(b"hash", &mut hash); - Ok(Self { M: M.to_vec(), hash }) + Ok(Self { + M: Arc::new(M.to_vec()), + hash, + }) } /// Get the verification keys for this [`TriptychInputSet`]. @@ -96,7 +99,7 @@ impl TriptychInputSet { #[derive(Clone, Eq, PartialEq)] pub struct TriptychStatement { params: Arc, - input_set: Arc, + input_set: TriptychInputSet, J: RistrettoPoint, hash: Vec, } @@ -127,7 +130,7 @@ impl TriptychStatement { #[allow(non_snake_case)] pub fn new( params: &Arc, - input_set: &Arc, + input_set: &TriptychInputSet, J: &RistrettoPoint, ) -> Result { // Check that the input vector is valid against the parameters @@ -161,7 +164,7 @@ impl TriptychStatement { } /// Get the input set for this [`TriptychStatement`]. - pub fn get_input_set(&self) -> &Arc { + pub fn get_input_set(&self) -> &TriptychInputSet { &self.input_set }