Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Modpow -1^2 == -1 #121

Closed
jounathaen opened this issue Jan 1, 2020 · 1 comment
Closed

Bug: Modpow -1^2 == -1 #121

jounathaen opened this issue Jan 1, 2020 · 1 comment

Comments

@jounathaen
Copy link

The following code calculates a^i for i = 1,2,3,4:

extern crate num_bigint;
extern crate num_traits;
use num_bigint::BigInt;

fn main()
{
    let a = BigInt::from(-1);
    let m = BigInt::from(10);
    
    println!("a = {}", a);
    let a_pow_1 = a.modpow(&BigInt::from(1), &m);
    println!("a^1 mod 10 = {}", a_pow_1);
    let a_pow_2 = a.modpow(&BigInt::from(2), &m);
    println!("a^2 mod 10 = {}", a_pow_2);
    let a_pow_3 = a.modpow(&BigInt::from(3), &m);
    println!("a^3 mod 10 = {}", a_pow_3);
    let a_pow_4 = a.modpow(&BigInt::from(4), &m);
    println!("a^4 mod 10 = {}", a_pow_4);
    
    let a_times_a = (&a * &a) % &m;
    println!("a*a mod 10 = {}", a_times_a);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7de58584f7dff3342bf0c8cce191bfb7

Expected Output:

a = -1
a^1 mod 10 = 9
a^2 mod 10 = 1
a^3 mod 10 = 9
a^4 mod 10 = 1
a*a mod 10 = 1

Real Output:

a = -1
a^1 mod 10 = 9
a^2 mod 10 = 9
a^3 mod 10 = 9
a^4 mod 10 = 9
a*a mod 10 = 1

I don't know, if my understanding of modulo calculus is correct, but from all I know, I'd assume, that -1^2 == 1 and that a * a == a^2 regardless of the modulus.

@cuviper
Copy link
Member

cuviper commented Jan 1, 2020

Sorry, this was fixed by #114, but I had neglected to publish a new release. Please try 0.2.4.

@cuviper cuviper closed this as completed Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants