Use Ring type

This commit is contained in:
Lucas Soriano del Pino 2021-05-10 17:37:56 +10:00
parent 80165ba91b
commit 38123a324f
No known key found for this signature in database
GPG key ID: EE611E973A1530E7
2 changed files with 26 additions and 36 deletions

View file

@ -8,6 +8,7 @@ use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint};
use curve25519_dalek::scalar::Scalar; use curve25519_dalek::scalar::Scalar;
use hash_edwards_to_edwards::hash_point_to_point; use hash_edwards_to_edwards::hash_point_to_point;
use rand::{CryptoRng, Rng}; use rand::{CryptoRng, Rng};
use ring::Ring;
use std::convert::TryInto; use std::convert::TryInto;
use tiny_keccak::Hasher; use tiny_keccak::Hasher;
@ -35,8 +36,8 @@ struct AggregationHashes {
impl AggregationHashes { impl AggregationHashes {
pub fn new( pub fn new(
ring: [EdwardsPoint; RING_SIZE], ring: Ring,
commitment_ring: [EdwardsPoint; RING_SIZE], commitment_ring: Ring,
I: EdwardsPoint, I: EdwardsPoint,
z: Scalar, z: Scalar,
H_p_pk: EdwardsPoint, H_p_pk: EdwardsPoint,
@ -44,30 +45,22 @@ impl AggregationHashes {
) -> Self { ) -> Self {
let z_key_image = z * H_p_pk; let z_key_image = z * H_p_pk;
let ring = ring
.iter()
.flat_map(|pk| pk.compress().as_bytes().to_vec())
.collect::<Vec<u8>>();
let commitment_ring = commitment_ring
.iter()
.flat_map(|pk| pk.compress().as_bytes().to_vec())
.collect::<Vec<u8>>();
let I = I.compress(); let I = I.compress();
let z_key_image = z_key_image.compress(); let z_key_image = z_key_image.compress();
let pseudo_output_commitment = pseudo_output_commitment.compress(); let pseudo_output_commitment = pseudo_output_commitment.compress();
let mu_P = Self::hash( let mu_P = Self::hash(
HASH_KEY_CLSAG_AGG_0, HASH_KEY_CLSAG_AGG_0,
&ring, ring.as_ref(),
&commitment_ring, commitment_ring.as_ref(),
&I, &I,
&z_key_image, &z_key_image,
&pseudo_output_commitment, &pseudo_output_commitment,
); );
let mu_C = Self::hash( let mu_C = Self::hash(
HASH_KEY_CLSAG_AGG_1, HASH_KEY_CLSAG_AGG_1,
&ring, ring.as_ref(),
&commitment_ring, commitment_ring.as_ref(),
&I, &I,
&z_key_image, &z_key_image,
&pseudo_output_commitment, &pseudo_output_commitment,
@ -157,7 +150,7 @@ fn clsag_round_hash_prefix(
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn final_challenge( fn final_challenge(
fake_responses: [Scalar; RING_SIZE - 1], fake_responses: [Scalar; RING_SIZE - 1],
ring: [EdwardsPoint; RING_SIZE], ring: Ring,
T_a: EdwardsPoint, T_a: EdwardsPoint,
T_b: EdwardsPoint, T_b: EdwardsPoint,
R_a: EdwardsPoint, R_a: EdwardsPoint,
@ -167,11 +160,7 @@ fn final_challenge(
I: EdwardsPoint, I: EdwardsPoint,
msg: &[u8], msg: &[u8],
) -> Result<(Scalar, Scalar)> { ) -> Result<(Scalar, Scalar)> {
let ring_concat = ring let prefix = clsag_round_hash_prefix(ring.as_ref(), todo!(), todo!(), msg);
.iter()
.flat_map(|pk| pk.compress().as_bytes().to_vec())
.collect::<Vec<u8>>();
let prefix = clsag_round_hash_prefix(&ring_concat, todo!(), todo!(), msg);
let h_0 = { let h_0 = {
let mut keccak = tiny_keccak::Keccak::v256(); let mut keccak = tiny_keccak::Keccak::v256();
keccak.update(&prefix); keccak.update(&prefix);
@ -183,11 +172,6 @@ fn final_challenge(
Scalar::from_bytes_mod_order_wide(&output) Scalar::from_bytes_mod_order_wide(&output)
}; };
let ring_concat = ring
.iter()
.flat_map(|pk| pk.compress().as_bytes().to_vec())
.collect::<Vec<u8>>();
let h_last = fake_responses let h_last = fake_responses
.iter() .iter()
.enumerate() .enumerate()
@ -282,7 +266,7 @@ impl From<Signature> for monero::util::ringct::Clsag {
pub struct Alice0 { pub struct Alice0 {
// secret index is always 0 // secret index is always 0
ring: [EdwardsPoint; RING_SIZE], ring: Ring,
fake_responses: [Scalar; RING_SIZE - 1], fake_responses: [Scalar; RING_SIZE - 1],
msg: [u8; 32], msg: [u8; 32],
// encryption key // encryption key
@ -308,6 +292,8 @@ impl Alice0 {
s_prime_a: Scalar, s_prime_a: Scalar,
rng: &mut (impl Rng + CryptoRng), rng: &mut (impl Rng + CryptoRng),
) -> Result<Self> { ) -> Result<Self> {
let ring = Ring::new(ring);
let mut fake_responses = [Scalar::zero(); RING_SIZE - 1]; let mut fake_responses = [Scalar::zero(); RING_SIZE - 1];
for response in fake_responses.iter_mut().take(RING_SIZE - 1) { for response in fake_responses.iter_mut().take(RING_SIZE - 1) {
*response = Scalar::random(rng); *response = Scalar::random(rng);
@ -419,7 +405,7 @@ pub struct Alice2 {
pub struct Bob0 { pub struct Bob0 {
// secret index is always 0 // secret index is always 0
ring: [EdwardsPoint; RING_SIZE], ring: Ring,
msg: [u8; 32], msg: [u8; 32],
// encryption key // encryption key
R_a: EdwardsPoint, R_a: EdwardsPoint,
@ -443,6 +429,8 @@ impl Bob0 {
s_b: Scalar, s_b: Scalar,
rng: &mut (impl Rng + CryptoRng), rng: &mut (impl Rng + CryptoRng),
) -> Result<Self> { ) -> Result<Self> {
let ring = Ring::new(ring);
let alpha_b = Scalar::random(rng); let alpha_b = Scalar::random(rng);
let p_k = ring[0]; let p_k = ring[0];
@ -486,7 +474,7 @@ impl Bob0 {
pub struct Bob1 { pub struct Bob1 {
// secret index is always 0 // secret index is always 0
ring: [EdwardsPoint; RING_SIZE], ring: Ring,
msg: [u8; 32], msg: [u8; 32],
// encryption key // encryption key
R_a: EdwardsPoint, R_a: EdwardsPoint,

View file

@ -1,33 +1,35 @@
use std::ops::Index; use std::ops::Index;
pub struct Ring<T> { use curve25519_dalek::edwards::EdwardsPoint;
elements: [T; 11],
pub struct Ring {
elements: [EdwardsPoint; 11],
bytes: [u8; 32 * 11], bytes: [u8; 32 * 11],
} }
impl<T> Ring<T> { impl Ring {
pub fn new(elements: [T; 11], serialize_element: impl Fn(&T) -> &[u8; 32]) -> Ring<T> { pub fn new(elements: [EdwardsPoint; 11]) -> Ring {
let mut bytes = [0u8; 32 * 11]; let mut bytes = [0u8; 32 * 11];
for (i, element) in elements.iter().enumerate() { for (i, element) in elements.iter().enumerate() {
let start = i * 32; let start = i * 32;
let end = (i + 1) * 32; let end = (i + 1) * 32;
bytes[start..end].copy_from_slice(serialize_element(element)); bytes[start..end].copy_from_slice(element.compress().as_bytes());
} }
Ring { elements, bytes } Ring { elements, bytes }
} }
} }
impl<T> AsRef<[u8]> for Ring<T> { impl AsRef<[u8]> for Ring {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
self.bytes.as_ref() self.bytes.as_ref()
} }
} }
impl<T> Index<usize> for Ring<T> { impl Index<usize> for Ring {
type Output = T; type Output = EdwardsPoint;
fn index(&self, index: usize) -> &Self::Output { fn index(&self, index: usize) -> &Self::Output {
&self.elements[index] &self.elements[index]