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