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