Large integer multiplication optimization method

By splitting large integer data and using SIMD instructions for parallel acceleration, combined with the multi-way divide-and-conquer algorithm and the Montgomery modular multiplication algorithm, the problem of slow large integer modular multiplication algorithm is solved, and the computational efficiency of the public key cryptography system is improved.

CN120687064APending Publication Date: 2025-09-23SPACE STAR TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510626583.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-15
Publication Date
2025-09-23

AI Technical Summary

Technical Problem

In the existing technology, there is still room for improvement in the software implementation of large integer modular multiplication algorithms. In particular, the 1024-bit large integer multiplication and modular multiplication operations are slow, which limits the scope of application of public key cryptography algorithms.

Method used

A large integer multiplication optimization method is adopted to achieve fast large integer multiplication by dividing the large integer data into multiple parts, using SIMD instructions for parallel acceleration, and combining the multi-way divide-and-conquer algorithm and the optimized Montgomery modular multiplication algorithm.

Benefits of technology

The software implementation speed of large integer modular multiplication has been improved, the computational efficiency of the Montgomery modular multiplication algorithm has been optimized, and the resource utilization and computational efficiency of the public key cryptography system have been improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120687064A_ABST
    Figure CN120687064A_ABST
Patent Text Reader

Abstract

The invention discloses a large integer multiplication optimization method. Parallelization acceleration of integer multiplication is achieved through an AVX instruction in SIMD instructions. According to the method, fast large integer multiplication is achieved through a multi-path division and conquer algorithm, the optimized Montgomery modular multiplication algorithm is adopted, the safety requirement of large integer modular multiplication in a specific application scene and factors of a software implementation environment are considered, the method can be applied to implementation of a public key cryptosystem, and the resource utilization rate of the system and the operation efficiency of the system can be effectively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field The invention relates to a large integer multiplication optimization method and belongs to the technical field of computer application. Background Art Large integers are integers that exceed what a computer can represent and operate on. For example, in a typical 32-bit computer architecture, integers are 32 bits long, and the instruction set supports operations between 32-bit integers. Integers larger than 32 bits are considered large integers, and new methods are needed to represent and operate on large integers. Large integer operations have a wide range of applications, including large-scale scientific computing and cryptography based on difficult problems in number theory. Public-key cryptography has become a cornerstone of internet security. Among these, RSA, based on the integer factorization problem, and ElGamal, based on the discrete logarithm problem, are among the most important. The basic encryption and decryption operations of these algorithms all involve arithmetic operations, exponentiation, and modular operations on large integers. For the RSA algorithm, a 1024-bit key generally meets the security requirements of most applications. However, the multiplication and modular exponentiation operations of 1024-bit integers are slow and complex, which limits the applicability of the RSA algorithm to a certain extent. Therefore, improving the encryption and decryption speed of these algorithms, particularly finding new computational methods to improve the computational efficiency of core computational components such as 1024-bit integer multiplication and modular multiplication, is a key issue in the practical application of these public-key cryptographic algorithms. To this end, cryptographers and number theorists have proposed numerous fast modular exponentiation algorithms. These algorithms utilize shifting and modular multiplication to perform modular exponentiation on large integers. Modular exponentiation can be reduced to modular multiplication, making the speed of modular multiplication a key constraint on algorithm speed improvement. The most famous modular multiplication algorithm is the Montgomery algorithm. The Montgomery algorithm comprises Montgomery reduction and its derivative, Montgomery modular multiplication. The Montgomery algorithm implements modular operations through shifting, avoiding the computationally most complex division. This concept provides an effective approach to modular exponentiation, making it one of the most commonly used and efficient modular multiplication algorithms, widely used in various software and hardware implementations. Various improvements have been made to the algorithm to improve efficiency, reduce space requirements, and enhance security. However, in software implementation, Montgomery modular multiplication still has significant room for improvement. This is because the large integer multiplication in the algorithm can utilize more efficient large-number multipliers. Numerous algorithms exist for multiplying large numbers. Karatsuba AA proposed the Karatsuba multiplication algorithm, which uses a binary search to split an integer into two equal-length segments and then applies this binary search recursively. This algorithm employs a squaring operation to eliminate one multiplication, thereby reducing the time complexity of large integer multiplication. The advantages of this algorithm are its simplicity in logic and implementation. The final split point in the binary search recursion can be customized to the specific instruction set, and it requires virtually no additional storage overhead, making it an ideal algorithm for public-key cryptography systems based on large-number operations. Toom further developed the binary division idea into a k-partitioning approach, dividing a large integer into k parts and performing a divide-and-conquer recursive algorithm, choosing a different k as the integer increases. Cook adapted Toom's k-partitioning method to speed up computer programs. Their dynamic divide-and-conquer recursive multiplication algorithm is known as the Toom-Cook multiplication algorithm. This algorithm further reduces the computational complexity of the binary division recursion, but it becomes impractical as k increases. Cooley and Tukey used the unit complex root and Fourier transform to invert the two equivalent representations, polynomial coefficient representation and function value representation, and proposed a fast algorithm for calculating discrete Fourier transform (DFT), namely the Cooley-Tukey algorithm, which reduced the amount of DFT calculation by several orders of magnitude. This algorithm has also been widely used. and Strassen proposed an algorithm for large integer multiplication based on number theoretic transformations (NTT). This algorithm uses integer modular arithmetic instead of floating-point operations, improving multiplication speed. Currently, the fastest integer multiplication algorithm is the Fürer algorithm, proposed by Fürer M. in 2007. However, this algorithm is applicable to very large integers. Currently, large integer multiplication is mostly used in public key cryptography, where the size does not exceed 2048 bits. Therefore, this algorithm is not widely used in practice. In summary, existing large integer modular multiplication still has room for improvement in software implementation. Summary of the Invention The technical problem solved by the present invention is: in view of the shortcomings of the existing large integer modular multiplication algorithm in the current prior art, a large integer multiplication optimization method is proposed. The present invention solves the above technical problems by the following technical solutions: A large integer multiplication optimization method, comprising: Collect large integer data from security scenarios, group them into pairs, and then preset a split width. Then, split the two large integer data in each group based on the split width. Determine the corresponding split items according to the split results of the large integer data, use the split items of the two large integer data in the same group to perform staggered multiplication, and add the split items of the two large integer data in the same group to the same group data; Calculate the multiplier of each group of large integer data according to the staggered multiplication result and the split width; Calculate the first sum and second sum of each group of large integer data according to the addition result of the same group of data and the split width; Use the split items and split widths of each large integer data to calculate the sum of the two groups of large integer data respectively; The total multiplier of each group of large integer data is calculated based on the divided sums of the two groups of large integer data, the first sum and the second sum of each group of large integer data, and the multiplier of each group of large integer data, and the total multiplier is sent to an external computer for key identification in a security scenario. The two large integer data in the i-th group of the security scenario include A i With B i , the split width is preset to b, b is a power of 2 and satisfies the large integer data A i With B i In b-base representation, no more than 4 bits are allowed; large integer data A i With B i The calculation unit length is log2 b bits; during the total multiplier calculation process, all calculation data are stored and recorded through the instruction register. The method for splitting the two large integer data in each group according to the split width is: The large integer data A i With B i It is divided into four parts, recorded as: A i =A i0 +A i1 b+A i2 b 2 +A i3 b 3 ,B i =B i0 +B i1 b+B i2 b 2 +B i3 b 3 Where A i0 \A i1 \A i2 \A i3 A i The segmentation items of each part of the big data, B i0 \B i1 \B i2 \B i3 Bi The segmentation items of each part of the big data. The method of using the split items of two large integer data in the same group to perform staggered multiplication is: M i0 =A i0 *B i0 , M i1 =A i1 *B i1 , M i2 =A i2 *B i2 , M i3 =A i3 *B i3 Where M i0 、M i1 、M i2 、M i3 is the result of misplaced multiplication; The method for adding data in the same group is: M i4 =A i0 +A i1 , M i5 =A i2 +A i3 , M i6 =B i0 +B i1 , M i7 =B i2 +B i3 Where M i4 、M i5 、M i6 、M i7 It is the result of adding the data in the same group. Calculate the multiplier M of each group of large integer data according to the staggered multiplication result and the split width i8 The method is: M i8 =(1+b)*(M i0 +b*M i1 +b 2 *M i2 +b 3 *M i3 ) Calculate the first sum M of each group of large integer data based on the addition result of the same group of data and the split width i9 , the second sum M i10 The method is: M i9 =b*M i4 *M i6 , M i10 =b 3 *Mi5 *M i7 . Calculate the sum of two sets of large integer data M i11 、M i12 The method is: M i11 =A i0 +A i2 +(A i1 +A i3 )*b M i12 =B i0 +B i2 +(B i1 +B i3 )*b; Calculate the total multiplier M of each group of large integer data i13 The method is: M i13 =(1+b 2 )*(M i8 +M i9 +M i10 )+b 2 *M i11 *M i12 . Calculate the result of the staggered multiplication and the multiplier M i8 , the first sum M i9 , the second sum M i10 , total multiplier M i13 In the process of multiplication, all kinds of data multiplication calculations are performed through AVX instructions. The method of performing multiplication calculation through AVX instructions is: Perform multiplier splitting for any multiplier X and any multiplier Y to obtain two multiplier X arrays and eight multiplier Y arrays respectively; After splitting the first multiplier X array into four arrays of equal size, all arrays are loaded into the first register using AVX instructions; Load the first multiplier Y array directly into the second register via AVX instructions; Multiply the contents of the first register and the second register using AVX instructions, and store the data in the third register after sorting; The first multiplier Y array is replaced by the second multiplier Y array, the third multiplier Y array, and the fourth multiplier Y array, respectively, and the multipliers are processed using AVX instructions and loaded into the fourth register, the fifth register, and the sixth register, respectively. Use AVX instructions to accumulate the contents of the third register, the fourth register, the fifth register, and the sixth register, and update the total accumulated content into the third register; The second, third, and fourth multiplier Y arrays are loaded into the second register using AVX instructions, and the total accumulation of the register contents is repeated. When the second multiplier Y array is loaded into the second register, the total accumulation is updated to the fourth register; when the third multiplier Y array is loaded into the third register, the total accumulation is updated to the fifth register; when the second multiplier Y array is loaded into the fourth register, the total accumulation is updated to the sixth register; and the total accumulation corresponding to each group of multiplier Y arrays is updated in the corresponding register. Obtain the contents of the third, fourth, fifth, and sixth registers after the total accumulation is completed, perform data sorting using AVX instructions, obtain the updated contents of the third, fourth, fifth, and sixth registers, use the contents of the third register as the lower 256 bits of the data result of the arbitrary multiplier X and the arbitrary multiplier Y, use the contents of the sixth register as the upper 256 bits of the data result of the arbitrary multiplier X and the arbitrary multiplier Y, and determine the multiplier calculation results of the arbitrary multiplier X and the arbitrary multiplier Y based on the current contents of the third and sixth registers. The updated contents of the third register, the fourth register, the fifth register, and the sixth register are updated respectively by shift accumulation, specifically: L4′=L4+{L3[127:0],32'b0} L6′=L6+{L5[127:0],32'b0} L3′={L3[255:128],32'b0}+{32'b0,L4′[127:0]}+{32'b0,L3[255:128]} L5′={L5[255:128],32'b0}+{32'b0,L6′[127:0]}+{L4′[255:128],32'b0} Where, L3L4L5L6 are the contents of the third register, fourth register, fifth register, and sixth register, L i [255:128] is register L i High 128 bits, L i [127:0] is register L i Lower 128 bits. The calculation result of the total multiplier is obtained by Montgomery modular multiplication, and the calculation method of Montgomery modular multiplication is: Preset selected parameters, the selected parameters are integer powers of the split width b; Calculate the Montgomery representation of the first set of large integer data and the Montgomery representation of the second set of large integer data; The extended Euclidean algorithm is used to calculate the expanded parameters of the segmentation width b; Montgomery representation products are calculated according to the Montgomery representation of the first set of large integer data, the Montgomery representation of the second set of large integer data, and the expanded parameter of the split width b, and the number of Montgomery representation products is the same as the number of arrays in the Montgomery representation of the second set of large integer data; Compare the last set of Montgomery representation products and the difference between the Montgomery representation products and the modulus. If the last set of Montgomery representation products is larger, output the difference between the Montgomery representation products and the modulus as the Montgomery modular multiplication; otherwise, output the last set of Montgomery representation products as the Montgomery modular multiplication. In the calculation method of Montgomery modular multiplication, the Montgomery representation of the first group of large integer data, the Montgomery representation of the second group of large integer data, and the expanded parameter of the split width b all adopt a calculation method of performing multiplier calculation through AVX instructions. The advantages of the present invention compared with the prior art are: The present invention provides a large integer multiplication optimization method, which realizes parallel acceleration of integer multiplication through SIMD instructions such as AVX instructions, and realizes fast large integer multiplication through an improved multi-way divide-and-conquer algorithm. The method can be applied to the implementation of public key cryptosystems. At the same time, the optimized large integer fast multiplication is applied to optimize the speed of the Montgomery modular multiplication algorithm. At the same time, the security requirements of large integer modular multiplication in specific application scenarios and the factors of the software implementation environment are taken into account, which can effectively improve the utilization rate of system resources and the computing efficiency of the system. BRIEF DESCRIPTION OF THE DRAWINGS Figure 1 This is a flow chart of the large integer multiplication optimization method provided by the present invention. DETAILED DESCRIPTION A large integer multiplication optimization method achieves parallel acceleration of integer multiplication through AVX instructions in SIMD instructions; fast large integer multiplication is achieved through a multi-way divide-and-conquer algorithm, and an optimized Montgomery modular multiplication algorithm is adopted. The method takes into account the security requirements of large integer modular multiplication in specific application scenarios and factors of the software implementation environment. It can be applied to the implementation of public key cryptography systems and can effectively improve system resource utilization and system computing efficiency. The large integer multiplication optimization method based on integer splitting has the following specific steps: Collect large integer data from security scenarios, group them into pairs, and then preset a split width. Then, split the two large integer data in each group based on the split width. Determine the corresponding split items according to the split results of the large integer data, use the split items of the two large integer data in the same group to perform staggered multiplication, and add the split items of the two large integer data in the same group to the same group data; Calculate the multiplier of each group of large integer data according to the staggered multiplication result and the split width; Calculate the first sum and second sum of each group of large integer data according to the addition result of the same group of data and the split width; Use the split items and split widths of each large integer data to calculate the sum of the two groups of large integer data respectively; The total multiplier of each group of large integer data is calculated based on the divided sums of the two groups of large integer data, the first sum and the second sum of each group of large integer data, and the multiplier of each group of large integer data, and the total multiplier is sent to an external computer for key identification in a security scenario. The two large integer data in the i-th group of the security scenario include A i With B i , the split width is preset to b, b is a power of 2 and satisfies the large integer data A i With B i In b-base representation, no more than 4 bits are allowed; large integer data A i With B i The calculation unit length is log2 b bits. During the total multiplier calculation process, all calculation data are stored and recorded through the instruction register, and the split width b is determined according to the operand length of the instruction set received during the instruction register recording process. The method for splitting the two large integer data in each group according to the split width is: The large integer data A i With B i It is divided into four parts, recorded as: A i =A i0 +A i1 b+A i2 b 2 +A i3 b 3 ,B i =B i0 +B i1 b+B i2 b 2 +B i3 b 3 Where A i0 \A i1 \A i2 \A i3 A i The segmentation items of each part of the big data, B i0 \B i1 \B i2 \B i3 B i The segmentation items of each part of the big data. The method of using the split items of two large integer data in the same group to perform staggered multiplication is: M i0 =Ai0 *B i0 , M i1 =A i1 *B i1 , M i2 =A i2 *B i2 , M i3 =A i3 *B i3 Where M i0 、M i1 、M i2 、M i3 is the result of misplaced multiplication; The method for adding data in the same group is: M i4 =A i0 +A i1 , M i5 =A i2 +A i3 , M i6 =B i0 +B i1 , M i7 =B i2 +B i3 Where M i4 、M i5 、M i6 、M i7 It is the result of adding the data in the same group. Calculate the multiplier M of each group of large integer data according to the staggered multiplication result and the split width i8 The method is: M i8 =(1+b)*(M i0 +b*M i1 +b 2 *M i2 +b 3 *M i3 ) Calculate the first sum M of each group of large integer data based on the addition result of the same group of data and the split width i9 , the second sum M i10 The method is: M i9 =b*M i4 *M i6 , M i10 =b 3 *M i5 *M i7 . Calculate the multiplier M of each group of large integer data according to the staggered multiplication result and the split width i8 The method is: Mi8 =(1+b)*(M i0 +b*M i1 +b 2 *M i2 +b 3 *M i3 ) Calculate the first sum M of each group of large integer data based on the addition result of the same group of data and the split width i9 , the second sum M i10 The method is: M i9 =b*M i4 *M i6 , M i10 =b 3 *M i5 *M i7 . Calculate the result of the staggered multiplication and the multiplier M i8 , the first sum M i9 , the second sum M i10 , total multiplier M i13 In the process of multiplication, all kinds of data multiplication calculations are performed through AVX instructions. The method of performing multiplication calculation through AVX instructions is: Perform multiplier splitting for any multiplier X and any multiplier Y to obtain two multiplier X arrays and eight multiplier Y arrays respectively; After splitting the first multiplier X array into four arrays of equal size, all arrays are loaded into the first register using AVX instructions; Load the first multiplier Y array directly into the second register via AVX instructions; Multiply the contents of the first register and the second register using AVX instructions, and store the data in the third register after sorting; The first multiplier Y array is replaced by the second multiplier Y array, the third multiplier Y array, and the fourth multiplier Y array, respectively, and the multipliers are processed using AVX instructions and loaded into the fourth register, the fifth register, and the sixth register, respectively. Use AVX instructions to accumulate the contents of the third register, the fourth register, the fifth register, and the sixth register, and update the total accumulated content into the third register; The second, third, and fourth multiplier Y arrays are loaded into the second register using AVX instructions, and the total accumulation of the register contents is repeated. When the second multiplier Y array is loaded into the second register, the total accumulation is updated to the fourth register; when the third multiplier Y array is loaded into the third register, the total accumulation is updated to the fifth register; when the second multiplier Y array is loaded into the fourth register, the total accumulation is updated to the sixth register; and the total accumulation corresponding to each group of multiplier Y arrays is updated in the corresponding register. Obtain the contents of the third, fourth, fifth, and sixth registers after the total accumulation is completed, perform data sorting using AVX instructions, obtain the updated contents of the third, fourth, fifth, and sixth registers, use the contents of the third register as the lower 256 bits of the data result of the arbitrary multiplier X and the arbitrary multiplier Y, use the contents of the sixth register as the upper 256 bits of the data result of the arbitrary multiplier X and the arbitrary multiplier Y, and determine the multiplier calculation results of the arbitrary multiplier X and the arbitrary multiplier Y based on the current contents of the third and sixth registers. The updated contents of the third register, fourth register, fifth register, and sixth register are updated by shift accumulation, specifically: L4′=L4+{L3[127:0],32'b0} L6′=L6+{L5[127:0],32'b0} L3′={L3[255:128],32'b0}+{32'b0,L4′[127:0]}+{32'b0,L3[255:128]} L5′={L5[255:128],32'b0}+{32'b0,L6′[127:0]}+{L4′[255:128],32'b0} Where, L3L4L5L6 are the contents of the third register, fourth register, fifth register, and sixth register, L i [255:128] is register L i High 128 bits, L i [127:0] is register L i Lower 128 bits. The result of the total multiplier is obtained by Montgomery modular multiplication, and the calculation method of Montgomery modular multiplication is: Preset selected parameters, the selected parameters are integer powers of the split width b; Calculate the Montgomery representation of the first set of large integer data and the Montgomery representation of the second set of large integer data; The extended Euclidean algorithm is used to calculate the expanded parameters of the segmentation width b; Montgomery representation products are calculated according to the Montgomery representation of the first set of large integer data, the Montgomery representation of the second set of large integer data, and the expanded parameter of the split width b, and the number of Montgomery representation products is the same as the number of arrays in the Montgomery representation of the second set of large integer data; Compare the last set of Montgomery representation products and the difference between the Montgomery representation products and the modulus. If the last set of Montgomery representation products is larger, output the difference between the Montgomery representation products and the modulus as the Montgomery modular multiplication; otherwise, output the last set of Montgomery representation products as the Montgomery modular multiplication. In the calculation method of Montgomery modular multiplication, the Montgomery representation of the first group of large integer data, the Montgomery representation of the second group of large integer data, and the expanded parameter of the split width b all adopt a calculation method of performing multiplier calculation through AVX instructions. The following is further described in conjunction with the accompanying drawings and preferred embodiments: In the current embodiment, if Figure 1 As shown, the specific calculation steps are: To address some of the shortcomings of existing large integer modular multiplication algorithms, a method for optimizing large integer multiplication speed is proposed by combining a multi-way divide-and-conquer algorithm. This algorithm selects an appropriate divide-and-conquer algorithm for the key length in a specific security scenario, significantly improving the software implementation speed of large integer modular multiplication.

[0001] The present invention solves the above technical problems by the following technical solutions:

[0002] Let A and B be two large integers to be multiplied, and N be a given modulus. In general, integers A, B, and N are required to be large integers not exceeding 1024 bits.

[0003] Determine the split width b. Generally, b is required to be a power of 2 and the integers A and B do not exceed 4 bits in base b.

[0004] Using the split width b, the multipliers A and B are recorded as follows: A=A0+A1b+A2b 2 +A3b 3 =(A0,A1,A2,A3) b , B=B0+B1b+B2b 2 +B3b 3 =(B0,B1,B2,B3) b .

[0005] Use 13 registers to store intermediate operation results and do the following calculations:

[0006] Calculate four multiplications: M0=A0*B0, M1=A1*B1, M2=A2*B2, M3=A3*B3.

[0007] Calculate four additions: M4=A0+A1, M5=A2+A3, M6=B0+B1, M7=B2+B3.

[0008] Calculation: M8 = (1 + b) * (M0 + b * M1 + b 2 *M2+b 3 *M3).

[0009] Calculation: M9 = b*M4*M6, M 10 =b 3 *M5*M7.

[0010] Calculation: M 11 =A0+A2+(A1+A3)*b,M 12 =B0+B2+(B1+B3)*b.

[0011] Calculation: M 12 =(1+b 2 )*(M8+M9+M 10 )+b 2 *M 11 *M 12 .

[0012] The resulting register M 12 The result is the final result of the large integer A*B.

[0013] Because A0, A1, A2, A3 and B0, B1, B2, B3 are integers not exceeding log2 b bits, when calculating the product of any two of them, a parallel acceleration solution for integer multiplication can be implemented by applying SIMD instructions.

[0014] Among them, the parallel acceleration solution for integer multiplication using SIMD instructions such as AVX instructions includes three parts: 1. dividing the multiplier into lengths suitable for processing by SIMD instruction registers in the environment; 2. multiplying the divided parts separately with the help of parallel multiplication instructions; 3. processing the results of the multiplications separately with the help of shift and parallel addition instructions to obtain the final product.

[0015] Next we optimize the speed of the Montgomery modular multiplication algorithm by applying optimized large integer fast multiplication.

[0016] For large integers A and B, we calculate the Montgomery modular product A*B mod N as follows.

[0017] First, the parameter R is selected. The parameter R is an integer power of the segmentation width b and is the minimum value that satisfies R>N.

[0018] Apply optimized large integer fast multiplication to calculate the Montgomery representation of A A = AR mod N = (A0, A1, A2, A3) b .

[0019] Apply the optimized large integer fast multiplication to calculate the Montgomery representation of B B = BR mod N = (B0, B1, B2, B3) b .

[0020] Calculate q=-N using the extended Euclidean algorithm-1 (mod b).

[0021] Apply the optimized large integer fast multiplication to calculate u0=B0*A0*q mod b, p0=(B0*A+u0*N) / b.

[0022] Apply the optimized large integer fast multiplication u1=B1*A0*q mod b, p1=(p0+B1*A+u1*N) / b.

[0023] Apply the optimized large integer fast multiplication to calculate u2=B2*A0*q mod b, p2=(p1+B2*A+u2*N) / b.

[0024] Apply the optimized large integer fast multiplication to calculate u3=B3*A0*q mod b, p3=(p2+B3*A+u3*N) / b.

[0025] Compare the values ​​of p3 and p3-N. When p3 ≥ N, A*B mod N = p3-N; otherwise, A*B mod N = p3.

[0026] In particular, when B=1, the above calculation steps are simplified to Montgomery reduction, and the obtained value is called Montgomery reduction with respect to A.

[0027] In algorithms such as RSA, modular multiplication is a key step in calculating the modular exponentiation of large integers. The modular exponentiation can be calculated by performing multiple modular multiplication operations, and finally a Montgomery reduction is performed to obtain the encryption and decryption results.

[0028] The above technical solution of the present invention can greatly improve the software implementation speed of large integer modular multiplication, effectively reduce the complexity of the algorithm, reduce resource usage, and is conducive to promoting the further optimization and application of related cryptographic algorithms. DETAILED DESCRIPTION

[0029] In order to make the purpose and technical solution of the present invention clearer, the present invention will be further described in detail below with reference to specific examples.

[0030] Let A and B be two given large integers to be multiplied, and N be the given modulus.

[0031] In existing cryptographic algorithms, large integers with a length of 1024 bits can meet the security requirements of most scenarios. Therefore, this specific implementation example is the modular exponentiation operation in the encryption and decryption operations of the 1024-bit RSA public key cryptographic algorithm. Since the basic operation based on the Montgomery modular exponentiation operation is the Montgomery modular multiplication, the integers A, B, and N are required to be large integers not exceeding 1024 bits.

[0032] Determine the split width b. Generally, b is required to be a power of 2 and the integers A and B do not exceed 4 bits in base b.

[0033] In this example, b=2 256 .

[0034] Using the split width b, the multipliers A and B are recorded as follows: A=A0+A1b+A2b 2 +A3b 3 =(A0,A1,A2,A3) b , B=B0+B1b+B2b 2 +B3b 3 =(B0,B1,B2,B3) b .

[0035] Use 13 registers to store intermediate operation results and do the following calculations:

[0036] Calculate four multiplications: M0=A0*B0, M1=A1*B1, M2=A2*B2, M3=A3*B3.

[0037] Calculate four additions: M4=A0+A1, M5=A2+A3, M6=B0+B1, M7=B2+B3.

[0038] Calculation: M8 = (1 + b) * (M0 + b * M1 + b 2 *M2+b 3 *M3).

[0039] Calculation: M9 = b*M4*M6, M 10 =b 3 *M5*M7.

[0040] Calculation: M 11 =A0+A2+(A1+A3)*b,M 12 =B0+B2+(B1+B3)*b.

[0041] Calculation: M 12 =(1+b 2 )*(M8+M9+M 10 )+b 2 *M 11 *M 12 .

[0042] The resulting register M 12 The result is the final result of the large integer A*B.

[0043] Because A0, A1, A2, A3 and B0, B1, B2, B3 are all integers not exceeding log2 b=256 bits, when calculating the product of any two of them, a parallel acceleration solution for integer multiplication can be implemented by applying SIMD instructions.

[0044] This specific embodiment uses AVX instructions to perform parallel acceleration on integer multiplication with a length less than 256 bits, including the following steps:

[0045] Let X and Y be two integers not exceeding 256 bits. Follow the steps below to calculate X*Y.

[0046] 1. Split the multiplier X. First, the bits in the binary representation of X are recorded from low to high as the array X[255:0] and split into two 128-bit arrays, namely X[255:128] and X[127:0].

[0047] 2. Split the multiplier Y. The bits in the binary representation of the multiplier Y are denoted as the array Y[255:0] from low to high, and split into eight 32-bit arrays, namely Y[255:224], Y[223:192], Y[191:160], Y[159:128], Y[127:96], Y[95:64], Y[63:32], and Y[31:0].

[0048] 3. Divide the array X[255:128] into four 32-bit arrays and load them into the AVX register L1 using the instruction _mm256_insert_epi32(), so that L1[255:128]={32′b0,

[0049] 4. Use instruction _mm256_insert_epi32() to load Y[255:224] into AVX register L2, so L2[255:0]={,32'b0,Y[255:224],32'b0,Y[255:224],32'b0,Y[255:224],32'b0,Y[255:224]}.

[0050] 5. Multiply the split parts separately using the parallel multiplication instruction. Call the instruction _mm256_mul_ps(L1,L2,L3) and use the instruction _mm256_add_ps() to sort the result in L3. The result is that L3[255:96] is the result of X[255:128]*Y[255:224].

[0051] 6. Replace Y[255:224] in step 4 with the next three 32-bit data in the multiplier Y, repeat steps 4 and 5, and obtain L4[223:64], L5[191:32], and L6[159:0].

[0052] 7. Use the instruction _m256_add_ps() to accumulate L3, L4, L5, and L6 in sequence, and save the result to L3.

[0053] 8. Repeat steps 4, 5, 6, and 7 three times, and save the results to L4, L5, and L6 respectively. You can get L3 = X[255:128]*Y[255:128], L4 = X[127:0]*Y[255:128], L5 = X[255:128]*Y[127:0], and L6 = X[127:0]*Y[127:0].

[0054] 9. Use shift and parallel add instructions to process the multiplication results. Use the instruction _mm256_add_ps() to arrange the above results so that L3 is the low-order 256 bits of the result of X*Y, and L6 is the high-order 256 bits of the result of X*Y, that is, L3 = (X*Y)[511:256], L6 = (X*Y)[255:0].

[0055] The sorting methods in step 5 and step 9 are similar, both are shift accumulation. Taking step 9 as an example, the details are as follows: L4′=L4+{L3[127:0],32'b0}, L6′=L6+{L5[127:0],32'b0}; L3′={L3[255:128],32'b0}+{32'b0,L4′[127:0]}+{32'b0,L3[255:128]}; L5′={L5[255:128],32'b0}+{32'b0,L6′[127:0]}+{L4′[255:128],32'b0}.

[0056] Next we optimize the speed of the Montgomery modular multiplication algorithm by applying optimized large integer fast multiplication.

[0057] For large integers A and B, we calculate the Montgomery modular product A*B mod N as follows.

[0058] First, the parameter R is selected. The parameter R is an integer power of the split width b and is the minimum value that satisfies R>N.

[0059] Apply optimized large integer fast multiplication to calculate the Montgomery representation of A A = AR mod N = (A0, A1, A2, A3)b .

[0060] Apply the optimized large integer fast multiplication to calculate the Montgomery representation of B B = BR mod N = (B0, B1, B2, B3) b .

[0061] Calculate q=-N using the extended Euclidean algorithm -1 (mod b).

[0062] Apply the optimized large integer fast multiplication to calculate u0=B0*A0*q mod b, p0=(B0*A+u0*N) / b.

[0063] Apply the optimized large integer fast multiplication u1=B1*A0*q mod b, p1=(p0+B1*A+u1*N) / b.

[0064] Apply the optimized large integer fast multiplication to calculate u2=B2*A0*q mod b, p2=(p1+B2*A+u2*N) / b.

[0065] Apply the optimized large integer fast multiplication to calculate u3=B3*A0*q mod b, p3=(p2+B3*A+u3*N) / b.

[0066] Compare the values ​​of p3 and p3-N. When p3 ≥ N, A*B mod N = p3-N; otherwise, A*B mod N = p3. Although the present invention has been disclosed above in terms of preferred embodiments, it is not intended to limit the present invention. Any person skilled in the art may make possible changes and modifications to the technical solutions of the present invention by using the methods and technical contents disclosed above without departing from the spirit and scope of the present invention. Therefore, any simple modifications, equivalent changes and modifications made to the above embodiments based on the technical essence of the present invention without departing from the content of the technical solutions of the present invention shall fall within the scope of protection of the technical solutions of the present invention. The contents not described in detail in the specification of the present invention belong to the common knowledge of those skilled in the art.

Claims

1. A large integer multiplication optimization method, characterized in that include: Collect large integer data from security scenarios, group them into pairs, and then preset a split width. Then, split the two large integer data in each group based on the split width. Determine the corresponding split items according to the split results of the large integer data, use the split items of the two large integer data in the same group to perform staggered multiplication, and add the split items of the two large integer data in the same group to the same group data; Calculate the multiplier of each group of large integer data according to the staggered multiplication result and the split width; Calculate the first sum and second sum of each group of large integer data according to the addition result of the same group of data and the split width; Use the split items and split widths of each large integer data to calculate the sum of the two groups of large integer data respectively; The total multiplier of each group of large integer data is calculated based on the divided sums of the two groups of large integer data, the first sum and the second sum of each group of large integer data, and the multiplier of each group of large integer data, and the total multiplier is sent to an external computer for key identification in a security scenario.

2. A large integer multiplication optimization method according to claim 1, characterized in that: The two large integer data in the i-th group of the security scenario include A i With B i , the split width is preset to b, b is a power of 2 and satisfies the large integer data A i With B i In b-base representation, no more than 4 bits are allowed; large integer data A i With B i The calculation unit length is log2 b bits; during the total multiplier calculation process, all calculation data are stored and recorded through the instruction register.

3. A large integer multiplication optimization method according to claim 2, characterized in that: The method for splitting the two large integer data in each group according to the split width is: The large integer data A i With B i It is divided into four parts, recorded as: A i =A i0 +A i1 b+A i2 b 2 +A i3 b 3 ,B i =B i0 +B i1 b+B i2 b 2 +B i3 b 3 Where A i0 \A i1 \A i2 \A i3 A i The segmentation items of each part of the big data, B i0 \B i1 \B i2 \B i3 For B i The segmentation items of each part of the big data.

4. A large integer multiplication optimization method according to claim 2, characterized in that: The method of using the split items of two large integer data in the same group to perform staggered multiplication is: M i0 =A i0 *B i0 ,M i1 =A i1 *B i1 ,M i2 =A i2 *B i2 ,M i3 =A i3 *B i3 Where M i0 、M i1 、M i2 、M i3 is the result of misplaced multiplication; The method for adding data in the same group is: M i4 =A i0 +A i1 ,M i5 =A i2 +A i3 ,M i6 =B i0 +B i1 ,M i7 =B i2 +B i3 Where M i4 、M i5 、M i6 、M i7 It is the result of adding the data in the same group.

5. A large integer multiplication optimization method according to claim 4, characterized in that: Calculate the multiplier M of each group of large integer data according to the staggered multiplication result and the split width i8 The method is: M i8 =(1+b)*(M i0 +b*M i1 +b 2 *M i2 +b 3 *M i3 ) Calculate the first sum M of each group of large integer data based on the addition result of the same group of data and the split width i9 , the second sum M i10 The method is: M i9 =b*M i4 *M i6 ,M i10 =b 3 *M i5 *M i7 。 6. A large integer multiplication optimization method according to claim 4, characterized in that: Calculate the sum of two sets of large integer data M i11 、M i12 The method is: M i11 =A i0 +A i2 +(A i1 +A i3 )*b M i12 =B i0 +B i2 +(B i1 +B i3 )*b; Calculate the total multiplier M of each group of large integer data i13 The method is: M i13 =(1+b 2 )*(M i8 +M i9 +M i10 )+b 2 *M i11 *M i12 。 7. A large integer multiplication optimization method according to claim 4, characterized in that: Calculate the result of the staggered multiplication and the multiplier M i8 , the first sum M i9 , the second sum M i10 , total multiplier M i13 In the process of multiplication, all kinds of data multiplication calculations are performed through AVX instructions. The method of performing multiplication calculation through AVX instructions is: Perform multiplier splitting for any multiplier X and any multiplier Y to obtain two multiplier X arrays and eight multiplier Y arrays respectively; After splitting the first multiplier X array into four arrays of equal size, all arrays are loaded into the first register using AVX instructions; Load the first multiplier Y array directly into the second register via AVX instructions; Multiply the contents of the first register and the second register using AVX instructions, and store the data in the third register after sorting; The first multiplier Y array is replaced by the second multiplier Y array, the third multiplier Y array, and the fourth multiplier Y array, respectively, and the multipliers are processed using AVX instructions and loaded into the fourth register, the fifth register, and the sixth register, respectively. Use AVX instructions to accumulate the contents of the third register, the fourth register, the fifth register, and the sixth register, and update the total accumulated content into the third register; The second, third, and fourth multiplier Y arrays are loaded into the second register using AVX instructions, and the total accumulation of the register contents is repeated. When the second multiplier Y array is loaded into the second register, the total accumulation is updated to the fourth register; when the third multiplier Y array is loaded into the third register, the total accumulation is updated to the fifth register; when the second multiplier Y array is loaded into the fourth register, the total accumulation is updated to the sixth register; and the total accumulation corresponding to each group of multiplier Y arrays is updated in the corresponding register. Obtain the contents of the third, fourth, fifth, and sixth registers after the total accumulation is completed, perform data sorting using AVX instructions, obtain the updated contents of the third, fourth, fifth, and sixth registers, use the contents of the third register as the lower 256 bits of the data result of the arbitrary multiplier X and the arbitrary multiplier Y, use the contents of the sixth register as the upper 256 bits of the data result of the arbitrary multiplier X and the arbitrary multiplier Y, and determine the multiplier calculation results of the arbitrary multiplier X and the arbitrary multiplier Y based on the current contents of the third and sixth registers.

8. A large integer multiplication optimization method according to claim 7, characterized in that: The updated contents of the third register, the fourth register, the fifth register, and the sixth register are updated respectively by shift accumulation, specifically: L4′=L4+{L3[127:0],32'b0} L6′=L6+{L5[127:0],32'b0} L3′={L3[255:128],32'b0}+{32'b0,L4′[127:0]}+{32'b0,L3[255:128]} L5′={L5[255:128],32'b0}+{32'b0,L6′[127:0]}+{L4′[255:128],32'b0} Where, L3L4L5L6 are the contents of the third register, fourth register, fifth register, and sixth register, L i [255:128] is register L i High 128 bits, L i [127:0] is register L i Lower 128 bits.

9. A large integer multiplication optimization method according to claim 7, characterized in that: The calculation result of the total multiplier is obtained by Montgomery modular multiplication, and the calculation method of Montgomery modular multiplication is: Preset selected parameters, the selected parameters are integer powers of the split width b; Calculate the Montgomery representation of the first set of large integer data and the Montgomery representation of the second set of large integer data; The extended Euclidean algorithm is used to calculate the expanded parameters of the segmentation width b; Montgomery representation products are calculated according to the Montgomery representation of the first set of large integer data, the Montgomery representation of the second set of large integer data, and the expanded parameter of the split width b, and the number of Montgomery representation products is the same as the number of arrays in the Montgomery representation of the second set of large integer data; Compare the last set of Montgomery representation products and the difference between the Montgomery representation products and the modulus. If the last set of Montgomery representation products is larger, output the difference between the Montgomery representation products and the modulus as the Montgomery modular multiplication; otherwise, output the last set of Montgomery representation products as the Montgomery modular multiplication.

10. A large integer multiplication optimization method according to claim 9, characterized in that: In the calculation method of Montgomery modular multiplication, the Montgomery representation of the first group of large integer data, the Montgomery representation of the second group of large integer data, and the expanded parameter of the split width b all adopt a calculation method of performing multiplier calculation through AVX instructions.

Citation Information

Cited By

  • All-pipeline universal Montgomery modular multiplication operation method for x86-64 platform

    CN121508862A