mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-07 05:38:07 -05:00
Add ring newtype for easy access of bytes and elements
This commit is contained in:
parent
05c1b63aa2
commit
80165ba91b
@ -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";
|
||||
|
35
monero-adaptor/src/ring.rs
Normal file
35
monero-adaptor/src/ring.rs
Normal 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]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user