用于RSA密码系统的蒙哥马利模乘的最终减法

时间:2017-08-10 17:29:31

标签: cryptography rsa public-key-encryption modular-arithmetic montgomery-multiplication

当在模幂运算算法中使用时,我对如何绕过radix-2 montgomery modular multiplication中模数的最终减法感到困惑。以下两篇论文提出了绕过减法的条件。

我不完全清楚在“预处理和后处理”方面需要什么来消除在蒙哥马利乘法结束时重复减去模数的需要。

阅读上述论文后,我的理解是,为了消除最终的减法,你必须:

  1. 将每个输入操作数零扩展到模幂运算两个

    e.g. new[2049 downto 0]  = (0, 0, old[2047 downto 0]) 
    
  2. 将蒙哥马利乘法中的循环限制增加2,这样就可以执行两次循环迭代
  3. 我已对工作算法进行了这些修改,但结果并不像我预期的那样,我不明白为什么。 因此,我认为我在这些文章中误解了一些内容,或者没有采取关键步骤。

    让我们在(类C伪代码)中引用我的(工作)radix-2蒙哥马利模幂运算函数。请注意,我已将操作数宽度扩展了两个最重要的零数字(只是为了确保我没有溢出)。它们过去只有2048位。

    let NUM_BITS = 2048
    let rsaSize_t be a 2050-bit vector type
    
    // Montgomery multiplication: outData = XYr^(-1) modulo M,     
    // where the radix r=2^n    (n=NUM_BITS) 
    function montMult( rsaSize_t X,       // Multiplier
                       rsaSize_t Y,       // Multiplicand
                       rsaSize_t M,       // Modulus
                       rsaSize_t outData) // Result
    {
        rsaSize_t S = 0;  // Running sum
    
        for (i=0; i<NUM_BITS; i++)
        {
            if (X.bit(i)==1) // Check ith bit of X
                S += Y;
    
            if (S.bit(0)==1) // check LSB of S
                S += M;
    
            S = S >> 1;   // Rightshift 1 bit
        }
    
        // HERE IS THE FINAL SUBTRACTION I WANT (NEED) TO AVOID
        if (S >= M)
        {
            S -= M;
        }
    
        outData = S.range(NUM_BITS-1,0);
    }
    
    
    //  montgomery modular exponentiation using square and multiply algorithm
    //  computes  M^e modulo n, where we precompute the transformation of the 
    //  base and running-partial sum into the montgomery domain 
    function rsaModExp( rsaSize_t e,     // exponent 
                        rsaSize_t n,     // modulus
                        rsaSize_t Mbar,  // precomputed: montgomery residue of the base w.r.t. the radix--> (2^2048)*base mod n 
                        rsaSize_t xbar,  // precomputed: montgomery residue of 1  w.r.t. the radix--> 2^2048 mod n                 
                        rsaSize_t *out)  // result
    {
        for (i=NUM_BITS-1; i>=0; i--)
        {
            montMult(xbar, xbar, n, xbar); // square
            if (e.bit(i)==1) 
                montMult(Mbar, xbar, n, xbar); // multiply
        }
    
        // undo montgomery transform
        montMult(xbar, 1, n, out);
    }
    

    我在文件中遗漏了什么吗?我不相信这是一个实现错误,因为我的代码完全符合论文中提出的内容。我相信我可能是一个概念错误。任何和所有帮助表示赞赏。

    谢谢!

1 个答案:

答案 0 :(得分:2)

不确定你的非工作实现有什么问题(如果我理解得很好,你展示的是一个有效的实现)。要使用Walter优化计算M^e mod n,如果您的数字全部符合2048位,则需要:

let NUM_BITS = 2050            // 2048 + 2
n < 2^(NUM_BITS - 2)           // precondition
M < 2 * n                      // precondition
let R = 2^(2 * NUM_BITS) mod n // pre-computed once for all
let M' = montMult(M, R, n)     // bring M in Montgomery form
let C' = montExp(M', e, n)     // Montgomery exponentiation
let C = montMult(C', 1, n)     // bring C' in normal form

最重要的:不要忘记检查前提条件。

蒙哥马利乘法包括NUM_BITS(在你的情况下为2050)迭代(if-A-bit-set-add-B,if-odd-add-n,div-by-two),最低有效位首先,所有数字都显示在NUM_BITS(在您的情况下为2050)位。

蒙哥马利指数也包括NUM_BITS(在你的情况下是2050)迭代(square,if-e-bit-set-mult),最重要的位是第一位,所有数字都在NUM_BITS上表示(在你的情况下是2050)位。希望它有所帮助。