📄 example.bas
字号:
EPS_SetBufferWord hBigModulus, 11, EPS_kgFULLWORD, &H5B3E4DBD
EPS_SetBufferWord hBigModulus, 12, EPS_kgFULLWORD, &HD1845866
EPS_SetBufferWord hBigModulus, 13, EPS_kgFULLWORD, &HB0F08B14
EPS_SetBufferWord hBigModulus, 14, EPS_kgFULLWORD, &HE393F6AA
EPS_SetBufferWord hBigModulus, 15, EPS_kgFULLWORD, &HD411A4A0 '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 &H0FF79239 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.
ShowResult hBigResult, kBIGOPLEN 'Display the results of this operation
'===============================================================
' 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, &HD081B582 'LSW
EPS_SetBufferWord hOp1, 1, EPS_kgFULLWORD, &H396C2BE
EPS_SetBufferWord hOp1, 2, EPS_kgFULLWORD, &H4D4CC654
EPS_SetBufferWord hOp1, 3, EPS_kgFULLWORD, &H945A4A20
EPS_SetBufferWord hOp1, 4, EPS_kgFULLWORD, &HB49E888D 'MSW
'We'll use C90260FF 4A27EB5B for the exponent:
EPS_SetBufferWord hExponent, 0, EPS_kgFULLWORD, &H4A27EB5B 'LSW
EPS_SetBufferWord hExponent, 1, EPS_kgFULLWORD, &HC90260FF 'MSW
'Initialize the modulus buffer to:
'D46DD318 96D2FC6F E9343A13 B1F2FAE0 3E7288AA. Note that
'the modulus is even.
EPS_SetBufferWord hModulus, 0, EPS_kgFULLWORD, &H3E7288AA 'LSW
EPS_SetBufferWord hModulus, 1, EPS_kgFULLWORD, &HB1F2FAE0
EPS_SetBufferWord hModulus, 2, EPS_kgFULLWORD, &HE9343A13
EPS_SetBufferWord hModulus, 3, EPS_kgFULLWORD, &H96D2FC6F
EPS_SetBufferWord hModulus, 4, EPS_kgFULLWORD, &HD46DD318 'MSW
'Now the exponentiation:
EPS_ExponentiateEven hOp1, hExponent, hResult, hModulus, 2, kOPLEN
'The result is 4CC44E08 F3962A83 D0506215 CC29A86D D6CAB55A.
ShowResult hResult, kOPLEN 'Display the results of this operation
'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, &HFF2B56FD 'LSW
EPS_SetBufferWord hOp1, 1, EPS_kgFULLWORD, &HAC23B8CD
EPS_SetBufferWord hOp1, 2, EPS_kgFULLWORD, &HD8E4D382
EPS_SetBufferWord hOp1, 3, EPS_kgFULLWORD, &H89C9680A
EPS_SetBufferWord hOp1, 4, EPS_kgFULLWORD, &H8C33EC45 'MSW
EPS_SetBufferWord hExponent, 0, EPS_kgFULLWORD, &H6C438B0D 'LSW
EPS_SetBufferWord hExponent, 1, EPS_kgFULLWORD, &HB1FD2DD8
EPS_SetBufferWord hExponent, 2, EPS_kgFULLWORD, &H7EC569B7 'MSW
EPS_SetBufferWord hModulus, 0, EPS_kgFULLWORD, &HF1E81F1C 'LSW
EPS_SetBufferWord hModulus, 1, EPS_kgFULLWORD, &H5B486EF6
EPS_SetBufferWord hModulus, 2, EPS_kgFULLWORD, &HBC0646F4
EPS_SetBufferWord hModulus, 3, EPS_kgFULLWORD, &HB2326AE7
EPS_SetBufferWord hModulus, 4, EPS_kgFULLWORD, &HFC40CD75 'MSW
EPS_ExponentiateEven hOp1, hExponent, hResult, hModulus, 3, kOPLEN
'Result: 85E77086 BADE14C7 A2F9C7A4 1D313E15 41504A8D
ShowResult hResult, kOPLEN 'Display the results of this operation
'=======================================================================
' 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, &H19E '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, &H11 'LSW
EPS_SetBufferWord hExponent, 1, EPS_kgFULLWORD, 0 'MSW
EPS_Exponentiate hOp1, hExponent, hResult, 1, kOPLEN
'Result is 000DD35F 9A67C42A 653A5AF9 5298382D 8B9E0000
ShowResult hResult, kOPLEN 'Display the results of this operation
'================================================================================
' 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: &H00000001 00000000 00000000 00000000 00000000 00000000
' n: &H00000000 D46DD318 96D2FC6F E9343A13 B1F2FAE0 3E7288AB
EPS_SetBufferWord hR, 0, EPS_kgFULLWORD, &H0
EPS_SetBufferWord hR, 1, EPS_kgFULLWORD, &H0
EPS_SetBufferWord hR, 2, EPS_kgFULLWORD, &H0 'NOTE: We could simply clear the hR buffer,
EPS_SetBufferWord hR, 3, EPS_kgFULLWORD, &H0 'then set word 5 to &H00000001.
EPS_SetBufferWord hR, 4, EPS_kgFULLWORD, &H0
EPS_SetBufferWord hR, 5, EPS_kgFULLWORD, &H1
EPS_SetBufferWord hN, 0, EPS_kgFULLWORD, &H3E7288AB 'LSW (Note: This word is n0)
EPS_SetBufferWord hN, 1, EPS_kgFULLWORD, &HB1F2FAE0
EPS_SetBufferWord hN, 2, EPS_kgFULLWORD, &HE9343A13
EPS_SetBufferWord hN, 3, EPS_kgFULLWORD, &H96D2FC6F
EPS_SetBufferWord hN, 4, EPS_kgFULLWORD, &HD46DD318 'MSW
EPS_SetBufferWord hN, 5, EPS_kgFULLWORD, &H0 '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: &H278D8C2D C90F96E1 FF962DE0 155C4078 A6B26CCB.
'The n' result is: &H2FAA6235 15152CEB BCC864EE CA0B229C 101ACDFD.
'The d result is: &H00000000 00000000 00000000 00000000 00000001.
ShowResult hRinverse, (kOPLEN + 1) 'Display the results of this operation
ShowResult hNPrime, (kOPLEN + 1) 'Display the results of this operation
ShowResult hD, (kOPLEN + 1) 'Display the results of this operation
'Let's try another, but this time the operands are only 5 words long.
'Load r and n, as follows:
' r: &HEC659EC5 59042634 7ADF382A FE642163 0765586B
' n: &H392795A3 5D0BE03E 7A5E30CD AF1EBF55 9D2AC839
EPS_SetBufferWord hR, 0, EPS_kgFULLWORD, &H765586B 'LSW
EPS_SetBufferWord hR, 1, EPS_kgFULLWORD, &HFE642163
EPS_SetBufferWord hR, 2, EPS_kgFULLWORD, &H7ADF382A
EPS_SetBufferWord hR, 3, EPS_kgFULLWORD, &H59042634
EPS_SetBufferWord hR, 4, EPS_kgFULLWORD, &HEC659EC5 'MSW
EPS_SetBufferWord hN, 0, EPS_kgFULLWORD, &H9D2AC839 'LSW (Note: This word is n0)
EPS_SetBufferWord hN, 1, EPS_kgFULLWORD, &HAF1EBF55
EPS_SetBufferWord hN, 2, EPS_kgFULLWORD, &H7A5E30CD
EPS_SetBufferWord hN, 3, EPS_kgFULLWORD, &H5D0BE03E
EPS_SetBufferWord hN, 4, EPS_kgFULLWORD, &H392795A3 'MSW
'Now solve the equation:
EPS_GCD hR, hN, hRinverse, hNPrime, hD, kOPLEN
'The r^1 result is: &H257A1EA7 C3E92303 2335BDA0 6D315EAF D6930725
'The n' result is: &H9B023040 FFFF6A92 996954CD AB0154F6 90CFDC26
'The d result is: &H00000000 00000000 00000000 00000000 00000001
ShowResult hRinverse, kOPLEN 'Display the results of this operation
ShowResult hNPrime, kOPLEN 'Display the results of this operation
ShowResult hD, kOPLEN 'Display the results of this operation
'Now let's try one where the GCD <> 1: Load r and n, as follows:
' r: &HB49E888D 945A4A20 4D4CC654 0396C2BE D081B582
' n: &H0B9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F
EPS_SetBufferWord hR, 0, EPS_kgFULLWORD, &HD081B582 'LSW
EPS_SetBufferWord hR, 1, EPS_kgFULLWORD, &H396C2BE
EPS_SetBufferWord hR, 2, EPS_kgFULLWORD, &H4D4CC654
EPS_SetBufferWord hR, 3, EPS_kgFULLWORD, &H945A4A20
EPS_SetBufferWord hR, 4, EPS_kgFULLWORD, &HB49E888D 'MSW
EPS_SetBufferWord hN, 0, EPS_kgFULLWORD, &HF3155D2F 'LSW (Note: This word is n0)
EPS_SetBufferWord hN, 1, EPS_kgFULLWORD, &HDF160E65
EPS_SetBufferWord hN, 2, EPS_kgFULLWORD, &H5F9FF641
EPS_SetBufferWord hN, 3, EPS_kgFULLWORD, &HDCD3CA5C
EPS_SetBufferWord hN, 4, EPS_kgFULLWORD, &HB9C69FD 'MSW
'Now solve the equation:
EPS_GCD hR, hN, hRinverse, hNPrime, hD, kOPLEN
'The r^1 result is: &H005E3FA5 9FD28298 049F59ED 8A72C9C6 6476B555
'The n' result is: &H05BA1EEC 2F90946B B8B4C5C8 63CFF47C 4EA9894D
'The d result is: &H00000000 00000000 00000000 00000000 00000007
ShowResult hRinverse, kOPLEN 'Display the results of this operation
ShowResult hNPrime, kOPLEN 'Display the results of this operation
ShowResult hD, kOPLEN 'Display the results of this operation
'============================================================================
' 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): &H96AFC140 ED8854BD 8AFD14A8 A26568F7 F5DD1D57
'N (in hModulus): &HCA454C18 3C3B7AFF 412D5731 0B807B7F 34BDDD51
EPS_SetBufferWord hOp1, 0, EPS_kgFULLWORD, &HF5DD1D57 'LSW
EPS_SetBufferWord hOp1, 1, EPS_kgFULLWORD, &HA26568F7
EPS_SetBufferWord hOp1, 2, EPS_kgFULLWORD, &H8AFD14A8
EPS_SetBufferWord hOp1, 3, EPS_kgFULLWORD, &HED8854BD
EPS_SetBufferWord hOp1, 4, EPS_kgFULLWORD, &H96AFC140 'MSW
EPS_SetBufferWord hModulus, 0, EPS_kgFULLWORD, &H34BDDD51 'LSW
EPS_SetBufferWord hModulus, 1, EPS_kgFULLWORD, &HB807B7F
EPS_SetBufferWord hModulus, 2, EPS_kgFULLWORD, &H412D5731
EPS_SetBufferWord hModulus, 3, EPS_kgFULLWORD, &H3C3B7AFF
EPS_SetBufferWord hModulus, 4, EPS_kgFULLWORD, &HCA454C18 'MSW
status = EPS_ModInverse(hOp1, hModulus, hResult, kOPLEN)
'Expected A^-1: &HC0D9ED6B 6C27FC68 223EF9AF 086B0D7B 42E093BC
ShowResult hResult, kOPLEN 'Display the results of this operation
'NOTE: 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, &HD081B582 'LSW
EPS_SetBufferWord hOp1, 1, EPS_kgFULLWORD, &H396C2BE
EPS_SetBufferWord hOp1, 2, EPS_kgFULLWORD, &H4D4CC654
EPS_SetBufferWord hOp1, 3, EPS_kgFULLWORD, &H945A4A20
EPS_SetBufferWord hOp1, 4, EPS_kgFULLWORD, &HB49E888D 'MSW
EPS_SetBufferWord hOp2, 0, EPS_kgFULLWORD, &HF3155D2F 'LSW
EPS_SetBufferWord hOp2, 1, EPS_kgFULLWORD, &HDF160E65
EPS_SetBufferWord hOp2, 2, EPS_kgFULLWORD, &H5F9FF641
EPS_SetBufferWord hOp2, 3, EPS_kgFULLWORD, &HDCD3CA5C
EPS_SetBufferWord hOp2, 4, EPS_kgFULLWORD, &HB9C69FD 'MSW
'We can OR the buffers as follows:
EPS_Or hOp1, hOp2, hResult, kOPLEN
'Result should be BF9EE9FD DCDBCA7C 5FDFF655 DF96CEFF F395FDAF.
ShowResult hResult, kOPLEN 'Display the results of this operation
'We can AND the buffers as follows:
EPS_And hOp1, hOp2, hResult, kOPLEN
'Result should be 009C088D 94524A00 4D0CC640 03160224 D0011502.
ShowResult hResult, kOPLEN 'Display the results of this operation
'We can XOR the buffers as follows:
EPS_Xor hOp1, hOp2, hResult, kOPLEN
'Result should be BF02E170 4889807C 12D33015 DC80CCDB 2394E8AD.
ShowResult hResult, kOPLEN 'Display the results of this operation
'We can complement buffer 1 as follows:
EPS_Complement hOp1, hResult, kOPLEN
'Result is 4B617772 6BA5B5DF B2B339AB FC693D41 2F7E4A7D.
ShowResult hResult, kOPLEN 'Display the results of this operation
'Test Done!
frmResult.txtResult = "Test Completed."
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -