timing.erl 3.64 KB
Newer Older
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-module(timing).
-export([test/0]).

test() ->
    randombytes(),
    randombytes(),
    randombytes(),
    hash(),
    box_keypair(),
    box(),
    box_before_after(),
    sign_keypair(),
    sign(),
    secretbox(),
    stream(),
    auth(),
    onetime_auth(),
    scalarmult(),
    ok.

randombytes() ->
    randombytes(100*1000).
    
randombytes(0) -> ok;
randombytes(N) ->
    enacl:randombytes(1024),
    randombytes(N-1).

hash() ->
    B = binary:copy(<<0>>, 4096),
    hash(B, 10*1000).
    
hash(_B, 0) -> ok;
hash(B, N) ->
    enacl:hash(B),
    hash(B, N-1).

box_keypair() ->
    box_keypair(10*1000).
    
box_keypair(0) -> ok;
box_keypair(N) ->
    enacl:box_keypair(),
    box_keypair(N-1).

box() ->
    #{ public := PK1} = enacl:box_keypair(),
    #{ secret := SK2} = enacl:box_keypair(),
    B = binary:copy(<<0>>, 1),
    Nonce = binary:copy(<<0>>, enacl:box_nonce_size()),
    box(B, Nonce, PK1, SK2, 10*1000).
    
box(_B, _Nonce, _PK1, _SK2, 0) -> ok;
box(B, Nonce, PK1, SK2, N) ->
    enacl:box(B, Nonce, PK1, SK2),
    enacl:box_seal(B, PK1),
    box(B, Nonce, PK1, SK2, N-1).

box_before_after() ->
    #{ public := PK1 } = enacl:box_keypair(),
    #{ secret := SK2 } = enacl:box_keypair(),
    box_beforenm(PK1, SK2, 10*1000),
    R = enacl:box_beforenm(PK1, SK2),
    B = binary:copy(<<0>>, 8192),
    Nonce = binary:copy(<<0>>, enacl:box_nonce_size()),
    box_afternm(B, Nonce, R, 10*1000),
    ok.
    
box_beforenm(_PK, _SK, 0) -> ok;
box_beforenm(PK, SK, N) ->
    enacl:box_beforenm(PK, SK),
    box_beforenm(PK, SK, N-1).
    
box_afternm(_Msg, _Nonce, _Key, 0) -> ok;
box_afternm(Msg, Nonce, Key, N) ->
    enacl:box_afternm(Msg, Nonce, Key),
    box_afternm(Msg, Nonce, Key, N-1).

sign_keypair() ->
    sign_keypair(10*1000).
    
sign_keypair(0) -> ok;
sign_keypair(N) ->
    enacl:sign_keypair(),
    #{ public := PK, secret := SK} = enacl:crypto_sign_ed25519_keypair(),
    enacl:crypto_sign_ed25519_public_to_curve25519(PK),
    enacl:crypto_sign_ed25519_secret_to_curve25519(SK),
    sign_keypair(N-1).

sign() ->
    Msg = binary:copy(<<0>>, 1024),
    #{ secret := SK } = enacl:sign_keypair(),
    sign(Msg, SK, 10*1000).
    
sign(_Msg, _SK, 0) -> ok;
sign(Msg, SK, N) ->
    enacl:sign(Msg, SK),
    enacl:sign_detached(Msg, SK),
    sign(Msg, SK, N-1).

secretbox() ->
    Msg = binary:copy(<<0>>, 8192),
    Nonce = binary:copy(<<0>>, enacl:secretbox_nonce_size()),
    Key = binary:copy(<<0>>, enacl:secretbox_key_size()),
    secretbox(Msg, Nonce, Key, 10*1000).
    
secretbox(_Msg, _Nonce, _Key, 0) -> ok;
secretbox(Msg, Nonce, Key, N) ->
    enacl:secretbox(Msg, Nonce, Key),
    secretbox(Msg, Nonce, Key, N-1).


stream() ->
    stream(16384, binary:copy(<<0>>, enacl:stream_nonce_size()), binary:copy(<<0>>, enacl:stream_key_size()), 10*1000).
    
stream(_L, _Nonce, _K, 0) -> ok;
stream(L, Nonce, K, N) ->
    enacl:stream(L, Nonce, K),
    stream(L, Nonce, K, N-1).

auth() ->
    Msg = binary:copy(<<0>>, 4096),
    Key = binary:copy(<<0>>, enacl:auth_key_size()),
    auth(Msg, Key, 10*1000).
    
auth(_Msg, _Key, 0) -> ok;
auth(Msg, Key, N) ->
    enacl:auth(Msg, Key),
    auth(Msg, Key, N-1).
    
onetime_auth() ->
    Msg = binary:copy(<<0>>, 16384),
    Key = binary:copy(<<0>>, enacl:onetime_auth_key_size()),
    onetime_auth(Msg, Key, 10*1000).
    
onetime_auth(_Msg, _Key, 0) -> ok;
onetime_auth(Msg, Key, N) ->
    enacl:onetime_auth(Msg, Key),
    onetime_auth(Msg, Key, N-1).
    
scalarmult() ->
    Secret = binary:copy(<<0>>, 32),
    BasePoint = binary:copy(<<1>>, 32),
    scalarmult(Secret, BasePoint, 10*1000).
    
scalarmult(_S, _B, 0) -> ok;
scalarmult(S, B, N) ->
    enacl:curve25519_scalarmult(S, B),
    scalarmult(S, B, N-1).