📄 example.c
字号:
EPS_SetBufferWord(hBigModulus, 13, EPS_kgFULLWORD, 0xB0F08B14);
EPS_SetBufferWord(hBigModulus, 14, EPS_kgFULLWORD, 0xE393F6AA);
EPS_SetBufferWord(hBigModulus, 15, EPS_kgFULLWORD, 0xD411A4A0); /* MSW */
/* Modulus is odd, so we can use our faster Montgomery Math approach:
** Find n0' for the new modulus: */
n0 = EPS_GetBufferWord(hBigModulus, 0, EPS_kgFULLWORD);
EPS_FindN0Prime(n0, &n0prime); /* Should be 0x0FF79239 for this modulus */
/* Now let's get the Montgomery Representations of bigbase: */
EPS_MontRep(hBigBase, hBigBasePrime, hBigModulus, kBIGOPLEN);
/* Now do the exponentiation: */
EPS_MontExp(hBigBasePrime, hExponent, hBigResult, hBigModulus, n0prime, 5, kBIGOPLEN);
/* Result should be:
** B8718CC3 ACD51B5D 125677EB 92892BAF E768AF50 6C83EDF1 717EDB6F AC2DFAD1
** DF65C846 F6B013B8 7475AED5 F6ADDDB7 2661A761 FA21D659 241584D8 16976C36. */
/* ===============================================================
** Exponentiation with an Even Modulus:
** =============================================================== */
/* Note that in the above exponentiations, the modulus was odd. This is the usual
** case in cryptographic applications, since it allows us to take advantage of
** some faster math algorithms (Montgomery Math). If, however, we have an even
** modulus, we can use the special EPS_ExponentiateEven() function to do the
** calculation. Let's load our operands, but we'll clear the LSB of the modulus
** to make it even: */
/* Use B49E888D 945A4A20 4D4CC654 0396C2BE D081B582 for operand1 (the base): */
EPS_SetBufferWord(hOp1, 0, EPS_kgFULLWORD, 0xD081B582); /* LSW */
EPS_SetBufferWord(hOp1, 1, EPS_kgFULLWORD, 0x0396C2BE);
EPS_SetBufferWord(hOp1, 2, EPS_kgFULLWORD, 0x4D4CC654);
EPS_SetBufferWord(hOp1, 3, EPS_kgFULLWORD, 0x945A4A20);
EPS_SetBufferWord(hOp1, 4, EPS_kgFULLWORD, 0xB49E888D); /* MSW */
/* We'll use C90260FF 4A27EB5B for the exponent: */
EPS_SetBufferWord(hExponent, 0, EPS_kgFULLWORD, 0x4A27EB5B); /* LSW */
EPS_SetBufferWord(hExponent, 1, EPS_kgFULLWORD, 0xC90260FF); /* MSW */
/* Initialize the modulus buffer to:
** D46DD318 96D2FC6F E9343A13 B1F2FAE0 3E7288AA. Note that
** the modulus is even. */
EPS_SetBufferWord(hModulus, 0, EPS_kgFULLWORD, 0x3E7288AA); /* LSW */
EPS_SetBufferWord(hModulus, 1, EPS_kgFULLWORD, 0xB1F2FAE0);
EPS_SetBufferWord(hModulus, 2, EPS_kgFULLWORD, 0xE9343A13);
EPS_SetBufferWord(hModulus, 3, EPS_kgFULLWORD, 0x96D2FC6F);
EPS_SetBufferWord(hModulus, 4, EPS_kgFULLWORD, 0xD46DD318); /* MSW */
/* Now the exponentiation: */
EPS_ExponentiateEven(hOp1, hExponent, hResult, hModulus, 2, kOPLEN);
/* The result is 4CC44E08 F3962A83 D0506215 CC29A86D D6CAB55A. */
/* Here's another exponentiation with an even modulus:
** Base: 8C33EC45 89C9680A D8E4D382 AC23B8CD FF2B56FD
** Exp: 7EC569B7 B1FD2DD8 6C438B0D
** Mod: FC40CD75 B2326AE7 BC0646F4 5B486EF6 F1E81F1C */
EPS_SetBufferWord(hOp1, 0, EPS_kgFULLWORD, 0xFF2B56FD); /* LSW */
EPS_SetBufferWord(hOp1, 1, EPS_kgFULLWORD, 0xAC23B8CD);
EPS_SetBufferWord(hOp1, 2, EPS_kgFULLWORD, 0xD8E4D382);
EPS_SetBufferWord(hOp1, 3, EPS_kgFULLWORD, 0x89C9680A);
EPS_SetBufferWord(hOp1, 4, EPS_kgFULLWORD, 0x8C33EC45); /* MSW */
EPS_SetBufferWord(hExponent, 0, EPS_kgFULLWORD, 0x6C438B0D); /* LSW */
EPS_SetBufferWord(hExponent, 1, EPS_kgFULLWORD, 0xB1FD2DD8);
EPS_SetBufferWord(hExponent, 2, EPS_kgFULLWORD, 0x7EC569B7); /* MSW */
EPS_SetBufferWord(hModulus, 0, EPS_kgFULLWORD, 0xF1E81F1C); /* LSW */
EPS_SetBufferWord(hModulus, 1, EPS_kgFULLWORD, 0x5B486EF6);
EPS_SetBufferWord(hModulus, 2, EPS_kgFULLWORD, 0xBC0646F4);
EPS_SetBufferWord(hModulus, 3, EPS_kgFULLWORD, 0xB2326AE7);
EPS_SetBufferWord(hModulus, 4, EPS_kgFULLWORD, 0xFC40CD75); /* MSW */
EPS_ExponentiateEven(hOp1, hExponent, hResult, hModulus, 3, kOPLEN);
/* Result: 85E77086 BADE14C7 A2F9C7A4 1D313E15 41504A8D */
/* =======================================================================
** Exponetiation without a Modulus:
** ======================================================================= */
/* We can also exponentiate without a modulus. Of course, things will overflow
** very quickly in this case, so we need to pick operands small enough such that
** the result will fit in our 5-word (160-bit) result buffer:
** Use 0000019E for the base, and 0011 for the exponent: */
EPS_SetBufferWord(hOp1, 0, EPS_kgFULLWORD, 0x0000019E); /* LSW */
EPS_SetBufferWord(hOp1, 1, EPS_kgFULLWORD, 0);
EPS_SetBufferWord(hOp1, 2, EPS_kgFULLWORD, 0);
EPS_SetBufferWord(hOp1, 3, EPS_kgFULLWORD, 0);
EPS_SetBufferWord(hOp1, 4, EPS_kgFULLWORD, 0);
EPS_SetBufferWord(hExponent, 0, EPS_kgFULLWORD, 0x00000011); /* LSW */
EPS_SetBufferWord(hExponent, 1, EPS_kgFULLWORD, 0); /* MSW */
EPS_Exponentiate(hOp1, hExponent, hResult, 1, kOPLEN);
/* Result is 000DD35F 9A67C42A 653A5AF9 5298382D 8B9E0000 */
/* ================================================================================
** Greatest Common Divisor:
** ================================================================================ */
/* The Greatest Common Divisor function EPS_GCD() can be used to solve equations of
** the form r*r^1 - n*n' = d. Given r and n, the EPS_GCD() function solves the equation
** using an extended Euclidean algorithm to return r^1, and n'. The EPS_GCD() function
** has the restriction that r must be greater than n, and r cannot be a multiple of n.
** In a typical cryptographic application, these restrictions are not a problem, since
** r and n are chosen to be relatively prime, with r being a power of 2 greater than n.
** Now we'll try a 160-bit GCD. We'll need storage for the r, r^1, n, n', and d operands.
** We'll need one extra word, due to the fact that we'll pick r to be the next power of
** 2 greater than n: */
hR = EPS_CreateBuffer(kOPLEN+1);
hRinverse = EPS_CreateBuffer(kOPLEN+1);
hN = EPS_CreateBuffer(kOPLEN+1);
hNPrime = EPS_CreateBuffer(kOPLEN+1);
hD = EPS_CreateBuffer(kOPLEN+1);
/* Now load r and n, as follows:
** r: 0x00000001 00000000 00000000 00000000 00000000 00000000
** n: 0x00000000 D46DD318 96D2FC6F E9343A13 B1F2FAE0 3E7288AB */
EPS_SetBufferWord(hR, 0, EPS_kgFULLWORD, 0x00000000);
EPS_SetBufferWord(hR, 1, EPS_kgFULLWORD, 0x00000000);
EPS_SetBufferWord(hR, 2, EPS_kgFULLWORD, 0x00000000); /* NOTE: We could simply clear the hR buffer, */
EPS_SetBufferWord(hR, 3, EPS_kgFULLWORD, 0x00000000); /* then set word 5 to 0x00000001. */
EPS_SetBufferWord(hR, 4, EPS_kgFULLWORD, 0x00000000);
EPS_SetBufferWord(hR, 5, EPS_kgFULLWORD, 0x00000001);
EPS_SetBufferWord(hN, 0, EPS_kgFULLWORD, 0x3E7288AB); /* LSW (Note: This word is n0) */
EPS_SetBufferWord(hN, 1, EPS_kgFULLWORD, 0xB1F2FAE0);
EPS_SetBufferWord(hN, 2, EPS_kgFULLWORD, 0xE9343A13);
EPS_SetBufferWord(hN, 3, EPS_kgFULLWORD, 0x96D2FC6F);
EPS_SetBufferWord(hN, 4, EPS_kgFULLWORD, 0xD46DD318); /* MSW */
EPS_SetBufferWord(hN, 5, EPS_kgFULLWORD, 0x00000000); /* Padding to bring buffer length up to r's length */
/* Now solve the equation: */
EPS_GCD(hR, hN, hRinverse, hNPrime, hD, (kOPLEN+1));
/* The r^1 result is: 0x278D8C2D C90F96E1 FF962DE0 155C4078 A6B26CCB.
** The n' result is: 0x2FAA6235 15152CEB BCC864EE CA0B229C 101ACDFD.
** The d result is: 0x00000000 00000000 00000000 00000000 00000001. */
/* Let's try another, but this time the operands are only 5 words long.
** Load r and n, as follows:
** r: 0xEC659EC5 59042634 7ADF382A FE642163 0765586B
** n: 0x392795A3 5D0BE03E 7A5E30CD AF1EBF55 9D2AC839 */
EPS_SetBufferWord(hR, 0, EPS_kgFULLWORD, 0x0765586B); /* LSW */
EPS_SetBufferWord(hR, 1, EPS_kgFULLWORD, 0xFE642163);
EPS_SetBufferWord(hR, 2, EPS_kgFULLWORD, 0x7ADF382A);
EPS_SetBufferWord(hR, 3, EPS_kgFULLWORD, 0x59042634);
EPS_SetBufferWord(hR, 4, EPS_kgFULLWORD, 0xEC659EC5); /* MSW */
EPS_SetBufferWord(hN, 0, EPS_kgFULLWORD, 0x9D2AC839); /* LSW (Note: This word is n0) */
EPS_SetBufferWord(hN, 1, EPS_kgFULLWORD, 0xAF1EBF55);
EPS_SetBufferWord(hN, 2, EPS_kgFULLWORD, 0x7A5E30CD);
EPS_SetBufferWord(hN, 3, EPS_kgFULLWORD, 0x5D0BE03E);
EPS_SetBufferWord(hN, 4, EPS_kgFULLWORD, 0x392795A3); /* MSW */
/* Now solve the equation: */
EPS_GCD(hR, hN, hRinverse, hNPrime, hD, kOPLEN);
/* The r^1 result is: 0x257A1EA7 C3E92303 2335BDA0 6D315EAF D6930725
** The n' result is: 0x9B023040 FFFF6A92 996954CD AB0154F6 90CFDC26
** The d result is: 0x00000000 00000000 00000000 00000000 00000001 */
/* Now let's try one where the GCD <> 1: Load r and n, as follows:
** r: 0xB49E888D 945A4A20 4D4CC654 0396C2BE D081B582
** n: 0x0B9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F */
EPS_SetBufferWord(hR, 0, EPS_kgFULLWORD, 0xD081B582); /* LSW */
EPS_SetBufferWord(hR, 1, EPS_kgFULLWORD, 0x0396C2BE);
EPS_SetBufferWord(hR, 2, EPS_kgFULLWORD, 0x4D4CC654);
EPS_SetBufferWord(hR, 3, EPS_kgFULLWORD, 0x945A4A20);
EPS_SetBufferWord(hR, 4, EPS_kgFULLWORD, 0xB49E888D); /* MSW */
EPS_SetBufferWord(hN, 0, EPS_kgFULLWORD, 0xF3155D2F); /* LSW (Note: This word is n0) */
EPS_SetBufferWord(hN, 1, EPS_kgFULLWORD, 0xDF160E65);
EPS_SetBufferWord(hN, 2, EPS_kgFULLWORD, 0x5F9FF641);
EPS_SetBufferWord(hN, 3, EPS_kgFULLWORD, 0xDCD3CA5C);
EPS_SetBufferWord(hN, 4, EPS_kgFULLWORD, 0x0B9C69FD); /* MSW */
/* Now solve the equation: */
EPS_GCD(hR, hN, hRinverse, hNPrime, hD, kOPLEN);
/* The r^1 result is: 0x005E3FA5 9FD28298 049F59ED 8A72C9C6 6476B555
** The n' result is: 0x05BA1EEC 2F90946B B8B4C5C8 63CFF47C 4EA9894D
** The d result is: 0x00000000 00000000 00000000 00000000 00000007 */
/* ============================================================================
** Modular Inverse:
** ============================================================================ */
/* Now let's compute a modular inverse. We'll compute Ainv, the modular inverse
** (if it exists) of A, with respect to modulus N. Ainv is the number that
** satisfies the equation (A)(Ainv) mod N = 1. Note that not all numbers
** have an inverse in a given modulus. The EPS_ModInverse function will
** return EPS_kgOK if inverse was found, else EPS_kgNOINVERSE. We'll use:
** A (in hOp1): 0x96AFC140 ED8854BD 8AFD14A8 A26568F7 F5DD1D57
** N (in hModulus): 0xCA454C18 3C3B7AFF 412D5731 0B807B7F 34BDDD51 */
EPS_SetBufferWord(hOp1, 0, EPS_kgFULLWORD, 0xF5DD1D57); /* LSW */
EPS_SetBufferWord(hOp1, 1, EPS_kgFULLWORD, 0xA26568F7);
EPS_SetBufferWord(hOp1, 2, EPS_kgFULLWORD, 0x8AFD14A8);
EPS_SetBufferWord(hOp1, 3, EPS_kgFULLWORD, 0xED8854BD);
EPS_SetBufferWord(hOp1, 4, EPS_kgFULLWORD, 0x96AFC140); /* MSW */
EPS_SetBufferWord(hModulus, 0, EPS_kgFULLWORD, 0x34BDDD51); /* LSW */
EPS_SetBufferWord(hModulus, 1, EPS_kgFULLWORD, 0x0B807B7F);
EPS_SetBufferWord(hModulus, 2, EPS_kgFULLWORD, 0x412D5731);
EPS_SetBufferWord(hModulus, 3, EPS_kgFULLWORD, 0x3C3B7AFF);
EPS_SetBufferWord(hModulus, 4, EPS_kgFULLWORD, 0xCA454C18); /* MSW */
status = EPS_ModInverse(hOp1, hModulus, hResult, kOPLEN);
/* Expected A^-1: 0xC0D9ED6B 6C27FC68 223EF9AF 086B0D7B 42E093BC
** Upon returning from the EPS_ModInverse function, always
** check "status" to be sure that the inverse exits. */
/* =========================================================================
** Bit-wise Logical Operations:
** ========================================================================= */
/* Now let's try some logical operations. First load the operands with 160-bit values:
** Use B49E888D 945A4A20 4D4CC654 0396C2BE D081B582 for operand1 and
** use 0B9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F for operand2. */
EPS_SetBufferWord(hOp1, 0, EPS_kgFULLWORD, 0xD081B582); /* LSW */
EPS_SetBufferWord(hOp1, 1, EPS_kgFULLWORD, 0x0396C2BE);
EPS_SetBufferWord(hOp1, 2, EPS_kgFULLWORD, 0x4D4CC654);
EPS_SetBufferWord(hOp1, 3, EPS_kgFULLWORD, 0x945A4A20);
EPS_SetBufferWord(hOp1, 4, EPS_kgFULLWORD, 0xB49E888D); /* MSW */
EPS_SetBufferWord(hOp2, 0, EPS_kgFULLWORD, 0xF3155D2F); /* LSW */
EPS_SetBufferWord(hOp2, 1, EPS_kgFULLWORD, 0xDF160E65);
EPS_SetBufferWord(hOp2, 2, EPS_kgFULLWORD, 0x5F9FF641);
EPS_SetBufferWord(hOp2, 3, EPS_kgFULLWORD, 0xDCD3CA5C);
EPS_SetBufferWord(hOp2, 4, EPS_kgFULLWORD, 0x0B9C69FD); /* MSW */
/* We can OR the buffers as follows: */
EPS_Or(hOp1, hOp2, hResult, kOPLEN);
/* Result should be BF9EE9FD DCDBCA7C 5FDFF655 DF96CEFF F395FDAF. */
/* We can AND the buffers as follows: */
EPS_And(hOp1, hOp2, hResult, kOPLEN);
/* Result should be 009C088D 94524A00 4D0CC640 03160224 D0011502. */
/* We can XOR the buffers as follows: */
EPS_Xor(hOp1, hOp2, hResult, kOPLEN);
/* Result should be BF02E170 4889807C 12D33015 DC80CCDB 2394E8AD. */
/* We can complement buffer 1 as follows: */
EPS_Complement(hOp1, hResult, kOPLEN);
/* Result is 4B617772 6BA5B5DF B2B339AB FC693D41 2F7E4A7D. */
/* Test Done! */
puts("Test Completed.");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -