【问题标题】:Modular multiplicative inverse function for big (negative) numbers大(负)数的模乘反函数
【发布时间】:2021-09-21 02:37:55
【问题描述】:

模乘逆在密码学中广泛使用。我有 following program 用于使用扩展欧几里得算法计算模乘逆:

extern crate num;
use num::bigint::BigInt;
use num::Integer;
use num::One;
use num::Zero;

fn modinv(n: &BigInt, p: &BigInt) -> BigInt {
    if p.is_one() { return BigInt::one() }

    let (mut a, mut m, mut x, mut inv) = (n.clone(), p.clone(), BigInt::zero(), BigInt::one());

    while a > BigInt::one() {
        let (div, rem) = a.div_rem(&m);
        inv -= div * &x;
        a = rem;
        std::mem::swap(&mut a, &mut m);
        std::mem::swap(&mut x, &mut inv);
    }
 
    if inv < BigInt::zero() { inv += p }

    inv
}

fn main() {

    let n = BigInt::parse_bytes(b"-243772585612020160733370897338805215918303827399330592839196552441720391139", 10).unwrap();
    let p = BigInt::parse_bytes(b"115792089237316195423570985008687907853269984665640564039457584007908834671663", 10).unwrap();

    println!("modinv({0}, {1}) = {2}", n, p, modinv(&n, &p));
}

这对于正的np 来说很好,但是当n 像上述情况一样为负时,我会得到以下输出:

modinv(-243772585612020160733370897338805215918303827399330592839196552441720391139, 115792089237316195423570985008687907853269984665640564039457584007908834671663) = 1

1 的输出不正确,我想要以下输出(使用 python shell):

In [1]: n = -243772585612020160733370897338805215918303827399330592839196552441720391139

In [2]: p = 115792089237316195423570985008687907853269984665640564039457584007908834671663

In [3]: pow(n, -1, p)
Out[3]: 78090076461723887468177075808811701300309702327169440891599636163808855875538

有没有办法改变上面的 modinv 函数,使其也像 python 一样处理负数?

【问题讨论】:

    标签: rust modulo inverse


    【解决方案1】:

    amxinv 的定义正下方添加行while a &lt; BigInt::zero() { a += p } 应该可以解决问题,使用a % m == a + m % m 这一事实。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 2016-06-10
      相关资源
      最近更新 更多