1#![cfg_attr(not(feature = "std"), no_std)]
5
6extern crate alloc;
7
8use core::ops::{Deref, DerefMut};
9
10use elliptic_curve::subtle::{Choice, ConditionallySelectable};
11use rand_core::{CryptoRng, RngCore};
12
13pub mod math;
14pub mod matrix;
15
16pub mod bip32;
17
18pub type SessionId = ByteArray<32>;
20
21pub type HashBytes = ByteArray<32>;
22
23pub fn random_bytes<const N: usize, R: CryptoRng + RngCore>(
25 rng: &mut R,
26) -> [u8; N] {
27 let mut buf = [0u8; N];
28 rng.fill_bytes(&mut buf);
29 buf
30}
31
32#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
35pub struct ByteArray<const T: usize>(pub [u8; T]);
36
37impl<const T: usize> ConditionallySelectable for ByteArray<T> {
38 fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
39 Self(<[u8; T]>::conditional_select(&a.0, &b.0, choice))
40 }
41}
42
43impl<const T: usize> AsRef<[u8]> for ByteArray<T> {
44 fn as_ref(&self) -> &[u8] {
45 &self.0
46 }
47}
48
49impl<const T: usize> Deref for ByteArray<T> {
50 type Target = [u8];
51
52 fn deref(&self) -> &Self::Target {
53 &self.0
54 }
55}
56
57impl<const N: usize> DerefMut for ByteArray<N> {
58 fn deref_mut(&mut self) -> &mut Self::Target {
59 &mut self.0
60 }
61}
62
63impl<const T: usize> Default for ByteArray<T> {
64 fn default() -> Self {
65 Self([0; T])
66 }
67}
68
69impl<const T: usize> ByteArray<T> {
70 pub const fn new(b: [u8; T]) -> Self {
71 Self(b)
72 }
73
74 pub fn random<R: CryptoRng + RngCore>(rng: &mut R) -> Self {
76 let mut bytes = [0; T];
77 rng.fill_bytes(&mut bytes);
78 ByteArray(bytes)
79 }
80}
81
82impl<const N: usize> From<[u8; N]> for ByteArray<N> {
83 fn from(b: [u8; N]) -> Self {
84 ByteArray(b)
85 }
86}
87
88impl<const N: usize> From<&[u8; N]> for ByteArray<N> {
89 fn from(b: &[u8; N]) -> Self {
90 ByteArray(*b)
91 }
92}
93
94impl<const N: usize> From<ByteArray<N>> for [u8; N] {
95 fn from(value: ByteArray<N>) -> Self {
96 value.0
97 }
98}