Execution method of modulo operation and processor

By using FMA hardware units to execute floating-point instructions, pre-calculate and multiply subtraction in multiple multiplications and subtraction, the problem of low modulus operation efficiency in the accumulation sum calculation in the prime domain is solved, and efficient accumulation calculation and accelerated homomorphic encryption are achieved. algorithm.

CN118915996APending Publication Date: 2024-11-08ALIPAY (HANGZHOU) INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410917970.1
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

Technical Problem

Accumulation sum calculations in the prime domain require complex modulo fetching operations, resulting in high computational overhead, especially when processing large amounts of data, which has a significant impact on efficiency.

Method used

The FMA hardware unit in the processor executes efficient floating-point number instructions, pre-calculate the target coefficient and target value, perform multiple multiplication and subtraction operations to determine the target remainder, reduce the number of conditional subtractions, and improve the efficiency of the modulus operation.

Benefits of technology

It significantly improves the execution efficiency of accumulated sum calculations in the prime domain, reduces calculation overhead, and improves the application speed of privacy computing algorithms such as homomorphic encryption.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118915996A_ABST
    Figure CN118915996A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses an execution method of a modulo operation. The execution method is executed by a target processor. The target processor includes a general purpose processing unit, and an FMA hardware unit to execute a product and melt add instruction (FMA) for a floating-point number. The method comprises the following steps that: a universal processing unit reads a target coefficient r and a target numerical value a to be subjected to modulus operation, wherein the target coefficient r is obtained by pre-calculating based on a modulus p of the modulus operation and a constraint digit k of the target numerical value a; the FMA hardware unit processes the floating-point number determined based on the target numerical value a and the target coefficient r to obtain a first product between the target numerical value a and the target coefficient r; the FMA hardware unit processes floating-point numbers corresponding to the intermediate value t and the modulus p to obtain a second product between the intermediate value t and the modulus p, and the intermediate value t is obtained by shifting the first product to the right by the constraint digit k; the general processing unit uses the target value a to subtract the integer number corresponding to the second product so as to determine the target remainder by using the obtained difference value.
Need to check novelty before this filing date? Find Prior Art

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 performing a modulo operation, and a processor. Background Art

[0002] Accumulation and summation calculations performed in the prime number field are common operations in many application areas, especially in cryptography and privacy computing.

[0003] Typically, homomorphic encryption is one of the main technical means to achieve data privacy protection. A larger ciphertext modulus is commonly used in homomorphic encryption schemes. At the same time, the residual number representation technology is used to convert the operations under the ciphertext modulus into multiple operations in the prime number domain. Cumulative summation is one of the operations that often appears.

[0004] The accumulation and summation of prime number domains requires complex modulo operations, which can ensure that the data remains within the prime number domain, but it also brings high computational overhead, especially when processing large amounts of data, which will have a significant impact on computational efficiency. Therefore, it is very necessary to develop efficient modulo operators to accelerate the accumulation and summation calculations in prime number domains. Summary of the invention

[0005] The embodiments of this specification describe a method and processor for executing a modulo operation, which can utilize a hardware unit in the processor for executing a fused-multiply-add instruction FMA to efficiently execute the modulo operation, thereby improving the execution efficiency of calculations such as accumulation and summation involving the modulo operation.

[0006] According to a first aspect, a method for executing a modulo operation is provided, which is executed by a target processor; the target processor includes a general processing unit and an FMA hardware unit for executing a fused multiplication and addition instruction FMA for floating-point numbers; the method includes:

[0007] The general processing unit reads the target coefficient and the target value to be subjected to the modulo operation; the target coefficient is pre-calculated based on the modulus corresponding to the modulo operation and the number of constrained bits corresponding to the target value. The FMA hardware unit processes the floating-point number determined based on the target value and the target coefficient to obtain the first product between the target value and the target coefficient. The FMA hardware unit processes the floating-point numbers corresponding to the intermediate value and the modulus to obtain the second product between the intermediate value and the modulus; the intermediate value is obtained by right-shifting the integer number corresponding to the first product by the number of constrained bits. The general processing unit uses the target value to subtract the integer number corresponding to the second product, and thereby determines the target remainder using the difference obtained.

[0008] In a possible implementation, reading a target value to be subjected to a modulo operation includes: reading an accumulated value from an accumulator register of the target processor as the target value; the accumulated value is obtained by sequentially adding a plurality of operands to the accumulator register.

[0009] Furthermore, in some possible embodiments, after determining the target remainder, the method further includes: updating the value of the accumulator register to the target remainder.

[0010] In a possible implementation, the method further includes: the general processing unit divides the bit string corresponding to the target value into a high m-bit bit string and a low m-bit bit string, where m is the floating point number precision supported by the FMA hardware unit. Based on this, the FMA hardware unit processes the floating point number determined based on the target value and the target coefficient, including: processing two floating point numbers to obtain corresponding sub-products for constructing the first product; one of the two floating point numbers corresponds to the target bit string, and the other corresponds to the target coefficient, and the target bit string is any one of the high m-bit bit string and the low m-bit bit string.

[0011] Further, in some possible embodiments, the constrained bit number is greater than the floating point number precision.

[0012] In some possible embodiments, processing two floating point numbers to obtain corresponding sub-products includes: 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; and the first value is determined based on m. 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 first output and m. Extracting precision bit portions from the first output and the second output as high-order and low-order bit strings, respectively, to construct the sub-product.

[0013] In a possible implementation, the FMA hardware unit processes the floating-point numbers corresponding to the intermediate value and the modulus to obtain a second product between the intermediate value and the modulus, including: executing an FMA instruction based on a third parameter set to obtain a third output; wherein the third parameter set includes two floating-point numbers as multipliers and a third value as an addend; one of the two floating-point numbers corresponds to the intermediate value, and the other corresponds to the modulus, and the third value is determined based on m. 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 a fourth value as an addend; and the fourth value is determined based on the third output and m. Extracting the precision bit part from the third output and the fourth output as the high-order and low-order bit strings, respectively, to construct the second product.

[0014] In a possible implementation, the target remainder is determined using the obtained difference, including: when it is determined that the difference is greater than or equal to the modulus, determining a result of subtracting the modulus from the difference as the target remainder.

[0015] In a possible implementation, determining a target remainder using the obtained difference includes: in a case where it is determined that the difference is smaller than the modulus, determining the difference as the target remainder.

[0016] According to the second aspect, a processor is provided, the processor comprising a general processing unit and an FMA hardware unit for executing a fused product-add instruction FMA for floating-point numbers; wherein the general processing unit is used to read a target coefficient and a target value to be subjected to a modulo operation; the target coefficient is pre-calculated based on the modulus corresponding to the modulo operation and the number of constrained bits corresponding to the target value. The FMA hardware unit is used to process a floating-point number determined based on the target value and the target coefficient, and obtain a first product between the target value and the target coefficient. The FMA hardware unit is also used to process floating-point numbers corresponding to an intermediate value and the modulus, respectively, and obtain a second product between the intermediate value and the modulus; the intermediate value is obtained by right-shifting the integer corresponding to the first product by the number of constrained bits. The general processing unit is also used to use the target value to subtract the integer corresponding to the second product, thereby determining a target remainder using the difference obtained.

[0017] In summary, by adopting the execution method or target processor of the modulo operation disclosed in the embodiments of this specification, efficient floating-point instructions are introduced into the modulo operation using the FMA hardware unit, thereby significantly improving the speed of the modulo operation. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] 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.

[0019] Figure 1 A schematic diagram showing the structure of the target processor disclosed in the embodiments of this specification;

[0020] Figure 2 A schematic flow chart of a method for executing a modulo operation disclosed in an embodiment of this specification is shown;

[0021] Figure 3 A flow chart of a method for calculating the product between two m-bit bit strings using an FMA instruction disclosed in an embodiment of this specification is shown. DETAILED DESCRIPTION

[0022] The solution provided in this specification is described below in conjunction with the accompanying drawings.

[0023] As mentioned above, the accumulation and summation of prime fields requires complex modulo operations (also called modular reduction operations). To help understand, let's first introduce several basic concepts mentioned in this paragraph. Prime field: A finite field whose number of elements is a prime number p. The set of elements in the prime field is {0,1,2,…,p-1}. Each element in the field can be added, subtracted, multiplied, and divided (except for division by 0), and the result of the operation is still in this field.

[0024] Cumulative summation (or cumulative calculation): Cumulative calculation refers to the process of gradually adding values ​​to a cumulative total. In mathematics and computer programming, this concept often involves iterating over a series of values ​​and adding them one by one to an accumulator (a variable used to store intermediate results) with an initial value (usually zero). In mathematics, it can be written using the summation symbol:

[0025] Cumulative calculation in the prime number field: Cumulative calculation is performed on the elements in the prime number field, and the calculation result is maintained in the prime number field through modular reduction operation.

[0026] In a traditional solution, the modulo operation involved in the accumulation and summation of the prime field needs to be implemented using a large number of conditional subtractions, which can be illustrated as the following pseudo code:

[0027] while a>=p:

[0028] a-=p

[0029] return a

[0030] That is, when it is determined that the accumulated value a satisfies the condition that it is greater than or equal to the modulus p, the accumulated value is updated to the result after subtracting the modulus, and this cycle is repeated until a does not meet the condition, and the current value of a is returned as the modulus result. When the accumulated sum a is much greater than the modulus p, the conditional subtraction will be executed multiple times, resulting in a large computational overhead and time consumption.

[0031] In addition, the applicant found that in the privacy computing scenario where there is an urgent need to accelerate the cumulative sum calculation in the prime number domain, the implementation of cryptographic algorithms such as homomorphic encryption depends on processors such as CPU and GPU. 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.

[0032] Based on the above observations and analysis, the applicant proposed an improvement scheme that does not require a large number of conditional subtractions, but instead makes full use of the hardware units in the processor that execute floating-point instructions to achieve efficient modulo operations, thereby accelerating prime field accumulation calculations and promoting the application of privacy computing algorithms such as homomorphic encryption.

[0033] Next, combine Figure 1 Introduce the processor based on which the above improvement scheme is implemented, and then further combine Figure 2 Introduce the specific implementation steps of the improvement plan.

[0034] For the sake of clarity and brevity, the processor on which the improved solution is implemented is called the target processor. Figure 1 As shown, the target processor includes a general processing unit (or general processor core) and an FMA hardware unit for executing a fused multiply add (FMA) instruction for floating point numbers.

[0035] 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.

[0036] The FMA hardware unit has the ability to execute FMA instructions. This type of instruction can perform the multiplication of two floating-point numbers and add another floating-point number in one operation, thereby improving computing efficiency and accuracy. The FMA hardware unit significantly improves processor performance by reducing the storage and transmission of intermediate results, especially in applications involving a large number of floating-point operations, such as scientific computing, graphics processing, and deep learning. In addition, it should be noted that the FMA hardware unit can also be referred to as a floating-point calculation unit. It should be understood that the floating-point calculation unit in a general processor supports FMA instructions and also supports the execution of floating-point subtraction instructions, etc.

[0037] In general, general-purpose processing units provide a wide range of computing capabilities, while FMA hardware units play an important role in specific high-performance computing tasks.

[0038] The above is a conceptual description of the target processor. The following describes the implementation steps of the target processor when executing the above improvement plan. Specifically, Figure 2 The following steps are shown in:

[0039] In step S210, the general processing unit reads a target coefficient r and a target value a to be subjected to a modulo operation; the target coefficient r is pre-calculated based on a modulus p corresponding to the modulo operation and a constrained number k corresponding to the target value a.

[0040] To facilitate understanding, the sources and value ranges of each value mentioned in this step are first introduced in turn.

[0041] 1) Modulus p and target value a.

[0042] First of all, you need to understand that Figure 2 The method shown here aims to calculate a mod p, and, The symbol Indicates floor operation.

[0043] In the scenario of performing modular accumulation calculations such as cumulative summation in the prime number domain, the target value a can be the accumulated value read from the accumulator register of the target processor, and this accumulated value is obtained by adding one or more operands to the accumulator register in sequence. The modulus p can be the number of elements in the prime number domain. It should be understood that the modulo operation can also be applied to other scenarios besides cumulative summation. In this case, the target value a can be the intermediate result of other calculations besides cumulative summation, and the modulus p can be pre-configured.

[0044] In addition, considering that the modulus p needs to be converted from an integer type to a floating-point type later, in order to prevent precision loss after conversion to a floating-point type and when participating in floating-point calculations, the number of bits of the modulus p needs to be controlled within the floating-point precision m, that is, It should be noted that for the values ​​mentioned in the article, such as the values ​​mentioned in this step, when there is no special indication that they are floating-point types, they are generally defaulted to integer types.

[0045] To help understand, let's 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:

[0046] (±)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).

[0047] 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 precisions in the improved scheme are the same and meet the same floating point standard. In addition, the IEEE 754 standard is mainly used for exemplary explanation in this article. 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.

[0048] Let’s go back to the numerical description in this step.

[0049] 2) The number of constrained digits k and the target coefficient r.

[0050] The number of bits constrained k refers to the upper limit of the number of bits of the target value a. k is greater than the number of bits of the modulus p, that is, Otherwise, there is no need to perform the modulo p operation on the target value a.

[0051] The target coefficient r is pre-calculated based on the modulus p and the number of constraint bits k. 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 formula of the target coefficient r can be:

[0052]

[0053] Among them, << represents the left shift operator, 1<<k means to shift the binary bit of the number 1 to the left by the specified k bits, and fill the right side with zeros, that is, 1<<k=2 k Considering that the target coefficient r needs to be converted from an integer type to a floating-point type in the future, in order to prevent precision loss after conversion to a floating-point type and when participating in floating-point calculations, the number of bits of the target coefficient r needs to be controlled within the floating-point precision m, that is, This means that the range of k is also required. Specifically, combined with the properties of the floor operation, it can be simply deduced that

[0054] From the above, we can see that Generally, it is preferred In this way, the value range of the target value a can be maximized. For example, based on the standard format defined for double-precision floating-point numbers in the IEEE 754 standard, it can be designed

[0055] In addition, when the target value a is the cumulative number in the cumulative summation scenario, a modulus is taken each time a number is added in the traditional modular accumulation calculation, which can be illustrated as the following pseudo code:

[0056]

[0057] In the embodiment of this description, k can be designed to be a larger value to support unified modulus after multiple additions. Specifically, the operands involved in the accumulation are standardized to a predetermined number of bits, so the larger the k, the more batch accumulations can be performed before each modular reduction operation. Since the total number of accumulations is fixed, increasing k can effectively reduce the number of modular reduction operations.

[0058] In addition, when the target value a is the sum of the modular multiplication and addition scenarios, a modulus is taken after each multiplication and addition in the traditional modular multiplication and addition calculation:

[0059]

[0060] In the embodiment of this description, k can be designed to be a larger value to support unified modulo after multiple multiplication and accumulation. Specifically, the operands involved in the multiplication operation in the modular multiplication and addition are standardized to a predetermined number of bits, and the number of operands involved in the accumulation is adaptively determined, so the larger k is, the more batch multiplication and accumulation can be performed before each modular reduction operation, and because the total number of multiplication and accumulation is fixed, increasing k can effectively reduce the number of modular reduction operations.

[0061] The above describes the various values ​​mentioned in this step. In this step S210, the general processor unit can read the target coefficient r and the target value a to be subjected to the modulo operation.

[0062] Next, in step S220, the FMA hardware unit processes the floating-point number determined based on the target value a and the target coefficient r to obtain a first product x (= a*r) between the target value a and the target coefficient r. It should be noted that the "first" in the first product, as well as the "second", "third" and other similar terms in the text, are used to distinguish between similar things and do not have other limiting functions such as sorting.

[0063] As mentioned above, the number of constraints k on the target value a satisfies:

[0064] Furthermore, in one implementation, At this point, the implementation of this step may include Figure 3 The following sub-steps S31-S34 are shown in:

[0065] Step S31, the general processing unit divides the bit string corresponding to the target value a into a high m-bit bit string and a low m-bit bit string. It can be understood that the floating point number precision represented by m is also the floating point number precision supported by the FMA hardware unit.

[0066] In this implementation, k>m, which means that the number of effective digits of the target value a may exceed the precision of the floating point number, and it is proposed to split the target value a before converting it into a floating point number. For intuitive description, the high m-bit string obtained by splitting is recorded as a.hi, and the low m-bit string is recorded as a.lo. Obviously, the two satisfy the following formula:

[0067] a=a.hi<<m+a.lo (2)

[0068] Step S32: The FMA hardware unit processes the floating-point numbers corresponding to the high-order m-bit string a.hi and the target coefficient r to obtain the corresponding first sub-product a.hi*r.

[0069] It should be understood that the conversion from integer to floating point can be performed by a general processing unit. In this paper, the converted floating point number is mainly taken as a double precision floating point number, and the conversion results of a.hi and r are recorded as double(a.hi) and double(r) respectively.

[0070] In this step S32, considering that the floating point product between double(a.hi) and double(r) may cause precision loss, it is proposed to execute the FMA instruction twice to obtain the high bit and low bit of a.hi*r respectively, so as to construct the complete a.hi*r. Specifically:

[0071] 1) First, execute the FMA instruction based on the first parameter set to obtain the first output. The first parameter set includes two first floating point numbers, double(a.hi) and double(r), as multipliers and the first value c1 as addend. Therefore, the calculation formula of the first output d1 can be written as:

[0072] d1=FMA(double(a.lo),double(r),c1)=double(a.lo)*double(r)+c1(3)

[0073] The first addend c1 is determined based on the floating point number precision m. For example, when the standard format of the floating point number is 1.M*2 E When the first addend c1 is set to c1 = 1*2 2m , 1*2 2mThe 1 in is taken from the 1 to the left of the decimal point in 1.M. Therefore, because the number of significant digits of double(a.lo)*double(ratio) is less than 2m (see above that a.lo and ratio are both designed to be less than m bits), the floating point number d1 calculated by formula (3) must be 1.M1*2 2m .

[0074] Furthermore, a bit string M1 of the mantissa (or fractional part) can be extracted from the first output d1, which is actually the high m bits of the product of a.1o and ratio.

[0075] 2) Execute the FMA instruction based on the second parameter set to obtain the second output. The first parameter set includes two first floating point numbers, double(a.hi) and double(r), as multipliers and the second value c2 as addend. Therefore, the calculation formula of the second output d2 can be written as:

[0076] d2=FMA(double(a.hi),double(r),c2)=double(a.hi)*double(r)+c2 (4)

[0077] The second value c2 is determined based on the first output d1 and m. For example, c2=2 2m +2 m -d1.

[0078] Furthermore, the mantissa bit string M2 can be extracted from the second output d2, which is actually the lower m bits of the product of a.lo and ratio. The proof is as follows:

[0079] c2=2 2m +2 m -d1=2 2m +2 m -1.M1*2 2m =2 m -0.M1*2 2m

[0080] =2 m -M1*2 m =2 m -M1<<52 (5)

[0081] Therefore, c2=2 m Substituting -M1<<52 into the above formula (4) we can get:

[0082] d2=(double(a.hi)*double(r)-M1<<52)+2 m (6)

[0083] Among them, the result without precision loss corresponding to double(a.hi)*double(r) is 2m bits, and double(a.hi)*double(r)-M1<<52 is equivalent to removing the high m bits in 2m bits. Therefore, according to formula (6), after removing the high m bits, add 2 m , the calculated floating point number d2 must be in the form of 1.M2*2 m The bit string M2 of the mantissa thus extracted is actually the lower m bits of the product of a.lo and ratio.

[0084] 3) Using the bit string M1 extracted based on the first output d1 and the bit string M2 extracted based on the second output d1, the first sub-product a.hi*r can be obtained. This can be written as:

[0085] a.hi*r=M1<<m+M2 (7)

[0086] From the above, the first sub-product a.hi*r between the high m-bit bit string a.hi and the target coefficient r can be obtained by executing the FMA instruction for the floating point number.

[0087] Step S33, the FMA hardware unit processes the floating-point numbers corresponding to the lower m-bit string a.lo and the target coefficient r to obtain the corresponding second sub-product a.lo*r.

[0088] It should be noted that the description of step S33 can refer to the introduction of step S32. In short, the execution process of step S33 can be obtained by replacing the high m-bit string a.hi in step S32 with the low m-bit string a.lo.

[0089] Step S34, the general processing unit constructs the first product x based on the first sub-product a.hi*r and the second sub-product a.lo*r. This can be written as:

[0090] x=(a.hi*r)<<m+a.lo*r (8)

[0091] Furthermore, formula (8) can be simply transformed into x = (a.hi < < m + a.lo) * r, and then substituted into formula (5), thereby obtaining:

[0092] x=a*r (9)

[0093] The above introduces sub-steps S31-S34 for implementing step S220. In this way, the first product x between the target value a and the target coefficient r can be obtained by executing the FMA instruction for the floating-point number and making full use of the precision bits of the floating-point number, thereby accelerating the modulo operation by making full use of the FMA hardware unit in the target processor, which is particularly suitable for the scenario where the throughput of floating-point multiplication calculation is much higher than that of integer multiplication in the target processor that performs the modulo operation.

[0094] In another implementation, At this time, different from Figure 3 In the illustrated implementation, the target value a does not need to be split, but the FMA hardware unit can directly process the floating point numbers corresponding to the target value a and the target coefficient r to obtain the first product x. The specific implementation process can refer to the aforementioned step S32. In short, the high m-bit bit string a.hi in step S32 is replaced with the target value a to obtain the implementation process in this implementation case.

[0095] From the above, the first product x between the target value a and the target coefficient r can be efficiently obtained by using the FMA hardware unit.

[0096] In step S230, the general processing unit right-shifts the first product x by the constrained bit number k to obtain an intermediate value t. It should be understood that the operand of the bit shift operation (including left shift or right shift) is generally an integer by default.

[0097] Specifically, the execution of this step can be recorded as:

[0098] t=x>>k(10)

[0099] Step S240, the FMA hardware unit processes the floating point numbers corresponding to the intermediate value t and the modulus p, and obtains the second product y between the intermediate value t and the modulus p. The relationship between the intermediate value t, the modulus p, and the second product y can be expressed as:

[0100] y=t*p(11)

[0101] It can be observed that formula (11) is very similar to formula (9), and the process of calculating the second product y in this step can be understood by analogy. In one implementation, k≥m. At this time, according to formula (10), it can be known that t obtained by right shifting 2m-bit x by k bits is not greater than m bits. Further, it is not necessary to split t as a is split in step S31 above, but the execution process of step S32 above can be referred to. In short, the high m-bit bit string a.hi in step S32 is replaced by the intermediate value t, and the target coefficient r is replaced by the modulus p, and the execution process of this step can be obtained.

[0102] In another implementation, k<m. At this time, according to formula (10), after the 2m-bit x is shifted right by k bits, the obtained t>m bits. Further, t needs to be split like a in the above step S31. Specifically, see Figure 3 In the entire execution process shown, in simple terms, the target value a can be replaced by the intermediate value t, and the target coefficient r can be replaced by the modulus p, so that the execution process of this step can be obtained.

[0103] From the above, the FMA hardware unit can be used to efficiently obtain the second product y between the intermediate value t and the modulus p.

[0104] Step S250: The general processing unit subtracts the second product y from the target value a to obtain a difference, namely, ay.

[0105] Need to explain, or Specifically, combined with formula (11), we can see that what needs to be proved is or This is deduced as follows:

[0106]

[0107] Further, according to a≤2 k (Because the number of constraints of a is k), that is thus, thereby, Then, following the above derivation, according to the definition of floor operation:

[0108]

[0109] The above proves or According to formula (11), this also proves that:

[0110]

[0111] From the above, the difference (ay) between the target value a and the second product y can be obtained.

[0112] In step S260, the general processing unit determines the target remainder u using the obtained difference (ay).

[0113] Specifically, when it is determined that the difference is greater than or equal to the modulus p, the result of subtracting the modulus p from the difference is determined as the target remainder u, otherwise the difference is determined as the target remainder u. This can be written as:

[0114]

[0115] The proof of u = a mod p can be based on And formula (13) can be simply deduced and will not be elaborated on here.

[0116] Thus, only one conditional subtraction is required in this step to obtain the correct result of a modulo p. In addition, it should be noted that in the scenario of cumulative calculation, the value of the cumulative register can be updated to the target remainder u after this step.

[0117] In summary, the execution method of the modulo operation disclosed in the embodiment of this specification is adopted, and the FMA hardware unit in the processor is used to introduce efficient floating-point instructions in the modulo operation, so as to significantly improve the speed of the modulo operation. Furthermore, when the modulo operation is a component operation of the accumulation calculation in the prime number domain, the accumulation calculation speed can be effectively improved, and then the privacy calculation speed such as the homomorphic encryption algorithm can be improved.

[0118] 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.

[0119] According to another embodiment of the present invention, there is also provided a computing device, comprising a memory and a processor, wherein the memory stores an executable code, and when the processor executes the executable code, Figure 2 or Figure 3 The method described.

[0120] 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.

[0121] 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 modulo operation, performed by a target processor; The target processor includes a general processing unit and an FMA hardware unit for executing a fused multiplication and addition instruction FMA for floating point numbers; the method includes: The general processing unit reads a target coefficient and a target value to be subjected to a modulo operation; the target coefficient is pre-calculated based on a modulus corresponding to the modulo operation and a constrained number of bits corresponding to the target value; The FMA hardware unit processes a floating point number determined based on the target value and the target coefficient to obtain a first product between the target value and the target coefficient; The FMA hardware unit processes the floating point numbers corresponding to the intermediate value and the modulus to obtain a second product between the intermediate value and the modulus; the intermediate value is obtained by right shifting the integer number corresponding to the first product by the number of constrained bits; The general processing unit subtracts an integer corresponding to the second product from the target value, thereby determining a target remainder using the obtained difference.

2. The method according to claim 1, wherein: Read the target value to be modulo-operated, including: An accumulated value is read from an accumulated register of the target processor as the target value; the accumulated value is obtained by sequentially adding a plurality of operands to the accumulated register.

3. The method according to claim 2, wherein: After determining the target remainder, the method further includes: The value of the accumulation register is updated to the target remainder.

4. The method according to claim 1, further comprising: The general processing unit divides the bit string corresponding to the target value into a high-m-bit bit string and a low-m-bit bit string, where m is the floating-point number precision supported by the FMA hardware unit; The FMA hardware unit processes a floating point number determined based on the target value and the target coefficient, including: Process two floating-point numbers to obtain corresponding sub-products for constructing the first product; one of the two floating-point numbers corresponds to a target bit string, and the other corresponds to the target coefficient, and the target bit string is any one of the high-m-bit bit string and the low-m-bit bit string.

5. The method according to claim 4, wherein: The constrained number of digits is greater than the floating point number precision.

6. The method according to claim 4, wherein: Process two floating point numbers and get the corresponding subproducts, including: Execute 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 addend; the first value is determined based on m; Execute 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 m; The precision bit parts are extracted from the first output and the second output as high-order and low-order bit strings respectively to construct the sub-product.

7. The method according to claim 1, wherein: The FMA hardware unit processes the floating point numbers corresponding to the intermediate value and the modulus to obtain a second product between the intermediate value and the modulus, including: Execute the FMA instruction based on a third parameter set to obtain a third output; wherein the third parameter set includes two floating point numbers as multipliers and a third value as addend; one of the two floating point numbers corresponds to the intermediate value, the other corresponds to the modulus, and the third value is determined based on m; 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 m; The precision bit parts are extracted from the third output and the fourth output as high-order and low-order bit strings respectively to construct the second product.

8. 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 greater than or equal to the modulus, a result of subtracting the modulus from the difference is determined as the target remainder.

9. 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 smaller than the modulus, the difference is determined as the target remainder.

10. A processor comprising a general processing unit and an FMA hardware unit for executing a fused multiplication and addition instruction FMA for floating point numbers; wherein: The general processing unit is used to read the target coefficient and the target value to be subjected to the modulo operation; the target coefficient is pre-calculated based on the modulus corresponding to the modulo operation and the number of constrained bits corresponding to the target value; The FMA hardware unit is used to process a floating point number determined based on the target value and the target coefficient to obtain a first product between the target value and the target coefficient; The FMA hardware unit is further used to process the floating point numbers corresponding to the intermediate value and the modulus, respectively, to obtain a second product between the intermediate value and the modulus; the intermediate value is obtained by right-shifting the integer number corresponding to the first product by the number of constrained bits; The general processing unit is further configured to subtract an integer corresponding to the second product from the target value, thereby determining a target remainder using the obtained difference.