Skip to content

Commit

Permalink
Merge pull request #255 from DaniPopes/fix-from-base-le
Browse files Browse the repository at this point in the history
fix: `from_base_le`
  • Loading branch information
prestwich authored Jun 1, 2023
2 parents 9538ff4 + 0e38504 commit 35167fb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix error in `from_base_be` that allowed instantiation of overflowing `Uint`.
- Updated `ark` to `0.4`, `fastrlp` to `0.3` and `pyo3` to `0.18`.

### Fixed

- `from_base_le` implementation by reversing the input iterator

## [1.8.0] — 2023-04-19

### Added
Expand Down
46 changes: 43 additions & 3 deletions src/base_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
base: u64,
digits: I,
) -> Result<Self, BaseConvertError> {
let digits: Vec<_> = digits.into_iter().collect();
let mut digits: Vec<_> = digits.into_iter().collect();
digits.reverse();
Self::from_base_be(base, digits)
}

Expand Down Expand Up @@ -170,7 +171,7 @@ mod tests {
]);

#[test]
fn test_base_le() {
fn test_to_base_le() {
assert_eq!(
Uint::<64, 1>::from(123456789)
.to_base_le(10)
Expand All @@ -188,8 +189,28 @@ mod tests {
]
);
}

#[test]
fn test_from_base_le() {
assert_eq!(
Uint::<64, 1>::from_base_le(10, [9, 8, 7, 6, 5, 4, 3, 2, 1]),
Ok(Uint::<64, 1>::from(123456789))
);
assert_eq!(
Uint::<256, 4>::from_base_le(10000000000000000000_u64, [
2372330524102404852,
0534330209902435677,
7066324924582287843,
0630363884335538722,
9
])
.unwrap(),
N
);
}

#[test]
fn test_base_be() {
fn test_to_base_be() {
assert_eq!(
Uint::<64, 1>::from(123456789)
.to_base_be(10)
Expand All @@ -208,6 +229,25 @@ mod tests {
);
}

#[test]
fn test_from_base_be() {
assert_eq!(
Uint::<64, 1>::from_base_be(10, [1, 2, 3, 4, 5, 6, 7, 8, 9]),
Ok(Uint::<64, 1>::from(123456789))
);
assert_eq!(
Uint::<256, 4>::from_base_be(10000000000000000000_u64, [
9,
0630363884335538722,
7066324924582287843,
0534330209902435677,
2372330524102404852
])
.unwrap(),
N
);
}

#[test]
fn test_from_base_be_overflow() {
assert_eq!(
Expand Down

0 comments on commit 35167fb

Please sign in to comment.