diff options
author | Raimo Niskanen <[email protected]> | 2018-09-13 15:26:24 +0200 |
---|---|---|
committer | Raimo Niskanen <[email protected]> | 2018-09-13 15:26:24 +0200 |
commit | 0f79e3f3d95fd8f04e3893e50c9f27b9e04c2c7e (patch) | |
tree | 87acb66bf5106181443099db0c9ea6e1924125e8 /lib/stdlib | |
parent | 510048b8efd0a72706f3232f0842ee9828c63f79 (diff) | |
download | otp-0f79e3f3d95fd8f04e3893e50c9f27b9e04c2c7e.tar.gz otp-0f79e3f3d95fd8f04e3893e50c9f27b9e04c2c7e.tar.bz2 otp-0f79e3f3d95fd8f04e3893e50c9f27b9e04c2c7e.zip |
Use long period counter for crypto_aes
Conflicts:
lib/crypto/doc/src/crypto.xml
lib/crypto/src/crypto.erl
Diffstat (limited to 'lib/stdlib')
-rw-r--r-- | lib/stdlib/src/rand.erl | 18 | ||||
-rw-r--r-- | lib/stdlib/test/rand_SUITE.erl | 43 |
2 files changed, 46 insertions, 15 deletions
diff --git a/lib/stdlib/src/rand.erl b/lib/stdlib/src/rand.erl index fdf9709633..9854c778a1 100644 --- a/lib/stdlib/src/rand.erl +++ b/lib/stdlib/src/rand.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 2015-2017. All Rights Reserved. +%% Copyright Ericsson AB 2015-2018. All Rights Reserved. %% %% Licensed under the Apache License, Version 2.0 (the "License"); %% you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ %% Test, dev and internal -export([exro928_jump_2pow512/1, exro928_jump_2pow20/1, + exro928_seed/1, exro928_next/1, exro928_next_state/1, format_jumpconst58/1, seed58/2]). %% Debug @@ -948,6 +949,9 @@ exs1024_jump({L, RL}, AS, JL, J, N, TN) -> -opaque exro928_state() :: {list(uint58()), list(uint58())}. +-spec exro928_seed( + list(uint58()) | integer() | {integer(), integer(), integer()}) -> + exro928_state(). exro928_seed(L) when is_list(L) -> {seed58_nz(16, L), []}; exro928_seed(X) when is_integer(X) -> @@ -979,7 +983,15 @@ exro928ss_next({[S15,S0|Ss], Rs}) -> exro928ss_next({[S15], Rs}) -> exro928ss_next({[S15|lists:reverse(Rs)], []}). +-spec exro928_next(exro928_state()) -> {{uint58(),uint58()}, exro928_state()}. +exro928_next({[S15,S0|Ss], Rs}) -> + SR = exro928_next_state(Ss, Rs, S15, S0), + {{S15,S0}, SR}; +exro928_next({[S15], Rs}) -> + exro928_next({[S15|lists:reverse(Rs)], []}). + %% Just update the state +-spec exro928_next_state(exro928_state()) -> exro928_state(). exro928_next_state({[S15,S0|Ss], Rs}) -> exro928_next_state(Ss, Rs, S15, S0); exro928_next_state({[S15], Rs}) -> @@ -1013,6 +1025,7 @@ exro928ss_uniform(Range, {Alg, SR}) -> exro928_jump({Alg, SR}) -> {Alg,exro928_jump_2pow512(SR)}. +-spec exro928_jump_2pow512(exro928_state()) -> exro928_state(). exro928_jump_2pow512(SR) -> polyjump( SR, fun exro928_next_state/1, @@ -1026,6 +1039,7 @@ exro928_jump_2pow512(SR) -> 16#7B7C4CC049C536E, 16#431801F9DB3AF2C, 16#41A1504ACD83F24, 16#6C41DCF2F867D7F]). +-spec exro928_jump_2pow20(exro928_state()) -> exro928_state(). exro928_jump_2pow20(SR) -> polyjump( SR, fun exro928_next_state/1, @@ -1209,6 +1223,7 @@ seed_nz(N, [S|Ss], M, NZ) -> %% Splitmix seeders, lowest bits of SplitMix64, zeros skipped %% ===================================================================== +-spec seed58(non_neg_integer(), uint64()) -> list(uint58()). seed58(0, _X) -> []; seed58(N, X) -> @@ -1224,6 +1239,7 @@ seed58(X_0) -> {Z,X} end. +-spec seed64(non_neg_integer(), uint64()) -> list(uint64()). seed64(0, _X) -> []; seed64(N, X) -> diff --git a/lib/stdlib/test/rand_SUITE.erl b/lib/stdlib/test/rand_SUITE.erl index c46e370dd5..4cb1c0b13d 100644 --- a/lib/stdlib/test/rand_SUITE.erl +++ b/lib/stdlib/test/rand_SUITE.erl @@ -1101,7 +1101,7 @@ measure_1(RangeFun, Fun, Alg, TMark) -> crypto_aes -> {rand, crypto:rand_seed_alg( - {crypto_aes,crypto:strong_rand_bytes(256)})}; + crypto_aes, crypto:strong_rand_bytes(256))}; random -> {random, random:seed(os:timestamp()), get(random_seed)}; _ -> @@ -1250,20 +1250,31 @@ gen_jump_p3(_, _, Acc) -> lists:reverse(Acc). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% short_jump(Config) when is_list(Config) -> - State_0 = {#{bits := Bits},_} = rand:seed(exro928ss, 4711), + Seed = erlang:system_time(), + short_jump( + rand:seed_s(exro928ss, Seed), + fun ({Alg,AlgState}) -> + {Alg,rand:exro928_jump_2pow20(AlgState)} + end), + short_jump( + crypto:rand_seed_alg_s(crypto_aes, integer_to_list(Seed)), + fun ({Alg,AlgState}) -> + {Alg,crypto:rand_plugin_aes_jump_2pow20(AlgState)} + end), + ok. + +short_jump({#{bits := Bits},_} = State_0, Jump2Pow20) -> Range = 1 bsl Bits, + State_1 = repeat(7, Range, State_0), %% - State_1a = repeat(1 bsl 20, Range, State_0), - State_1b = exro928_jump_2pow20(State_0), - check(17, Range, State_1a, State_1b), + State_2a = repeat(1 bsl 20, Range, State_1), + State_2b = Jump2Pow20(State_1), + check(17, Range, State_2a, State_2b), %% - {_,State_2a} = rand:uniform_s(Range, State_1a), - State_3a = exro928_jump_2pow20(State_2a), - State_3b = repeat((1 bsl 20) + 1, Range, State_1b), - check(17, Range, State_3a, State_3b). - -exro928_jump_2pow20({Alg, AlgState}) -> - {Alg,rand:exro928_jump_2pow20(AlgState)}. + {_,State_3a} = rand:uniform_s(Range, State_2a), + State_4a = Jump2Pow20(State_3a), + State_4b = repeat((1 bsl 20) + 1, Range, State_2b), + check(17, Range, State_4a, State_4b). repeat(0, _Range, State) -> State; @@ -1275,8 +1286,12 @@ check(0, _Range, _StateA, _StateB) -> ok; check(N, Range, StateA, StateB) -> {V,NewStateA} = rand:uniform_s(Range, StateA), - {V,NewStateB} = rand:uniform_s(Range, StateB), - check(N - 1, Range, NewStateA, NewStateB). + case rand:uniform_s(Range, StateB) of + {V,NewStateB} -> + check(N - 1, Range, NewStateA, NewStateB); + {Wrong,_} -> + ct:fail({Wrong,neq,V,for,N}) + end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Data |