Giter Site home page Giter Site logo

Define `U256` type about lambdaworks HOT 10 CLOSED

lambdaclass avatar lambdaclass commented on August 16, 2024
Define `U256` type

from lambdaworks.

Comments (10)

GianfrancoBazzani avatar GianfrancoBazzani commented on August 16, 2024 1

Here:
https://github.com/GianfrancoBazzani/lambdaworks/blob/fb61e11a73a326f0807bc8385e28d37eb737b6b3/math/src/unsigned_integer/element.rs#L1464

from lambdaworks.

ilitteri avatar ilitteri commented on August 16, 2024 1

Here: https://github.com/GianfrancoBazzani/lambdaworks/blob/fb61e11a73a326f0807bc8385e28d37eb737b6b3/math/src/unsigned_integer/element.rs#L1464

I think that you've just found a bug. @MauroToscano @ajgara @schouhy take a look when you can and tell me if I'm mistaken.

I think that you should make the PR with the test passing and file an issue related to this so it can be studied later.

from lambdaworks.

GianfrancoBazzani avatar GianfrancoBazzani commented on August 16, 2024

Can i take it?

from lambdaworks.

ilitteri avatar ilitteri commented on August 16, 2024

Can i take it?

Sure, we'd love it!

from lambdaworks.

GianfrancoBazzani avatar GianfrancoBazzani commented on August 16, 2024

Hi, thanks! Where should be implemented? in the same math/src/unsigned_integer/element.rs file?

from lambdaworks.

GianfrancoBazzani avatar GianfrancoBazzani commented on August 16, 2024

Hello :), in principle, I have done and tested. I defined a new type pub type U256 = UnsignedInteger<4>; (4 limbs * 64 = 256) and the tests for all implemented methods. There is only one test that I cannot pass,related with the multiplication.

Test code failing below:

#[test]
    fn mul_two_256_bit_integers_works_4() {
        let a = U256::from("15bf61fcf53a3f0ae1e8e555d");
        let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f");
        let c_expected = U256::from(
            "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3",
        );

        assert_eq!(a * b, c_expected);
    }

With error:

mul_two_256_bit_integers_works_4' panicked at 'UnsignedInteger multiplication overflow.'

The fact is if i use the mul method instead of * operator the tests passes fine, and if i debug the values there really is no overflow.

Test code passing below:

 #[test]
    fn mul_two_256_bit_integers_works_4() {
        let a = U256::from("15bf61fcf53a3f0ae1e8e555d");
        let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f");
        let c_expected = U256::from(
            "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3",
        );

        let (overflow, c) = U256::mul(&a, &b);
        dbg!(overflow);
        dbg!(c);
        assert_eq!(c, c_expected);
    }

with debug output:

overflow = UnsignedInteger { limbs: [ 0, 0, 0, 0,],}
c = UnsignedInteger {limbs: [1247146686250749232,13248668456779837188,14705552823470605556,5165987579446635203,]}

Question:
As can be seen there is any overflow. Since 'UnsignedInteger' implements the 'Mul' trait, shouldn't the mul method and the * operator behave the same?

from lambdaworks.

ilitteri avatar ilitteri commented on August 16, 2024

Hi, thanks! Where should be implemented? in the same math/src/unsigned_integer/element.rs file?

It would be nice to add the line above the U384 definition.

EDIT: It's ok where you put it.

from lambdaworks.

ilitteri avatar ilitteri commented on August 16, 2024

Hello :), in principle, I have done and tested. I defined a new type pub type U256 = UnsignedInteger<4>; (4 limbs * 64 = 256) and the tests for all implemented methods. There is only one test that I cannot pass,related with the multiplication.

Test code failing below:

#[test]
    fn mul_two_256_bit_integers_works_4() {
        let a = U256::from("15bf61fcf53a3f0ae1e8e555d");
        let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f");
        let c_expected = U256::from(
            "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3",
        );

        assert_eq!(a * b, c_expected);
    }

With error:

mul_two_256_bit_integers_works_4' panicked at 'UnsignedInteger multiplication overflow.'

The fact is if i use the mul method instead of * operator the tests passes fine, and if i debug the values there really is no overflow.

Test code passing below:

 #[test]
    fn mul_two_256_bit_integers_works_4() {
        let a = U256::from("15bf61fcf53a3f0ae1e8e555d");
        let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f");
        let c_expected = U256::from(
            "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3",
        );

        let (overflow, c) = U256::mul(&a, &b);
        dbg!(overflow);
        dbg!(c);
        assert_eq!(c, c_expected);
    }

with debug output:

overflow = UnsignedInteger { limbs: [ 0, 0, 0, 0,],}
c = UnsignedInteger {limbs: [1247146686250749232,13248668456779837188,14705552823470605556,5165987579446635203,]}

Question: As can be seen there is any overflow. Since 'UnsignedInteger' implements the 'Mul' trait, shouldn't the mul method and the * operator behave the same?

To answer your question, if I'm not mistaken, yes. I'll look into this, it could be a nice bug to issue.

Where can I find this code?

from lambdaworks.

schouhy avatar schouhy commented on August 16, 2024

Thanks for pointing this out! It should be fixed now. Let us know how it goes!

from lambdaworks.

ilitteri avatar ilitteri commented on August 16, 2024

Resolved in #50

from lambdaworks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.