Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use subtle::{Choice, ConstantTimeEq};
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub mod batch;
pub mod note_bytes;

/// The size of the memo.
pub const MEMO_SIZE: usize = 512;
Expand Down
66 changes: 66 additions & 0 deletions src/note_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/// Represents a fixed-size array of bytes for note components.
#[derive(Clone, Copy, Debug)]
pub struct NoteBytesData<const N: usize>(pub [u8; N]);

impl<const N: usize> AsRef<[u8]> for NoteBytesData<N> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl<const N: usize> AsMut<[u8]> for NoteBytesData<N> {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}

impl<const N: usize> From<&[u8]> for NoteBytesData<N> {
fn from(s: &[u8]) -> Self {
Self(s.try_into().unwrap())
}
}

impl<const N: usize> From<(&[u8], &[u8])> for NoteBytesData<N> {
fn from(s: (&[u8], &[u8])) -> Self {
let mut result: [u8; N] = [0; N];
result[..s.0.len()].copy_from_slice(s.0);
result[s.0.len()..].copy_from_slice(s.1);
NoteBytesData::from(result.as_ref())
}
}

/// Defines the ability to concatenate two byte slices.
pub trait NoteByteConcat: for<'a> From<(&'a [u8], &'a [u8])> {}

impl<const N: usize> NoteByteConcat for NoteBytesData<N> {}

/// Defines the behavior for types that can provide read-only access to their internal byte array.
pub trait NoteByteReader: AsRef<[u8]> + for<'a> From<&'a [u8]> + Clone + Copy {}

impl<const N: usize> NoteByteReader for NoteBytesData<N> {}

/// Defines the behavior for types that support both read and write access to their internal byte array.
pub trait NoteByteWriter: NoteByteReader + AsMut<[u8]> {}

impl<const N: usize> NoteByteWriter for NoteBytesData<N> {}

/// Provides a unified interface for handling fixed-size byte arrays used in Orchard note encryption.
pub trait NoteBytes:
AsRef<[u8]>
+ AsMut<[u8]>
+ for<'a> From<&'a [u8]>
+ for<'a> From<(&'a [u8], &'a [u8])>
+ Clone
+ Copy
+ Send
{
/// Constructs a new NoteBytesData from an empty slice of bytes.
fn new() -> Self;
}

impl<const N: usize> NoteBytes for NoteBytesData<N> {
/// Constructs a new NoteBytesData from an empty slice of bytes.
fn new() -> Self {
Self([0u8; N])
}
}