nervosnetwork / nervosnetwork/ckb

A better implementation for `RationalU256`.

Open
#3,543 3 comments 0 reactions 0 assignees View on GitHub
good first issue stale t:enhancement
Dominant language
Rust
Stars
1.2k
Forks
266
Avg merge
10d 5h
Merged PRs (30d)
4

Description

## Issue

Panicked at 'U256: attempt to multiply with overflow' for some values.

https://github.com/nervosnetwork/ckb/blob/649389d90a142cf39cdbc2019b8fbde188383ca5/util/rational/src/lib.rs#L128-L140

### Reproduce

Multiply two `RationalU256` numbers, when both two numerators (or denominators) are big but their greatest common divisor is small.

For example:

```rust
use ckb_types::{core::RationalU256, u256, U256};
fn main() {
let x = u256!("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
let x_rational = RationalU256::from_u256(x.clone());
let numerator = 33u32;
let denominator = 100u32;
let y_rational = RationalU256::new(U256::from(numerator), U256::from(denominator));
let z_rational = x_rational * y_rational; // panicked in this line!
let z = z_rational.into_u256();
println!("{:#x} = {:#x} * {}/{}", z, x, numerator, denominator);
}
```

Ref: nervosnetwork/ckb-light-client#44

Contributor guide

Open the contributing guide

Research direction

Start in util/rational/src/lib.rs at lines 128-140 and reproduce the panic with the example using RationalU256 and large U256 values. Read the multiplication path and inspect how the greatest common divisor is handled when numerators or denominators are large. Done means the example no longer panics and produces the expected value from into_u256().

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
blockchain
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.