From 3d9aee932cd974e61a22ff62e8917a6e8f6d8b32 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 12 May 2021 11:31:07 +1000 Subject: [PATCH] Remove Ring datatype We no longer need it now that we do this within hash_to_scalar --- monero-adaptor/src/clsag.rs | 70 ++---------------------------------- monero-adaptor/src/lib.rs | 1 + monero-adaptor/src/macros.rs | 3 +- 3 files changed, 4 insertions(+), 70 deletions(-) diff --git a/monero-adaptor/src/clsag.rs b/monero-adaptor/src/clsag.rs index bd187dac..8be2f4af 100644 --- a/monero-adaptor/src/clsag.rs +++ b/monero-adaptor/src/clsag.rs @@ -2,8 +2,6 @@ use curve25519_dalek::constants::ED25519_BASEPOINT_POINT; use curve25519_dalek::edwards::EdwardsPoint; use curve25519_dalek::scalar::Scalar; use hash_edwards_to_edwards::hash_point_to_point; -use std::convert::TryInto; -use std::ops::{Index, Sub}; pub const RING_SIZE: usize = 11; @@ -28,8 +26,6 @@ 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 mu_P = hash_to_scalar!( b"CLSAG_agg_0" || ring || commitment_ring || I || D_inv_8 || pseudo_output_commitment @@ -41,7 +37,7 @@ pub fn sign( dbg!(hex::encode(mu_P.as_bytes())); dbg!(hex::encode(mu_C.as_bytes())); - let adjusted_commitment_ring = &commitment_ring - pseudo_output_commitment; + let adjusted_commitment_ring = commitment_ring.map(|point| point - pseudo_output_commitment); let compute_ring_element = |L: EdwardsPoint, R: EdwardsPoint| { hash_to_scalar!( @@ -118,8 +114,6 @@ pub fn verify( commitment_ring: &[EdwardsPoint; RING_SIZE], pseudo_output_commitment: EdwardsPoint, ) -> bool { - let ring = Ring::new(ring); - let commitment_ring = Ring::new(commitment_ring); let D = D_inv_8 * Scalar::from(8u8); let mu_P = hash_to_scalar!( @@ -132,7 +126,7 @@ pub fn verify( dbg!(hex::encode(mu_P.as_bytes())); dbg!(hex::encode(mu_C.as_bytes())); - let adjusted_commitment_ring = &commitment_ring - pseudo_output_commitment; + let adjusted_commitment_ring = commitment_ring.map(|point| point - pseudo_output_commitment); let mut h = h_0; @@ -226,69 +220,9 @@ impl From for monero::util::ringct::Clsag { } } -#[derive(Clone)] -pub(crate) struct Ring<'a> { - points: &'a [EdwardsPoint; 11], - bytes: [u8; 32 * 11], -} - -impl<'a> Ring<'a> { - fn new(points: &[EdwardsPoint; 11]) -> Ring<'_> { - let mut bytes = [0u8; 32 * 11]; - - for (i, element) in points.iter().enumerate() { - let start = i * 32; - let end = (i + 1) * 32; - - bytes[start..end].copy_from_slice(element.compress().as_bytes()); - } - - Ring { points, bytes } - } -} - -impl<'a, 'b> Sub for &'b Ring<'a> { - type Output = [EdwardsPoint; 11]; - - fn sub(self, rhs: EdwardsPoint) -> Self::Output { - self.points - .iter() - .map(|point| { - dbg!(hex::encode(point.compress().as_bytes())); - dbg!(hex::encode(rhs.compress().as_bytes())); - - let result = point - rhs; - - dbg!(hex::encode(result.compress().as_bytes())); - - result - }) - .collect::>() - .try_into() - .expect("arrays have same length") - } -} - -impl<'a> AsRef<[u8]> for Ring<'a> { - fn as_ref(&self) -> &[u8] { - self.bytes.as_ref() - } -} - -impl<'a> Index for Ring<'a> { - type Output = EdwardsPoint; - - fn index(&self, index: usize) -> &Self::Output { - &self.points[index] - } -} - #[cfg(test)] mod tests { use super::*; - use curve25519_dalek::edwards::CompressedEdwardsY; - use itertools::Itertools; - use rand::rngs::OsRng; use rand::SeedableRng; #[test] diff --git a/monero-adaptor/src/lib.rs b/monero-adaptor/src/lib.rs index 9ac277b3..ad279495 100644 --- a/monero-adaptor/src/lib.rs +++ b/monero-adaptor/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(array_map)] #![allow(non_snake_case)] #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] diff --git a/monero-adaptor/src/macros.rs b/monero-adaptor/src/macros.rs index b3049a15..a4325493 100644 --- a/monero-adaptor/src/macros.rs +++ b/monero-adaptor/src/macros.rs @@ -1,4 +1,3 @@ -use crate::clsag::Ring; use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint}; use std::borrow::Cow; @@ -59,7 +58,7 @@ impl ToCowBytes for [u8; 11] { } } -impl<'a> ToCowBytes for Ring<'a> { +impl<'a> ToCowBytes for [EdwardsPoint; 11] { fn to_cow_bytes(&self) -> Vec> { vec![ CowBytes::Owned(self[0].compress().0),