crypto_bigint/limb/
encoding.rs
1use super::{Limb, Word};
4use crate::Encoding;
5
6impl Encoding for Limb {
7 const BIT_SIZE: usize = Self::BIT_SIZE;
8 const BYTE_SIZE: usize = Self::BYTE_SIZE;
9
10 #[cfg(target_pointer_width = "32")]
11 type Repr = [u8; 4];
12 #[cfg(target_pointer_width = "64")]
13 type Repr = [u8; 8];
14
15 #[inline]
16 fn from_be_bytes(bytes: Self::Repr) -> Self {
17 Limb(Word::from_be_bytes(bytes))
18 }
19
20 #[inline]
21 fn from_le_bytes(bytes: Self::Repr) -> Self {
22 Limb(Word::from_le_bytes(bytes))
23 }
24
25 #[inline]
26 fn to_be_bytes(&self) -> Self::Repr {
27 self.0.to_be_bytes()
28 }
29
30 #[inline]
31 fn to_le_bytes(&self) -> Self::Repr {
32 self.0.to_le_bytes()
33 }
34}
35
36#[cfg(test)]
37mod test {
38 use super::*;
39 use proptest::prelude::*;
40
41 prop_compose! {
42 fn limb()(inner in any::<Word>()) -> Limb {
43 Limb(inner)
44 }
45 }
46
47 proptest! {
48 #[test]
49 fn roundtrip(a in limb()) {
50 assert_eq!(a, Limb::from_be_bytes(a.to_be_bytes()));
51 assert_eq!(a, Limb::from_le_bytes(a.to_le_bytes()));
52 }
53 }
54
55 proptest! {
56 #[test]
57 fn reverse(a in limb()) {
58 let mut bytes = a.to_be_bytes();
59 bytes.reverse();
60 assert_eq!(a, Limb::from_le_bytes(bytes));
61
62 let mut bytes = a.to_le_bytes();
63 bytes.reverse();
64 assert_eq!(a, Limb::from_be_bytes(bytes));
65 }
66 }
67}