Skip to content

Commit 1eba3bb

Browse files
authored
crypto-bigint: add From conversions between UInt and limb arrays (#460)
These will make it much easier to incrementally retrofit `UInt` into the `elliptic-curve` crate and the respective curve implementations. From an encapsulation perspective, it's a bit problematic because it's exposing the internal representation used by `UInt`, so it additionally adds TODOs to eventually remove these as they're a bit of a hack.
1 parent 3e9fa33 commit 1eba3bb

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

crypto-bigint/src/uint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl<const LIMBS: usize> UInt<LIMBS> {
4242
};
4343

4444
/// Borrow the limbs of this [`UInt`].
45+
// TODO(tarcieri): eventually phase this out?
4546
pub const fn limbs(&self) -> &[Limb; LIMBS] {
4647
&self.limbs
4748
}
@@ -56,6 +57,7 @@ impl<const LIMBS: usize> UInt<LIMBS> {
5657
}
5758
}
5859

60+
// TODO(tarcieri): eventually phase this out?
5961
impl<const LIMBS: usize> AsRef<[Limb]> for UInt<LIMBS> {
6062
fn as_ref(&self) -> &[Limb] {
6163
self.limbs()

crypto-bigint/src/uint/from.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ impl From<U128> for u128 {
141141
}
142142
}
143143

144+
// TODO(tarcieri): eventually phase this out?
145+
impl<const LIMBS: usize> From<[Limb; LIMBS]> for UInt<LIMBS> {
146+
fn from(limbs: [Limb; LIMBS]) -> Self {
147+
Self { limbs }
148+
}
149+
}
150+
151+
// TODO(tarcieri): eventually phase this out?
152+
impl<const LIMBS: usize> From<UInt<LIMBS>> for [Limb; LIMBS] {
153+
fn from(n: UInt<LIMBS>) -> [Limb; LIMBS] {
154+
n.limbs
155+
}
156+
}
157+
144158
#[cfg(test)]
145159
mod tests {
146160
use crate::U128;

0 commit comments

Comments
 (0)