Execution method of modular multiplication operation and processor
By using floating-point number calculation unit and FMA instructions to perform modular multiplication operations in the processor, the problem of the performance bottleneck of modular multiplication operations is solved, efficient calculation is achieved, and the safety and reliability of the system are improved.
Patent Information
- Application Number
- CN202410918869.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-09
- Publication Date
- 2024-11-08
AI Technical Summary
Modular multiplication operations are performance bottlenecks in algorithms in the fields of privacy computing and cryptography, resulting in increased processor load and energy consumption, affecting system security and reliability.
The floating-point number calculation unit is used to perform efficient modulus multiplication operations, calculate the product of the two target factors and the modulus through the FMA instruction, and determine the target remainder by using the difference to reduce the calculation amount of integer multiplication.
It significantly improves the speed and efficiency of modem multiplication operation, reduces the load and energy consumption of the processor, and improves the safety and reliability of the system.
Smart Images

Figure CN118915997A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present specification relate to the field of computer technology, and more particularly, to a method and device for executing a modular multiplication operation, and a processor. Background Art
[0002] Modular multiplication refers to calculating the product of two integers and then performing a modulo operation. In fields such as privacy computing and cryptography, such as lattice cryptography, RSA encryption, and elliptic curve encryption, modular multiplication is the core computing step and the performance bottleneck of these algorithms.
[0003] Therefore, the embodiments of this specification propose an efficient modular multiplication operator, which can significantly improve the computational efficiency of related algorithms, reduce the processor load and energy consumption, and thus improve the security and reliability of the system. Summary of the invention
[0004] The embodiments of this specification describe a method and a processor for performing a modular multiplication operation.
[0005] According to a first aspect, a method for executing a modular multiplication operation is provided, which is executed by a target processor; the target processor includes a general processing unit and a floating-point number calculation unit. The method includes: the general processing unit reads a target coefficient, two target factors to be subjected to a modular multiplication operation, and corresponding moduli; the target coefficient is pre-calculated based on the moduli. The floating-point number calculation unit processes two floating-point numbers corresponding to the two target factors to obtain a first high-order value and a first low-order value corresponding to a first product of the two. The floating-point number calculation unit processes the two floating-point numbers corresponding to the first low-order value and the target coefficient to obtain a second high-order value and a second low-order value corresponding to a second product of the two. The floating-point number calculation unit processes the two floating-point numbers corresponding to the second low-order value and the moduli to obtain a third high-order value corresponding to a third product of the two. The general processing unit subtracts the third high-order value from the first high-order value, thereby determining a target remainder using the difference obtained.
[0006] In some embodiments, the two target factors are respectively a polynomial coefficient and a rotation factor in a number theoretic transformation.
[0007] In some embodiments, the floating-point number calculation unit processes the two floating-point numbers corresponding to the two target factors to obtain a first high-order value and a first low-order value corresponding to a first product of the two, including: executing an FMA instruction based on a first parameter set to obtain a first output; wherein the first parameter set includes the two floating-point numbers as multipliers and the first value as addend; the first value is determined based on the precision of the floating-point number. Executing an FMA instruction based on a second parameter set to obtain a second output; wherein the second parameter set includes the two floating-point numbers as multipliers and the second value as addend; the second value is determined based on the first output and the precision of the floating-point number. Extracting the precision bit parts from the first output and the second output respectively, corresponding to the first high-order value and the first low-order value.
[0008] In some embodiments, the floating-point number calculation unit processes the two floating-point numbers corresponding to the first low-order value and the target coefficient to obtain a second high-order value and a second low-order value corresponding to the second product of the two, including: executing an FMA instruction based on a third parameter set to obtain a third output; wherein the third parameter set includes the two floating-point numbers as multipliers and the third value as addend; the third value is determined based on the floating-point number precision. Executing an FMA instruction based on a fourth parameter set to obtain a fourth output; wherein the fourth parameter set includes the two floating-point numbers as multipliers and the fourth value as addend; the fourth value is determined based on the third output and the floating-point number precision. Extracting the precision bit parts from the third output and the fourth output respectively, corresponding to the second high-order value and the second low-order value.
[0009] In some embodiments, the floating point number calculation unit processes the two floating point numbers corresponding to the second low-order value and the modulus to obtain a third high-order value corresponding to a third product of the two, including: executing an FMA instruction based on a fifth parameter set to obtain a fifth output; wherein the fifth parameter set includes the two floating point numbers as multipliers and the fifth value as addend; the fifth value is determined based on the floating point number precision. Extract the precision bit portion from the fifth output as the third high-order value.
[0010] In some embodiments, one of the two target factors is the result of a modulo operation after shifting the original factor left by r bits; wherein r is the number of constrained bits of the target factor, and the modulo operation and the modular multiplication operation use the same modulus. Wherein, determining the target remainder using the obtained difference includes: if it is determined that the difference is not less than 0, determining the difference as the target remainder; if it is determined that the difference is less than 0, determining the sum of the difference and the modulus as the target remainder.
[0011] In some embodiments, determining the target remainder using the obtained difference includes: when it is determined that the difference is not less than 0, shifting the difference left by r bits and performing a modulo operation to obtain the target remainder; wherein r is the number of constrained bits of the target factor, and the modulo operation and the modular multiplication operation use the same modulus. When it is determined that the difference is less than 0, shifting the sum of the difference and the modulus left by r bits and performing a modulo operation to obtain the target remainder.
[0012] According to a second aspect, a method for executing a modular multiplication operation is provided, which is executed by a target processor; the target processor includes a general processing unit and a floating-point number calculation unit. The method includes: the general processing unit reads two target factors and corresponding moduli to be subjected to modular multiplication operation, and a transformation factor obtained by pre-calculating the second target factor; the floating-point number calculation unit processes the two floating-point numbers corresponding to the first target factor and the transformation factor to obtain a first high-order value corresponding to the first product of the two; the floating-point number calculation unit processes the two floating-point numbers corresponding to the two target factors to obtain a second product of the two; the floating-point number calculation unit processes the first high-order value and the two floating-point numbers corresponding to the moduli to obtain a third product of the two; the general processing unit uses the second product to subtract the third product, thereby determining the target remainder using the obtained difference.
[0013] In some embodiments, the two target factors are respectively a polynomial coefficient and a rotation factor in a number theoretic transformation.
[0014] In some embodiments, the pre-calculation includes: shifting the second target factor left by r bits, performing a modulo operation and then rounding it down; wherein r is the number of constrained bits of the target factor, and the modulo operation and the modular multiplication operation use the same modulus.
[0015] In some embodiments, the floating-point number calculation unit processes the two floating-point numbers corresponding to the transformation factor and the second target factor to obtain a first high-order value corresponding to a first product of the two, including: executing an FMA instruction based on a first parameter set to obtain a first output; wherein the first parameter set includes the two floating-point numbers as multipliers and a first value as an addend; the first value is determined based on the precision of the floating-point numbers. Extract the precision bit portion from the first output as the first high-order value.
[0016] In some embodiments, the floating-point number calculation unit processes the two floating-point numbers corresponding to the two target factors to obtain a second product of the two, including: executing an FMA instruction based on a second parameter set to obtain a second output; wherein the second parameter set includes the two floating-point numbers as multipliers and a second value as an addend; and the second value is determined based on the floating-point number precision. Executing an FMA instruction based on a third parameter set to obtain a third output; wherein the third parameter set includes the two floating-point numbers as multipliers and a third value as an addend; and the third value is determined based on the second output and the floating-point number precision. Extracting the precision bit parts from the second output and the third output respectively, and splicing them to obtain the second product.
[0017] In some embodiments, the floating-point number calculation unit processes the two floating-point numbers corresponding to the first high-order value and the modulus to obtain a third product of the two, including: executing an FMA instruction based on a fourth parameter set to obtain a fourth output; wherein the fourth parameter set includes the two floating-point numbers as multipliers and the fourth value as addend; the fourth value is determined based on the floating-point number precision. Executing an FMA instruction based on a fifth parameter set to obtain a fifth output; wherein the fifth parameter set includes the two floating-point numbers as multipliers and the fifth value as addend; the fifth value is determined based on the fourth output and the floating-point number precision. Extracting the precision bit parts from the fourth output and the fifth output respectively, and splicing them to obtain the third product.
[0018] In some embodiments, the target remainder is determined using the obtained difference, including: when it is determined that the difference is less than the modulus, the difference is determined as the target remainder; when it is determined that the difference is not less than the modulus, the result of subtracting the modulus from the difference is determined as the target remainder.
[0019] According to a third aspect, a processor is provided, comprising a general processing unit and a floating-point number calculation unit; wherein the general processing unit is used to read a target coefficient, and two target factors and corresponding moduli to be subjected to modular multiplication operations; the target coefficient is pre-calculated based on the moduli. The floating-point number calculation unit is used to process two floating-point numbers corresponding to the two target factors, and obtain a first high-order value and a first low-order value corresponding to a first product of the two. The floating-point number calculation unit is also used to process two floating-point numbers corresponding to the first low-order value and the target coefficient, and obtain a second high-order value and a second low-order value corresponding to a second product of the two. The floating-point number calculation unit is also used to process two floating-point numbers corresponding to the second low-order value and the moduli, and obtain a third high-order value corresponding to a third product of the two. The general processing unit is also used to use the first high-order value to subtract the third high-order value, thereby determining the target remainder using the difference obtained.
[0020] According to a fourth aspect, a processor is provided, comprising a general processing unit and a floating-point number calculation unit. The general processing unit is used to read two target factors and corresponding moduli to be subjected to modular multiplication operations, and a transformation factor obtained by pre-calculating the second target factor. The floating-point number calculation unit is used to process the two floating-point numbers corresponding to the first target factor and the transformation factor, and obtain a first high-order value corresponding to the first product of the two. The floating-point number calculation unit is used to process the two floating-point numbers corresponding to the two target factors, and obtain a second product of the two. The floating-point number calculation unit is used to process the two floating-point numbers corresponding to the first high-order value and the moduli, and obtain a third product of the two. The general processing unit is used to subtract the third product from the second product, thereby determining the target remainder using the difference obtained.
[0021] In summary, by using the execution method and processor of the modular multiplication operation disclosed in the embodiments of this specification, and by using a floating-point calculation unit to introduce efficient floating-point instructions into the modular multiplication operation, a significant improvement in the speed of the modular multiplication operation can be achieved. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0023] Figure 1 A schematic diagram showing the structure of the target processor disclosed in the embodiments of this specification;
[0024] Figure 2 One of the flowcharts of the method for executing the modular multiplication operation disclosed in the embodiments of this specification is shown;
[0025] Figure 3 The second flowchart of the method for executing the modular multiplication operation disclosed in the embodiment of this specification is shown. DETAILED DESCRIPTION
[0026] The solution provided in this specification is described below in conjunction with the above-mentioned drawings.
[0027] The embodiments of this specification disclose an efficient modular multiplication operator, and specifically propose to use floating point instructions to accelerate modular multiplication operations. To help understanding, the inventive concept is first introduced below.
[0028] In fields such as privacy computing and cryptography, the implementation of related algorithms relies on processors such as CPUs and GPUs. Floating-point instructions are commonly used computing instructions in these processors. Due to the rapid growth in demand for graphics computing and scientific computing (including deep learning), the floating-point computing capabilities of processors are also constantly improving.
[0029] In the conventional implementation of modular multiplication, the product of two integers is calculated using integer instructions. However, in many processors, the throughput of floating-point multiplication is much higher than that of integer multiplication.
[0030] Based on the above observations and analysis, the applicant proposes to make full use of the hardware unit in the processor that executes floating-point instructions to achieve efficient modular multiplication operations, thereby accelerating the application of cryptographic algorithms such as lattice cryptography and homomorphic encryption, and privacy computing algorithms.
[0031] Next, combine Figure 1 , introduces the processor on which the above-mentioned efficient modular multiplication operator is executed (hereinafter referred to as the target processor for clarity), and then further combines Figure 2 This section describes the specific steps of how a processor executes an operator.
[0032] Figure 1 The target processor shown in FIG. 1 includes a general processing unit (or general processor core) and a floating point calculation unit.
[0033] The general processing unit is the core component of the target processor, responsible for executing most common computing tasks and instructions, such as arithmetic operations, logical operations, data movement, etc. It has high flexibility and can process various types of data and operations.
[0034] The floating-point calculation unit is used to execute floating-point instructions, such as the Fused MultiplyAdd (FMA) instruction or floating-point subtraction instruction. The FMA instruction can perform the multiplication of two floating-point numbers and add another floating-point number in one operation, thereby significantly improving the computing efficiency and accuracy and enhancing the performance of the processor by reducing the storage and transmission of intermediate results.
[0035] In general, general-purpose processing units provide a wide range of computing capabilities, while floating-point computing units play an important role in specific high-performance computing tasks.
[0036] The above is a conceptual description of the target processor. Next, the steps of using the target processor to execute the above-mentioned efficient modular multiplication operator are introduced, and then the correctness of the modular multiplication operation result is mathematically proved.
[0037] Figure 2 One of the flowcharts of the method for executing the modular multiplication operation disclosed in the embodiment of this specification is shown. The method can be performed by Figure 1 The target processor shown in FIG. 1 executes the above operation, comprising the following steps:
[0038] In step S210, the general processing unit reads the target coefficient z, as well as the two target factors a and b to be subjected to modular multiplication operation and the corresponding modulus p; the target coefficient z is pre-calculated based on the modulus p; in step S220, the floating-point number calculation unit processes the two floating-point numbers corresponding to the two target factors a and b, and obtains the first high-order value u.hi and the first low-order value u.lo corresponding to the first product u of the two; in step S230, the floating-point number calculation unit processes the first low-order value u.lo and the two floating-point numbers corresponding to the target coefficient z, and obtains the second high-order value s.hi and the second low-order value s.lo corresponding to the second product s of the two; in step S240, the floating-point number calculation unit processes the second low-order value s.lo and the two floating-point numbers corresponding to the modulus p, and obtains the third high-order value v.hi corresponding to the third product v of the two; in step S250, the general processing unit subtracts the third high-order value v.hi from the first high-order value u.hi, and thereby determines the target remainder res using the obtained difference.
[0039] To help understanding, the above process steps are first presented in Table 1 in a centralized and simple manner with the help of mathematical symbols and formulas.
[0040] Table 1 (Objective: Calculate res = (a*b) mod p)
[0041]
[0042] Next, the above steps are introduced in combination with Table 1:
[0043] First, in step S210, the general processing unit reads a target coefficient z, as well as two target factors a and b and a corresponding modulus p to be subjected to modular multiplication operation; the target coefficient z is pre-calculated based on the modulus p.
[0044] For example, in number theory transformation, a may be a polynomial coefficient and b may be a rotation factor; or, in a cryptographic algorithm, a may be input data and b may be a fixed parameter.
[0045] It can be understood that precomputation means that certain results are calculated and stored in advance before actual calculation or processing, so that these pre-calculated results can be directly accessed in subsequent use, thereby improving efficiency. For example, the calculation of the target coefficient z can be performed using the following formula:
[0046] z=p -1 mod 2 r (8)
[0047] Where r represents the number of constrained bits of the target factors a, b and the modulus p, that is, the number of bits of a, b, and p are all less than r. According to the definition of the modulo operation, the calculation of formula (8) can also constrain z to be within r bits.
[0048] Based on the two target factors a and b and the target coefficient z read above, subsequent steps can be executed.
[0049] In step S220, the floating-point number calculation unit processes the two floating-point numbers corresponding to the two target factors a and b to obtain a first high-order value u.hi and a first low-order value u.lo corresponding to a first product u of the two.
[0050] It should be noted that the "first" in the first product, as well as similar terms such as "second" and "third" elsewhere in the text, are used to distinguish things of the same kind and do not have other limiting functions such as sorting.
[0051] To help understand, let's first briefly introduce the concepts of floating point numbers. The representation of floating point numbers is based on scientific notation. In computer science, floating point numbers usually use the binary system in the form of: (±)1.M×2 E , where M is the mantissa (not necessarily starting with 1, depending on the floating point standard), and E is the exponent. Therefore, the storage of floating point numbers is divided into three parts: the sign bit that determines the positive or negative (±) of the number, the exponent bit that stores the exponent E, and the mantissa bit (or precision bit) that stores the mantissa M (usually omitting the leading 1 to save storage space).
[0052] The floating point precision m refers to the number of bits of precision in the floating point standard, that is, the number of bits occupied by the above-mentioned mantissa M. It can be understood that the floating point numbers in the text have the same precision and meet the same floating point standard. In addition, the IEEE 754 standard is mainly used for exemplary explanation in the text. In the IEEE 754 standard, a double-precision floating point number occupies 64 bits, of which 1 bit is used for the sign, 11 bits are used for the exponent, and 52 bits are used for the mantissa. Compared with single-precision floating point numbers, double-precision floating point numbers are a more accurate floating point representation form, which uses more bits to store the exponent and mantissa parts, thereby providing higher precision and a larger range.
[0053] The above introduces the relevant concepts of floating point numbers. It should be understood that in this step, the conversion of two integer numbers, namely the target factors a and b, into corresponding floating point numbers can be performed by the general processing unit; in addition, the constrained number r of the target factors a, b, etc. is configured to be equal to the floating point precision m, that is, r=m.
[0054] In the following description of the execution of this step, the converted floating point number is mainly taken as a double-precision floating point number as an example, and the conversion results of the target factors a and b are recorded as double(a) and double(b) respectively.
[0055] Specifically, this step proposes executing the above FMA instruction twice based on double(a) and double(b) to obtain the first high-order value u.hi and the first low-order value u.lo.
[0056] 1) The FMA instruction is executed for the first time, with the first parameter set as input, and the first output d1 is obtained. The first parameter set includes two floating point numbers double(a) and double(b) as multipliers, and the first value c1 as addend. The execution of this FMA instruction can be expressed as follows:
[0057] d1=FMA(double(a),double(b),c1)=double(a)*double(b)+c1 (9)
[0058] Furthermore, the mantissa (or fractional part) bit string M can be extracted from the first output d1. i , which is actually the high m bits of the first product u between a and b, and the corresponding integer can be used as the first high-order value u.hi. The explanation is as follows:
[0059] The first addend c1 in formula (9) is determined based on the floating point precision m. For example, when the standard format of floating point numbers is 1.M*2 E When the first addend c1 is set to c1 = 1*2 2m , 1*2 2m The 1 in is taken from the 1 to the left of the decimal point in 1.M.
[0060] The number of significant digits of double(a)*double(b) in formula (9) is less than 2m because both a and b are controlled within r (=m) bits. Therefore, the floating point number d1 calculated using formula (9) must be multiplied by the first addend c1 (=1*2 2m ) is limited to the following forms:
[0061] d1=1.M1*2 2m (10)
[0062] The mantissa part M1 depends on the calculation result of double(a)*double(b), and the m-bit bit string corresponding to the mantissa part M1 is the upper m bits of the first product u between a and b.
[0063] The above description is based on the first output d1 of the first execution of the FMA instruction to obtain the first high-order value u.hi.
[0064] 2) The FMA instruction is executed for the second time, with the second parameter set as input, and the second output d2 is obtained. The second parameter set includes the two floating point numbers double(a) and double(b) as multipliers, and the second value c2 as addend. The execution of this FMA instruction can be expressed as follows:
[0065] d2=FMA(double(a),double(b),c2)=double(a)*double(b)+c2 (11)
[0066] Furthermore, the mantissa (or fractional part) bit string M2 can be extracted from the second output d2, which is actually the lower m bits of the first product u between a and b, and the corresponding integer can be used as the first lower value u.lo. This is explained as follows:
[0067] The second value c2 in formula (11) is determined based on the first output d1 and m. For example:
[0068] c2=2 2m +2 m -d1(12)
[0069] After substituting formula (10) into formula (12), the following derivation can be performed:
[0070] c2=2 2m +2 m -1.M1*2 2m =2 m -0.M1*2 2m
[0071] =2 m -M1*2 m =2 m -(M1<<m) (13)
[0072] Among them, << represents a left shift operation, M1<<m means that the binary bits of M1 are shifted to the left by m bits and the right side is filled with 0, that is, M1<<m=M1*2 m It is important to understand that left or right shift operations have a lower priority than addition and subtraction operations.
[0073] Furthermore, substituting formula (13) into formula (11) yields:
[0074] d2=double(a)*double(b)-(M1<<m)+2 m (14)
[0075] Among them, the result of double(a)*double(b) without loss of precision is 2m bits, and double(a)*double(b)-(M1<<m) is equivalent to removing the high m bits M1 in the 2m bit string. m , that is, add 1*2 m , the calculated floating point number d2 must be in the form of 1.M2*2 m Therefore, the m-bit string M2 of the mantissa is the lower m bits of the first product u between a and b.
[0076] The above description is based on the second output d2 of the second execution of the FMA instruction to obtain the second high-order value u.lo.
[0077] From the above, by using the floating point calculation unit to process the two floating point numbers corresponding to the two target factors a and b, the first high-order value u.hi and the first low-order value u.lo corresponding to the first product u can be obtained. For the relationship between a, b, u, u.hi and u.lo, refer to formulas (1) and (2) in Table 1.
[0078] Before, after or simultaneously with executing step S220, in step S230, the floating-point number calculation unit processes the two floating-point numbers corresponding to the first low-order value u.lo and the target coefficient z to obtain the second high-order value s.hi and the second low-order value s.lo corresponding to the second product s of the two.
[0079] It should be noted that the execution process of this step can refer to the description of step S220. In short, a, b, u.hi, and u.lo in step S220 are replaced with u.lo, z, s.hi, and s.lo in this step to obtain the execution process of this step. In addition, for the relationship between u.lo, z, s.hi, and s.lo in this step, refer to formulas (3) and (4) in Table 1.
[0080] Next, in step S240, the floating-point number calculation unit processes the two floating-point numbers corresponding to the second low-order value s.lo and the modulus p to obtain a third high-order value v.hi corresponding to a third product v of the two.
[0081] It should be noted that the execution of this step can refer to the description of step S220. The difference is that in this step, only one FMA instruction needs to be executed to obtain the third high-order value v.hi, and there is no need to continue to execute the second FMA instruction to obtain the third low-order value v.lo. In addition, for the relationship between s.lo, p, v.hi and v.lo mentioned in this step, refer to formulas (5) and (6) in Table 1.
[0082] Then, in step S250, the general processing unit uses the first high-order value u.hi to subtract the third high-order value v.hi, and uses the obtained difference to determine the target remainder res. For this, please refer to formula (7) in Table 1. Further, formula (7) can be refined as follows:
[0083]
[0084] It should be noted that res calculated using formula (15) is equal to u*2 -r mod p, which will be proved later. Therefore, in order to obtain u mod p, post-processing or pre-processing can be added.
[0085] If post-processing is used, then execute another step res = (res * 2 r ) mod p. It should be noted that in order to improve the calculation efficiency, R = 2 is generally pre-calculated r , that is to say, res = (res*R) mod p may be executed in post-processing.
[0086] If the preprocessing method is used, then in step S210, one of the two target factors read by the general unit is the result of precalculating the original factor. Taking the target factor b as an example, the precalculation can be recorded as b=(b*R) mod p.
[0087] Although modular multiplication is involved in preprocessing or postprocessing, it is not necessary to pay attention to the time consumption of this stage in practical applications. On the one hand, the data obtained by pre-calculation can be reused, such as the rotation factors in number theory transformation. On the other hand, pre-calculation or post-calculation can be accumulated multiple times and calculated together (become *R k mod p). In addition, some optimization schemes can be adopted for pre-calculation or post-calculation, including using FMA instructions for calculation, but this is not the focus of this improvement scheme, so it will not be discussed further.
[0088] From the above, we can get the target remainder res, which satisfies res=umod p, or expressed as res=(a*b)mod p.
[0089] In summary, by adopting the execution method of the modular multiplication operation disclosed in the embodiments of this specification, efficient floating-point instructions are introduced into the modular multiplication operation using the floating-point calculation unit to replace the time-consuming integer multiplication instructions, which can significantly improve the speed of the modular multiplication operation.
[0090] To help understand, the following is the res calculated using formula (15), which is equal to u*2 -r mod p, that is, u*R -1 modp is used to prove it. Specifically, it can be divided into proving the following two points:
[0091] ①Prove that 0≤res<p
[0092] ②Prove that res≡u*R -1 (mod p), that is, res mod p = u*R -1 mod p
[0093] 1. The proof of ① is as follows:
[0094] For the difference u.hi-v.hi in formula (15), it can be derived (the content after the symbol / / indicates a comment):
[0095]
[0096] Among them, (u>>r)-(v>>r)=uv>>r. Combining the two operations of >>r and modR, we can see that they are equivalent. To prove that the equation holds, we only need to prove that uv≡0(modR). The proof process is as follows:
[0097]
[0098] Therefore, uv≡0(mod p), which proves that (u>>r)-(v>>r)=uv>>r.
[0099] Continuing the above derivation of u.hi-v.hi:
[0100] u.hi-v.hi=uv>>r
[0101] =us.lo*p>>r / / Substitute into formula (5)
[0102] For us.lo*p, we can deduce:
[0103] us.lo*p≤u≤(p-1) 2 <R*p, where the first “≤” holds because s.lo*p≥0; the second “≤” holds because u=a*b, where a, b≤p-1; “<” holds because p-1<R, and p-1<p.
[0104] It can also be deduced that:
[0105] us.lo*p≥-s.lo*p>-R*p, where “≥” holds because u≥0; ">” holds because -s.lo>-R*p.
[0106] From the above, -R*p<us.lo*p<R*p, and then we can get: -p<us.lo*p>>r<p, that is, -p<u.hi-v.hi<p. Based on this, according to formula (15), we can get:
[0107] When u.hi-v.hi≥0, 0≤res=u.hi-v.hi<p;
[0108] When u.hi-v.hi<0, 0=-p+p<res=u.hi-v.hi+p<p.
[0109] Therefore, 0≤res<p, and the proof of ① is completed.
[0110] 2. The proof of ② is as follows:
[0111] To prove that res ≡ u*R -1 (mod p), according to the two parallel expressions in formula (15), we need to prove the following formulas (16) and (17):
[0112] u.hi-v.hi≡u*R -1 (mod p) (16)
[0113] u.hi-v.hi+p≡u*R -1 (mod p) (17)
[0114] Obviously, u.hi-v.hi≡u.hi-v.hi+p(mod p), therefore, we can choose to prove either formula (16) or (17), and choose formula (16) for proof. Specifically, substituting u.hi-v.hi=(us.lo*p)>>r derived in proof ① above into formula (16), we can convert it into proof:
[0115] (us.lo*p)>>r≡u*R -1 (mod p) (18)
[0116] The proof of formula (18) includes:
[0117]
[0118] →us.lo*p≡[(1-pz)u+(u*z>>r)*R*p](mod p) (20)
[0119] For 1-pz in formula (20), since 1≡RR -1 (mod p), pz≡0(mod p), so:
[0120] 1-pz≡RR-1 (mod p)(21)
[0121] According to (21), formula (20) can be transformed into:
[0122] us.lo*p>>r≡RR -1 u+(u*z>>r)*R*p>>r(mod p)
[0123] →us.lo*p>>r≡[R -1 u+(u*z>>r)*p]*R>>r(mod p)
[0124] →us.lo*p>>r≡[R -1 u+(u*z>>r)*p](mod p)
[0125] →us.lo*p>>r≡R -1 u(mod p)
[0126] Therefore, formula (18) is proved, and then formula (16) is proved, and then ② is proved.
[0127] The above proves ① and ② respectively, which also proves that res calculated by formula (15) satisfies: res = u*R -1 modp, and then prove that the target remainder res = a*b mod u after introducing the above preprocessing or postprocessing.
[0128] According to another embodiment, another method for executing modular multiplication operation by introducing floating point instructions is provided. Figure 3 The second flow chart of the method for executing the modular multiplication operation disclosed in the embodiment of this specification is shown. The method can be performed by Figure 1 The target processor shown in FIG. 1 executes the above operation, comprising the following steps:
[0129] In step S310, the general processing unit reads two target factors a and b and the corresponding modulus p to be subjected to modular multiplication operation, and the transformation factor b′ obtained by pre-calculating the second target factor b; in step S320, the floating-point number calculation unit processes the two floating-point numbers corresponding to the first target factor a and the transformation factor b′, and obtains the first high-order value x.hi corresponding to the first product x of the two; in step S330, the floating-point number calculation unit processes the two floating-point numbers corresponding to the two target factors a and b, and obtains the second product y of the two; in step S340, the floating-point number calculation unit processes the first high-order value x.hi and the two floating-point numbers corresponding to the modulus p, and obtains the third product z of the two; in step S350, the general processing unit subtracts the third product z from the second product y, and thereby determines the target remainder res using the obtained difference.
[0130] To help understanding, the above process steps are first presented in Table 2 in a centralized and simple manner with the help of mathematical symbols and formulas.
[0131] Table 2 (Objective: Calculate res = (a*b) mod p)
[0132]
[0133] Next, the above steps are introduced in combination with Table 2:
[0134] First, in step S310, the general processing unit reads two target factors a and b to be subjected to modular multiplication operation and the corresponding modulus p, as well as a transformation factor b′ obtained by pre-calculating the second target factor b.
[0135] For example, in number theory transformation, a may be a polynomial coefficient and b may be a rotation factor; or, in a cryptographic algorithm, a may be input data and b may be a fixed parameter.
[0136] Exemplarily, the above pre-calculation may adopt the following formula:
[0137] R=2 r (27)
[0138]
[0139] Here, r represents the constrained number of bits of the target factors a, b and the modulus p, that is, the number of bits of a, b, and p are all less than r; from this, a simple inference shows that b′ calculated in formula (28) is also within r bits.
[0140] Based on the two target factors a and b and the transformation factor b′ read above, subsequent steps can be performed.
[0141] Next, in step S320, the floating-point number calculation unit processes the two floating-point numbers corresponding to the first target factor a and the transformation factor b′ to obtain a first high-order value x.hi corresponding to a first product x of the two.
[0142] It should be noted that the execution of this step can refer to Figure 2 The difference is that in this step, only one FMA instruction needs to be executed to obtain the first high-order value x.hi, and there is no need to continue to execute the second FMA instruction to obtain the first low-order value x.lo. In addition, for the relationship between a, b′, x.hi and x.lo mentioned in this step, refer to formulas (22) and (23) in Table 2.
[0143] Before, after or simultaneously with step S320, step S330 may be executed, where the floating-point number calculation unit processes two floating-point numbers corresponding to the two target factors a and b to obtain a second product y of the two.
[0144] It should be noted that the execution of this step can refer to Figure 2 The difference is that in this step, after extracting the precision bits of the two outputs obtained by executing the FMA instruction twice, the two precision bits are further concatenated to obtain a 2m-bit bit string, and the converted integer is used as the second product y. In addition, for the relationship between a, b, and y mentioned in this step, refer to formula (24) in Table 2.
[0145] On the other hand, after executing step S320 to obtain the first high-order value x.hi, step S340 can be executed, and the floating-point number calculation unit processes the two floating-point numbers corresponding to the first high-order value x.hi and the modulus p to obtain a third product z of the two.
[0146] It should be noted that the execution of this step can refer to the description of step S330. In addition, for the relationship between x.hi, p, and z mentioned in this step, refer to formula (25) in Table 2.
[0147] Thereafter, in step S350, the general processing unit subtracts the third product z from the second product y, thereby determining the target remainder res using the obtained difference.
[0148] Specifically, when it is determined that the difference is less than the modulus, the difference is determined as the target remainder; when it is determined that the difference is not less than the modulus, the result of subtracting the modulus from the difference is determined as the target remainder. This can be written as:
[0149]
[0150] From the above, we can get the target remainder res, which satisfies res = u mod p, or expressed as res = (a*b) mod p. The proof of this is as follows:
[0151] yz=a*bx.hi*p / / Substitute into formula (24) and (25)
[0152] Therefore, on the one hand, yz≡a*bx.hi*p(mod p), so:
[0153] yz≡a*b(mod p) (30)
[0154] That is, the difference yz is of the same modulus as the product of the target factors a and b.
[0155] On the other hand, we can continue to deduce:
[0156]
[0157] According to 0≤α<1, 0≤β<1, and a<R, it is easy to get Furthermore, combined with formula (30), it can be seen that only one conditional subtraction is required to obtain the final result. Specifically:
[0158] when When When res = yz;
[0159] when When , res=yzp.
[0160] The above completes the proof of formula (29).
[0161] In summary, by adopting the execution method of the modular multiplication operation disclosed in the embodiments of this specification, efficient floating-point instructions are introduced into the modular multiplication operation using the floating-point calculation unit to replace the time-consuming integer multiplication instructions, which can significantly improve the speed of the modular multiplication operation.
[0162] According to another embodiment, there is also provided a computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute Figure 2 or Figure 3 The method described.
[0163] According to another embodiment, a computing device is provided, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, Figure 2 or Figure 3 The method described.
[0164] Those skilled in the art should be aware that in one or more of the above examples, the functions described in the present invention can be implemented by hardware, software, firmware or any combination thereof. When implemented by software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or codes on a computer-readable medium.
[0165] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific implementation method of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made on the basis of the technical solution of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for performing a modular multiplication operation, performed by a target processor; The target processor includes a general processing unit and a floating point calculation unit; the method includes: The general processing unit reads a target coefficient, two target factors to be subjected to modular multiplication operation, and a corresponding modulus; the target coefficient is pre-calculated based on the modulus; The floating point number calculation unit processes the two floating point numbers corresponding to the two target factors to obtain a first high-order value and a first low-order value corresponding to a first product of the two floating point numbers; The floating point number calculation unit processes the two floating point numbers corresponding to the first low-order value and the target coefficient to obtain a second high-order value and a second low-order value corresponding to a second product of the two; The floating point number calculation unit processes the two floating point numbers corresponding to the second low-order value and the modulus to obtain a third high-order value corresponding to a third product of the two; The general processing unit subtracts the third high-order value from the first high-order value, thereby determining a target remainder using the obtained difference.
2. The method according to claim 1, wherein: The two target factors are respectively the polynomial coefficients and the rotation factors in the number theory transformation.
3. The method according to claim 1, wherein: The floating point number calculation unit processes the two floating point numbers corresponding to the two target factors to obtain a first high-order value and a first low-order value corresponding to a first product of the two floating point numbers, including: Executing the FMA instruction based on a first parameter set to obtain a first output; wherein the first parameter set includes the two floating-point numbers as multipliers and a first value as an addend; the first value is determined based on the floating-point number precision; Executing the FMA instruction based on a second parameter set to obtain a second output; wherein the second parameter set includes the two floating-point numbers as multipliers and a second value as addend; the second value is determined based on the first output and the floating-point number precision; The precision bit parts are extracted from the first output and the second output respectively as the first high-order value and the first low-order value respectively.
4. The method according to claim 1, wherein: The floating point number calculation unit processes two floating point numbers corresponding to the first low-order value and the target coefficient to obtain a second high-order value and a second low-order value corresponding to a second product of the two, including: Executing the FMA instruction based on a third parameter set to obtain a third output; wherein the third parameter set includes the two floating point numbers as multipliers and a third value as addend; the third value is determined based on the floating point number precision; Executing the FMA instruction based on a fourth parameter set to obtain a fourth output; wherein the fourth parameter set includes the two floating-point numbers as multipliers and a fourth value as an addend; the fourth value is determined based on the third output and the floating-point number precision; The precision bit parts are extracted from the third output and the fourth output respectively as the second high-order value and the second low-order value respectively.
5. The method according to claim 1, wherein: The floating point number calculation unit processes the two floating point numbers corresponding to the second low-order value and the modulus to obtain a third high-order value corresponding to a third product of the two, including: Executing the FMA instruction based on a fifth parameter set to obtain a fifth output; wherein the fifth parameter set includes the two floating point numbers as multipliers and a fifth value as an addend; the fifth value is determined based on the floating point number precision; A precision bit portion is extracted from the fifth output as the third high-order value.
6. The method according to claim 1, wherein: One of the two target factors is the result of a modulo operation after the original factor is shifted left by r bits; wherein r is the number of constrained bits of the target factor, and the modulo operation and the modular multiplication operation use the same modulus; The obtained difference is used to determine the target remainder, including: In the case where it is determined that the difference is not less than 0, determining the difference as the target remainder; When it is determined that the difference is less than 0, the sum of the difference and the modulus is determined as the target remainder.
7. The method according to claim 1, wherein: The obtained difference is used to determine the target remainder, including: When it is determined that the difference is not less than 0, the difference is shifted left by r bits and then modulo operation is performed to obtain the target remainder; wherein r is the number of constrained bits of the target factor, and the modulo operation and the modular multiplication operation use the same modulus; When it is determined that the difference is less than 0, the sum of the difference and the modulus is shifted left by r bits and then subjected to the modulus operation to obtain the target remainder.
8. A method for performing a modular multiplication operation, performed by a target processor; The target processor includes a general processing unit and a floating point calculation unit; the method includes: The general processing unit reads two target factors and corresponding moduli to be subjected to modular multiplication operation, and a transformation factor obtained by precalculating the second target factor; The floating point number calculation unit processes the two floating point numbers corresponding to the first target factor and the transformation factor to obtain a first high-order value corresponding to a first product of the two; The floating point number calculation unit processes the two floating point numbers corresponding to the two target factors to obtain a second product of the two floating point numbers; The floating point number calculation unit processes the first high-order value and the two floating point numbers corresponding to the modulus to obtain a third product of the two; The general processing unit subtracts the third product from the second product, thereby determining a target remainder using the obtained difference.
9. The method according to claim 8, wherein: The two target factors are respectively the polynomial coefficients and the rotation factors in the number theory transformation.
10. The method according to claim 8, wherein: The pre-calculation includes: shifting the second target factor left by r bits, performing a modulo operation and then rounding it down; wherein r is the number of constrained bits of the target factor, and the modulo operation and the modular multiplication operation use the same modulus.
11. The method according to claim 8, wherein: The floating point number calculation unit processes the two floating point numbers corresponding to the transformation factor and the second target factor to obtain a first high-order value corresponding to a first product of the two, including: Executing the FMA instruction based on a first parameter set to obtain a first output; wherein the first parameter set includes the two floating-point numbers as multipliers and a first value as an addend; the first value is determined based on the floating-point number precision; A precision bit portion is extracted from the first output as the first high-order value.
12. The method according to claim 8, wherein: The floating point number calculation unit processes the two floating point numbers corresponding to the two target factors to obtain a second product of the two floating point numbers, including: Executing the FMA instruction based on a second parameter set to obtain a second output; wherein the second parameter set includes the two floating-point numbers as multipliers and a second value as an addend; and the second value is determined based on the floating-point number precision; Executing the FMA instruction based on a third parameter set to obtain a third output; wherein the third parameter set includes the two floating-point numbers as multipliers and a third value as addend; the third value is determined based on the second output and the floating-point number precision; The precision bit parts are respectively extracted from the second output and the third output, and the second product is obtained by concatenating them.
13. The method according to claim 8, wherein: The floating point number calculation unit processes the first high-order value and two floating point numbers corresponding to the modulus to obtain a third product of the two, including: Executing the FMA instruction based on a fourth parameter set to obtain a fourth output; wherein the fourth parameter set includes the two floating point numbers as multipliers and a fourth value as an addend; the fourth value is determined based on the floating point number precision; Executing the FMA instruction based on a fifth parameter set to obtain a fifth output; wherein the fifth parameter set includes the two floating-point numbers as multipliers and a fifth value as an addend; the fifth value is determined based on the fourth output and the floating-point number precision; The precision bit parts are extracted from the fourth output and the fifth output respectively, and the resultant parts are concatenated to obtain the third product.
14. The method according to claim 8, wherein: The obtained difference is used to determine the target remainder, including: In the case where it is determined that the difference is less than the modulus, determining the difference as the target remainder; When it is determined that the difference is not less than the modulus, a result of subtracting the modulus from the difference is determined as the target remainder.
15. A processor comprising a general processing unit and a floating point calculation unit; wherein: The general processing unit is used to read the target coefficient, as well as two target factors and corresponding moduli to be subjected to modular multiplication operation; The target coefficient is pre-calculated based on the modulus; The floating point number calculation unit is used to process the two floating point numbers corresponding to the two target factors to obtain a first high-order value and a first low-order value corresponding to a first product of the two floating point numbers; The floating point number calculation unit is further used to process the two floating point numbers corresponding to the first low-order value and the target coefficient to obtain a second high-order value and a second low-order value corresponding to a second product of the two; The floating point number calculation unit is further used to process the two floating point numbers corresponding to the second low-order value and the modulus to obtain a third high-order value corresponding to a third product of the two; The general processing unit is further configured to subtract the third high-order value from the first high-order value, thereby determining a target remainder using the obtained difference.
16. A processor comprising a general processing unit and a floating point calculation unit; wherein: The general processing unit is used to read two target factors and corresponding moduli to be subjected to modular multiplication operation, and a transformation factor obtained by precalculating the second target factor; The floating point number calculation unit is used to process the two floating point numbers corresponding to the first target factor and the transformation factor to obtain a first high-order value corresponding to a first product of the two; The floating point number calculation unit is used to process the two floating point numbers corresponding to the two target factors to obtain a second product of the two; The floating point number calculation unit is used to process the two floating point numbers corresponding to the first high-order value and the modulus to obtain a third product of the two; The general processing unit is used for subtracting the third product from the second product, thereby determining a target remainder using the obtained difference.
