From 53916aab6baca6f2adf8cac55c3532a739003076 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 May 2021 12:47:20 +1000 Subject: [PATCH] Make `Ring` an implementation detail of clsag module --- monero-adaptor/src/clsag.rs | 17 +++++++++-------- monero-adaptor/src/lib.rs | 25 +++++++++---------------- monero-adaptor/src/ring.rs | 12 ++++++------ 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/monero-adaptor/src/clsag.rs b/monero-adaptor/src/clsag.rs index 32336e9f..db5e2e99 100644 --- a/monero-adaptor/src/clsag.rs +++ b/monero-adaptor/src/clsag.rs @@ -17,8 +17,8 @@ pub fn sign( signing_key: Scalar, H_p_pk: EdwardsPoint, alpha: Scalar, - ring: &Ring, - commitment_ring: &Ring, + ring: &[EdwardsPoint; RING_SIZE], + commitment_ring: &[EdwardsPoint; RING_SIZE], fake_responses: [Scalar; RING_SIZE - 1], z: Scalar, pseudo_output_commitment: EdwardsPoint, @@ -28,6 +28,8 @@ pub fn sign( ) -> Signature { let D = z * H_p_pk; let D_inv_8 = D * INV_EIGHT; + let ring = Ring::new(ring); + let commitment_ring = Ring::new(commitment_ring); let mus = AggregationHashes::new( &ring, @@ -92,11 +94,14 @@ pub fn sign( pub fn verify( sig: &Signature, msg: &[u8], - ring: &Ring, - commitment_ring: &Ring, + ring: &[EdwardsPoint; RING_SIZE], + commitment_ring: &[EdwardsPoint; RING_SIZE], pseudo_output_commitment: EdwardsPoint, H_p_pk: EdwardsPoint, ) -> bool { + let ring = Ring::new(ring); + let commitment_ring = Ring::new(commitment_ring); + let mus = AggregationHashes::new( &ring, &commitment_ring, @@ -312,8 +317,6 @@ mod tests { x * ED25519_BASEPOINT_POINT }); - let ring = Ring::new(ring); - let mut commitment_ring = [EdwardsPoint::default(); RING_SIZE]; let real_commitment_blinding = Scalar::random(&mut OsRng); @@ -323,8 +326,6 @@ mod tests { x * ED25519_BASEPOINT_POINT }); - let commitment_ring = Ring::new(commitment_ring); - // TODO: document let pseudo_output_commitment = commitment_ring[0]; diff --git a/monero-adaptor/src/lib.rs b/monero-adaptor/src/lib.rs index 7f6e2bc1..8e70387f 100644 --- a/monero-adaptor/src/lib.rs +++ b/monero-adaptor/src/lib.rs @@ -14,7 +14,6 @@ use rand::{CryptoRng, Rng}; use tiny_keccak::{Hasher, Keccak}; use clsag::{Signature, RING_SIZE}; -use ring::Ring; mod clsag; mod ring; @@ -83,9 +82,9 @@ impl AdaptorSignature { pub struct Alice0 { // secret index is always 0 - ring: Ring, + ring: [EdwardsPoint; RING_SIZE], fake_responses: [Scalar; RING_SIZE - 1], - commitment_ring: Ring, + commitment_ring: [EdwardsPoint; RING_SIZE], pseudo_output_commitment: EdwardsPoint, msg: [u8; 32], // encryption key @@ -113,9 +112,6 @@ impl Alice0 { s_prime_a: Scalar, rng: &mut (impl Rng + CryptoRng), ) -> Result { - let ring = Ring::new(ring); - let commitment_ring = Ring::new(commitment_ring); - let mut fake_responses = [Scalar::zero(); RING_SIZE - 1]; for response in fake_responses.iter_mut().take(RING_SIZE - 1) { *response = Scalar::random(rng); @@ -226,9 +222,9 @@ pub struct Alice2 { } pub struct Bob0 { - ring: Ring, + ring: [EdwardsPoint; RING_SIZE], msg: [u8; 32], - commitment_ring: Ring, + commitment_ring: [EdwardsPoint; RING_SIZE], pseudo_output_commitment: EdwardsPoint, R_a: EdwardsPoint, R_prime_a: EdwardsPoint, @@ -251,9 +247,6 @@ impl Bob0 { s_b: Scalar, rng: &mut (impl Rng + CryptoRng), ) -> Result { - let ring = Ring::new(ring); - let commitment_ring = Ring::new(commitment_ring); - let alpha_b = Scalar::random(rng); let p_k = ring[0]; @@ -300,9 +293,9 @@ impl Bob0 { } pub struct Bob1 { - ring: Ring, + ring: [EdwardsPoint; RING_SIZE], msg: [u8; 32], - commitment_ring: Ring, + commitment_ring: [EdwardsPoint; RING_SIZE], pseudo_output_commitment: EdwardsPoint, R_a: EdwardsPoint, R_prime_a: EdwardsPoint, @@ -634,9 +627,9 @@ mod tests { assert!(clsag::verify( &sig, msg_to_sign, - todo!(), - todo!(), - todo!(), + &ring, + &commitment_ring, + pseudo_output_commitment, todo!() )); } diff --git a/monero-adaptor/src/ring.rs b/monero-adaptor/src/ring.rs index d078c8ab..424db196 100644 --- a/monero-adaptor/src/ring.rs +++ b/monero-adaptor/src/ring.rs @@ -3,13 +3,13 @@ use std::ops::Index; use curve25519_dalek::edwards::EdwardsPoint; #[derive(Clone)] -pub struct Ring { - elements: [EdwardsPoint; 11], +pub struct Ring<'a> { + elements: &'a [EdwardsPoint; 11], bytes: [u8; 32 * 11], } -impl Ring { - pub fn new(elements: [EdwardsPoint; 11]) -> Ring { +impl<'a> Ring<'a> { + pub fn new(elements: &[EdwardsPoint; 11]) -> Ring<'_> { let mut bytes = [0u8; 32 * 11]; for (i, element) in elements.iter().enumerate() { @@ -23,13 +23,13 @@ impl Ring { } } -impl AsRef<[u8]> for Ring { +impl<'a> AsRef<[u8]> for Ring<'a> { fn as_ref(&self) -> &[u8] { self.bytes.as_ref() } } -impl Index for Ring { +impl<'a> Index for Ring<'a> { type Output = EdwardsPoint; fn index(&self, index: usize) -> &Self::Output {