nervosnetwork/ckb

A better implementation for `RationalU256`.

Open

#3,543 opened on Aug 11, 2022

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Rust (1,210 stars) (260 forks)user submission
good first issuestalet:enhancement

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:

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

A better implementation for `RationalU256`. · nervosnetwork/ckb#3543 | Good First Issue