Random number generation methods, apparatus, computer equipment and storage media

By combining timestamps and the operating system's random number interface to generate random numbers, the problem of insufficient security of random numbers on general-purpose computers is solved, and the security and confidentiality of random numbers are improved.

CN115686437BActive Publication Date: 2026-11-17TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202110832377.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-07-22
Publication Date
2026-11-17
Estimated Expiration
2041-07-22

AI Technical Summary

Technical Problem

Existing technologies for generating random numbers on general-purpose computers have deficiencies in confidentiality and security, making it difficult to guarantee the security and randomness of random numbers.

Method used

After iteratively executing the delay instruction, bit data is obtained based on the current timestamp to generate the first entropy source data. Then, the second entropy source data is obtained by combining the operating system's random number interface, and finally, a random number is generated.

Benefits of technology

It improves the security and confidentiality of random numbers, avoids the security risks caused by relying entirely on the operating system's random number interface, and enhances the diversity of entropy sources and the unpredictability of random numbers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115686437B_ABST
    Figure CN115686437B_ABST
Patent Text Reader

Abstract

This application relates to a method, apparatus, computer device, and storage medium for generating random numbers. The method includes: iteratively obtaining bit data based on the current timestamp after executing a delay instruction, until a target number of bits are obtained, at which point the iteration stops; generating first entropy source data based on the target number of bits; calling the operating system's random number interface to obtain second entropy source data; and generating random numbers based on the first and second entropy source data. This method can improve the confidentiality and randomness of random number generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a random number generation method, apparatus, computer device, and storage medium. Background Technology

[0002] Cryptographic security is the cornerstone of modern information security, and the security of random numbers plays a crucial role in cryptographic security. For example, public-key cryptography algorithms require random numbers for encryption, signing, and key generation. However, ensuring the security of generated random numbers on general-purpose computers has always been a challenging problem in the industry.

[0003] The entropy source of random numbers is the origin of their randomness. To ensure the security of generated random numbers, finding a safe and suitable entropy source is crucial. However, random numbers generated using traditional methods have certain shortcomings in terms of confidentiality and security. Summary of the Invention

[0004] Therefore, it is necessary to provide a random number generation method, apparatus, computer device, and storage medium that can improve the randomness and confidentiality of the generated random numbers, in order to address the above-mentioned technical problems.

[0005] A method for generating random numbers, the method comprising:

[0006] The iteration proceeds by obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits has been obtained and the iteration stops.

[0007] Based on the target number of bits of data, generate first entropy source data;

[0008] Call the operating system's random number interface to obtain the second entropy source data;

[0009] Random numbers are generated based on the first entropy source data and the second entropy source data.

[0010] In one embodiment, the iteration, after executing a delay instruction, involves obtaining bit data based on the current timestamp, and stopping the iteration when the target number of bits has been obtained. This includes:

[0011] Determine the target length of the first entropy source data to be generated;

[0012] The iteration continues with the steps of obtaining the current timestamp after executing the delay instruction and generating bit data based on the current timestamp until the target number of obtained bit data matches the target length of the first entropy source data, at which point the iteration stops.

[0013] In one embodiment, obtaining bit data based on the current timestamp includes:

[0014] Call the operating system's timestamp interface to obtain the current timestamp;

[0015] Extract the low-order data of a preset number of bits from the current timestamp, and generate bit data based on the low-order data.

[0016] In one embodiment, extracting low-order data of a preset number of bits from the current timestamp and generating bit data based on the low-order data includes:

[0017] Extract the last bit of the current timestamp to obtain a single bit of data; or

[0018] Extract the second-to-last bit of the current timestamp to obtain a single bit of data; or

[0019] Extract the last and second-to-last bits of the current timestamp, and perform an XOR operation on the last and second-to-last bits to obtain a single bit of data; or

[0020] Extract the last and second-to-last bits of the current timestamp to obtain two bits of data; or

[0021] Extract the last two bits and the next two bits of the current timestamp, and perform an XOR operation on the last two bits and the next two bits to obtain two bits of data.

[0022] In one embodiment, generating the first entropy source data based on the target number of bits includes:

[0023] The target number of bits are concatenated in their respective generating order or reverse order to output the first entropy source data of the target length.

[0024] In one embodiment, the step of calling the operating system's random number interface to obtain the second entropy source data includes:

[0025] Determine the target length of the second entropy source data to be generated;

[0026] According to the target length, the operating system's random number interface is invoked to obtain the second entropy source data.

[0027] In one embodiment, generating random numbers based on the first entropy source data and the second entropy source data includes:

[0028] When the first entropy source data satisfies the preset randomness condition, the first entropy source data is transformed into a first random sequence;

[0029] When the second entropy source data meets the preset randomness condition, the second entropy source data is transformed into a second random sequence;

[0030] Random numbers are generated based on the first random sequence and the second random sequence.

[0031] In one embodiment, the method further includes:

[0032] The entropy threshold is determined based on the number of bits in the first entropy source data and the number of bits in the random number to be generated;

[0033] Entropy estimation is performed on the first entropy source data to obtain the first estimated entropy;

[0034] When the first estimated entropy is greater than the entropy threshold, it is determined that the first entropy source data satisfies the preset randomness condition.

[0035] In one embodiment, transforming the first entropy source data into a first random sequence includes:

[0036] Obtain the target number of bits for the random number to be generated;

[0037] The first entropy source data and the target number of bits are input into the key generation function to obtain a first random sequence of the target number of bits corresponding to the first entropy source data.

[0038] In one embodiment, the method further includes:

[0039] The entropy threshold is determined based on the number of bits in the second entropy source data and the number of bits in the random number to be generated;

[0040] Entropy estimation is performed on the second entropy source data to obtain the second estimated entropy;

[0041] When the second estimated entropy is greater than the entropy threshold, it is determined that the second entropy source data meets the preset randomness condition.

[0042] In one embodiment, transforming the second entropy source data into a second random sequence includes:

[0043] Obtain the target number of bits for the random number to be generated;

[0044] The second entropy source data and the target number of bits are input into the key generation function to obtain a second random sequence of the target number of bits corresponding to the second entropy source data.

[0045] In one embodiment, the method further includes:

[0046] Randomness detection is performed on the first random sequence;

[0047] Randomness detection is performed on the second random sequence;

[0048] When the first random sequence passes the randomness detection and when the second random sequence passes the randomness detection, the step of generating random numbers based on the first random sequence and the second random sequence is executed.

[0049] In one embodiment, generating random numbers based on the first random sequence and the second random sequence includes:

[0050] Perform an XOR operation between the first random sequence and the second random sequence to obtain the result.

[0051] When the calculation result passes the randomness test, the calculation result is used as the generated random number.

[0052] In one embodiment, obtaining bit data based on the current timestamp includes:

[0053] Call the operating system's timestamp interface to obtain the current nanosecond-level timestamp;

[0054] Bit data is generated based on the nanosecond-level timestamp.

[0055] A random number generation device, the device comprising:

[0056] The first entropy source acquisition module is used to iterate through the steps of obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits is obtained and the iteration stops; and to generate first entropy source data based on the target number of bits.

[0057] The second entropy source acquisition module is used to call the operating system's random number interface to obtain second entropy source data;

[0058] The random number generation module is used to generate random numbers based on the first entropy source data and the second entropy source data.

[0059] A computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program performing the following steps:

[0060] The iteration proceeds by obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits has been obtained and the iteration stops.

[0061] Based on the target number of bits of data, generate first entropy source data;

[0062] Call the operating system's random number interface to obtain the second entropy source data;

[0063] Random numbers are generated based on the first entropy source data and the second entropy source data.

[0064] A computer-readable storage medium having a computer program stored thereon, the computer program performing the following steps when executed by a processor:

[0065] The iteration proceeds by obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits has been obtained and the iteration stops.

[0066] Based on the target number of bits of data, generate first entropy source data;

[0067] Call the operating system's random number interface to obtain the second entropy source data;

[0068] Random numbers are generated based on the first entropy source data and the second entropy source data.

[0069] A computer program comprising computer instructions stored in a computer-readable storage medium, wherein a processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions to cause the computer device to perform the steps of the above-described random number generation method.

[0070] The aforementioned random number generation method, apparatus, computer equipment, and storage medium utilize two sources of entropy for the random numbers: entropy source data collected based on the current timestamp and random numbers provided by the operating system's built-in random number interface. Combining these two sources increases the diversity of entropy sources and enhances the security and confidentiality of the final generated random numbers. Specifically, the first entropy source data is generated through multiple iterations, with each iteration based on bit data obtained from the current timestamp. Each iteration requires a delay instruction to increase random perturbation in the time dimension, resulting in higher randomness of the bit data obtained based on the current timestamp in each iteration. This increases the randomness and confidentiality of the generated first entropy source data, thus significantly improving the security of the random numbers generated based on the first and second entropy source data. Furthermore, the random number interface provided by the operating system is accessible to any process. Compared to methods that rely entirely on the operating system's random number interface, the method provided in this application uses self-collected data for the first entropy source data. Combining this self-collected data with random numbers obtained from the operating system greatly avoids the security risks associated with completely trusting the operating system. Attached Figure Description

[0071] Figure 1 This is a diagram illustrating the application environment of a random number generation method in one embodiment;

[0072] Figure 2 This is a flowchart illustrating a random number generation method in one embodiment;

[0073] Figure 3 This is a schematic diagram of the process of collecting first entropy source data based on the current timestamp in one embodiment;

[0074] Figure 4 This is a schematic diagram of the process for collecting first entropy source data based on nanosecond-level timestamps in one embodiment;

[0075] Figure 5 This is a schematic diagram of the process of generating a first random sequence based on first entropy source data in one embodiment;

[0076] Figure 6 This is a schematic diagram of the process for generating a second random sequence based on second entropy source data in one embodiment;

[0077] Figure 7 This is a schematic diagram of a process for generating random numbers by combining two entropy sources in one embodiment;

[0078] Figure 8 This is a flowchart illustrating the process of generating random numbers by combining two entropy sources in one embodiment.

[0079] Figure 9 This is a flowchart illustrating a random number generation method in a specific embodiment.

[0080] Figure 10 This is a structural block diagram of a random number generation device in one embodiment;

[0081] Figure 11 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation

[0082] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0083] To make the embodiments of this application more detailed, some terms are explained below:

[0084] Random numbers: Random numbers are data that satisfy the properties of randomness and unpredictability. The quality of random numbers can be understood as the randomness and unpredictability of the random numbers. Generally speaking, the higher the quality of random numbers, the more difficult it is to crack the security mechanisms based on those random numbers.

[0085] Randomness: An ideal random binary bit stream can be obtained by flipping a coin completely fairly and unbiasedly. For example, heads are 0 and tails are 1. The probability of getting 0 or 1 on each flip is exactly 1 / 2, and each result is independent of the others, meaning that the results produced do not affect future results. Random number generators like the "fair coin" are completely ideal random number generators. In practical applications, the random numbers output by an ideal random number generator are often used as a benchmark for testing and evaluating other random number generators.

[0086] Unpredictability: Random numbers used in cryptographic applications need to be unpredictable to ensure the security of the encryption mechanism. Unpredictability of random numbers means that, based on previously generated random numbers, it should be impossible to predict the next output random number, i.e., forward unpredictability. Simultaneously, it should be impossible to derive a seed from the generated random numbers, i.e., backward unpredictability.

[0087] Based on their generation method, random numbers can be divided into true random numbers and pseudo random numbers. True random numbers are generated through physical phenomena (such as coin tosses, dice rolling, spinning wheels, noise from electronic components, nuclear fission, etc.). The values ​​of these random numbers are completely unpredictable and possess true randomness. For example, a true random number generator converts the mouse movement trajectory into a seed by having the user move the mouse a certain distance, and then uses this seed to generate random numbers. Another example is a random number generator that uses resistors and oscillators to generate thermal noise as an entropy source. Yet another example is the operating system's random number interface, which uses hardware noise to generate random numbers, such as `dev / random` and ` / dev / urandom` in Unix / Linux.

[0088] Pseudo-random numbers are random numbers generated using random algorithms. Finite state machines cannot generate truly random numbers. Computer devices, being finite state machines, typically generate random numbers based on algorithms. Although these numbers may have high entropy and appear to be truly random, they are theoretically predictable and lack true randomness. Random numbers generated in this way are called pseudo-random numbers.

[0089] Entropy: In this application, entropy refers to the entropy of a random number, which is a measure of the randomness of a random number. The randomness of a random number is its entropy. The higher the entropy of a random number, the higher its quality; conversely, the lower the entropy, the lower its quality.

[0090] Entropy source: The source of randomness in data.

[0091] Randomness testing: The process of verifying the randomness of random numbers. It typically involves designing test cases based on randomness assumptions for statistical verification. Each test case verifies a specific randomness assumption, and the statistical results determine whether the assumption is true or false. Under the randomness assumption, the statistic will follow a reference distribution. A critical value, such as 99%, is determined from this reference distribution. During testing, the test results are compared with this critical value to determine whether the randomness of the random numbers is valid.

[0092] Approximate entropy: Approximate entropy is a measure used to quantify the regularity and unpredictability of data fluctuations, and can be used to measure randomness.

[0093] Approximate entropy estimation: A statistical algorithm used to estimate approximate entropy. It determines randomness by comparing the frequency of an m-bit string and an m+1-bit string in the sequence under test, and then comparing this frequency with that in a normally distributed sequence.

[0094] Poker detection: A national-level cryptographic randomness detection algorithm. Its purpose is to detect whether a subsequence in the test sequence matches too many non-overlapping templates. Too many match indicate that the test sequence is not random.

[0095] KDF (Key Derivation Function), also known as Key Derivation Algorithm, can output a binary data string of a specified length based on input data.

[0096] The random number generation method provided in this application can be applied to, for example... Figure 1 In the application environment shown, terminal 102 communicates with server 104 via a network. In one embodiment, server 104 can iterate through the steps of obtaining bit data based on the current timestamp after executing a delay instruction until a target number of bits is obtained, at which point the iteration stops; based on the target number of bits, it generates first entropy source data; it calls the operating system's random number interface to obtain second entropy source data; and it generates random numbers based on the first and second entropy source data.

[0097] In some embodiments, server 104 can send the generated random number to terminal 102. Alternatively, the above-described random number generation method can also be executed by terminal 102. Terminal 102 iterates through the steps of obtaining bit data based on the current timestamp after executing the delay instruction until the target number of bits is obtained, at which point the iteration stops; based on the target number of bits, it generates first entropy source data; it calls the operating system's random number interface to obtain second entropy source data; and it generates a random number based on the first and second entropy source data. After generating the random number, terminal 102 sends the generated random number to server 104.

[0098] For example, in some application scenarios, the server can use the random number generation method provided in the embodiments of this application to generate a random number (secret), and use an encryption algorithm to generate a token using the random number and carrier data (such as user account, account password, etc.). The server returns the token to the client, and the client can carry the token every time it makes a request to the server to indicate the client's identity.

[0099] In some embodiments, the server 104 may also generate a key for encrypting data based on the generated random number, encrypt the data using the key, and then transmit it to other computer devices.

[0100] In some embodiments, the random number generation method provided in this application can be executed by a blockchain node in a blockchain network. For example, after generating a random number using the above-described random number generation method, the blockchain node can use the generated random number to generate a key, use the key to encrypt data sent to other blockchain nodes, and also use the key to encrypt the data and store it on the blockchain to ensure data security.

[0101] The terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets and portable wearable devices, and the server 104 can be implemented by a standalone server or a server cluster consisting of multiple servers.

[0102] In one embodiment, such as Figure 2 As shown, a random number generation method is provided, which can be applied to... Figure 1 The following steps are used as an example of computer equipment (terminal 102 or server 104):

[0103] Step 202: Iterate through the steps of obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits is obtained and the iteration stops.

[0104] The delay instruction is a computer program used to add random perturbation, also known as a placeholder instruction. In this embodiment, the randomness originates from the timestamp. Before obtaining the current timestamp, the computer device executes the delay instruction. The delay instruction occupies a certain execution time, which can improve the randomness of the obtained timestamp. Furthermore, since computer instructions have scheduling priorities, even if the computer device executes the same delay instruction in each iteration, the additional execution time each time is not deterministic or the same, thereby further improving the randomness of the obtained timestamp.

[0105] Since the delay instruction is intended to increase the execution time of a computer program, it can be a functionally meaningless computer program. In other words, the random number generation method provided in this application does not depend on, nor uses, the result obtained after executing the delay instruction. For example, the delay instruction can be a computer program that performs addition, subtraction, multiplication, and division of any number, or it can be a computer program that performs shift operations or XOR operations on any number. It is understood that the number used for calculation and the result obtained are not important; what is important is that the delay instruction increases the program execution time before the current timestamp is obtained. Of course, to ensure the efficiency of random number generation, the execution time of the delay instruction should not be too long. For example, if the delay instruction can increase the program execution time at the nanosecond, microsecond, or millisecond level, then the delay instruction can ensure that the current timestamp is random at the nanosecond, microsecond, or millisecond level, and also ensure the efficiency of random number generation.

[0106] A timestamp is data that changes over time. A timestamp can be obtained by converting the current system time of a computer device. Optionally, the computer device can obtain the timestamp corresponding to the current time through the timestamp interface provided by the operating system. Alternatively, the computer device can obtain the current system time, convert the current system time into a timestamp, and then obtain the current timestamp.

[0107] In this embodiment, the current timestamp obtained by the computer device can be a nanosecond-level timestamp, a microsecond-level timestamp, or a millisecond-level timestamp. It is understood that nanosecond-level timestamps change more frequently. During multiple iterations, the program execution time added by each delay instruction is more likely to differ at the nanosecond level. Therefore, the nanosecond-level timestamp obtained after executing the delay instruction changes more rapidly, making the randomness of nanosecond-level timestamps somewhat higher than that of microsecond-level and millisecond-level timestamps.

[0108] The timestamp acquired by the computer device can be a binary sequence or a decimal sequence. This application primarily uses a binary timestamp as the illustration. In this embodiment, the bit data is binary data, and the number of bits can be set according to actual needs, as long as the bit data obtained in each iteration has a certain degree of randomness. For example, the bit data can be a single bit (1 bit), a two-bit bit (2 bits), a three-bit bit, or a four-bit bit. In each iteration, the computer device can acquire the bit data based on the current timestamp. The computer device can also acquire the number of bits *m* of the random number to be generated, and determine the target length *n* of the first entropy source data to be generated based on the number of bits of the random number, for example, *n* = 2^m, thereby determining the number of iterations based on this target length.

[0109] Specifically, in this embodiment, in order to improve the randomness of the final generated random number, the computer device can iterate multiple times, execute a delay instruction during each iteration, obtain the current timestamp after executing the delay instruction, and obtain the bit data obtained in the current iteration based on the current timestamp. In this way, bit data can be obtained in each iteration, and after multiple iterations, the target number of bit data can be obtained.

[0110] Step 204: Generate the first entropy source data based on the target number of bits.

[0111] In this embodiment, the entropy source of the random number, i.e., its randomness, comes from two aspects: one is the entropy source data collected based on the current timestamp, and the other is the random number provided by the random number interface built into the operating system. Combining these two sources can increase the diversity of the entropy source and improve the security and confidentiality of the final generated random number. The bit data is the entropy source data collected based on the current timestamp. The computer device generates the first entropy source data based on the target number of bits obtained through multiple iterations. Clearly, the first entropy source data is self-collected data, generated through multiple iterations, with each iteration based on the bit data obtained from the current timestamp. Each iteration requires a delay instruction to increase random perturbation in the time dimension, making the randomness of the bit data obtained based on the current timestamp higher, thereby increasing the randomness and confidentiality of the generated first entropy source data. Therefore, the security of the random number ultimately generated based on the first and second entropy source data will be greatly improved.

[0112] Specifically, the computer device can concatenate the target number of bits obtained through multiple iterations, following the order of the iterations (i.e., the order in which they were generated), from least significant bit to most significant bit, to obtain the first entropy source data. Alternatively, the computer device can concatenate the obtained bits from least significant bit to most significant bit in reverse order of the iterations to obtain the first entropy source data. Optionally, the computer device can also concatenate the bits obtained in each iteration sequentially from most significant bit to least significant bit to obtain the first entropy source data.

[0113] Step 206: Call the operating system's random number interface to obtain the second entropy source data.

[0114] In this embodiment, the computer device also obtains random numbers as a second entropy source by calling the random number interface provided by the operating system. To generate truly random numbers, the operating system maintains a dedicated entropy pool for collecting noise. The entropy pool's information comes from external entropy information such as device driver noise, mouse click sounds, device fan noise, or other environmental noise. This collected noise is used to generate truly random numbers. If the entropy pool does not have sufficient noise resources, it needs to wait for the pool to collect more. The greater the physical disorder of the device, the faster the entropy increases, the more saturated the entropy pool becomes, and the higher the efficiency of randomness.

[0115] Optionally, computer devices can obtain random numbers by calling `dev / random` or ` / dev / urandom`. ` / dev / random` is a true random number generator because it generates random numbers using the background noise of the entropy pool as a seed. If the entropy pool is empty, read operations on ` / dev / random` will be blocked until enough ambient noise is collected. This design makes ` / dev / random` a true random number generator, providing the maximum possible random data entropy. Therefore, it is a blocking random number generator, requiring the wait for the entropy pool to accumulate sufficient entropy information before it can be called, resulting in lower efficiency in generating random numbers. To improve the efficiency of random number generation, the operating system also provides another non-blocking random number interface: ` / dev / urandom`. This is a non-blocking random number generator that reuses information from the entropy pool to generate pseudo-random numbers.

[0116] As shown above, although the random numbers generated by the operating system's random number interface can be based on hardware noise and possess high randomness, the random numbers in the entropy pool are vulnerable to being compromised because the operating system's random number interface is accessible to any process, resulting in insufficient confidentiality. Furthermore, the `dev / random` interface is a blocking interface, leading to very low performance in generating random numbers, making it unsuitable for scenarios requiring the generation of a large number of random numbers. While the `dev / urandom` interface is non-blocking, it still returns random numbers even when no randomness is available in the system's entropy pool, further compromising security.

[0117] Therefore, this application combines two different entropy sources to increase the diversity of entropy sources and improve the security and confidentiality of the final generated random numbers.

[0118] In one embodiment, calling the operating system's random number interface to obtain second entropy source data includes: determining the target length of the second entropy source data to be generated; and calling the operating system's random number interface to obtain the second entropy source data according to the target length.

[0119] In this embodiment, the computer device can also obtain the number of bits m of the random number to be generated, determine the target length n of the second entropy source data to be generated based on the number of bits of the random number, for example, n = 2m, and then call the random number interface of the operating system according to the target length to collect a random bit string of length n as the second entropy source data.

[0120] Step 208: Generate random numbers based on the first entropy source data and the second entropy source data.

[0121] Specifically, after the computer device collects the first entropy source data based on timestamps and obtains the second entropy source data by calling the random number interface provided by the operating system, the two different entropy source data can be combined to generate the final random number.

[0122] In the aforementioned random number generation method, the entropy source of the random numbers comes from two aspects: one is entropy source data collected based on the current timestamp, and the other is random numbers provided by the operating system's built-in random number interface. Combining these two sources increases the diversity of entropy sources and improves the security and confidentiality of the final generated random numbers. Specifically, the first entropy source data is generated through multiple iterations, with bit data obtained based on the current timestamp in each iteration. Each iteration requires a delay instruction to increase random perturbation in the time dimension, making the randomness of the bit data obtained based on the current timestamp higher, thereby increasing the randomness and confidentiality of the generated first entropy source data. Therefore, the security of the random numbers ultimately generated based on the first and second entropy source data is greatly improved. Furthermore, the random number interface provided by the operating system is accessible to any process. Compared to methods that completely rely on the operating system's random number interface to obtain random numbers, the method provided in this application uses self-collected data for the first entropy source data required to generate random numbers. Combining this self-collected data with random numbers obtained from the operating system can greatly avoid the security risks associated with completely trusting the operating system.

[0123] In one embodiment, the iteration of obtaining bit data based on the current timestamp after executing a delay instruction, until the target number of bits of data is obtained, includes: determining the target length of the first entropy source data to be generated; and the iteration of obtaining the current timestamp after executing a delay instruction and generating bit data based on the current timestamp, until the target number of bits of data obtained matches the target length of the first entropy source data, at which point the iteration stops.

[0124] In this embodiment, the computer device needs to first determine the target length of the first entropy source data, which is the number of bits. Furthermore, the computer device also needs to first determine the number of bits in the bit data generated based on the current timestamp at each iteration, and determine the number of iterations based on the target length and the number of bits in the bit data.

[0125] In some embodiments, the computer device also needs to first determine how to generate the first entropy source data based on the combination of the target number of bit data, and determine the number of iterations by combining the target length of the first entropy source data, the number of bits of the bit data, and the combination method of the first entropy source data.

[0126] For example, if the target length of the first entropy source data is n, and the bit data generated based on the current timestamp is a single bit, then when combining the single bits in the order of generation to obtain the first entropy source data, it needs to be iterated n times. After obtaining the nth single bit data on the nth iteration, the iteration stops.

[0127] For example, if the target length of the first entropy source data is n (an even number greater than 0), and the bit data generated based on the current timestamp is a two-bit double-bit data, then when combining the double-bit data in the order of generation to obtain the first entropy source data, it needs to iterate n / 2 times. After obtaining the n / 2th double-bit data in the n / 2th iteration, the iteration stops.

[0128] For example, if the target length of the first entropy source data is n, and the bit data generated based on the current timestamp is a two-bit double-bit data, and the double-bit data is combined by overwriting one bit in the order of generation to obtain the first entropy source data, then it needs to be iterated n-1 times. After obtaining the n-1th double-bit data in the n-1th iteration, the iteration stops.

[0129] like Figure 3 The diagram shown illustrates a process for collecting first entropy source data based on the current timestamp in one embodiment. (Refer to...) Figure 3 After starting the data acquisition, the target length of the first entropy source data to be generated is first set, i.e., the number of bits n. Then, the current iteration process begins. During the current iteration, a delay instruction is executed. After the delay instruction is executed, the current timestamp is obtained, and a single bit of data is generated based on the current timestamp. Then, it is determined whether the target number of single bits of data obtained after the current iteration has accumulated to n. If not, the next iteration process is continued. If yes, the iteration process ends. The n bits of data obtained from n iterations are combined sequentially to form a data sequence of length n, which is used as the first entropy source data.

[0130] In one embodiment, obtaining bit data based on the current timestamp after executing a delay instruction includes: executing a delay instruction during the current iteration; calling the operating system's timestamp interface after executing the delay instruction to obtain the current timestamp; extracting low-order data of a preset number of bits from the current timestamp; and generating bit data based on the low-order data.

[0131] As mentioned earlier, the timestamp changes each time the computer device implements the random number generation method provided in this application embodiment or executes the iterative process in the method. The timestamp can be binary data or decimal data. The low-order bits of the timestamp are the data located in the lowest bit position. For example, for a 2-byte binary timestamp 1100 0010, the position of the bits starting from the first 1 decreases sequentially, and the position of the last bit 0 is the lowest. In this embodiment, during multiple iterations, the computer device is more likely to have differences in the low-order bits of the timestamp due to the increased program execution time of each delay instruction. The greater the degree of change, that is, the faster the entropy of the low-order bits increases, the higher the randomness of the low-order bits of the timestamp obtained after executing the delay instruction.

[0132] In one embodiment, extracting a preset number of low-order bits from the current timestamp and generating bit data based on the low-order bits includes: extracting the last bit of the current timestamp to obtain a single bit of bit data; or, extracting the second-to-last bit of the current timestamp to obtain a single bit of bit data; or, extracting the last bit and the second-to-last bit of the current timestamp, and performing an XOR operation on the last bit and the second-to-last bit of the current timestamp to obtain a single bit of bit data; or, extracting the last bit and the second-to-last bit of the current timestamp to obtain a double bit of bit data; or, extracting the last two bits and the second-to-last two bits of the current timestamp, and performing an XOR operation on the last two bits and the second-to-last two bits of the current timestamp to obtain a double bit of bit data.

[0133] Specifically, computer devices generate bit data based on the current timestamp in various ways. The generated bit data can be a single bit, two, three, or four bits, raw data extracted from the current timestamp, or data obtained by performing calculations on raw data extracted from the current timestamp.

[0134] In one embodiment, the computer device can directly extract the last bit of the current timestamp to obtain a single bit of data. In another embodiment, the computer device can extract the second-to-last bit of the current timestamp to obtain a single bit of data, and so on. The computer device can also extract the third-to-last bit, or even the fourth-to-last bit, of the current timestamp as the obtained single bit of data. In yet another embodiment, the computer device can extract the last and second-to-last bits of the current timestamp, perform an XOR operation on the last and second-to-last bits, and obtain a single bit of data. Therefore, when the length of the first entropy source data is n, the computer device needs to iterate n times to obtain n single bits of data, and then combine them to obtain the first entropy source data.

[0135] In one embodiment, the computer device can further extract the last bit of the timestamp obtained in the previous iteration and the last bit of the timestamp obtained in the current iteration, and perform an XOR operation on the two last bit data to obtain a single bit of data. Therefore, when the length of the first entropy source data is n, the computer device needs to iterate n+1 times to obtain n single bits of data, and then combine them to obtain the first entropy source data.

[0136] In one embodiment, the computer device can also extract the last and second-to-last bits of the current timestamp to obtain two-bit data. Therefore, when the length of the first entropy source data is n (an even number), the computer device needs to iterate n / 2 times to obtain n / 2 two-bit data units, which are then combined to obtain the first entropy source data.

[0137] In one embodiment, the computer device can further extract the last and second-last timestamp data obtained in the previous iteration, extract the last and second-last timestamp data obtained in the current iteration, and perform an XOR operation on the two obtained last and second-last timestamp data to obtain two-bit data. Therefore, when the length of the first entropy source data is n (an even number greater than 0), the computer device needs to iterate (n+2) / 2 times to obtain n / 2 two-bit data, which are then combined to obtain the first entropy source data.

[0138] In one embodiment, the computer device can also extract the last two bits and the next two bits of the current timestamp, and perform an XOR operation on the last two bits and the next two bits to obtain two-bit data. For example, for a 2-byte binary timestamp 1100 0010, extracting the last two bits "10" and the next two bits "00", and performing an XOR operation on them, yields "10". Therefore, when the length of the first entropy source data is n (an even number greater than 0), the computer device needs to iterate n / 2 times to obtain n / 2 two-bit data, which are then combined sequentially to obtain the first entropy source data.

[0139] In one embodiment, obtaining bit data based on the current timestamp includes: calling the operating system's timestamp interface to obtain the current nanosecond-level timestamp; and generating bit data based on the nanosecond-level timestamp.

[0140] like Figure 4 The diagram shown illustrates a process for collecting first entropy source data based on nanosecond-level timestamps in one embodiment. (Refer to...) Figure 4 After initiating data acquisition, the target length of the first entropy source data to be generated, i.e., the number of bits n, is first set. Then, the iteration process begins. During each iteration, a delay instruction is executed. After the delay instruction, the current nanosecond-level timestamp is obtained. The last and second-to-last bits of the current nanosecond-level timestamp are then extracted and XORed to obtain a single bit of data. Next, it is determined whether the target number of single bits obtained after the iteration has accumulated to n. If not, the next iteration process continues. If yes, the iteration process ends. The n bits obtained from n iterations are sequentially combined to form a data sequence of length n, which is then output as the first entropy source data.

[0141] In one embodiment, generating random numbers based on first entropy source data and second entropy source data includes: transforming the first entropy source data into a first random sequence when the first entropy source data satisfies a preset randomness condition; transforming the second entropy source data into a second random sequence when the second entropy source data satisfies the preset randomness condition; and generating random numbers based on the first random sequence and the second random sequence.

[0142] The first entropy source data is collected based on timestamps and has a certain degree of randomness. The second entropy source data is obtained based on the system's random number interface and also has high randomness. To further improve the quality of the final generated random numbers, the computer device can transform the first entropy source data into a first random sequence when it meets preset randomness conditions, and transform the second entropy source data into a second random sequence when it meets preset randomness conditions. The final random number is then generated based on the generated first and second random sequences.

[0143] The preset randomness conditions can be the conditions that need to be met to pass the randomness verification. The computer equipment can perform randomness detection on the obtained first entropy source data and second entropy source data respectively, including but not limited to approximate entropy estimation, frequency detection, intra-block frequency detection, discrete Fourier transform detection, overlapping subsequence detection, summation detection, random walk detection, autocorrelation detection, etc.

[0144] In one embodiment, the method further includes: determining an entropy threshold based on the number of bits in the first entropy source data and the number of bits in the random number to be generated; performing entropy estimation on the first entropy source data to obtain a first estimated entropy; and determining that the first entropy source data satisfies a preset randomness condition when the first estimated entropy is greater than the entropy threshold.

[0145] In this embodiment, after obtaining the first entropy source data, the computer device determines the entropy threshold based on the number of bits n of the first entropy source data and the number of bits m of the random number to be generated. For example, the entropy threshold is m / n. When n = 2m, the entropy threshold is 1 / 2. The computer device performs entropy estimation on the first entropy source data to obtain the corresponding first estimated entropy e1. When the first estimated entropy e1 satisfies e1*n > m, that is, e1 > m / n, it is determined that the first entropy source data has passed the approximate entropy estimation detection, and the computer device can determine that the first entropy source data meets the preset randomness condition.

[0146] In one embodiment, transforming the first entropy source data into a first random sequence includes: obtaining the target number of bits for the random number to be generated; inputting the first entropy source data and the target number of bits into a key generation function to obtain a first random sequence of the target number of bits corresponding to the first entropy source data.

[0147] The key generation function is used to derive one or more keys from a given key, and can be implemented using a hash algorithm. In this embodiment, after obtaining the first entropy source data, the computer device can use the key generation function to convert the first entropy source data into a first random sequence with a target number of bits. For example, based on the first entropy source data S1 and the target number of bits m of the random number to be generated, a KDF(S1, m) operation is performed, and the output is a random bit string of length m, which is called the first random sequence K1. In this embodiment, the computer device can use the KDF algorithm mentioned in the domestic standard document GM / T 0003 "SM2 Elliptic Curve Public Key Cryptography Algorithm", or it can use the KDF algorithm defined in other national standards to derive the first random sequence from the first entropy source data.

[0148] In one embodiment, for the first random sequence derived from the first entropy source data, the computer device can further perform a randomness test on it. If the randomness test is passed, the output can be used as one of the data sources for generating the final random numbers, thereby further improving the quality of the final generated random numbers. Similarly, randomness testing of the first random sequence includes, but is not limited to, methods such as poker testing, frequency testing, intra-block frequency testing, discrete Fourier transform testing, overlapping subsequence testing, summation testing, random walk testing, autocorrelation testing, etc. For example, the computer device can perform a poker test on it. When the generated first random sequence passes the poker test, it is output; when the generated first random sequence fails the poker test, the step of generating the first entropy source data is returned and re-executed until the re-obtained first random sequence passes the poker test.

[0149] like Figure 5 The diagram shown illustrates a process for generating a first random sequence based on collected first entropy source data in one embodiment. (Refer to...) Figure 5 After initiating data collection, first set the target length of the random numbers to be generated, i.e., the number of bits, m. Then, using the timestamp-based random number collection method mentioned earlier, collect first entropy source data S1 of length n (e.g., n = 2m). Next, check whether the first entropy source data S1 meets the preset randomness conditions, such as... Figure 5 As shown, an approximate entropy estimate is performed on the first entropy source data S1 to obtain the corresponding first estimated entropy e1. Next, based on the first estimated entropy e1, it is determined whether the first entropy source data S1 satisfies a preset randomness condition, such as... Figure 5As shown, it determines whether the first estimated entropy e1 satisfies e1*n>m. If it does, then based on the first entropy source data S1 and the number of bits m of the random number to be generated, the key generation function KDF(S1, m) is used to output a random bit string of length m, i.e., the first random sequence K1. If it does not satisfy the condition, the process returns to the step of collecting the first entropy source data S1 of length n (e.g., n=2m) using the timestamp-based random number collection method mentioned earlier, and is repeated. After obtaining the first random sequence K1, its randomness is checked again, as follows: Figure 5 As shown, a poker test is performed on the first random sequence K1. If the first random sequence K1 passes the poker test, it is output and used to generate the final random number. If the first random sequence K1 fails the poker test, the process also needs to return to the step of collecting the first entropy source data S1 of length n (e.g., n = 2m) using the timestamp-based random number collection method mentioned earlier.

[0150] In one embodiment, the method further includes: determining an entropy threshold based on the number of bits in the second entropy source data and the number of bits in the random number to be generated; performing entropy estimation on the second entropy source data to obtain a second estimated entropy; and determining that the second entropy source data satisfies a preset randomness condition when the second estimated entropy is greater than the entropy threshold.

[0151] In this embodiment, after obtaining the second entropy source data, the computer device determines the entropy threshold based on the number of bits n of the second entropy source data and the number of bits m of the random number to be generated. For example, the entropy threshold is m / n. When n = 2m, the entropy threshold is 1 / 2. The computer device performs entropy estimation on the second entropy source data to obtain the corresponding second estimated entropy e2. When the second estimated entropy e2 satisfies e2*n > m, that is, e2 > m / n, it is determined that the second entropy source data has passed the approximate entropy estimation detection, and the computer device can determine that the second entropy source data meets the preset randomness condition.

[0152] In one embodiment, transforming the second entropy source data into a second random sequence includes: obtaining the target number of bits for the random number to be generated; inputting the second entropy source data and the target number of bits into a key generation function to obtain a second random sequence of the target number of bits corresponding to the second entropy source data.

[0153] In this embodiment, after the computer device obtains the second entropy source data, it can convert the second entropy source data into a second random sequence with a target number of bits through a key generation function. For example, based on the second entropy source data S2 and the target number of bits m of the random number to be generated, the KDF(S2, m) operation is performed, and a random bit string of length m is output after the operation, which is called the second random sequence K2.

[0154] In one embodiment, for the second random sequence derived from the second entropy source data, the computer device can further perform randomness detection on it. If the randomness detection is passed, the output can be used as one of the data sources for generating the final random numbers, thereby further improving the quality of the final generated random numbers. Similarly, randomness detection of the second random sequence includes, but is not limited to, methods such as poker detection, frequency detection, intra-block frequency detection, discrete Fourier transform detection, overlapping subsequence detection, summation detection, random walk detection, autocorrelation detection, etc. For example, the computer device can perform poker detection on it. When the generated second random sequence passes the poker detection, it is output; when the generated second random sequence fails the poker detection, the step of generating the second entropy source data is returned and re-executed until the re-obtained second random sequence passes the poker detection.

[0155] In one embodiment, the method further includes: performing randomness detection on the first random sequence; performing randomness detection on the second random sequence; and when the first random sequence passes the randomness detection and the second random sequence passes the randomness detection, performing the step of generating random numbers based on the first random sequence and the second random sequence.

[0156] like Figure 6 The diagram shown illustrates a process for generating a second random sequence based on collected second entropy source data in one embodiment. (Refer to...) Figure 6 After initiating data collection, first set the target length of the random numbers to be generated, i.e., the number of bits *m*. Then, using the previously mentioned method of calling the random number interface provided by the operating system, collect second entropy source data S2 of length *n* (e.g., *n* = 2*m). Next, check whether the second entropy source data S2 meets the preset randomness conditions, such as… Figure 6 As shown, an approximate entropy estimate is performed on the second entropy source data S2 to obtain the corresponding second estimated entropy e2. Next, based on the second estimated entropy e2, it is determined whether the second entropy source data S2 satisfies the preset randomness condition, such as... Figure 6 As shown, it determines whether the second estimated entropy e2 satisfies e1*n>m. If it does, then based on the second entropy source data S2 and the number of bits m of the random number to be generated, the key generation function KDF(S2, m) is used to output a random bit string of length m, i.e., the second random sequence K2. If it does not satisfy the condition, the process returns to the step mentioned earlier, which involves collecting the second entropy source data S2 of length n (e.g., n=2m) by calling the random number interface provided by the operating system, and is executed again. After obtaining the second random sequence K2, its randomness is checked again, as follows: Figure 6As shown, a poker test is performed on the second random sequence K2. If the second random sequence K2 passes the poker test, it is output and used to generate the final random number. If the second random sequence K2 fails the poker test, the process also needs to return to the step of collecting the second entropy source data S2 of length n (e.g., n = 2m) by calling the random number interface provided by the operating system and re-execute it.

[0157] In one embodiment, generating a random number based on a first random sequence and a second random sequence includes: performing an XOR operation on the first random sequence and the second random sequence to obtain the operation result; when the operation result passes the randomness detection, the operation result is used as the generated random number.

[0158] The computer device performs an XOR operation on a first random sequence obtained through timestamp acquisition and processing, and a second random sequence obtained through a random number interface acquisition and processing, to obtain the result. The result is then subjected to randomness detection. This randomness detection includes, but is not limited to, methods such as poker detection, frequency detection, intra-block frequency detection, discrete Fourier transform detection, overlapping subsequence detection, summation detection, random walk detection, and autocorrelation detection. In this embodiment, the computer device further performs poker detection on the result, using the result that passes the poker detection as the generated random number.

[0159] like Figure 7 The diagram shown illustrates a process for generating random numbers using two entropy sources in one embodiment. (Refer to...) Figure 7 The process is as follows:

[0160] Step 1: Collect bit data based on timestamps, process and detect it, and output the first random sequence of m bits, denoted as K1;

[0161] Step 2: Collect random numbers based on the random number interface provided by the operating system, process and detect them, and output a second random sequence of m bits, denoted as K2;

[0162] Step 3: Perform a bitwise XOR operation on K1 and K2 to obtain K, i.e.

[0163] Step 4: Perform a poker test on K. If the poker test passes, output K as the final random number. If the poker test fails, return to Step 1 and re-execute the process.

[0164] The execution order of steps one and two can be interchanged.

[0165] like Figure 8 The diagram shown is a flowchart illustrating a method for generating random numbers using two entropy sources in one embodiment. (Refer to...) Figure 8The first entropy source acquired by the computer device is an entropy source based on nanosecond-level timestamps, namely the first entropy source data S1 mentioned earlier. Entropy estimation and detection are then performed on this data. If the entropy estimation passes, a first random sequence K1 is derived using the KDF function, followed by a poker test on K1. Similarly, the second entropy source acquired by the computer device is a random number obtained from the operating system's random number interface, namely the second entropy source data S2 mentioned earlier. Entropy estimation and detection are then performed on this data. If the entropy estimation passes, a second random sequence K2 is derived using the KDF function, followed by a poker test on K2. When both the first random sequence K1 and the second random sequence K2 pass the poker test, a bitwise XOR operation is performed on them to obtain the result K. This result K is then subjected to a poker test. If the poker test passes, K is output as the final generated random number. The execution order of entropy estimation and poker testing can be interchanged.

[0166] In one embodiment, the random numbers generated in this application can be used as the entropy source of other software random number generators, that is, as the seed for other software random number generators to generate new random numbers. The random numbers generated in this application can also be used in other scenarios that require the use of random numbers, such as for encryption, signing, and key generation in public-key cryptography algorithms, etc.

[0167] like Figure 9 The diagram shown is a flowchart illustrating a random number generation method in a specific embodiment. (Refer to...) Figure 9 It includes the following steps:

[0168] Step 902: Start collecting the first entropy source data and set the target length n of the first entropy source data to be generated;

[0169] Next, the iterative process begins;

[0170] Step 904: During this iteration, execute the delay instruction;

[0171] Step 906: Obtain the current nanosecond-level timestamp;

[0172] Step 908: Extract the last and second-to-last bits of the current nanosecond-level timestamp;

[0173] Step 910: Perform an XOR operation on the last bit data and the second-to-last bit data to obtain a single bit of bit data;

[0174] Step 912: Determine whether the target number of single-bit data obtained after the current iteration has accumulated to n; if not, return to step 906 to continue the next iteration process; if yes, proceed to step 914.

[0175] Step 914: Combine the obtained n bits of data sequentially to form a data sequence of length n, and output it as the first entropy source data S1;

[0176] Step 916: Perform approximate entropy estimation on the first entropy source data S1 to obtain the corresponding first estimated entropy e1;

[0177] Step 918: Determine whether the first estimated entropy e1 satisfies e1*n>m;

[0178] Where m is the length of the random number to be generated;

[0179] If satisfied, proceed to step 920;

[0180] If the condition is not met, return to step 902 and execute again;

[0181] Step 920: Using the key generation function, based on the first entropy source data S1 and the number of bits m of the random number to be generated, output a first random sequence K1 of length m;

[0182] Step 922: Determine whether the first random sequence K1 output passes the poker test;

[0183] If the first random sequence K1 passes the poker test, then proceed to step 934;

[0184] If the first random sequence K1 fails the poker test, return to step 902 and repeat the process.

[0185] Step 924: Call the operating system's random number interface to obtain the second entropy source data S2;

[0186] Step 926: Perform approximate entropy estimation on the second entropy source data S2 to obtain the corresponding second estimated entropy e2;

[0187] Step 928: Determine whether the second estimated entropy e2 satisfies e2*n>m;

[0188] If satisfied, proceed to step 930;

[0189] If the condition is not met, return to step 924 and execute again;

[0190] Step 930: Using the key generation function, based on the second entropy source data S2 and the number of bits m of the random number to be generated, output a second random sequence K2 of length m;

[0191] Step 932: Determine whether the output second random sequence K2 passes the poker detection;

[0192] If the second random sequence K2 passes the poker test, then proceed to step 934;

[0193] If the second random sequence K2 fails the poker test, return to step 924 and repeat the process.

[0194] Step 934: Perform an XOR operation on the first random sequence K1 and the second random sequence K2 to obtain the result K;

[0195] Step 936: Determine whether the calculation result K passes the poker test;

[0196] If the calculation result passes the poker test, proceed to step 938;

[0197] If the calculation result fails the poker test, return to step 902 and repeat the process;

[0198] Step 938: Use the calculation result K as the generated random number.

[0199] The random number generation method provided in this application overcomes the shortcomings of traditional methods that obtain random numbers through the system's random number interface: First, different processes have access to the random number interface provided by the operating system, resulting in insufficient confidentiality; second, the random number interface is entirely dependent on the operating system and lacks self-collected data, meaning that the operating system must be completely trusted, which poses a security risk for projects with extremely high security requirements; third, without the introduction of algorithms such as entropy estimation and poker detection, the quality of random numbers is difficult to guarantee. This application, however, proposes an entropy source acquisition method based on nanosecond-level timestamps, combined with random numbers generated by the random number interface provided by the operating system, and incorporates multiple techniques such as approximate entropy evaluation, the KDF algorithm, and poker detection, thus solving the confidentiality and security deficiencies of traditional solutions and improving the confidentiality and randomness of random numbers.

[0200] It should be understood that although the steps in the flowchart above are shown sequentially as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowchart above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps.

[0201] In one embodiment, such as Figure 10As shown, a random number generation device 1000 is provided. This device can be a software module, a hardware module, or a combination of both as part of a computer device. Specifically, the device includes: a first entropy source acquisition module 1002, a second entropy source acquisition module 1004, and a random number generation module 1006, wherein:

[0202] The first entropy source acquisition module 1002 is used to iterate the steps of obtaining bit data according to the current timestamp after executing the delay instruction until the target number of bit data is obtained and then stop iterating; and to generate first entropy source data based on the target number of bit data.

[0203] The second entropy source acquisition module 1004 is used to call the operating system's random number interface to obtain second entropy source data;

[0204] The random number generation module 1006 is used to generate random numbers based on the first entropy source data and the second entropy source data.

[0205] In one embodiment, the first entropy source acquisition module 1002 is further configured to determine the target length of the first entropy source data to be generated; iterate the steps of obtaining the current timestamp after executing the delay instruction and generating bit data according to the current timestamp until the target number of bit data obtained matches the target length of the first entropy source data, at which point the iteration stops.

[0206] In one embodiment, the first entropy source acquisition module 1002 is further configured to execute a delay instruction during the current iteration; after executing the delay instruction, call the timestamp interface of the operating system to obtain the current timestamp; extract low-order data of a preset number of bits from the current timestamp, and generate bit data based on the low-order data.

[0207] In one embodiment, the first entropy source acquisition module 1002 is further configured to extract the last bit of the current timestamp to obtain a single bit of bit data; or extract the second-to-last bit of the current timestamp to obtain a single bit of bit data; or extract the last bit and the second-to-last bit of the current timestamp, and perform an XOR operation on the last bit and the second-to-last bit of the current timestamp to obtain a single bit of bit data; or extract the last two bits and the second-to-last two bits of the current timestamp, and perform an XOR operation on the last two bits and the second-to-last two bits of the current timestamp to obtain a double bit of bit data; or extract the last two bits and the second-to-last two bits of the current timestamp, and perform an XOR operation on the last two bits and the second-to-last two bits of the current timestamp to obtain a double bit of bit data.

[0208] In one embodiment, the first entropy source acquisition module 1002 is further configured to concatenate the target number of bit data according to their respective generation order or reverse order, and output the first entropy source data of the target length.

[0209] In one embodiment, the second entropy source acquisition module 1004 is further configured to determine the target length of the second entropy source data to be generated; and to obtain the second entropy source data by calling the operating system's random number interface according to the target length.

[0210] In one embodiment, the random number generation module 1006 is further configured to transform the first entropy source data into a first random sequence when the first entropy source data meets the preset randomness condition; transform the second entropy source data into a second random sequence when the second entropy source data meets the preset randomness condition; and generate random numbers based on the first random sequence and the second random sequence.

[0211] In one embodiment, the random number generation module 1006 is further configured to determine an entropy threshold based on the number of bits in the first entropy source data and the number of bits in the random number to be generated; perform entropy estimation on the first entropy source data to obtain a first estimated entropy; and determine that the first entropy source data satisfies a preset randomness condition when the first estimated entropy is greater than the entropy threshold.

[0212] In one embodiment, the random number generation module 1006 is further configured to obtain the target number of bits of the random number to be generated; input the first entropy source data and the target number of bits into the key generation function to obtain a first random sequence of the target number of bits corresponding to the first entropy source data.

[0213] In one embodiment, the random number generation module 1006 is further configured to determine an entropy threshold based on the number of bits in the second entropy source data and the number of bits in the random number to be generated; perform entropy estimation on the second entropy source data to obtain a second estimated entropy; and determine that the second entropy source data satisfies a preset randomness condition when the second estimated entropy is greater than the entropy threshold.

[0214] In one embodiment, the random number generation module 1006 is further configured to obtain the target number of bits of the random number to be generated; input the second entropy source data and the target number of bits into the key generation function to obtain a second random sequence of the target number of bits corresponding to the second entropy source data.

[0215] In one embodiment, the random number generation module 1006 is further configured to perform randomness detection on the first random sequence; perform randomness detection on the second random sequence; and generate a random number based on the first random sequence and the second random sequence when the first random sequence passes the randomness detection and the second random sequence passes the randomness detection.

[0216] In one embodiment, the random number generation module 1006 is further configured to perform an XOR operation on the first random sequence and the second random sequence to obtain the operation result; when the operation result passes the randomness detection, the operation result is used as the generated random number.

[0217] In one embodiment, the first entropy source acquisition module 1002 is also used to call the operating system's timestamp interface to obtain the current nanosecond-level timestamp; and generate bit data based on the nanosecond-level timestamp.

[0218] The aforementioned random number generation device 1000 uses two sources for its random number entropy: entropy source data collected based on the current timestamp and random numbers provided by the operating system's built-in random number interface. Combining these two sources increases the diversity of entropy sources and enhances the security and confidentiality of the final generated random numbers. Specifically, the first entropy source data is generated through multiple iterations, with each iteration based on bit data obtained from the current timestamp. Each iteration requires a delay instruction to increase random perturbation in the time dimension, making the randomness of the bit data obtained based on the current timestamp higher, thus increasing the randomness and confidentiality of the generated first entropy source data. Therefore, the security of the random numbers ultimately generated based on the first and second entropy source data is greatly improved. Furthermore, the random number interface provided by the operating system is accessible to any process. Compared to methods that rely entirely on the operating system's random number interface to obtain random numbers, the method provided in this application uses self-collected data for the first entropy source data required to generate random numbers. Combining this self-collected data with random numbers obtained from the operating system greatly avoids the security risks associated with completely trusting the operating system.

[0219] Specific limitations regarding the random number generation device 1000 can be found in the limitations regarding the random number generation method described above, and will not be repeated here. Each module in the aforementioned random number generation device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independently of the processor in a computer device, or stored in software in the memory of a computer device, so that the processor can call and execute the operations corresponding to each module.

[0220] In one embodiment, a computer device is provided, the computer device may be... Figure 1 The internal structure diagram of a server or terminal in a system can be as follows: Figure 11 As shown, the computer device includes a processor, memory, and a network interface connected via a system bus. The processor provides computational and control capabilities. The memory includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage medium. The network interface is used to communicate with external terminals via a network connection. When executed by the processor, the computer program implements a random number generation method.

[0221] When the computer device is a terminal, it may also include a display screen and an input device. The display screen may be an LCD screen or an e-ink screen, and the input device may be a touch layer covering the display screen, or buttons, a trackball, or a touchpad mounted on the computer device casing, or an external keyboard, touchpad, or mouse, etc.

[0222] Those skilled in the art will understand that Figure 11 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0223] In one embodiment, a computer device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above method embodiments.

[0224] In one embodiment, a computer-readable storage medium is provided storing a computer program that, when executed by a processor, implements the steps in the above method embodiments.

[0225] In one embodiment, a computer program product or computer program is provided, the computer program product or computer program including computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and executes the computer instructions, causing the computer device to perform the steps in the above method embodiments.

[0226] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical storage, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.

[0227] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0228] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.

Claims

1. A method for generating random numbers, characterized in that, The method includes: The iteration proceeds by obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits has been obtained and the iteration stops. Based on the target number of bits of data, generate first entropy source data; Call the operating system's random number interface to obtain the second entropy source data; Obtain the target number of bits for the random number to be generated; A first entropy threshold is determined based on the number of bits in the first entropy source data and the target number of bits; entropy estimation is performed on the first entropy source data to obtain a first estimated entropy; when the first estimated entropy is greater than the first entropy threshold, the first entropy source data is transformed into a first random sequence. A second entropy threshold is determined based on the number of bits in the second entropy source data and the target number of bits; entropy estimation is performed on the second entropy source data to obtain a second estimated entropy; when the second estimated entropy is greater than the second entropy threshold, the second entropy source data is transformed into a second random sequence. A random number is generated based on the first random sequence and the second random sequence.

2. The method according to claim 1, characterized in that, The iteration, which involves obtaining bit data based on the current timestamp after executing a delay instruction, continues until the target number of bits has been obtained, including: Determine the target length of the first entropy source data to be generated; The iteration continues with the steps of obtaining the current timestamp after executing the delay instruction and generating bit data based on the current timestamp until the target number of obtained bit data matches the target length of the first entropy source data, at which point the iteration stops.

3. The method according to claim 1, characterized in that, The step of obtaining bit data based on the current timestamp after executing the delay instruction includes: During the current iteration, a delay instruction is executed; After executing the delay instruction, call the operating system's timestamp interface to obtain the current timestamp; Extract the low-order data of a preset number of bits from the current timestamp, and generate bit data based on the low-order data.

4. The method according to claim 3, characterized in that, The step of extracting low-order data of a preset number of bits from the current timestamp and generating bit data based on the low-order data includes: Extract the last bit of the current timestamp to obtain a single bit of data; or Extract the second-to-last bit of the current timestamp to obtain a single bit of data; or Extract the last and second-to-last bits of the current timestamp, and perform an XOR operation on the last and second-to-last bits to obtain a single bit of data; or Extract the last and second-to-last bits of the current timestamp to obtain two bits of data; or Extract the last two bits and the next two bits of the current timestamp, and perform an XOR operation on the last two bits and the next two bits to obtain two bits of data.

5. The method according to claim 1, characterized in that, The generation of first entropy source data based on the target number of bits includes: The target number of bits are concatenated in their respective generating order or reverse order to output the first entropy source data of the target length.

6. The method according to claim 1, characterized in that, The step of calling the operating system's random number interface to obtain the second entropy source data includes: Determine the target length of the second entropy source data to be generated; According to the target length, the operating system's random number interface is invoked to obtain the second entropy source data.

7. The method according to claim 1, characterized in that, The step of transforming the first entropy source data into a first random sequence includes: Obtain the target number of bits for the random number to be generated; The first entropy source data and the target number of bits are input into the key generation function to obtain a first random sequence of the target number of bits corresponding to the first entropy source data.

8. The method according to claim 1, characterized in that, The step of transforming the second entropy source data into a second random sequence includes: Obtain the target number of bits for the random number to be generated; The second entropy source data and the target number of bits are input into the key generation function to obtain a second random sequence of the target number of bits corresponding to the second entropy source data.

9. The method according to claim 1, characterized in that, The method further includes: Randomness detection is performed on the first random sequence; Randomness detection is performed on the second random sequence; When the first random sequence passes the randomness detection and when the second random sequence passes the randomness detection, the step of generating random numbers based on the first random sequence and the second random sequence is executed.

10. A random number generation device, characterized in that, The device includes: The first entropy source acquisition module is used to iterate through the steps of obtaining bit data based on the current timestamp after executing the delay instruction, until the target number of bits is obtained and the iteration stops; and to generate first entropy source data based on the target number of bits. The second entropy source acquisition module is used to call the operating system's random number interface to obtain second entropy source data; A random number generation module is used to: obtain the target number of bits for the random number to be generated; determine a first entropy threshold based on the number of bits in the first entropy source data and the target number of bits; perform entropy estimation on the first entropy source data to obtain a first estimated entropy; when the first estimated entropy is greater than the first entropy threshold, transform the first entropy source data into a first random sequence; determine a second entropy threshold based on the number of bits in the second entropy source data and the target number of bits; perform entropy estimation on the second entropy source data to obtain a second estimated entropy; when the second estimated entropy is greater than the second entropy threshold, transform the second entropy source data into a second random sequence; and generate a random number based on the first random sequence and the second random sequence.

11. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 9.

12. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 9.

Citation Information

Patent Citations

  • High-speed generation method of true random numbers

    CN106775583A

  • Random bit generation method and device, electronic device and storage medium

    CN110851112A

  • Injecting CPU time jitter to improve entropy quality for random number generator

    US20180004486A1