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
#![allow(clippy::derive_hash_xor_eq)]
mod add;
mod bit_and;
mod bit_or;
mod cmp;
mod encoding;
mod from;
mod mul;
mod sub;
#[cfg(feature = "rand")]
mod rand;
use core::fmt;
use subtle::{Choice, ConditionallySelectable};
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_error!("this crate builds on 32-bit and 64-bit platforms only");
#[cfg(target_pointer_width = "32")]
pub const BIT_SIZE: usize = 32;
#[cfg(target_pointer_width = "32")]
pub const BYTE_SIZE: usize = 4;
#[cfg(target_pointer_width = "32")]
pub type Inner = u32;
#[cfg(target_pointer_width = "32")]
pub(crate) type SignedInner = i32;
#[cfg(target_pointer_width = "32")]
pub type Wide = u64;
#[cfg(target_pointer_width = "32")]
pub(crate) type SignedWide = i64;
#[cfg(target_pointer_width = "64")]
pub const BIT_SIZE: usize = 64;
#[cfg(target_pointer_width = "64")]
pub const BYTE_SIZE: usize = 8;
#[cfg(target_pointer_width = "64")]
pub type Inner = u64;
#[cfg(target_pointer_width = "64")]
pub(crate) type SignedInner = i64;
#[cfg(target_pointer_width = "64")]
pub type Wide = u128;
#[cfg(target_pointer_width = "64")]
pub(crate) type SignedWide = i128;
pub(crate) const HI_BIT: usize = BIT_SIZE - 1;
#[derive(Copy, Clone, Debug, Default, Hash)]
#[repr(transparent)]
pub struct Limb(pub Inner);
impl Limb {
pub const ZERO: Self = Limb(0);
pub const ONE: Self = Limb(1);
pub const MAX: Self = Limb(Inner::MAX);
#[inline]
pub(crate) const fn ct_select(a: Self, b: Self, c: Inner) -> Self {
Self(a.0 ^ (c & (a.0 ^ b.0)))
}
}
impl ConditionallySelectable for Limb {
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
Self(Inner::conditional_select(&a.0, &b.0, choice))
}
}
impl fmt::Display for Limb {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::UpperHex::fmt(self, f)
}
}
impl fmt::LowerHex for Limb {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}
impl fmt::UpperHex for Limb {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::UpperHex::fmt(&self.0, f)
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl zeroize::DefaultIsZeroes for Limb {}