您的位置:首页 > 其它

试试 intel rng 随机数生成器

2014-04-16 15:38 381 查看
今天看蒙特卡罗方法,看到对随机数的质量蛮强调的,搜索了一下随机数生成器,注意到intel的 drng,下载了guide 跟着实验了一下。

Bull Mountain Software Implementation Guide

下载地址 https://software.intel.com/file/37157
Intel® Digital Random Number Generator (DRNG) Software Implementation Guide

地址:https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide/

还可以下个paper参考下:

基于Intel_RNG的真随机数生成器研究

地址:http://download.csdn.net/detail/deltatang/7202395

顺便贴一下如何检测 服务器是intel芯片以及如何判断是否支持RNG的代码:

文件 get_cpuid_v1_lix64.s

.intel_syntax noprefix
.text
.global get_cpuid_info_v1

get_cpuid_info_v1:
mov r8, rdi # array addr
mov r9, rsi # leaf
mov r10, rdx # subleaf
push rax
push rbx
push rcx
push rdx
mov eax, r9d
mov ecx, r10d

cpuid

mov DWORD PTR [r8], eax
mov DWORD PTR [r8+4], ebx
mov DWORD PTR [r8+8], ecx
mov DWORD PTR [r8+12], edx
pop rdx
pop rcx
pop rbx
pop rax
ret 0

#get_cpuid_info_v1 ENDP
#_TEXT ENDS


文件:get_cpuid_v1_lix64.h

typedef struct {
unsigned int EAX;
unsigned int EBX;
unsigned int ECX;
unsigned int EDX;
} CPUIDinfo;

extern void get_cpuid_info_v1(CPUIDinfo *info, const unsigned int func, const unsigned int subfunc);


文件:rngchk.c

#include <stdio.h>
#include <stdlib.h>

#include "get_cpuid_v1_lix64.h"

void _CPUID(CPUIDinfo *info, const unsigned int func, const unsigned int subfunc)
{
get_cpuid_info_v1(info, func, subfunc);
}

typedef unsigned int DWORD;

int _rdrand_check_support()
{
CPUIDinfo info;
int got_intel_cpu=0;

_CPUID(&info, 0, 0);

if(memcmp((char *)(&info.EBX), "Genu", 4) == 0 &&
memcmp((char *)(&info.EDX), "ineI", 4) == 0 &&
memcmp((char *)(&info.ECX), "ntel", 4) == 0) {
got_intel_cpu = 1;
}

if (got_intel_cpu) {
_CPUID(&info, 1, 0);
if ((info.ECX & 0x40000000)==0x40000000) return 1;
} else
{
return 2;
}

return 0;
}

int main(int argc, char **argv) {
int ret = _rdrand_check_support();
printf("is not intel cpu?       [%c]\n", (ret == 2 ? 'Y' : 'N'));
printf("is intel rng supported? [%c]\n", (ret == 1 ? 'Y' : 'N'));
}


然后执行:

gcc -g -c get_cpuid_v1_lix64.s -o get_cpuid_v1_lix64.o
gcc rngchk.c get_cpuid_v1_lix64.o -o rngchk


得到 rngchk 执行结果:

[root@localhost rng]# ./rngchk
is not intel cpu?

is intel rng supported?


结果发现俺测试服务器不支持,好吧,先留着,以后再说吧:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: