Skip to content

Commit

Permalink
Merge pull request #270 from DaniPopes/byte-fn
Browse files Browse the repository at this point in the history
feat: add `Uint::byte`
  • Loading branch information
gakonst authored Jul 25, 2023
2 parents 1519f44 + 8d6ea93 commit e4a7270
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Introduce `ark-ff-04` feature flag for conversion to `[email protected]` types.
### Added

- Introduce `ark-ff-04` feature flag for conversion to `[email protected]` types
- Support for [`alloy-rlp`](https://github.com/alloy-rs/rlp)
- MSRV (Minimum Supported Rust Version) is now set at 1.65.0, from previously undefined.
- MSRV (Minimum Supported Rust Version) is now set at 1.65.0, from previously undefined
- Implement `TryFrom<bool>` for `Uint`
- New method: `byte`

### Changed

- Make `serde::Deserialize` impl more permissive
- Use Ethereum `Quantity` encoding for serde serialization when human-readable
- Fix error in `from_base_be` that allowed instantiation of overflowing `Uint`.
- Updated `fastrlp` to `0.3`, `pyo3` to `0.19`, and `sqlx-core` to `0.7`.
- Fix error in `from_base_be` that allowed instantiation of overflowing `Uint`
- Updated `fastrlp` to `0.3`, `pyo3` to `0.19`, and `sqlx-core` to `0.7`
- Improved `fastrlp` perfomance
- Improved `proptest` performance
- Made `support` module and its modules public
Expand All @@ -28,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

- Automatic detection of nightly features. Enable them instead with the `nightly` cargo feature.
- Automatic detection of nightly features. Enable them instead with the `nightly` cargo feature
- Dependency on `derive_more`

### Fixed
Expand Down
49 changes: 47 additions & 2 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use core::ops::{
};

impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// Returns whether a specific bit is set.
///
/// Returns `false` if `index` exceeds the bit width of the number.
#[must_use]
#[inline]
pub const fn bit(&self, index: usize) -> bool {
if index >= BITS {
return false;
Expand All @@ -14,6 +18,8 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
self.limbs[limbs] & (1 << bits) != 0
}

/// Sets a specific bit to a value.
#[inline]
pub fn set_bit(&mut self, index: usize, value: bool) {
if index >= BITS {
return;
Expand All @@ -26,6 +32,46 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
}
}

/// Returns a specific byte. The byte at index `0` is the least significant
/// byte (little endian).
///
/// # Panics
///
/// Panics if `index` exceeds the byte width of the number.
///
/// # Examples
///
/// ```
/// # use ruint::uint;
/// let x = uint!(0x1234567890_U64);
/// let bytes = [
/// x.byte(0), // 0x90
/// x.byte(1), // 0x78
/// x.byte(2), // 0x56
/// x.byte(3), // 0x34
/// x.byte(4), // 0x12
/// x.byte(5), // 0x00
/// x.byte(6), // 0x00
/// x.byte(7), // 0x00
/// ];
/// assert_eq!(bytes, x.to_le_bytes());
/// ```
///
/// Panics if out of range.
///
/// ```should_panic
/// # use ruint::uint;
/// let x = uint!(0x1234567890_U64);
/// let _ = x.byte(8);
/// ```
#[inline]
#[must_use]
#[track_caller]
#[allow(clippy::cast_possible_truncation)] // intentional
pub const fn byte(&self, index: usize) -> u8 {
(self.limbs[index / 8] >> ((index % 8) * 8)) as u8
}

/// Reverses the order of bits in the integer. The least significant bit
/// becomes the most significant bit, second least-significant bit becomes
/// second most-significant bit, etc.
Expand Down Expand Up @@ -660,8 +706,7 @@ mod tests {
}

#[test]
#[allow(clippy::cast_lossless)]
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_lossless, clippy::cast_possible_truncation)]
fn test_small() {
const_for!(BITS in [1, 2, 8, 16, 32, 63, 64] {
type U = Uint::<BITS, 1>;
Expand Down

0 comments on commit e4a7270

Please sign in to comment.