nervosnetwork/ckb

A better implementation for `RationalU256`.

Open

#3,543 opened on 2022年8月11日

GitHub で見る
 (3 comments) (0 reactions) (0 assignees)Rust (260 forks)user submission
good first issuestalet:enhancement

Repository metrics

Stars
 (1,210 stars)
PR merge metrics
 (平均マージ 13d 23h) (30d で 29 merged PRs)

説明

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

コントリビューターガイド