Bristol's first 23 Cryptography

Cryptography thing 52

Number 23: write a C program to achieve Montgomery algorithm

The blog I will go through the actual implementation of a Montgomery algorithm, to complement the theoretical aspects of our week Montgomery algorithm. This implementation in C Montgomery algorithm, the computer 64 is written as a digit. Modulus \ (m \) it is possible to, and \ (2 ^ {64} -1 \) as large, \ (A \) and \ (B \) energy and \ (m-1 \) as large. We use \ (R & lt = 2 ^ {64} \) . In the previous blog, most of the information given comes from [1], so please refer to the information herein.

After read the last blog, you know we need four steps. For our purposes, we refer to these three phases.

1.The GCD Operation

This function performs a binary Extended Euclidean Algorithm to find a \ (r ^ {- 1} \) and \ (m ^ { '} \ ) such that \ (rr ^ {- 1} = 1 + mm ^ { '} \) . These algorithms integer later need to use. Algorithm \ (r ^ {- 1} \) and \ (m ^ { '} \ ) is calculated \ (m, m ^ {'} \) , the purpose of this description is not a binary blog Extended Euclidean Algorithm . You want to know more can see the link [1] and [2].

2.Transform the Multipliers

The second phase is to calculate two values \ (abar = ar \ mod m \) and \ (br BBAR = \ MOD m \) . Since \ (R & lt = 2 ^ {64} \) , where only 64-bit right shift . It is the output 128, the first 64 bits are \ (a, b \) values, after all 64 0. Then calculated \ (m \) modulus. This function accepts a 64-bit \ (X \) , while receiving the low 64-bit \ (Y \) and a \ (m \) values. After returning a 64-bit value.

uint64 modul64(uint64 x, uint64 y, uint64 z);

uint64It is defined as:

typedef unsigned long long uint64;

3.Montgomery Multiplication

This function is defined to receive 64-bit abar, bbar, m and mprime. Then return to 64-bit value.

First calculate \ (T * = Abar BBAR \) . The get a 128-bit integer.

Then calculated \ (U = (T + ((TM) ^ { '} \ R & lt MOD) * m) / R & lt \) . \ (T \) is a 128-bit integer. Here you can calculate. It is defined as follows:

tm = tlo*mprime;
mulul64(tm,m,&tmmhi,%tmmlo);

Then calculate:

ulo = tlo + tmmlo;
uhi = thi + tmmhi;
if (ulo < tlo) uhi = uhi +1; // test for overflow from ulo and add if necessary to uhi
ov = (uhi < thi) | ((uhi == thi) & (ulo < tlo)); // check for carry

The final step is reduced to about \ (m \) .

ulo = uhi;
uhi = 0;
if(ov > 0 || ulo >= m) // test if there was overflow or ulo is higher that
             ulo = ulo – m;
return ulo;
4.The Inverse Transformation

Finally calculate \ (a * b \ mod m = ur ^ {- 1} \ mod m \)

Before calling the function.

mulul64(p, rinv, &phi, &plo); // performs multiplication and returns two 64 bit values phi and plo
p = modul64(phi, plo, m); // returns value of 128bit input mod m

Here \ (p \) is the result of the Montgomery algorithm.

[1] http://www.hackersdelight.org/MontgomeryMultiplication.pdf
[2] http://www.ucl.ac.uk/~ucahcjm/combopt/ext_gcd_python_programs.pdf

Guess you like

Origin www.cnblogs.com/zhuowangy2k/p/10962516.html