hacspec_lib/seq/
bytes.rs

1//! # Efficient and safe bytes
2//!
3//! [`Bytes`] can be used when secret byte sequences ([`ByteSeq`]) are used but
4//! they are too slow for using hacspec in a release version that interacts with
5//! other Rust code, i.e. requires a lot conversions between [`U8`] and [`u8`].
6#![allow(non_snake_case, dead_code)]
7
8use super::*;
9
10#[cfg(feature = "release")]
11pub type Byte = u8;
12#[cfg(not(feature = "release"))]
13pub type Byte = U8;
14
15#[cfg(feature = "release")]
16pub type DoubleByte = u16;
17#[cfg(not(feature = "release"))]
18pub type DoubleByte = U16;
19
20#[cfg(feature = "release")]
21pub type QuadByte = u32;
22#[cfg(not(feature = "release"))]
23pub type QuadByte = U32;
24
25#[cfg(feature = "release")]
26pub type Bytes = PublicSeq<Byte>;
27#[cfg(not(feature = "release"))]
28pub type Bytes = Seq<Byte>;
29
30#[cfg(feature = "release")]
31#[macro_export]
32macro_rules! create_bytes {
33    ($( $b:expr ),+) => {
34        Bytes::from_vec(
35            vec![
36                $(
37                    $b
38                ),+
39            ]
40        )
41    };
42}
43
44#[cfg(not(feature = "release"))]
45#[macro_export]
46macro_rules! create_bytes {
47    ($( $b:expr ),+) => {
48        Bytes::from_vec(
49            vec![
50                $(
51                    U8($b)
52                ),+
53            ]
54        )
55    };
56}
57
58impl PublicSeq<u8> {
59    #[inline(always)]
60    #[cfg_attr(feature = "use_attributes", not_hacspec)]
61    pub fn from_public_slice(v: &[u8]) -> PublicSeq<u8> {
62        Self { b: v.to_vec() }
63    }
64
65    #[inline(always)]
66    #[cfg_attr(feature = "use_attributes", not_hacspec)]
67    pub fn from_native(b: Vec<u8>) -> PublicSeq<u8> {
68        Self { b }
69    }
70
71    #[inline(always)]
72    #[cfg_attr(feature = "use_attributes", not_hacspec)]
73    pub fn to_native(&self) -> Vec<u8> {
74        self.b.clone()
75    }
76
77    #[inline(always)]
78    #[cfg_attr(feature = "use_attributes", not_hacspec)]
79    pub fn into_native(self) -> Vec<u8> {
80        self.b
81    }
82
83    #[inline(always)]
84    #[cfg_attr(feature = "use_attributes", not_hacspec)]
85    pub fn as_slice(&self) -> &[u8] {
86        self.b.as_slice()
87    }
88}
89
90impl Seq<U8> {
91    #[inline(always)]
92    #[cfg_attr(feature = "use_attributes", not_hacspec)]
93    pub fn from_native(b: Vec<u8>) -> Self {
94        Self::from_public_slice(&b)
95    }
96
97    #[inline(always)]
98    #[cfg_attr(feature = "use_attributes", not_hacspec)]
99    pub fn as_slice(&self) -> &[U8] {
100        self.b.as_slice()
101    }
102}
103
104#[cfg(feature = "release")]
105#[inline(always)]
106pub fn Byte(x: u8) -> Byte {
107    x
108}
109
110#[cfg(not(feature = "release"))]
111#[inline(always)]
112pub fn Byte(x: u8) -> Byte {
113    U8(x)
114}
115
116pub trait ByteTrait {
117    fn declassify(self) -> u8;
118}
119
120impl ByteTrait for u8 {
121    #[inline(always)]
122    fn declassify(self) -> u8 {
123        self
124    }
125}
126
127#[inline(always)]
128pub fn declassify_usize_from_U8(x: Byte) -> usize {
129    x.into()
130}
131
132// === FIXME: NOT BYTES ANYMORE - MOVE ===
133
134pub trait U16Trait {
135    fn into_bytes(self) -> Bytes;
136}
137impl U16Trait for u16 {
138    #[cfg(feature = "release")]
139    fn into_bytes(self) -> Bytes {
140        Bytes::from_native_slice(&u16::to_be_bytes(self))
141    }
142    #[cfg(not(feature = "release"))]
143    fn into_bytes(self) -> Bytes {
144        Bytes::from_seq(&U16_to_be_bytes(U16(self)))
145    }
146}
147#[cfg(not(feature = "release"))]
148impl U16Trait for U16 {
149    fn into_bytes(self) -> Bytes {
150        Bytes::from_seq(&U16_to_be_bytes(self))
151    }
152}
153#[cfg(feature = "release")]
154impl U16Trait for U16 {
155    fn into_bytes(self) -> Bytes {
156        Bytes::from_native(u16::to_be_bytes(self.0).to_vec())
157    }
158}
159
160pub trait U32Trait {
161    fn into_bytes(self) -> Bytes;
162}
163
164impl U32Trait for u32 {
165    #[cfg(feature = "release")]
166    fn into_bytes(self) -> Bytes {
167        Bytes::from_native_slice(&u32::to_be_bytes(self))
168    }
169
170    #[cfg(not(feature = "release"))]
171    fn into_bytes(self) -> Bytes {
172        Bytes::from_seq(&U32_to_be_bytes(U32(self)))
173    }
174}
175#[cfg(not(feature = "release"))]
176impl U32Trait for U32 {
177    fn into_bytes(self) -> Bytes {
178        Bytes::from_seq(&U32_to_be_bytes(self))
179    }
180}
181#[cfg(feature = "release")]
182impl U32Trait for U32 {
183    fn into_bytes(self) -> Bytes {
184        Bytes::from_native(u32::to_be_bytes(self.0).to_vec())
185    }
186}
187
188#[cfg(feature = "release")]
189#[inline(always)]
190pub fn DoubleByte(x: u16) -> DoubleByte {
191    x
192}
193
194#[cfg(not(feature = "release"))]
195#[inline(always)]
196pub fn DoubleByte(x: u16) -> DoubleByte {
197    U16(x)
198}
199
200#[cfg(feature = "release")]
201#[inline(always)]
202pub fn QuadByte(x: u32) -> QuadByte {
203    x
204}
205
206#[cfg(not(feature = "release"))]
207#[inline(always)]
208pub fn QuadByte(x: u32) -> QuadByte {
209    U32(x)
210}