利用ADC的误差来源产生随机数的方法
本章介绍了使用软件触发方式触发ADC,配置普通信道和DMA。根据随机数需要的位数来配置信道数,一次转换最多到16信道,将16信道转换的数值可组合计算成一个32位的随机数。底下是DMA和ADC的配置代码。
可以看到,代码中并没有对ADC做自校准,转换时间也使用最短的,这种情况下,ADC的准确度会是最差的,有助于乱度的提升。利用上电时SRAM的内容来计算随机数的方式
这范例只是简单的利用累加来获得一个随机数
以上两种方式建议应用在上电后执行,因为SRAM内容在运行后会初始化,ADC也会有其他应用上的需求,上电后执行并获得一个随机数,将这个随机数当成Seed,之后可以利用标准C函式库中提供的随机数生成器,产生后续的随机数。
SRAM的方式限定在POR后使用。如果只是一般的reset,SRAM会维持内容,造成产生的随机数都是相同。
ADC的方式则没有限制,但是因为使用ADC外设的资源,推荐放在开机时执行,不会影响后续的ADC应用。
范例运行和分析
本篇应用笔记适用于AT32各系列MCU,只要有ADC外设皆可适用。
范例固件AN0175_SourceCode_V2.0.0运行在AT32403A AT-START版上,透过 PuTTY(免费开源终端仿真器)等终端仿真应用程序,将数据存储在工作站上。
在工作站上编译NIST统计测试集程序包,以生成可执行程序。接下来运行NIST统计测试集程序分析数据以及统计测试。
以下是使用范例固件AN0175_SourceCode_V2.0.0在上电后会产生的一个随机数,在收集约319万笔随机数后,进行NIST统计测试。
AT32403A AT-START 版
1) 串口(PA9)
具有串口的计算机,运行 Linux 系统
下载到AT32403A AT-START版运行
1) AN0175_SourceCode_V2.0.0
计算机端运行
1) 终端仿真器如PuTTY
2) 统计测试集源程序
https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/document...
https://github.com/usnistgov/SP800-90B_EntropyAssessment
NIST SP800-22b统计测试集
基于NIST统计测试集:April 27, 2010: NIST SP 800-22rev1a (dated April 2010), A Statistical Test Suite for the Validation of Random Number Generators and Pseudo Random Number Generators for Cryptographic Applications, that describes the test suite.
统计测试集源程序下载:
https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/document...
NIST SP800-90b统计测试集
基于NIST统计测试集:November 21, 2014: NIST requests comments on the latest revision of NIST SP 800-90A, Recommendation for Random Number Generation Using Deterministic Random Bit Generators, which is dated November 2014.
统计测试集源程序下载:
https://github.com/usnistgov/SP800-90B_EntropyAssessment
统计测试集结果:需先转换成符合2-bit-wide symbols数据输入格式。
./ea_non_iid 0421_2.bin 2 -i -a -v
Opening file: '0421_2.bin'
Loaded 50888144 samples of 4 distinct 2-bit-wide symbols
Number of Binary Symbols: 101776288
Running non-IID tests...
Running Most Common Value Estimate...
Bitstring MCV Estimate: mode = 50891714, p-hat = 0.50003507693265448, p_u = 0.50016273956095891
Most Common Value Estimate (bit string) = 0.999531 / 1 bit(s)
Literal MCV Estimate: mode = 12725005, p-hat = 0.25005834364876817, p_u = 0.25021470996034195
Most Common Value Estimate = 1.998761 / 2 bit(s)
Running Entropic Statistic Estimates (bit strings only)...
Bitstring Collision Estimate: X-bar = 2.5000060058338387, sigma-hat = 0.50000000610486417, p = 0.50989562404154842
Collision Test Estimate (bit string) = 0.971726 / 1 bit(s)
Bitstring Markov Estimate: P_0 = 0.49996492306734552, P_1 = 0.50003507693265448, P_0,0 = 0.4999425562646943, P_0,1 = 0.5000574437353057, P_1,0 = 0.49998729655651403, P_1,1 = 0.50001270344348603, p_max = 2.9554800761609014e-39
Markov Test Estimate (bit string) = 0.999936 / 1 bit(s)
Bitstring Compression Estimate: X-bar = 5.2176714331187366, sigma-hat = 1.0152961906603262, p = 0.019654761320726077
Compression Test Estimate (bit string) = 0.944830 / 1 bit(s)
Running Tuple Estimates...
Bitstring t-Tuple Estimate: t = 23, p-hat_max = 0.52357011476148263, p_u = 0.52369763546518522
Bitstring LRS Estimate: u = 24, v = 50, p-hat = 0.50053161737274598, p_u = 0.50065927992920534
T-Tuple Test Estimate (bit string) = 0.933194 / 1 bit(s)
Literal t-Tuple Estimate: t = 11, p-hat_max = 0.27527598152543398, p_u = 0.27543726106146299
Literal LRS Estimate: u = 12, v = 24, p-hat = 0.25086994374062016, p_u = 0.25102647882990431
T-Tuple Test Estimate = 1.860204 / 2 bit(s)
LRS Test Estimate (bit string) = 0.998099 / 1 bit(s)
LRS Test Estimate = 1.994089 / 2 bit(s)
Running Predictor Estimates...
Bitstring MultiMCW Prediction Estimate: N = 101776225, Pglobal' = 0.50008960368099831 (C = 50884239) Plocal can't affect result (r = 26)
Multi Most Common in Window (MultiMCW) Prediction Test Estimate (bit string) = 0.999741 / 1 bit(s)
Literal MultiMCW Prediction Estimate: N = 50888081, Pglobal' = 0.25014573559900838 (C = 12721480) Plocal can't affect result (r = 12)
Multi Most Common in Window (MultiMCW) Prediction Test Estimate = 1.999159 / 2 bit(s)
Bitstring Lag Prediction Estimate: N = 101776287, Pglobal' = 0.50019269251081444 (C = 50894762) Plocal can't affect result (r = 25)
Lag Prediction Test Estimate (bit string) = 0.999444 / 1 bit(s)
Literal Lag Prediction Estimate: N = 50888143, Pglobal' = 0.25015172047634626 (C = 12721800) Plocal can't affect result (r = 13)
Lag Prediction Test Estimate = 1.999125 / 2 bit(s)
Bitstring MultiMMC Prediction Estimate: N = 101776286, Pglobal' = 0.50008456811129076 (C = 50883757) Plocal can't affect result (r = 27)
Multi Markov Model with Counting (MultiMMC) Prediction Test Estimate (bit string) = 0.999756 / 1 bit(s)
Literal MultiMMC Prediction Estimate: N = 50888142, Pglobal' = 0.2502104743048289 (C = 12724789) Plocal can't affect result (r = 13)
Multi Markov Model with Counting (MultiMMC) Prediction Test Estimate = 1.998786 / 2 bit(s)
Bitstring LZ78Y Prediction Estimate: N = 101776271, Pglobal' = 0.50008006313488451 (C = 50883291) Plocal can't affect result (r = 26)
LZ78Y Prediction Test Estimate (bit string) = 0.999769 / 1 bit(s)
Literal LZ78Y Prediction Estimate: N = 50888127, Pglobal' = 0.25021764352136133 (C = 12725150) Plocal can't affect result (r = 13)
LZ78Y Prediction Test Estimate = 1.998745 / 2 bit(s)
H_original: 1.860204
H_bitstring: 0.933194
min(H_original, 2 X H_bitstring): 1.860204
关于雅特力