forked from danaj/Math-Prime-Util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench-random-bytes.pl
103 lines (98 loc) · 4.76 KB
/
bench-random-bytes.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env perl
use strict;
use warnings;
use Math::Prime::Util;
use Math::Prime::Util::GMP;
use Math::Prime::Util::ChaCha;
use Math::Prime::Util::ISAAC;
use Bytes::Random::XS; # XS version calling Drand01()
use Bytes::Random; # Just a loop around CORE::rand!
use Bytes::Random::Secure;
use Math::Random::MTwist;
use Crypt::PRNG;
#use Crypt::Random;
#use Data::Entropy::Algorithms;
use Crypt::OpenSSL::Random; # note rand_bytes == rand_pseudo_bytes
use Rand::Urandom;
use Benchmark qw/:all/;
Math::Prime::Util::ISAAC::srand;
Math::Prime::Util::ChaCha::srand;
use Math::Random::ISAAC::PP; my $mripp=Math::Random::ISAAC::PP->new();
use Math::Random::ISAAC::XS; my $mrixs=Math::Random::ISAAC::XS->new();
# On a Macbook early 2015, the fastest XS methods can pump out over 1 GB/s.
# In theory we could see 4+ GB/s from a module.
my $trial = shift || -1;
if (0) {
print "# 8 random bytes\n";
cmpthese($trial,{
"MPU" => sub { Math::Prime::Util::random_bytes(8); },
"MPU::GMP" => sub { Math::Prime::Util::GMP::random_bytes(8); },
"BRXS" => sub { Bytes::Random::XS::random_bytes(8); },
"BR" => sub { Bytes::Random::random_bytes(8); },
"BRS" => sub { Bytes::Random::Secure::random_bytes(8); },
"MRMT" => sub { Math::Random::MTwist::_randstr(8); },
# "DEA" => sub { Data::Entropy::Algorithms::rand_bits(8*8); },
"Crypt::PRNG" => sub { Crypt::PRNG::random_bytes(8); },
"rand" => sub { pack('C*', map { int(rand 256) } 1..8); },
# "Crypt::Random" => sub { Crypt::Random::makerandom_octet(Length=>8,Strength=>0); },
});
}
if (0) {
print "# 256 random bytes\n";
cmpthese($trial,{
"MPU" => sub { Math::Prime::Util::random_bytes(256); },
"MPU X" => sub { Math::Prime::Util::GMP::random_bytes(256); },
"BRXS" => sub { Bytes::Random::XS::random_bytes(256); },
"PP MPU ISAAC" => sub { Math::Prime::Util::ISAAC::random_bytes(256); },
"PP MPU ChaCha" => sub { Math::Prime::Util::ChaCha::random_bytes(256); },
"PP MR ISAAC" => sub { pack("L*",map{$mripp->irand}1..64); },
"XS MR ISAAC" => sub { pack("L*",map{$mrixs->irand}1..64); },
"XS MR ISAAC2" => sub { my$s='';$s.=pack("L",$mrixs->irand)for 1..64;$s; },
"CryptX" => sub { Crypt::PRNG::random_bytes(256); },
"BRS" => sub { Bytes::Random::Secure::random_bytes(256); },
"MRMTwist" => sub { Math::Random::MTwist::_randstr(256); },
"rand" => sub { pack('L*', map { int(rand 4294967296) } 1..64); },
"DEA" => sub { Data::Entropy::Algorithms::rand_bits(8*256); },
"Crypt::Random" => sub { Crypt::Random::makerandom_octet(Length=>256,Strength=>0); },
"BR" => sub { Bytes::Random::random_bytes(256); },
});
}
if (0) {
print "# 16384 random bytes\n";
cmpthese($trial,{
"MPU" => sub { Math::Prime::Util::random_bytes(16384); },
"MPU X" => sub { Math::Prime::Util::xrandom_bytes(16384); },
#"MPU::GMP" => sub { Math::Prime::Util::GMP::random_bytes(16384); },
"BRXS" => sub { Bytes::Random::XS::random_bytes(16384); },
#"BR" => sub { Bytes::Random::random_bytes(16384); },
#"BRS" => sub { Bytes::Random::Secure::random_bytes(16384); },
#"MRMT" => sub { Math::Random::MTwist::_randstr(16384); },
# "DEA" => sub { Data::Entropy::Algorithms::rand_bits(8*16384); },
#"CryptX" => sub { Crypt::PRNG::random_bytes(16384); },
#"rand" => sub { pack('C*', map { int(rand 256) } 1..16384); },
# "Crypt::Random" => sub { Crypt::Random::makerandom_octet(Length=>16384,Strength=>0); },
});
}
if (1) {
print "# 64k random bytes\n";
cmpthese($trial,{
"entropy" => sub { Math::Prime::Util::entropy_bytes(64*1024); },
"MPU" => sub { Math::Prime::Util::random_bytes(64*1024); },
"MPU X" => sub { Math::Prime::Util::GMP::random_bytes(64*1024); },
"BRXS" => sub { Bytes::Random::XS::random_bytes(64*1024); },
"PP MPU ISAAC" => sub { Math::Prime::Util::ISAAC::random_bytes(64*1024); },
"PP MPU ChaCha" => sub { Math::Prime::Util::ChaCha::random_bytes(64*1024); },
"PP MR ISAAC" => sub { pack("L*",map{$mripp->irand}1..16384); },
"XS MR ISAAC" => sub { pack("L*",map{$mrixs->irand}1..16384); },
"XS MR ISAAC2" => sub { my$s='';$s.=pack("L",$mrixs->irand)for 1..16384;$s; },
"CryptX" => sub { Crypt::PRNG::random_bytes(64*1024); },
"BRS" => sub { Bytes::Random::Secure::random_bytes(64*1024); },
"MRMTwist" => sub { Math::Random::MTwist::_randstr(64*1024); },
"rand" => sub { pack('L*', map { int(rand 4294967296) } 1..16384); },
#"DEA" => sub { Data::Entropy::Algorithms::rand_bits(8*65536); },
#"Crypt::Random" => sub { Crypt::Random::makerandom_octet(Length=>65536,Strength=>0); },
"BR" => sub { Bytes::Random::random_bytes(64*1024); },
"OpenSSL" => sub { Crypt::OpenSSL::Random::random_bytes(64*1024); },
"Urandom" => sub { Rand::Urandom::rand_bytes(64*1024); },
});
}