briansmith / ring

Safe, fast, small crypto using Rust
Other
3.69k stars 697 forks source link

bigint: Improve temporary array alignment. #1892

Closed briansmith closed 8 months ago

briansmith commented 8 months ago

Replace pointer-to-usize as conversions with use of align_offset. This avoids any dependency on the way pointers are encoded.

Previously when the buffer was already aligned the way we needed it, we'd treat the buffer as being misaligned by ALIGNMENT bytes. This wasn't problematic but it wasn't optimal. By using align_offset, we'll recognize it as being misaligned by 0 bytes, i.e. already aligned.

Previously we were allocating ALIGNMENT extra Limbs to have slack for alignment where we could have allocated ALIGNMMENT / LIMB_BYTES extra limbs. Now, since we make the minimum alignment adjustment necessary, we can allocate 1 less limb than that.

Be explicit that the type of the buffer elements is Limb. Use core::mem::{align_of}::size_of::<Limb>() instead of LIMB_BYTES.

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (c4742e0) 96.02% compared to head (00c2368) 96.02%. Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1892 +/- ## ======================================= Coverage 96.02% 96.02% ======================================= Files 136 136 Lines 20776 20784 +8 Branches 226 226 ======================================= + Hits 19950 19958 +8 Misses 792 792 Partials 34 34 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

briansmith commented 8 months ago

@joshlf Are you around? This isn't dealing with unsafe exactly but it seems like you'd be the best person to review it.