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
pub trait Unsigned:
    num::Unsigned
    + num::traits::WrappingAdd
    + num::traits::WrappingSub
    + num::traits::WrappingShl
    + num::traits::WrappingShr
    + std::ops::BitAnd<Output = Self>
    + std::ops::BitOr<Output = Self>
    + std::ops::BitXor<Output = Self>
    + std::ops::Shl<Output = Self>
    + std::ops::Shr<Output = Self>
    + From<u8>
    + Copy
{
    type Array;

    const BITS: Self;
    const BITSU32: u32;
    const BYTES: usize;
    const P: Self;
    const Q: Self;

    fn from_le_bytes(bytes: Self::Array) -> Self;
    fn to_le_bytes(a: Self) -> Vec<u8>;
}

impl Unsigned for u8 {
    type Array = [u8; 1];

    const BITS: Self = u8::BITS as Self;
    const BITSU32: u32 = u8::BITS;
    const BYTES: usize = 1;
    const P: Self = 0xb7u8;
    const Q: Self = 0x9fu8;

    fn from_le_bytes(bytes: Self::Array) -> u8 {
        u8::from_le_bytes(bytes)
    }

    fn to_le_bytes(a: Self) -> Vec<u8> {
        a.to_le_bytes().to_vec()
    }
}

impl Unsigned for u16 {
    type Array = [u8; 2];

    const BITS: Self = u16::BITS as Self;
    const BITSU32: u32 = u16::BITS;
    const BYTES: usize = 2;
    const P: Self = 0xb7e1u16;
    const Q: Self = 0x9e37u16;

    fn from_le_bytes(bytes: Self::Array) -> u16 {
        u16::from_le_bytes(bytes)
    }

    fn to_le_bytes(a: Self) -> Vec<u8> {
        a.to_le_bytes().to_vec()
    }
}

impl Unsigned for u32 {
    type Array = [u8; 4];

    const BITS: Self = u32::BITS as Self;
    const BITSU32: u32 = u32::BITS;
    const BYTES: usize = 4;
    const P: Self = 0xb7e15163u32;
    const Q: Self = 0x9e3779b9u32;

    fn from_le_bytes(bytes: Self::Array) -> u32 {
        u32::from_le_bytes(bytes)
    }

    fn to_le_bytes(a: Self) -> Vec<u8> {
        a.to_le_bytes().to_vec()
    }
}

impl Unsigned for u64 {
    type Array = [u8; 8];

    const BITS: Self = u64::BITS as Self;
    const BITSU32: u32 = u64::BITS;
    const BYTES: usize = 8;
    const P: Self = 0xb7e151628aed2a6bu64;
    const Q: Self = 0x9e3779b97f4a7c15u64;

    fn from_le_bytes(bytes: Self::Array) -> u64 {
        u64::from_le_bytes(bytes)
    }

    fn to_le_bytes(a: Self) -> Vec<u8> {
        a.to_le_bytes().to_vec()
    }
}

impl Unsigned for u128 {
    type Array = [u8; 16];

    const BITS: Self = u128::BITS as Self;
    const BITSU32: u32 = u128::BITS;
    const BYTES: usize = 16;
    const P: Self = 0xb7e151628aed2a6abf7158809cf4f3c7u128;
    const Q: Self = 0x9e3779b97f4a7c15f39cc0605cedc835u128;

    fn from_le_bytes(bytes: Self::Array) -> u128 {
        u128::from_le_bytes(bytes)
    }

    fn to_le_bytes(a: Self) -> Vec<u8> {
        a.to_le_bytes().to_vec()
    }
}