⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.c

📁 CRC计算器
💻 C
📖 第 1 页 / 共 3 页
字号:
	EPS_Square(hOp1, hResult, kOPLEN);









	/* ===============================================================

	**  Shifting:

	** =============================================================== */



	/* Now let's test the word shift functions. These functions shift a buffer left or right

	** by a given number of words (not bits!). First the left shift. We'll shift operand1

	** left one word at a time, and after each shift we'll see that a zero word has been

	** shifted into the LSW position, and the buffer is 1 word longer. operand1 starts with a

	** value of B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9, so the shifted result

	** will be: B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000. */

	EPS_ClearBuffer(hResult, kRESULTLEN);  /* Make room for growing result */

	EPS_WordShiftLeft(hResult, hOp1, 1, kOPLEN);

	/* Shifting again gives us B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000 00000000: */

	EPS_WordShiftLeft(hResult, hResult, 1, kOPLEN+1);

	/* Once more gives us B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000 00000000 00000000: */

	EPS_WordShiftLeft(hResult, hResult, 1, kOPLEN+2);



	/* Now we shift back towards the right. Note that with each shift a zero word is

	** shifted into the MSW position, and the LSW is lost. The result of the first

	** shift is 00000000 B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000 00000000: */

	EPS_WordShiftRight(hResult, hResult, 1, kOPLEN+3);

	/* Shifting again gives 00000000 00000000 B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000: */

	EPS_WordShiftRight(hResult, hResult, 1, kOPLEN+2);

	/* Once more gives us 00000000 00000000 00000000 B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9: */

	EPS_WordShiftRight(hResult, hResult, 1, kOPLEN+1);







	/* ===============================================================

	**  Testing Buffers:

	** =============================================================== */



	/* Now let's try the EPS_TestBuffer() function. The result buffer still holds the result

	** of the previous shifting, so it's non-zero. Its MSW is B48F48B0. In CypherMathWin32, a buffer

	** is considered to be "less than zero" if the MSB of its MSW is set, which is the

	** case here: */

	status = EPS_TestBuffer(hResult, kOPLEN);   /* Should be "less than zero" */



	/* Note that constants for result code returned in "status" are defined

	** in CypherMathWin32.h for the various result values. */



	/* We can make the buffer "greater than zero" by clearing the MSB of its MSW: */

	EPS_SetBufferWord(hResult, kOPLEN-1, EPS_kgFULLWORD, (0xB48F48B0 & 0x7FFFFFFF));  /* Mask out sign bit */

	/* Now we retest the buffer: */

	status = EPS_TestBuffer(hResult, kOPLEN);   /* Should be "greater than zero" now */

	/* Clear the buffer: */

	EPS_ClearBuffer(hResult, kOPLEN);

	status = EPS_TestBuffer(hResult, kOPLEN);   /* Should be "equal to zero" now */

	



	/* ===============================================================

	**  Comparing Two Buffers:

	** =============================================================== */



	/* To test the EPS_Compare() function we'll compare the operand1 buffer to operand2.

	** Operand1 still holds 348F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9, and

	** operand2 still holds FB9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F from the

	** earlier operations. Therefore comparing (operand1 - operand2) should result in

	** a "less than" status, since the compare is unsigned. Note that neither operand

	** is actually altered: */

	status = EPS_Compare(hOp1, hOp2, kOPLEN);  /* Should be "less than" */



	/* Note that constants for result code returned in "status" are defined

	** in CypherMathWin32.h for the various result values. */



	/* If we compare them in the other order, we'll get a "greater than" status: */

	status = EPS_Compare(hOp2, hOp1, kOPLEN);  /* Should be "greater than" */

	/* If we copy operand1 into the result buffer, then compare operand1 to the result

	** buffer, we'll get the "equal to" status: */

	EPS_CopyBuffer(hOp1, hResult, kOPLEN);

	status = EPS_Compare(hOp1, hResult, kOPLEN);    /* Should be "equal to" */





	/* ===============================================================

	**  Setting a Buffer to "1":

	** =============================================================== */



	/* Now we'll test the EPS_UnityBuffer() function, which sets a buffer to 1. We'll

	** use the result buffer: */

	EPS_UnityBuffer(hResult, kOPLEN);  /* result buffer is now "1" */







	/* ===============================================================

	**  Modular Math:

	** =============================================================== */



	/* Now on to some modular calculations: */



	/* We'll need a buffer to hold the modulus for modular calculations: */

	hModulus = EPS_CreateBuffer(kOPLEN);



	/* We'll need storage for the Montgomery Representations of operand1 and operand2.

	** Call the buffers operand1prime and operand2prime: */

	hOp1Prime = EPS_CreateBuffer(kOPLEN);

	hOp2Prime = EPS_CreateBuffer(kOPLEN);



	/* First, re-initialize our 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'll also need a modulus, so let's initialize the modulus buffer

	** to D46DD318 96D2FC6F E9343A13 B1F2FAE0 3E7288AB. Note that

	** the modulus is odd and its MSB is set. This is typical in cryptographic

	** applications: */



	EPS_SetBufferWord(hModulus, 0, EPS_kgFULLWORD, 0x3E7288AB);   /* LSW (Note: This word is n0) */

	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 */





	/* ===============================================================

	**  Finding n0':

	** =============================================================== */



	/* For the subsequent modular calculation, we'll need the value of n0', so 

	** let's calculate it now. We'll need the LSW of the modulus, n0: */

	n0 = EPS_GetBufferWord(hModulus, 0, EPS_kgFULLWORD);    

	EPS_FindN0Prime(n0, &n0prime);  /* Should be 0x101ACDFD for this modulus  */





	/* ===============================================================

	**  Computing Montgomery Representations of Numbers:

	** =============================================================== */



	/* Now let's get the Montgomery Representations of operand1 and operand2. We'll

	** store them in operand1prime and operand2prime. First convert operand1 to operand1prime: */

	EPS_MontRep(hOp1, hOp1Prime, hModulus, kOPLEN);

	/* operand1prime is now A3E9C74C F51EBFFE 8C3ACA2A 423CE7BD 85F3DB83, the

	** Montgomery Image of operand1 (with respect to our modulus[]). */



	/* Now convert operand2 to operand2prime: */

	EPS_MontRep(hOp2, hOp2Prime, hModulus, kOPLEN);

	/* operand2prime is now 6E1A32F2 458BF7CC 5C924A0C 090B2B40 58C77D2D, the

	** Montgomery Image of operand2 (with respect to our modulus[]). */





	/* The inverse of the MontRep function is the NormalRep function. We'll use

	** hOp1Prime to test this. It's normal representation should, of course, be

	** hOp1:

	**   a' = 0xA3E9C74C F51EBFFE 8C3ACA2A 423CE7BD 85F3DB83 */

	EPS_NormalRep(hOp1Prime, hResult, hModulus, n0prime, kOPLEN);

	/* Result is 0xB49E888D 945A4A20 4D4CC654 0396C2BE D081B582, the original operand1 */







	/* ===============================================================

	**  Computing Montgomery Products:

	** =============================================================== */



	/* Let's compute the Montgomery Product of operand1prime and operand2prime, with respect

	** to our modulus, and the n0prime we calculated above. Store the result in result[]: */

	EPS_MontPro(hOp1Prime, hOp2Prime, hResult, hModulus, n0prime, kOPLEN);

	/* The result is 9F9598E2 313C7C9A 8D2D2C9E 7BF2EAF2 D3CB1D77. */





	/* ===============================================================

	**  Montgomery Exponentiation:

	** =============================================================== */



	/* Now let's try a Montgomery Exponentiation. We'll use operand1prime as the Montgomery

	** Image of our base, an exponent of 0x1234, and the same modulus from the previous

	** calculation. The Montgomery image of the result will be given by the following

	** expression: (operand1prime[] ^ 0x1234) % modulus[]. We'll store the result in 

	** the result[] buffer. First, create a buffer for the exponent and set it: */

	hExponent = EPS_CreateBuffer(kOPLEN);

	EPS_SetBufferWord(hExponent, 0, EPS_kgFULLWORD, 0x1234);  /* LSW */



	/* Now the exponetiation. Note the use of "1" for the exponent length and kOPLEN

	** for the base's length: */

	EPS_MontExp(hOp1Prime, hExponent, hResult, hModulus, n0prime, 1, kOPLEN);

	/* The result is 43C7C091 942B7AFD 98671C13 DC085365 1F223812.   */







	/* Let's try a longer exponent, 64 bits. We'll use C90260FF 4A27EB5B: */

	EPS_SetBufferWord(hExponent, 0, EPS_kgFULLWORD, 0x4A27EB5B);  /* LSW */

	EPS_SetBufferWord(hExponent, 1, EPS_kgFULLWORD, 0xC90260FF);  /* MSW */





	/* Now the exponetiation. Note the use of "2" for the exponent length this time. The

	** base is the same so again its length will be kOPLEN: */

	EPS_MontExp(hOp1Prime, hExponent, hResult, hModulus, n0prime, 2, kOPLEN);

	/* The result is D45DFE57 06728526 81F7FAB8 F6E099FD B9A8D248. */









	/* ===============================================================

	**  Montgomery Exponentiation in fixed-time:

	** ===============================================================

	** The EPS_MontExpFT function attempts to control the running time as closely

	** as possible, to avoid variations in running time based on the value of the

	** exponent.  Of course, this routine will run somewhat slower than EPS_MontExp,

	** but if your application requires additional resistance to timing attacks,

	** EPS_MontExpFT is the function to use. We'll repeat the previous operation: */

	EPS_MontExpFT(hOp1Prime, hExponent, hResult, hModulus, n0prime, 2, kOPLEN);

	/* The result is still D45DFE57 06728526 81F7FAB8 F6E099FD B9A8D248. */





	

	/* ===============================================================

	**  Bigger Montgomery Exponentiation:

	** =============================================================== */



	/* Now let's try a bigger Montgomery exponentiation, say a 512 bit (16 word)

	** base raised to a 160 bit (5 word) exponent with a 512 bit (16 word) modulus. */



	/* Allocate the storage for the big base and its Montgomery Image: */

	hBigBase = EPS_CreateBuffer(kBIGOPLEN);

	hBigBasePrime = EPS_CreateBuffer(kBIGOPLEN);



	/* Allocate the storage for the big modulus: */

	hBigModulus = EPS_CreateBuffer(kBIGOPLEN);



	/* Allocate storage for the result of the big exponentiation: */

	hBigResult = EPS_CreateBuffer(kBIGOPLEN);





	/* Load a 512 bit number for our base: */

	EPS_SetBufferWord(hBigBase,  0, EPS_kgFULLWORD, 0x8255C104);  /* LSW */

	EPS_SetBufferWord(hBigBase,  1, EPS_kgFULLWORD, 0xBF782257);

	EPS_SetBufferWord(hBigBase,  2, EPS_kgFULLWORD, 0x020DADAB);

	EPS_SetBufferWord(hBigBase,  3, EPS_kgFULLWORD, 0x43EC1D3E);

	EPS_SetBufferWord(hBigBase,  4, EPS_kgFULLWORD, 0x23BBECC4);

	EPS_SetBufferWord(hBigBase,  5, EPS_kgFULLWORD, 0x47BD54B3);

	EPS_SetBufferWord(hBigBase,  6, EPS_kgFULLWORD, 0xDF89B717);

	EPS_SetBufferWord(hBigBase,  7, EPS_kgFULLWORD, 0x781D21F2);

	EPS_SetBufferWord(hBigBase,  8, EPS_kgFULLWORD, 0xA3DC4F71);

	EPS_SetBufferWord(hBigBase,  9, EPS_kgFULLWORD, 0x25248B58);

	EPS_SetBufferWord(hBigBase, 10, EPS_kgFULLWORD, 0x07E7ADAF);

	EPS_SetBufferWord(hBigBase, 11, EPS_kgFULLWORD, 0x58186B50);

	EPS_SetBufferWord(hBigBase, 12, EPS_kgFULLWORD, 0x3872CE99);

	EPS_SetBufferWord(hBigBase, 13, EPS_kgFULLWORD, 0x49A9E7CD);

	EPS_SetBufferWord(hBigBase, 14, EPS_kgFULLWORD, 0x021F9990);

	EPS_SetBufferWord(hBigBase, 15, EPS_kgFULLWORD, 0xB3085510);  /* MSW */

 

	/* Load a 160 bit number for our exponent: */

	EPS_SetBufferWord(hExponent, 0, EPS_kgFULLWORD, 0xC8F8EF67);  /* LSW */

	EPS_SetBufferWord(hExponent, 1, EPS_kgFULLWORD, 0x74C80102);

	EPS_SetBufferWord(hExponent, 2, EPS_kgFULLWORD, 0xE2C942B5);

	EPS_SetBufferWord(hExponent, 3, EPS_kgFULLWORD, 0xD0192D54);

	EPS_SetBufferWord(hExponent, 4, EPS_kgFULLWORD, 0x6B2CD935);  /* MSW */



	/* Load a 512 bit number for our modulus: */

	EPS_SetBufferWord(hBigModulus,  0, EPS_kgFULLWORD, 0x7FC543F7);  /* LSW */

	EPS_SetBufferWord(hBigModulus,  1, EPS_kgFULLWORD, 0x9CC81F42);

	EPS_SetBufferWord(hBigModulus,  2, EPS_kgFULLWORD, 0xA9FE5F24);

	EPS_SetBufferWord(hBigModulus,  3, EPS_kgFULLWORD, 0x6AF17AC8);

	EPS_SetBufferWord(hBigModulus,  4, EPS_kgFULLWORD, 0x263E9023);

	EPS_SetBufferWord(hBigModulus,  5, EPS_kgFULLWORD, 0xD88C58D7);

	EPS_SetBufferWord(hBigModulus,  6, EPS_kgFULLWORD, 0xD02BBE13);

	EPS_SetBufferWord(hBigModulus,  7, EPS_kgFULLWORD, 0x12DB6E7D);

	EPS_SetBufferWord(hBigModulus,  8, EPS_kgFULLWORD, 0x71C86224);

	EPS_SetBufferWord(hBigModulus,  9, EPS_kgFULLWORD, 0x3FE365CF);

	EPS_SetBufferWord(hBigModulus, 10, EPS_kgFULLWORD, 0xCE254454);

	EPS_SetBufferWord(hBigModulus, 11, EPS_kgFULLWORD, 0x5B3E4DBD);

	EPS_SetBufferWord(hBigModulus, 12, EPS_kgFULLWORD, 0xD1845866);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -