Skip to content

Commit 01527dc

Browse files
committed
Convert pointers without bytemuck
1 parent d91710a commit 01527dc

4 files changed

Lines changed: 16 additions & 21 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ rust-version = "1.85"
1818

1919
[dependencies]
2020
subtle = { version = "2.6", default-features = false }
21-
bytemuck = { version = "1.24", default-features = false, features = ["must_cast", "must_cast_extra"] }
2221

2322
# optional dependencies
2423
der = { version = "0.8.0-rc.9", optional = true, default-features = false }

src/limb.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,6 @@ impl Serialize for Limb {
231231
#[cfg(feature = "zeroize")]
232232
impl zeroize::DefaultIsZeroes for Limb {}
233233

234-
// SAFETY: `Limb` is a newtype of an integer POD type
235-
#[allow(unsafe_code)]
236-
unsafe impl bytemuck::Zeroable for Limb {}
237-
238-
// SAFETY: `Limb` is a newtype of an integer POD type
239-
#[allow(unsafe_code)]
240-
unsafe impl bytemuck::Pod for Limb {}
241-
242234
#[cfg(test)]
243235
mod tests {
244236
#[cfg(feature = "alloc")]

src/uint/encoding.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ impl<const LIMBS: usize> Uint<LIMBS> {
223223
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
224224
pub struct EncodedUint<const LIMBS: usize>([Word; LIMBS]);
225225

226+
#[allow(unsafe_code)]
227+
const fn cast_slice(limbs: &[Word]) -> &[u8] {
228+
let new_len = size_of_val(limbs);
229+
unsafe { core::slice::from_raw_parts(limbs.as_ptr() as *mut u8, new_len) }
230+
}
231+
232+
#[allow(unsafe_code)]
233+
const fn cast_slice_mut(limbs: &mut [Word]) -> &mut [u8] {
234+
let new_len = size_of_val(limbs);
235+
unsafe { core::slice::from_raw_parts_mut(limbs.as_mut_ptr() as *mut u8, new_len) }
236+
}
237+
226238
impl<const LIMBS: usize> EncodedUint<LIMBS> {
227239
const fn new_le(value: &Uint<LIMBS>) -> Self {
228240
let mut buffer = [0; LIMBS];
@@ -233,8 +245,7 @@ impl<const LIMBS: usize> EncodedUint<LIMBS> {
233245

234246
// We could cast the whole `buffer` to bytes at once,
235247
// but IndexMut does not work in const context.
236-
let dst_bytes: &mut [u8] =
237-
bytemuck::must_cast_slice_mut(core::slice::from_mut(&mut buffer[i]));
248+
let dst_bytes: &mut [u8] = cast_slice_mut(core::slice::from_mut(&mut buffer[i]));
238249

239250
// `copy_from_slice` can be used here when MSRV moves past 1.87
240251
let mut j = 0;
@@ -257,7 +268,7 @@ impl<const LIMBS: usize> EncodedUint<LIMBS> {
257268
// We could cast the whole `buffer` to bytes at once,
258269
// but IndexMut does not work in const context.
259270
let dst_bytes: &mut [u8] =
260-
bytemuck::must_cast_slice_mut(core::slice::from_mut(&mut buffer[LIMBS - 1 - i]));
271+
cast_slice_mut(core::slice::from_mut(&mut buffer[LIMBS - 1 - i]));
261272

262273
// `copy_from_slice` can be used here when MSRV moves past 1.87
263274
let mut j = 0;
@@ -280,13 +291,13 @@ impl<const LIMBS: usize> Default for EncodedUint<LIMBS> {
280291

281292
impl<const LIMBS: usize> AsRef<[u8]> for EncodedUint<LIMBS> {
282293
fn as_ref(&self) -> &[u8] {
283-
bytemuck::must_cast_slice(&self.0)
294+
cast_slice(&self.0)
284295
}
285296
}
286297

287298
impl<const LIMBS: usize> AsMut<[u8]> for EncodedUint<LIMBS> {
288299
fn as_mut(&mut self) -> &mut [u8] {
289-
bytemuck::must_cast_slice_mut(&mut self.0)
300+
cast_slice_mut(&mut self.0)
290301
}
291302
}
292303

0 commit comments

Comments
 (0)