nervosnetwork/ckb

A better implementation for `RationalU256`.

Open

#3.543 aberto em 11 de ago. de 2022

Ver no GitHub
 (3 comments) (0 reactions) (0 assignees)Rust (260 forks)user submission
good first issuestalet:enhancement

Métricas do repositório

Stars
 (1.210 stars)
Métricas de merge de PR
 (Mesclagem média 13d 23h) (29 fundiu PRs em 30d)

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

Guia do colaborador