Add ring newtype for easy access of bytes and elements

This commit is contained in:
Thomas Eizinger 2021-05-10 17:18:25 +10:00 committed by Lucas Soriano del Pino
parent 05c1b63aa2
commit 80165ba91b
No known key found for this signature in database
GPG Key ID: EE611E973A1530E7
2 changed files with 37 additions and 0 deletions

View File

@ -11,6 +11,8 @@ use rand::{CryptoRng, Rng};
use std::convert::TryInto;
use tiny_keccak::Hasher;
mod ring;
pub const RING_SIZE: usize = 11;
const HASH_KEY_CLSAG_AGG_0: &str = "CLSAG_agg_0";
const HASH_KEY_CLSAG_AGG_1: &str = "CLSAG_agg_1";

View File

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