1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! # Efficient and safe bytes
//!
//! [`Bytes`] can be used when secret byte sequences ([`ByteSeq`]) are used but
//! they are too slow for using hacspec in a release version that interacts with
//! other Rust code, i.e. requires a lot conversions between [`U8`] and [`u8`].
#![allow(non_snake_case, dead_code)]

use super::*;

#[cfg(feature = "release")]
pub type Byte = u8;
#[cfg(not(feature = "release"))]
pub type Byte = U8;

#[cfg(feature = "release")]
pub type DoubleByte = u16;
#[cfg(not(feature = "release"))]
pub type DoubleByte = U16;

#[cfg(feature = "release")]
pub type QuadByte = u32;
#[cfg(not(feature = "release"))]
pub type QuadByte = U32;

#[cfg(feature = "release")]
pub type Bytes = PublicSeq<Byte>;
#[cfg(not(feature = "release"))]
pub type Bytes = Seq<Byte>;

#[cfg(feature = "release")]
#[macro_export]
macro_rules! create_bytes {
    ($( $b:expr ),+) => {
        Bytes::from_vec(
            vec![
                $(
                    $b
                ),+
            ]
        )
    };
}

#[cfg(not(feature = "release"))]
#[macro_export]
macro_rules! create_bytes {
    ($( $b:expr ),+) => {
        Bytes::from_vec(
            vec![
                $(
                    U8($b)
                ),+
            ]
        )
    };
}

impl PublicSeq<u8> {
    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn from_public_slice(v: &[u8]) -> PublicSeq<u8> {
        Self { b: v.to_vec() }
    }

    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn from_native(b: Vec<u8>) -> PublicSeq<u8> {
        Self { b }
    }

    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn to_native(&self) -> Vec<u8> {
        self.b.clone()
    }

    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn into_native(self) -> Vec<u8> {
        self.b
    }

    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn as_slice(&self) -> &[u8] {
        self.b.as_slice()
    }
}

impl Seq<U8> {
    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn from_native(b: Vec<u8>) -> Self {
        Self::from_public_slice(&b)
    }

    #[inline(always)]
    #[cfg_attr(feature = "use_attributes", not_hacspec)]
    pub fn as_slice(&self) -> &[U8] {
        self.b.as_slice()
    }
}

#[cfg(feature = "release")]
#[inline(always)]
pub fn Byte(x: u8) -> Byte {
    x
}

#[cfg(not(feature = "release"))]
#[inline(always)]
pub fn Byte(x: u8) -> Byte {
    U8(x)
}

pub trait ByteTrait {
    fn declassify(self) -> u8;
}

impl ByteTrait for u8 {
    #[inline(always)]
    fn declassify(self) -> u8 {
        self
    }
}

#[inline(always)]
pub fn declassify_usize_from_U8(x: Byte) -> usize {
    x.into()
}

// === FIXME: NOT BYTES ANYMORE - MOVE ===

pub trait U16Trait {
    fn into_bytes(self) -> Bytes;
}
impl U16Trait for u16 {
    #[cfg(feature = "release")]
    fn into_bytes(self) -> Bytes {
        Bytes::from_native_slice(&u16::to_be_bytes(self))
    }
    #[cfg(not(feature = "release"))]
    fn into_bytes(self) -> Bytes {
        Bytes::from_seq(&U16_to_be_bytes(U16(self)))
    }
}
#[cfg(not(feature = "release"))]
impl U16Trait for U16 {
    fn into_bytes(self) -> Bytes {
        Bytes::from_seq(&U16_to_be_bytes(self))
    }
}
#[cfg(feature = "release")]
impl U16Trait for U16 {
    fn into_bytes(self) -> Bytes {
        Bytes::from_native(u16::to_be_bytes(self.0).to_vec())
    }
}

pub trait U32Trait {
    fn into_bytes(self) -> Bytes;
}

impl U32Trait for u32 {
    #[cfg(feature = "release")]
    fn into_bytes(self) -> Bytes {
        Bytes::from_native_slice(&u32::to_be_bytes(self))
    }

    #[cfg(not(feature = "release"))]
    fn into_bytes(self) -> Bytes {
        Bytes::from_seq(&U32_to_be_bytes(U32(self)))
    }
}
#[cfg(not(feature = "release"))]
impl U32Trait for U32 {
    fn into_bytes(self) -> Bytes {
        Bytes::from_seq(&U32_to_be_bytes(self))
    }
}
#[cfg(feature = "release")]
impl U32Trait for U32 {
    fn into_bytes(self) -> Bytes {
        Bytes::from_native(u32::to_be_bytes(self.0).to_vec())
    }
}

#[cfg(feature = "release")]
#[inline(always)]
pub fn DoubleByte(x: u16) -> DoubleByte {
    x
}

#[cfg(not(feature = "release"))]
#[inline(always)]
pub fn DoubleByte(x: u16) -> DoubleByte {
    U16(x)
}

#[cfg(feature = "release")]
#[inline(always)]
pub fn QuadByte(x: u32) -> QuadByte {
    x
}

#[cfg(not(feature = "release"))]
#[inline(always)]
pub fn QuadByte(x: u32) -> QuadByte {
    U32(x)
}