SIMD instruction-oriented sm4 fine-grained slicing optimization method and system
By segmenting the plaintext blocks of the SM4 algorithm into basic operation units and optimizing the slice storage structure using SIMD instructions, fine-grained parallel processing of the SM4 algorithm is achieved. This solves the problems of high computational overhead and long startup time introduced by the data slicing algorithm, and improves encryption efficiency.
Patent Information
- Application Number
- CN202311126100.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-01
- Publication Date
- 2026-11-10
- Estimated Expiration
- 2043-09-01
AI Technical Summary
In the existing SM4 algorithm, the data slicing algorithm introduces a large computational overhead, resulting in a large parallel granularity and increasing the startup time of the cryptographic algorithm. This makes it difficult to perform encryption and decryption operations in a timely manner, especially when dealing with small amounts of short data.
The SM4 fine-grained slicing optimization method based on SIMD instructions is adopted. Plaintext groups are segmented into basic operation units. SIMD instructions are used to convert them into a slice storage structure with a slice window width greater than 1. When performing linear and nonlinear transformation iterations, bits at the same offset position are stored in the same register word for logical operations, thereby realizing parallel logical operations of different basic operation units.
It reduces memory usage and encryption algorithm startup time, improves the parallel processing capability of the SM4 algorithm, and reduces data conversion overhead.
Smart Images

Figure CN117272337B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of acceleration and optimization technology for cryptographic algorithm execution, specifically to a fine-grained SM4 slicing optimization method and system for SIMD instructions. Background Technology
[0002] Bit slicing is an effective technique for efficiently implementing block cipher algorithms at the software level. It involves storing multiple plaintext blocks of the same granularity in separate registers and then manipulating these registers to process the plaintext data in parallel. Algorithms using bit slicing decompose the encryption process into a series of bitwise operations using logic gates such as XOR, AND, OR, and NOT. This allows for N parallel encryption operations on a single N-bit processor, meaning N bitwise operations can be performed simultaneously.
[0003] When bit slicing is applied to block cipher algorithms, two additional data slicing processes are introduced before and after encryption or decryption: plaintext data slicing and ciphertext data slicing. Figure 1 As shown, the slicing performance of data slicing algorithms can affect the encryption and decryption performance of slice block cipher algorithms to a certain extent. In order to reduce the computational overhead introduced by data slicing algorithms, effective data slicing algorithms must be designed.
[0004] Data slicing algorithms are based on the order of data blocks. l and slice window width w Two parameters determine this. When in N Implemented on bit processors l Step w When using the bit-width data slicing algorithm, the following equation holds true: (1) In the above formula, n This represents the block length for the block cipher algorithm. In the SM4 algorithm, n is 128.
[0005] Therefore, when the processor platform is determined, the order *l* of the data slicing algorithm and the slice window width *w* are inversely proportional, and their product equals the target processor word length. When the slice window width is set in 1-bit units, each stored word after slicing contains 1 bit from different plaintext blocks, and encryption or decryption operations can be completed using only logical operations. Therefore, by setting the slice window width to 1 bit, a general-purpose slice block cipher algorithm can be designed. In the general data slicing algorithm, to satisfy the slicing of block cipher algorithms with different block lengths, the slice window width is set to 1. Since the design structure of the block cipher algorithm is not considered, there is a problem of large parallel granularity. Furthermore, the algorithm needs to continuously wait for the arrival of 128 plaintext blocks before slicing them and performing encryption / decryption operations, resulting in a large proportion of data conversion overhead and increasing the startup time of the cipher algorithm. Summary of the Invention
[0006] The technical problem to be solved by this invention is to provide a fine-grained slicing optimization method and system for SIMD instructions in response to the above-mentioned problems in the prior art. This invention aims to implement fine-grained parallel slicing for the SM4 algorithm to reduce memory usage and startup time during the execution of the SM4 algorithm.
[0007] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows: A fine-grained SM4 slicing optimization method for SIMD instructions includes: S101, will the length be n The plaintext is divided into segments of bits. n / M indivual M The basic operation unit is arranged in order. For the basic operation units that form a sequential storage structure after being arranged in order, the SIMD instruction is used to convert them into a slice storage structure with a slice window width greater than 1 as a slice plaintext group using a specified slicing method. S102, perform iterative linear and nonlinear transformations of the SM4 algorithm on the slice plaintext block. During the iteration of linear and nonlinear transformations, bits at the same offset position in different basic operation units that need to perform the same operation logic are stored in the same register word for logical operation to achieve parallel logical operation of different basic operation units. Finally, after 32 iterations, a 128-bit slice ciphertext block is obtained. S103, slice the ciphertext group using the specified slicing method to obtain the ciphertext group.
[0008] Optionally, in step S101, when using a specified slicing method and SIMD instructions to convert the data into a slice storage structure with a slice window width greater than 1 as a slice plaintext group, the specified slice window width is less than or equal to the number of basic operation units. n / M Furthermore, the SM4 algorithm has a plaintext block length of 128 bits (n), a basic operation unit bit length of 32 bits, and a maximum slice window width of 4 bits, enabling parallel processing of the SM4 algorithm. N / 4 A basic operating unit, of which N This refers to the processor's data bit width.
[0009] Optionally, step S101, which uses a specified slicing method and SIMD instructions to convert the plaintext blocks into slice storage structures with a slice window width greater than 1, includes: firstly, using SIMD lookup instructions, rearranging the 16 bytes of the basic operation units of the 32 plaintext blocks in the sequential storage structure to achieve parallel logical operations of different basic operation units, and mapping the rearranged positions of the 16 bytes as follows: [F,B,7,3,E,A,6,2,D,9,5,1,C,8,4,0], In this positional mapping, each index position is represented by a hexadecimal character, indicating the index position before rearrangement. The byte at index position 0 has an index position of 15 before rearrangement, the byte at index position 1 has an index position of 11, and so on, with the byte at index position 15 having an index position of 0 before rearrangement. Then, using SIMD logical shift operations, SIMD logical XOR operations, and SIMD logical AND operations, combined with five sets of masks MASK0 to MASK4, SIMD logical operation instructions are used to perform pairwise bit slicing of the 32 basic operation units. The hexadecimal representation of the five sets of masks MASK0 to MASK4 is as follows: MASK0=5555555555555555555555555555555555555555555555555555555555555, MASK1=33333333333333333333333333333333333333333333333333333, MASK2=0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F, MASK3=00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF, MASK4=0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF; Among them, the pairwise bit slicing of 32 basic operation units using SIMD logic operation instructions, combined with 5 sets of masks MASK0 to MASK4, includes: S201, initialize the position mapping to the index array index, initialize the 5 sets of masks MASK0~MASK4 to the mask m0~m4, and set the index position of each plaintext block after rearrangement in the 32 basic operation units in the sequential storage structure; initialize the loop variable j to 0; S202, using SIMD logic operation instructions, performs a left shift operation SHL on the loop variable j in parallel and then assigns the result to the variable. k ; S203, initialize loop variable i to 0; S204, based on variables k And the value of the loop variable i, through Calculate the value of variable r, where SHL is the left shift operation of the SIMD logic instruction. Indicates calculation; S205, through execution Calculate the value of the temporary variable temp, where USHR is the unsigned right shift operation of the SIMD logical operation instruction. This is the (r+k)th basic operation unit; S206, through execution Update the value of the temporary variable temp, where XOR is the exclusive OR operation of the SIMD logical operation instruction. This is the r-th basic operation unit; S207, through execution Update the value of the temporary variable temp, where AND is the AND operation of the SIMD logical operation instruction. Let j be the j-th mask; S208, through execution Update the r-th basic operation unit ; S209, through execution Update the value of the temporary variable temp; S210, by executing Update the (r+k)th basic operation unit The value; S211, increment the loop variable i by 1. If the loop variable i is less than 16, then jump to step S204; otherwise, jump to step S212. S212, increment the loop variable j by 1. If the loop variable j is less than 5, jump to step S202; otherwise, determine that the pairwise bit slice of the 32 basic operation units has been completed.
[0010] Optionally, when converting the ciphertext into a slice storage structure with a slice window width greater than 1 using a specified slicing method in step S101, the method further includes using SIMD instructions to represent the subkey generated by the key expansion algorithm slicing as a slice subkey for decrypting ciphertext blocks: first, the 32-bit subkey is expanded into a 128-bit expanded subkey using SIMD expansion instructions. ek Next, the amplification key ek The bytes in the instruction are rearranged using SIMD lookup table instructions, and then the amplified key is... ek Perform 8 iterations of the loop, and in any given iteration... i In the next iteration, the amplified key is first generated using SIMD instructions. ek Unsigned left shift using SIMD instructions i Then, proceed with the unsigned left shift. i The result after bit shifting is applied using a signed right shift instruction of the SIMD instruction, and the result is stored in a sliced byte array. K The i element K [ i In the process, after completing 8 loop operations, the result is obtained from the slice byte array. K The subkey of the slice after the slicing is completed.
[0011] Optionally, the functional expression for the linear transformation in step S102 is:
[0012] In the above formula, The result of a linear transformation. It is a block matrix. The output is a nonlinear transformation. For logical XOR, For circular left shift, , and Let be an intermediate variable, and we have:
[0013] , , , , , in, ~ For nonlinear transformation output components, and for any i-th element It is 128 bits in size.
[0014] Optionally, the nonlinear transformation in step S102 consists of four parallel S-boxes, and the functional expression of the S-boxes is: , In the above formula, It is a nonlinear transformation function. and For a fixed parameter vector, For a finite field The inverse operation on, This is intermediate iteration data for the SM4 algorithm, and the computation of the S-box will be performed over a finite field. The inverse operation on the top is transformed into the tower domain. This is done to reduce the complexity of the inversion operation.
[0015] Optionally, the finite field The inverse operation on the top is transformed into the tower domain. include: S301, using isomorphic mapping matrix , finite field The elements in the finite field are represented as finite fields. Linear polynomials over; using isomorphic mapping matrices finite field The elements on are represented as finite fields Linear polynomials over; using isomorphic mapping matrices finite field The elements on are represented as finite fields Linear polynomials over; S302, the inversion operation of the S-box is optimized using the tower domain structure shown in the following formula, thereby recursively transforming the inversion operation in the S-box into a finite field. Multiplication and addition operations: , In the above formula, , , There are three isomorphic mapping matrices. and These are the function expressions corresponding to the finite field transformations. , and Let the independent variable be a polynomial basis. and For a finite field The parameters on, and For a finite field The parameters on, , and is the independent variable under a normal basis.
[0016] Optionally, the slicing method used in step S103 is the same as the slicing method used in step S101.
[0017] Furthermore, the present invention also provides an SM4 fine-grained slicing optimization system for SIMD instructions, including a microprocessor and a memory interconnected thereto, wherein the microprocessor is programmed or configured to execute the SM4 fine-grained slicing optimization method for SIMD instructions.
[0018] Furthermore, the present invention also provides a computer-readable storage medium storing a computer program for being programmed or configured by a microprocessor to execute the SM4 fine-grained slicing optimization method for SIMD instructions.
[0019] Compared with existing technologies, this invention has the following advantages: In traditional data slicing algorithms, when using 128-bit SIMD instructions, the slice window width is set to 1, requiring up to 128 plaintext packets to be sliced. This not only occupies up to 1KB of memory, but also means the SM4 algorithm needs to continuously wait for the arrival of these 128 plaintext packets before performing encryption and decryption operations. When faced with small-scale, short data, these 128 plaintext packets cannot be replenished in time, causing the SM4 algorithm to enter a continuous waiting period and unable to encrypt and decrypt existing plaintext packets in a timely manner, resulting in a long startup time for the encryption algorithm. In contrast, this invention's fine-grained SM4 slicing optimization method for SIMD instructions, based on the structural characteristics of the SM4 algorithm, includes slicing the plaintext packets with a length of... n The plaintext is divided into segments of bits. n / M indivual MThe basic operation unit is arranged sequentially. For the basic operation units that form a sequential storage structure after being arranged sequentially, a specified slicing method is used to convert them into a slice storage structure with a slice window width greater than 1 using SIMD instructions. This slice is then used as a plaintext block. The SM4 algorithm is used to iterate through linear and nonlinear transformations on the plaintext blocks. During the iteration of linear and nonlinear transformations, bits at the same offset position in different basic operation units that require the same operation logic are stored in the same register word for logical operation, so as to realize parallel logical operation of different basic operation units. After 32 iterations, a 128-bit slice ciphertext block is obtained. The slice ciphertext block is then sliced using the specified slicing method to obtain the ciphertext block. This not only reduces memory usage but also greatly reduces the startup time of the encryption algorithm. Attached Figure Description
[0020] Figure 1 This is a schematic diagram illustrating the basic principle of the method in an embodiment of the present invention.
[0021] Figure 2 This is a sequential storage structure for 32 plaintext packets on a 128-bit processor in an embodiment of the present invention.
[0022] Figure 3 This is the preprocessing method for 32 plaintext packets in this embodiment of the invention.
[0023] Figure 4 This is the slice storage structure corresponding to the subkey in this embodiment of the invention.
[0024] Figure 5 This is a schematic diagram illustrating the operational principle of SIMD instructions in an embodiment of the present invention.
[0025] Figure 6 This is a schematic diagram illustrating the operational principle of the table lookup instruction TBL in an embodiment of the present invention. Detailed Implementation
[0026] like Figure 1 As shown, the SM4 fine-grained slicing optimization method for SIMD instructions in this embodiment includes: S101, will the length be n The plaintext is divided into segments of bits. n / M indivual M The basic operation unit is arranged in order. For the basic operation units that form a sequential storage structure after being arranged in order, the SIMD instruction is used to convert them into a slice storage structure with a slice window width greater than 1 as a slice plaintext group using a specified slicing method. S102, perform iterative linear and nonlinear transformations of the SM4 algorithm on the slice plaintext block. During the iteration of linear and nonlinear transformations, bits at the same offset position in different basic operation units that need to perform the same operation logic are stored in the same register word for logical operation to achieve parallel logical operation of different basic operation units. Finally, after 32 iterations, a 128-bit slice ciphertext block is obtained. S103, slice the ciphertext group using the specified slicing method to obtain the ciphertext group.
[0027] Bit slicing is an effective technique for efficiently implementing block cipher algorithms at the software level. It involves storing data slices of the same granularity into multiple plaintext blocks and processing them in parallel by manipulating these registers. Algorithms using bit slicing decompose the encryption process into a series of bitwise operations using logic gates such as XOR, AND, OR, and NOT. This allows for N parallel encryption operations on a single N-bit processor, meaning N bitwise operations can be performed simultaneously. In this embodiment, the SM4 fine-grained slicing optimization method for SIMD instructions introduces two additional data slicing processes before and after encryption / decryption: slicing plaintext blocks and slicing ciphertext blocks. The slicing performance of the data slicing algorithm can significantly impact the encryption and decryption performance of the slice block cipher algorithm. To reduce the computational overhead introduced by the data slicing algorithm, an efficient data slicing algorithm must be designed.
[0028] In general data slicing algorithms, to accommodate block ciphers with different block lengths, the slice window width is set to 1. However, since the design structure of the block cipher algorithm is not considered, this results in a large parallel granularity, leading to a significant data conversion overhead and increased startup time. The key to designing fine-grained parallel slice block cipher algorithms is to select an appropriate slice window width based on the structural characteristics of the specific block cipher algorithm. In step S101 of this embodiment, when using a specified slicing method and SIMD instructions to convert the data into a slice storage structure with a slice window width greater than 1 as sliced plaintext blocks, the specified slice window width is less than or equal to the number of basic operation units. n / M Furthermore, the SM4 algorithm has a plaintext block length of 128 bits (n), a basic operation unit bit length of 32 bits, and a maximum slice window width of 4 bits, enabling parallel processing of the SM4 algorithm. N / 4 A basic operating unit, of which N The processor data bit width is set to 4 bits. Setting the slice window width to 4 bits means that only 32 plaintext blocks need to be sliced on a 128-bit processor, which not only reduces memory usage but also significantly reduces the startup time of the encryption algorithm.
[0029] In this embodiment, during the encryption and decryption process of the block cipher algorithm, the block length is first divided into blocks of length 1. n The plaintext input of bits is decomposed into n / M indivual M The basic operation unit is a bit-based unit, and then iterative processing is performed on this basic operation unit. During the iteration process of the cryptographic algorithm, bits located at the same offset within different basic operation units perform the same operational logic. This means that bits performing the same operational logic can be stored together in the same register word, and when performing logical operations on the register word, the same logical operation can be performed on all bits simultaneously. Therefore, the number of basic operation units determines the size of the slice window. When the slice window width is greater than the number of basic operation units, the sliced data block processed by the data slicing algorithm must contain bits from different basic operation units located at different offsets. Since bits located at different offsets in different basic operation units usually perform different operational logic, block cipher algorithms cannot be represented as slice block cipher algorithms. Therefore, the slice window width must be less than or equal to the number of basic operation units. In the SM4 cryptographic algorithm, the 128-bit plaintext input is first divided into four 32-bit basic operation units, and then nonlinear and linear transformations are performed on the 32-bit basic operation units. After 32 iterations, the 128-bit ciphertext is output. Therefore, in the design of the slice SM4 block cipher algorithm, the maximum slice window width is 4. Due to the order of the slice block cipher algorithm l With processor data bit width N The relationship between them is l=N / 4 This means that the minimum data block size that the slice SM4 cryptographic algorithm can process in parallel is [missing value]. N / 4 128 plaintext packets. When implementing the slicing SM4 algorithm with minimal parallel granularity using a SIMD instruction set with a data width of 128 bits, the slicing SM4 algorithm can process 128 / 4 = 32 plaintext packets in parallel. The sequential storage structure of these 32 plaintext packets in memory is as follows: Figure 2 As shown. Figure 2 middle, Indicates the first i The first plaintext group j Units. To represent these 32 plaintext blocks as a suitable slice structure, we divide each plaintext block into 4 segments, using 32-bit basic operation units as the unit, and then arrange each segment of all 32 plaintext blocks sequentially, as follows: Figure 2 As shown, the next step is to use the specified slicing method and SIMD instructions to convert it into a slice storage structure with a slice window width greater than 1 as a slice plaintext group.
[0030] In step S101 of this embodiment, the specified slicing method is used to convert the plaintext packets into a slice storage structure with a slice window width greater than 1 using SIMD instructions. This includes: firstly, using SIMD lookup instructions, rearranging the 16 bytes of the basic operation units of the 32 plaintext packets in the sequential storage structure to achieve parallel logical operations of different basic operation units, and the position mapping of the rearranged 16 bytes is as follows: [F,B,7,3,E,A,6,2,D,9,5,1,C,8,4,0], In this positional mapping, each index position is represented by a hexadecimal character, indicating the index position before rearrangement. The byte at index position 0 has an index position of 15 before rearrangement, the byte at index position 1 has an index position of 11, and so on, with the byte at index position 15 having an index position of 0 before rearrangement. Then, using SIMD logical shift operations, SIMD logical XOR operations, and SIMD logical AND operations, combined with five sets of masks MASK0 to MASK4, SIMD logical operation instructions are used to perform pairwise bit slicing of the 32 basic operation units. The hexadecimal representation of the five sets of masks MASK0 to MASK4 is as follows: MASK0=5555555555555555555555555555555555555555555555555555555555555, MASK1=33333333333333333333333333333333333333333333333333333, MASK2=0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F, MASK3=00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF, MASK4=0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF; Among them, the pairwise bit slicing of 32 basic operation units using SIMD logic operation instructions, combined with 5 sets of masks MASK0 to MASK4, includes: S201, initialize the position mapping to the index array index, initialize the 5 sets of masks MASK0~MASK4 to the mask m0~m4, and set the index position of each plaintext group after rearrangement in the 32 basic operation units in the sequential storage structure; initialize the loop variable j to 0; S202, using SIMD logic operation instructions, performs a left shift operation SHL on the loop variable j in parallel and then assigns the result to the variable. k ; S203, initialize loop variable i to 0; S204, based on variables k And the value of the loop variable i, through Calculate the value of variable r, where SHL is the left shift operation of the SIMD logic instruction. Indicates calculation; S205, through execution Calculate the value of the temporary variable temp, where USHR is the unsigned right shift operation of the SIMD logical operation instruction. This is the (r+k)th basic operation unit; S206, through execution Update the value of the temporary variable temp, where XOR is the exclusive OR operation of the SIMD logical operation instruction. This is the r-th basic operation unit; S207, through execution Update the value of the temporary variable temp, where AND is the AND operation of the SIMD logical operation instruction. Let j be the j-th mask; S208, through execution Update the r-th basic operation unit ; S209, through execution Update the value of the temporary variable temp; S210, by executing Update the (r+k)th basic operation unit The value; S211, increment the loop variable i by 1. If the loop variable i is less than 16, then jump to step S204; otherwise, jump to step S212. S212, increment the loop variable j by 1. If the loop variable j is less than 5, jump to step S202; otherwise, determine that the pairwise bit slice of the 32 basic operation units has been completed.
[0031] Figure 3This describes how to convert between sequential storage structures and slice storage structures with a 4-bit slice window width for 32-order data blocks. Unlike general data slicing algorithms, the slicing method specified in this embodiment first uses SIMD lookup instructions to rearrange the bytes of each plaintext block before actually starting slicing. This step is equivalent to sequentially arranging each segment of all 32 plaintext blocks. Then, the slicing method specified in this embodiment slices the byte-rearranged array. After slicing is complete, the slice array... A [0:8], A [8:16] A [16:24] A [24:32] These correspond to sliced plaintext blocks X0, X1, X2, and X3, respectively. X0 represents the slice storage structure of the first 32-bit basic operation unit of all 32 plaintext blocks, X1 represents the slice storage structure of the second 32-bit basic operation unit of all 32 plaintext blocks, and so on. In each call to the slice SM4 algorithm, the slicing method specified in this embodiment needs to be called twice: once before formal encryption, corresponding to the plaintext data slicing step in the slice block cipher algorithm model, and once after encryption, corresponding to the ciphertext data slicing step in the slice block cipher algorithm model. If the length of the plaintext message to be encrypted is greater than 32 plaintext blocks, the slicing method specified in this embodiment can be modified to slice both the previous 32-level ciphertext block and the next 32-level plaintext block simultaneously, further reducing the overhead of slicing operations in the encryption algorithm.
[0032] In step S101 of this embodiment, when converting the slicing into a slice storage structure with a slice window width greater than 1 using a specified slicing method, it further includes using SIMD instructions to represent the subkey generated by the key expansion algorithm slicing as a slice subkey for decrypting ciphertext blocks: First, the 32-bit subkey is expanded into a 128-bit expanded subkey using SIMD expansion instructions (DUP instructions in the ARM instruction set, which may vary in different instruction sets). ek Next, the amplification key ek The bytes in the instruction are rearranged using SIMD lookup table instructions, and then the amplified key is... ek Perform 8 iterations of the loop, and in any given iteration... i In the next iteration, the amplified key is first generated using SIMD instructions. ek Unsigned left shift using SIMD instructions i The bits (SHL in the ARM instruction set, which may vary in different instruction sets) are then followed by an unsigned left shift. iThe result after bit shifting is applied using a signed right shift instruction from the SIMD instruction set (SSHR in the ARM instruction set, but may vary in different instruction sets), and the result is stored in a sliced byte array. K The i element K [ i In the process, after completing 8 loop operations, the result is obtained from the slice byte array. K The subkey of the slice after slicing is completed. In the SM4 slicing algorithm, since the plaintext block is converted from a sequential storage structure to a slice storage structure, the subkey generated by the key expansion algorithm also needs to be represented as key slice data in order to XOR it with the plaintext slice data. Its slice structure is as follows: Figure 4 As shown, where rk i,j Indicates the first i The first wheel key j Ones.
[0033] In the sliced representation of plaintext and round keys, the nonlinear and linear transformations in the SM4 algorithm's round functions need to be converted into a series of logical operations, which are then implemented using logical instructions from the processor's instruction set. This section describes how to represent linear transformations as logical operations with sliced data as input. Linear Transformation L It consists of a circular left shift and an XOR operation. The functional expression for the linear transformation in step S102 of this embodiment is:
[0034] In the above formula, The result of a linear transformation. It is a block matrix. The output is a nonlinear transformation. For logical XOR, For circular left shift, , and As intermediate variables, the above calculations consist of matrix multiplication, logical XOR, and logical shifting, where matrix multiplication is defined in a finite field. GF The addition operation on (2) is equivalent to the logical XOR operation. Therefore, the logical XOR instruction can be used to generate a linear transformation function with a slice structure as input. Among them are:
[0035] , , , , , in, ~ For nonlinear transformation output components, and for any i-th element The size is 128 bits. Linear transformation. L Receive nonlinear transformation The output is used as input. Nonlinear transformation. The algorithm consists of four parallel S-boxes. In the slice SM4 algorithm, it's no longer feasible to implement the S-boxes using lookup tables. Therefore, the S-boxes need to be equivalently converted into Boolean functions. One approach is to construct eight truth tables with 8-bit inputs and 1-bit outputs using the S-boxes, and then synthesize eight Boolean functions from these eight truth tables. The output value of each Boolean function represents a 1-bit output bit of the S-box lookup table. However, since the Boolean functions have as many as eight input variables, the Boolean functions synthesized from the truth tables have high complexity and require many gate functions, making them unsuitable for a high-performance implementation of the slice SM4 algorithm.
[0036] Another approach is to start from the S-box algebraic expression and decompose it into basic logical operations. In this embodiment, the nonlinear transformation in step S102 consists of four parallel S-boxes, and the function expression of the S-box is: , In the above formula, It is a nonlinear transformation function. and For a fixed parameter vector, For a finite field The inverse operation on, This is intermediate iteration data for the SM4 algorithm, and the computation of the S-box will be performed over a finite field. The inverse operation on the top is transformed into the tower domain. This is done to reduce the complexity of the inversion operation. According to the above formula, the computational complexity of the S-box is mainly determined by the finite field... The inverse operation is determined by the fact that for a finite field, one approach to finding the inverse of an element is to use the extended Euclidean algorithm. However, for larger finite fields, such as... Finding the inverse function is computationally complex and slow. However, by utilizing knowledge of finite field theory, we can... The inverse operation on the top is transformed into the tower domain. This completes the process, thus greatly reducing the complexity of the inversion operation.
[0037] This embodiment will use a finite field The inverse operation on the top is transformed into the tower domain. include: S301, using isomorphic mapping matrix , finite field The elements in the finite field are represented as finite fields. Linear polynomials over; using isomorphic mapping matrices finite field The elements on are represented as finite fields Linear polynomials over; using isomorphic mapping matrices finite field The elements on are represented as finite fields Linear polynomials over; S302, the inversion operation of the S-box is optimized using the tower domain structure shown in the following formula, thereby recursively transforming the inversion operation in the S-box into a finite field. Multiplication and addition operations: , In the above formula, , , There are three isomorphic mapping matrices. and These are the function expressions corresponding to the finite field transformations. , and Let the independent variable be a polynomial basis. and For a finite field The parameters on, and For a finite field The parameters on, , and is the independent variable under a normal basis.
[0038] Through a series of finite field transformations, the inversion operation in the S-box can be recursively transformed into a finite field operation. Multiplication and addition operations on finite fields. Multiplication and addition operations on the S-box are equivalent to logical AND and logical XOR operations. Therefore, we can use the logical AND and logical XOR instructions in the processor instruction set to generate the inverse function of the S-box with bit slice structure as input.
[0039] The slicing method used in step S103 of this embodiment is the same as the slicing method used in step S101, so its implementation will not be described in detail here.
[0040] SIMD instructions are hardware instructions that perform the same operation on multiple data operands simultaneously. Typically, a SIMD instruction takes two vectors as input (each vector has a set of operands), performs the same operation on the two sets of operands (one operand for each vector), and outputs a vector with the result. Figure 5The diagram illustrates the process of performing the op1 operation on vectors A and B to obtain vector C, expressed as: C = Aop1B, where op1 represents any operation. Operations in a SIMD unit typically include basic arithmetic operations (such as addition, subtraction, multiplication, etc.), logical operations (such as logical AND, logical OR, logical XOR, etc.), and other operations, such as absolute value (abs) and square root (sqrt). Table 1 lists some of the SIMD instructions used in this embodiment and provides instruction descriptions.
[0041] Table 1: SIMD directives and their descriptions.
[0042]
[0043] The SIMD pseudo-instructions involved in this embodiment are mainly divided into two categories: (1) SIMD logical operation instructions: SIMD logical operation instructions include left shift instruction SHL, unsigned right shift instruction USHR, signed right shift instruction SSHR, and logical AND and logical XOR instructions. These logical operation instructions can simultaneously perform the same logical operation on 16 channels of 8-bit data, 8 channels of 16-bit data, 4 channels of 32-bit data, and 2 channels of 64-bit data, thereby accelerating the data processing process. (2) SIMD table lookup instructions: namely, table lookup instruction TBL, which can realize parallel table lookup of 16 channels of 8-bit data. It includes three operands, operand 1 to operand 3. Operand 1 represents the result vector, operand 2 stores the vector table, and operand 3 represents the index vector. Figure 6 The table lookup instruction TBL, where d1:d0 represents operand 1, d2 represents operand 1, d3 represents operand 3, and zero indicates that if operand 1 is insufficient, zeros are padded. The command format of TBL can be represented as: TBL d2, {d0, d1}, d3 The lookup instruction TBL first uses each byte in the index vector (operand 3) to index the corresponding byte value in the vector table (operand 2), and then inserts the index value into the result vector (operand 1) at the position corresponding to the original index position in the index vector (operand 3).
[0044] To theoretically evaluate the efficiency of the SM4 fine-grained slicing optimization method for SIMD instructions in this embodiment, a detailed comparison of the computational complexity of the SM4 fine-grained slicing optimization method for SIMD instructions in this embodiment and the general slicing method for comparison will be conducted below. In this embodiment, LOAD and STORE are used to represent data loading and storage operations, respectively; SHL represents left shift operation; SHR represents right shift operation; XOR represents XOR operation; AND represents AND operation; NOT represents NOT operation; ADD represents addition operation; SUB represents subtraction operation; MUL represents multiplication operation; and MOD represents modulo operation. In the general slicing method for comparison, the processor instruction word length is 128 bits, which is the same as the block length of the SM4 algorithm. In this case, the required data block size is 128 × 128 bits, that is, a total of 128 plaintext blocks need to be sliced. As shown in Algorithm 4.1, the general slicing method can eliminate the memory circular copy process and can directly perform standard data slicing operations on the input data block (Algorithm 4.2). Therefore, the computational complexity of the general slicing method is as follows: (1) Loading predefined constant data from memory into NEON registers m0, m1, ..., m7 requires a total of 8 LOAD operations; (2) Next, the adjacent 1 bits, 2 bits, 4 bits, 8 bits, 16 bits, 32 bits, and 64 bits of the 128 plaintext blocks are swapped in pairs to complete the data slicing process. In each swap, 2 LOAD operations, 2 STORE operations, 1 SHL left shift operation, and 1 SUB subtraction operation are required. Then, the 128 plaintext blocks are paired in pairs, and the data of each pair is swapped. In the pairwise data swap, a total of 7 LOAD operations, 4 STORE operations, 2 XOR operations, 6 AND operations, 2 NOT operations, 1 SHL left shift operation, 1 SHR right shift operation, 2 ADD addition operations, 1 SUB subtraction operation, and 1 MUL multiplication operation are required. Since the MUL operation is multiplied by 2, MUL can be converted into a single left shift SHL. (3) In the process of general data slicing, a total of 8+(2+7×64)×7=3158 LOAD operations, (2+4×64)×7=1806 STORE operations, (2×64)×7=896 XOR operations, (6×64)×7=2688 AND operations, (2×64)×7=896 NOT operations, (1+(1+1)×64)×7=903 SHL operations (MUL operations are equivalently converted to SHL operations), (1×64)×7=448 SHR operations, (2×64)×7=896 ADD operations, and (1+1×64)×7=455 SUB operations are required.In this embodiment of the SM4 fine-grained slicing optimization method for SIMD instructions, although the processor instruction word length is 128 bits, the required data block size is only 32×128 bits, that is, only 32 plaintext blocks need to be sliced. The fine-grained slicing method directly performs data slicing operations on the input data block. Therefore, the computational complexity of the fine-grained slicing method is: (1) Loading predefined constant data from memory into NEON registers m0, m1, ..., m4 requires a total of 5 LOAD operations; (2) Next, the adjacent 1 bits, 2 bits, 4 bits, 8 bits, and 16 bits of the 32 plaintext blocks are swapped in pairs to complete the data slicing process. In each swap, one LOAD operation, one STORE operation, and one SHL left shift operation are required. Then, the 32 plaintext blocks are paired in pairs, and the data swap of each pair is completed. During pairwise data exchange, a total of 5 LOAD operations, 7 STORE operations, 3 XOR operations, 1 AND operation, 2 SHL left shift operations, 1 SHR right shift operation, 2 ADD addition operations, 1 SUB subtraction operation, and 1 MOD modulo operation are required. Note that since the modulo operation is a power of 2, it can be equivalently converted to the AND operation. (3) Therefore, in the process of fine-grained data slicing, a total of 5 + (1 + 5 × 16) × 5 = 410 LOAD operations, (1 + 7 × 16) × 5 = 565 STORE operations, (3 × 16) × 5 = 240 XOR operations, ((1 + 1) × 16) × 5 = 80 AND operations (MOD operations are equivalently converted to AND operations), (1 + 2 × 16) × 5 = 165 SHL operations, (1 × 16) × 5 = 80 SHR operations, (2 × 16) × 5 = 160 ADD operations, and (1 × 16) × 5 = 80 SUB operations are required. Table 2 lists the comparison of the computational load in the data slicing process between the SM4 fine-grained slicing optimization method (fine-grained) and the general slicing method (general) for SIMD instructions in this embodiment.
[0045] Table 2: Comparison of computational load during data slicing.
[0046]
[0047] As shown in Table 2, from the perspective of the total number of operations, fine-grained data slicing reduces the computational cost by 12146 compared to the general slicing method. (1780) / 1780 = 582.4%. However, in reality, different types of instructions have different instruction cycles when the CPU executes these instructions. Since integer and logical operations are relatively simple, most CPUs can complete these operations in just one clock cycle. However, when executing the LOAD instruction and the STORE instruction, the CPU needs multiple clock cycles to complete the operation. Assuming that the CPU needs one clock cycle to execute arithmetic and logical instructions, the reduction in computational load of the fine-grained slicing method compared to the general slicing method varies depending on the clock cycles required to execute the LOAD and STORE instructions, as shown in Table 3.
[0048] Table 3: Comparison of reductions in computational load.
[0049]
[0050] As shown in Table 3, the computational load gradually decreases as the number of clock cycles required to perform LOAD and STORE operations increases. This indicates that LOAD and STORE operations are not the main factors affecting the computational load of the slicing method; arithmetic and logical operations dominate the computational load. Compared to the general slicing method, the SM4 fine-grained slicing optimization method for SIMD instructions in this embodiment reduces the computational load of arithmetic and logical operations by 7182. 805) / 805=792.2%, which shows that the slicing performance of the SM4 fine-grained slicing optimization method for SIMD instructions in this embodiment is significant and has great advantages.
[0051] Furthermore, this embodiment also provides an SM4 fine-grained slicing optimization system for SIMD instructions, including a microprocessor and a memory interconnected, wherein the microprocessor is programmed or configured to execute the SM4 fine-grained slicing optimization method for SIMD instructions. This embodiment also provides a computer-readable storage medium storing a computer program for being programmed or configured by a microprocessor to execute the SM4 fine-grained slicing optimization method for SIMD instructions.
[0052] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-readable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create a machine for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The functions specified in one or more boxes. These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable apparatus for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0053] The above description is merely a preferred embodiment of the present invention. The scope of protection of the present invention is not limited to the above embodiments. All technical solutions falling within the scope of the present invention's concept are within the scope of protection of the present invention. It should be noted that for those skilled in the art, any improvements and modifications made without departing from the principles of the present invention should also be considered within the scope of protection of the present invention.
Claims
1. A fine-grained SM4 slicing optimization method for SIMD instructions, characterized in that, include: S101, will the length be n The plaintext is divided into segments of bits. n / M indivual M The basic operation units are arranged sequentially. For each basic operation unit that forms a sequential storage structure, a specified slicing method is used with SIMD instructions to convert it into a sliced storage structure with a slice window width greater than 1, which is then used as a slice plaintext group. The slice window width is less than or equal to the number of basic operation units. n / M ; S102, perform iterative linear and nonlinear transformations of the SM4 algorithm on the slice plaintext block. During the iteration of linear and nonlinear transformations, bits at the same offset position in different basic operation units that need to perform the same operation logic are stored in the same register word for logical operation to achieve parallel logical operation of different basic operation units. Finally, after 32 iterations, a 128-bit slice ciphertext block is obtained. S103, slice the sliced ciphertext group using the specified slicing method to obtain the ciphertext group; Step S101, which uses a specified slicing method and SIMD instructions to convert the plaintext blocks into slice storage structures with a slice window width greater than 1, includes: First, using SIMD lookup instructions, rearranging the 16 bytes of the basic operation units of the 32 plaintext blocks in the sequential storage structure to achieve parallel logical operations of different basic operation units, and mapping the rearranged positions of the 16 bytes as follows: [F,B,7,3,E,A,6,2,D,9,5,1,C,8,4,0], In this positional mapping, each index position is represented by a hexadecimal character, indicating the index position before rearrangement. The byte at index position 0 has an index position of 15 before rearrangement, the byte at index position 1 has an index position of 11, and so on, with the byte at index position 15 having an index position of 0 before rearrangement. Then, using SIMD logical shift operations, SIMD logical XOR operations, and SIMD logical AND operations, combined with five sets of masks MASK0 to MASK4, SIMD logical operation instructions are used to perform pairwise bit slicing of the 32 basic operation units. The hexadecimal representation of the five sets of masks MASK0 to MASK4 is as follows: MASK0=5555555555555555555555555555555555555555555555555555555555555, MASK1=33333333333333333333333333333333333333333333333333333, MASK2=0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F, MASK3=00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF, MASK4 = 0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF; Among them, combining the 5 sets of masks MASK0 to MASK4, the pairwise bit slicing of 32 basic operation units using SIMD logic operation instructions includes: S201, initialize the position mapping to the index array index, initialize the 5 sets of masks MASK0~MASK4 to the mask m0~m4, and set the index position of each plaintext block after rearrangement in the 32 basic operation units in the sequential storage structure; initialize the loop variable j to 0; S202, using SIMD logic operation instructions, performs a left shift operation SHL on the loop variable j in parallel and then assigns the result to the variable. k ; S203, initialize loop variable i to 0; S204, based on variables k And the value of the loop variable i, through Calculate the value of variable r, where SHL is the left shift operation of the SIMD logic instruction. Indicates calculation; S205, through execution Calculate the value of the temporary variable temp, where USHR is the unsigned right shift operation of the SIMD logical operation instruction. This is the (r+k)th basic operation unit; S206, through execution Update the value of the temporary variable temp, where XOR is the exclusive OR operation of the SIMD logical operation instruction. This is the r-th basic operation unit; S207, through execution Update the value of the temporary variable temp, where AND is the AND operation of the SIMD logical operation instruction. Let j be the j-th mask; S208, through execution Update the r-th basic operation unit ; S209, through execution Update the value of the temporary variable temp; S210, by executing Update the (r+k)th basic operation unit The value; S211, increment the loop variable i by 1. If the loop variable i is less than 16, then jump to step S204; otherwise, jump to step S212. S212, increment the loop variable j by 1. If the loop variable j is less than 5, jump to step S202; otherwise, determine that the pairwise bit slice of the 32 basic operation units has been completed.
2. The SM4 fine-grained slicing optimization method for SIMD instructions according to claim 1, characterized in that, In step S101, when the specified slicing method is used to convert the plaintext into a slice storage structure with a slice window width greater than 1 using SIMD instructions, the length n of the plaintext packet processed by the SM4 algorithm is 128 bits, and the number of bits of the basic operation unit is... M With a 32-bit architecture and a maximum slice window width of 4, the SM4 algorithm can be processed in parallel. N / 4 A basic operating unit, of which N This refers to the processor's data bit width.
3. The SM4 fine-grained slicing optimization method for SIMD instructions according to claim 1, characterized in that, In step S101, when converting the ciphertext into a slice storage structure with a slice window width greater than 1 using a specified slicing method, the method further includes using SIMD instructions to represent the subkey generated by the key expansion algorithm slicing as a slice subkey for decrypting ciphertext blocks: first, the 32-bit subkey is expanded into a 128-bit amplified subkey using SIMD expansion instructions. ek Next, the amplification key ek The bytes in the instruction are rearranged using SIMD lookup table instructions, and then the amplified key is... ek Perform 8 iterations of the loop, and in any given iteration... i In the next iteration, the amplified key is first generated using SIMD instructions. ek Unsigned left shift using SIMD instructions i Then, the unsigned left shift is performed. i The result after bit shifting is applied using a signed right shift instruction of the SIMD instruction, and the result is stored in a sliced byte array. K The i element K [ i In the process, after completing 8 loop operations, the result is obtained from the slice byte array. K The subkey of the slice after the slicing is completed.
4. The SM4 fine-grained slicing optimization method for SIMD instructions according to claim 1, characterized in that, The functional expression for the linear transformation in step S102 is: In the above formula, The result of a linear transformation. It is a block matrix. The output is a nonlinear transformation. For logical XOR, For circular left shift, , and Let be an intermediate variable, and we have: , , , , , in, ~ For nonlinear transformation output components, and for any i-th element It is 128 bits in size.
5. The SM4 fine-grained slicing optimization method for SIMD instructions according to claim 1, characterized in that, In step S102, the nonlinear transformation consists of four parallel S-boxes, and the functional expression of the S-boxes is: , In the above formula, It is a nonlinear transformation function. and For a fixed parameter vector, For a finite field The inverse operation on, This is intermediate iteration data for the SM4 algorithm, and the computation of the S-box will be performed over a finite field. The inverse operation on the top is transformed into the tower domain. This is done to reduce the complexity of the inversion operation.
6. The SM4 fine-grained slicing optimization method for SIMD instructions according to claim 5, characterized in that, The finite field The inverse operation on the top is transformed into the tower domain. include: S301, using isomorphic mapping matrix , finite field The elements in the finite field are represented as finite fields. Linear polynomials over; using isomorphic mapping matrices finite field The elements on are represented as finite fields Linear polynomials over; using isomorphic mapping matrices finite field The elements on are represented as finite fields Linear polynomials over; S302, the inversion operation of the S-box is optimized using the tower domain structure shown in the following formula, thereby recursively transforming the inversion operation in the S-box into a finite field. Multiplication and addition operations: , In the above formula, , , There are three isomorphic mapping matrices. , and These are the function expressions corresponding to the finite field transformations. , and Let the independent variable be a polynomial basis. and For a finite field The parameters on, and For a finite field The parameters on, , and is the independent variable under a normal basis.
7. The SM4 fine-grained slicing optimization method for SIMD instructions according to claim 1, characterized in that, The slicing method used in step S103 is the same as the slicing method used in step S101.
8. A fine-grained SM4 slicing optimization system for SIMD instructions, comprising interconnected microprocessors and memory, characterized in that, The microprocessor is programmed or configured to execute the SM4 fine-grained slicing optimization method for SIMD instructions as described in any one of claims 1 to 7.
9. A computer-readable storage medium storing a computer program, characterized in that, The computer program is used to be programmed or configured by a microprocessor to execute the SM4 fine-grained slicing optimization method for SIMD instructions as described in any one of claims 1 to 7.
