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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Crate-Name: big-unsigned-ints
// Author: Joseph Paul Tortorelli | 0xSilene
// Github: 0xSilene | 228Labs
// Version: 1.0.0
// Date: 4 Sept 2019

/*
|-----METADATA-----|
Rustc Version Tested: 1.37.0 Stable / 1.39.0 Nightly

|-----LICENSE-----|
MIT License

Copyright (c) [2019] [Joseph Paul Tortorelli]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


|-----INFORMATION-----|
Description:
    A Simple Rust Library with no dependencies that allows you to use larger unsigned integers as tulpe structs made up as fixed-length u64 arrays
Notes:
    - This Library is !#[no_std]
    - The Structs all use differing length arrays of u64 primitives while all being able to implement the standard traits by keeping the array size under 32
    - By Default, the traits Clone and Copy are not implemented. Only Debug is implemented.
Warnings:
    - An Unsafe Block is Used In Each Of The from_u8_byte_array_to_<struct>() functions because it treats an array of u8s as an array of u64s using the transmute memory function
Development Notes:
    - Implement conversion from U<bits> to u8 byte array
        - Add safety check for type as types are not checked in args
    - Implementing the array len check in the unsafe block may not be necessary; Look into some more
    - Write some more tests to check for failures

On Transmute:
    Transmute will check the length of the two values but NOT the type.
*/

#![no_std]
use core::mem::transmute;
use core::fmt;

#[derive(Debug, Clone, Copy)]
pub struct U256 (pub[u64;4]); // 256 bits (32 bytes)

#[derive(Debug, Clone, Copy)]
pub struct U384 (pub[u64;6]); // 384 bits (48 bytes)

#[derive(Debug, Clone, Copy)]
pub struct U512 (pub [u64;8]); // 512 bits (64 bytes)

#[derive(Debug, Clone, Copy)]
pub struct U1024 (pub [u64;16]); // 1024 bits (128 bytes)

#[derive(Debug, Clone, Copy)]
pub struct U2048 (pub [u64;32]); // 2048 bits (256 bytes)

impl U256 {
    // If Needed, a Simple Method To Initialize an empty U256 struct, which its member can be accessed like so U256::init().0
    pub fn init () -> U256 {
        return U256([0u64;4]);
    }
    // UNSAFE | Wrapper For Main From Function For Easier Usage / Synatx
    pub fn convert_from_bytes (bytes: [u8;32]) -> [u64;4] {
        return U256::from(bytes).0;
    }
    // SAFE | Wrapper For Main Into Function For Easier Usage / Synatx
    pub fn convert_to_bytes (largebytes: [u64;4]) -> [u8;32] {
        let output: [u8;32] = U256(largebytes).into();
        return output;
    }
}
impl From<[u8;32]> for U256 {
    fn from(byte_array: [u8;32]) -> Self {
        unsafe {
            return transmute::<[u8;32], U256>(byte_array);
        }
    }
}
impl Into<[u8;32]> for U256 {
    fn into(self) -> [u8;32] {
        let U256(array) = self;
        let mut output: [u8;32] = [0;32];
        let mut counter: usize = 0;

        for &x in array.iter() {
            let bytes = x.to_be_bytes();
                for &byte in bytes.iter() {
                    output[counter] = byte;
                    counter += 1;
                }
        }
        return output;
    }
}
impl fmt::Display for U256 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let x = self.0;
        
        // i for index and max - 1 so we can return the final write!
        let mut i: usize = 0;
        let max = x.len() - 1;
        //write!(f, "{:016X}", x[i]);
        while i < max {
            i += 1;
            write!(f, "0x{:X} ", x[i]);
        }
        write!(f, "0x{:X}", x[i])
    }
}

impl U384 {
    // Function That Sets 6 Array Elements to 0u64
    pub fn new () -> U384 {
        let x = U384([0u64;6]);
        return x;
    }
    // Function That Takes an Array of 48 u8s as input and outputs the tulpe struct U384, or [u64;6]
    pub fn from_u8_byte_array_to_u384 (input: [u8;48]) -> U384 {
        if input.len() == 48 {
            unsafe {
                // Requires the type (u8) and an array of 48 elements and is required to return [u64;6]
                return transmute::<[u8;48], U384>(input);
            }
        }
        else {
            panic!("Byte Array Not Size of 48 for U384");
        }
    }
}
impl From<[u8;48]> for U384 {
    fn from(byte_array: [u8;48]) -> Self {
        unsafe {
            return transmute::<[u8;48], U384>(byte_array);
        }
    }
}
impl Into<[u8;48]> for U384 {
    fn into(self) -> [u8;48] {
        let U384(array) = self;
        let mut output: [u8;48] = [0;48];
        let mut counter: usize = 0;

        for &x in array.iter() {
            let bytes = x.to_be_bytes();
                for &byte in bytes.iter() {
                    output[counter] = byte;
                    counter += 1;
                }
        }
        return output;
    }
}
impl fmt::Display for U384 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let x = self.0;
        
        // i for index and max - 1 so we can return the final write!
        let mut i: usize = 0;
        let max = x.len() - 1;
        //write!(f, "{:016X}", x[i]);
        while i < max {
            i += 1;
            write!(f, "0x{:X} ", x[i]);
        }
        write!(f, "0x{:X}", x[i])
    }
}

impl U512 {
    // Function That Sets 8 Array Elements to 0
    pub fn new () -> U512 {
        let x = U512([0u64;8]);
        return x;
    }
    // Function That Takes an Array of 64 u8s as input and outputs the tulpe struct U512, or [u64;8]
    pub fn from_u8_byte_array_to_u512 (input: [u8;64]) -> U512 {
        if input.len() == 64 {
            unsafe {
                // Requires the type (u8) and an array of 68 elements and is required to return [u64;8]
                return transmute::<[u8;64], U512>(input);
            }
        }
        else {
            panic!("Byte Array Not Size of 64 for U512");
        }
    }
}
impl From<[u8;64]> for U512 {
    fn from(byte_array: [u8;64]) -> Self {
        unsafe {
            return transmute::<[u8;64], U512>(byte_array);
        }
    }
}
impl Into<[u8;64]> for U512 {
    fn into(self) -> [u8;64] {
        let U512(array) = self;
        let mut output: [u8;64] = [0;64];
        let mut counter: usize = 0;

        for &x in array.iter() {
            let bytes = x.to_be_bytes();
                for &byte in bytes.iter() {
                    output[counter] = byte;
                    counter += 1;
                }
        }
        return output;
    }
}
impl fmt::Display for U512 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let x = self.0;
        
        // i for index and max - 1 so we can return the final write!
        let mut i: usize = 0;
        let max = x.len() - 1;
        //write!(f, "{:016X}", x[i]);
        while i < max {
            i += 1;
            write!(f, "0x{:X} ", x[i]);
        }
        write!(f, "0x{:X}", x[i])
    }
}
impl U1024 {
    // Function That Sets 16 Array Elements to 0
    pub fn new () -> U1024 {
        let x = U1024([0u64;16]);
        return x;
    }
    // Function That Takes an Array of 128 u8s as input and outputs the tulpe struct U1024, or [u64;16]
    pub fn from_u8_byte_array_to_u1024 (input: [u8;128]) -> U1024 {
        if input.len() == 128 {
            unsafe {
                // Requires the type (u8) and an array of 128 elements and is required to return [u64;16]
                return transmute::<[u8;128], U1024>(input);
            }
        }
        else {
            panic!("Byte Array Not Size of 128 for U1024");
        }
    }
}
impl From<[u8;128]> for U1024 {
    fn from(byte_array: [u8;128]) -> Self {
        unsafe {
            return transmute::<[u8;128], U1024>(byte_array);
        }
    }
}
impl Into<[u8;128]> for U1024 {
    fn into(self) -> [u8;128] {
        let U1024(array) = self;
        let mut output: [u8;128] = [0;128];
        let mut counter: usize = 0;

        for &x in array.iter() {
            let bytes = x.to_be_bytes();
                for &byte in bytes.iter() {
                    output[counter] = byte;
                    counter += 1;
                }
        }
        return output;
    }
}
impl fmt::Display for U1024 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let x = self.0;
        
        // i for index and max - 1 so we can return the final write!
        let mut i: usize = 0;
        let max = x.len() - 1;
        //write!(f, "{:016X}", x[i]);
        while i < max {
            i += 1;
            write!(f, "0x{:X} ", x[i]);
        }
        write!(f, "0x{:X}", x[i])
    }
}
impl U2048 {
    // Function That Sets 32 Array Elements to 0
    pub fn new () -> U2048 {
        let x = U2048([0u64;32]);
        return x;
    }
    // Function That Takes an Array of 256 u8s as input and outputs the tulpe struct U1024, or [u64;32]
    pub fn from_u8_byte_array_to_u2048 (input: [u8;256]) -> U2048 {
        if input.len() == 256 {
            unsafe {
                // Requires the type (u8) and an array of 256 elements and is required to return [u64;32]
                return transmute::<[u8;256], U2048>(input);
            }
        }
        else {
            panic!("Byte Array Not Size of 256 for U2048");
        }
    }
}
impl From<[u8;256]> for U2048 {
    fn from(byte_array: [u8;256]) -> Self {
        unsafe {
            return transmute::<[u8;256], U2048>(byte_array);
        }
    }
}
impl Into<[u8;256]> for U2048 {
    fn into(self) -> [u8;256] {
        let U2048(array) = self;
        let mut output: [u8;256] = [0;256];
        let mut counter: usize = 0;

        for &x in array.iter() {
            let bytes = x.to_be_bytes();
                for &byte in bytes.iter() {
                    output[counter] = byte;
                    counter += 1;
                }
        }
        return output;
    }
}
impl fmt::Display for U2048 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let x = self.0;
        
        // i for index and max - 1 so we can return the final write!
        let mut i: usize = 0;
        let max = x.len() - 1;
        //write!(f, "{:016X}", x[i]);
        while i < max {
            i += 1;
            write!(f, "0x{:X} ", x[i]);
        }
        write!(f, "0x{:X}", x[i])
    }
}

/*
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn check_structs_zero() {
        assert_eq!(U256::new().0,[0u64;4]);
        assert_eq!(U384::new().0,[0u64;6]);
        assert_eq!(U512::new().0,[0u64;8]);
        assert_eq!(U1024::new().0,[0u64;16]);
        assert_eq!(U2048::new().0,[0u64;32]);
    }
    #[test]
    fn check_from_byte_array_conversion(){
        assert_eq!(U256::from_u8_byte_array_to_u256([0xFFu8;32]).0,[0xFFFFFFFFFFFFFFFFu64;4]);
        assert_eq!(U384::from_u8_byte_array_to_u384([0xFFu8;48]).0,[0xFFFFFFFFFFFFFFFFu64;6]);
        assert_eq!(U512::from_u8_byte_array_to_u512([0xFFu8;64]).0,[0xFFFFFFFFFFFFFFFFu64;8]);
        assert_eq!(U1024::from_u8_byte_array_to_u1024([0xFFu8;128]).0,[0xFFFFFFFFFFFFFFFFu64;16]);
        assert_eq!(U2048::from_u8_byte_array_to_u2048([0xFFu8;256]).0,[0xFFFFFFFFFFFFFFFFu64;32]);
    }
    #[test]
    fn check_to_byte_array_conversion(){
        //assert_eq!(U256::from_u256_to_u8_byte_array(U256([0xFFFFFFFFFFFFFFFFu64;4])),[0xFFu8;32]);
        assert_eq!(U256::from_u256_to_byte_array(U256([0xFFFFFFFFFFFFFFFFu64;4])),[0xFFu8;32]);
    }

}
*/