Skip to content

Commit cceef45

Browse files
committed
Streamline naming
More changes before 1.0 with a more direct naming scheme.
1 parent 014d50c commit cceef45

File tree

5 files changed

+139
-122
lines changed

5 files changed

+139
-122
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
Pretty large change, but OTOH, this ought to happen before a 1.0 release as well.
1212
- Generichashes must support the finalized state
1313

14-
- Implement missing EQC tests
15-
- stream_chacha20...
16-
- stream_xor...
17-
1814
## [Unreleased]
1915

2016
### Compatibility

bench/timing.erl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test() ->
2020

2121
randombytes() ->
2222
randombytes(100*1000).
23-
23+
2424
randombytes(0) -> ok;
2525
randombytes(N) ->
2626
enacl:randombytes(1024),
@@ -29,15 +29,15 @@ randombytes(N) ->
2929
hash() ->
3030
B = binary:copy(<<0>>, 4096),
3131
hash(B, 10*1000).
32-
32+
3333
hash(_B, 0) -> ok;
3434
hash(B, N) ->
3535
enacl:hash(B),
3636
hash(B, N-1).
3737

3838
box_keypair() ->
3939
box_keypair(10*1000).
40-
40+
4141
box_keypair(0) -> ok;
4242
box_keypair(N) ->
4343
enacl:box_keypair(),
@@ -47,9 +47,9 @@ box() ->
4747
#{ public := PK1} = enacl:box_keypair(),
4848
#{ secret := SK2} = enacl:box_keypair(),
4949
B = binary:copy(<<0>>, 1),
50-
Nonce = binary:copy(<<0>>, enacl:box_nonce_size()),
50+
Nonce = binary:copy(<<0>>, enacl:box_NONCEBYTES()()),
5151
box(B, Nonce, PK1, SK2, 10*1000).
52-
52+
5353
box(_B, _Nonce, _PK1, _SK2, 0) -> ok;
5454
box(B, Nonce, PK1, SK2, N) ->
5555
enacl:box(B, Nonce, PK1, SK2),
@@ -62,23 +62,23 @@ box_before_after() ->
6262
box_beforenm(PK1, SK2, 10*1000),
6363
R = enacl:box_beforenm(PK1, SK2),
6464
B = binary:copy(<<0>>, 8192),
65-
Nonce = binary:copy(<<0>>, enacl:box_nonce_size()),
65+
Nonce = binary:copy(<<0>>, enacl:box_NONCEBYTES()()),
6666
box_afternm(B, Nonce, R, 10*1000),
6767
ok.
68-
68+
6969
box_beforenm(_PK, _SK, 0) -> ok;
7070
box_beforenm(PK, SK, N) ->
7171
enacl:box_beforenm(PK, SK),
7272
box_beforenm(PK, SK, N-1).
73-
73+
7474
box_afternm(_Msg, _Nonce, _Key, 0) -> ok;
7575
box_afternm(Msg, Nonce, Key, N) ->
7676
enacl:box_afternm(Msg, Nonce, Key),
7777
box_afternm(Msg, Nonce, Key, N-1).
7878

7979
sign_keypair() ->
8080
sign_keypair(10*1000).
81-
81+
8282
sign_keypair(0) -> ok;
8383
sign_keypair(N) ->
8484
enacl:sign_keypair(),
@@ -91,7 +91,7 @@ sign() ->
9191
Msg = binary:copy(<<0>>, 1024),
9292
#{ secret := SK } = enacl:sign_keypair(),
9393
sign(Msg, SK, 10*1000).
94-
94+
9595
sign(_Msg, _SK, 0) -> ok;
9696
sign(Msg, SK, N) ->
9797
enacl:sign(Msg, SK),
@@ -100,51 +100,51 @@ sign(Msg, SK, N) ->
100100

101101
secretbox() ->
102102
Msg = binary:copy(<<0>>, 8192),
103-
Nonce = binary:copy(<<0>>, enacl:secretbox_nonce_size()),
104-
Key = binary:copy(<<0>>, enacl:secretbox_key_size()),
103+
Nonce = binary:copy(<<0>>, enacl:secretbox_NONCEBYTES()()),
104+
Key = binary:copy(<<0>>, enacl:secretbox_KEYBYTES()),
105105
secretbox(Msg, Nonce, Key, 10*1000).
106-
106+
107107
secretbox(_Msg, _Nonce, _Key, 0) -> ok;
108108
secretbox(Msg, Nonce, Key, N) ->
109109
enacl:secretbox(Msg, Nonce, Key),
110110
secretbox(Msg, Nonce, Key, N-1).
111111

112112

113113
stream() ->
114-
stream(16384, binary:copy(<<0>>, enacl:stream_nonce_size()), binary:copy(<<0>>, enacl:stream_key_size()), 10*1000).
115-
114+
stream(16384, binary:copy(<<0>>, enacl:stream_NONCEBYTES()), binary:copy(<<0>>, enacl:stream_KEYBYTES()), 10*1000).
115+
116116
stream(_L, _Nonce, _K, 0) -> ok;
117117
stream(L, Nonce, K, N) ->
118118
enacl:stream(L, Nonce, K),
119119
stream(L, Nonce, K, N-1).
120120

121121
auth() ->
122122
Msg = binary:copy(<<0>>, 4096),
123-
Key = binary:copy(<<0>>, enacl:auth_key_size()),
123+
Key = binary:copy(<<0>>, enacl:auth_KEYBYTES()),
124124
auth(Msg, Key, 10*1000).
125-
125+
126126
auth(_Msg, _Key, 0) -> ok;
127127
auth(Msg, Key, N) ->
128128
enacl:auth(Msg, Key),
129129
auth(Msg, Key, N-1).
130-
130+
131131
onetime_auth() ->
132132
Msg = binary:copy(<<0>>, 16384),
133-
Key = binary:copy(<<0>>, enacl:onetime_auth_key_size()),
133+
Key = binary:copy(<<0>>, enacl:onetime_auth_KEYBYTES()),
134134
onetime_auth(Msg, Key, 10*1000).
135-
135+
136136
onetime_auth(_Msg, _Key, 0) -> ok;
137137
onetime_auth(Msg, Key, N) ->
138138
enacl:onetime_auth(Msg, Key),
139139
onetime_auth(Msg, Key, N-1).
140-
140+
141141
scalarmult() ->
142142
Secret = binary:copy(<<0>>, 32),
143143
BasePoint = binary:copy(<<1>>, 32),
144144
scalarmult(Secret, BasePoint, 10*1000).
145-
145+
146146
scalarmult(_S, _B, 0) -> ok;
147147
scalarmult(S, B, N) ->
148148
enacl:curve25519_scalarmult(S, B),
149149
scalarmult(S, B, N-1).
150-
150+

eqc_test/enacl_eqc.erl

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ v_binary(_, _) -> false.
8383

8484

8585
%% Typical generators based on the binaries
86-
nonce() -> g_binary(enacl:box_nonce_size()).
87-
nonce_valid(N) -> v_binary(enacl:box_nonce_size(), N).
86+
nonce() -> g_binary(enacl:box_NONCEBYTES()).
87+
nonce_valid(N) -> v_binary(enacl:box_NONCEBYTES(), N).
8888

8989
%% Generator of natural numbers
9090
g_nat() ->
@@ -111,10 +111,10 @@ keypair_bad() ->
111111
#{ public := PK, secret := SK} = enacl:box_keypair(),
112112
case X of
113113
pk ->
114-
PKBytes = enacl:box_public_key_bytes(),
114+
PKBytes = enacl:box_PUBLICKEYBYTES(),
115115
{oneof([return(a), nat(), ?SUCHTHAT(B, binary(), byte_size(B) /= PKBytes)]), SK};
116116
sk ->
117-
SKBytes = enacl:box_secret_key_bytes(),
117+
SKBytes = enacl:box_SECRETKEYBYTES(),
118118
{PK, oneof([return(a), nat(), ?SUCHTHAT(B, binary(), byte_size(B) /= SKBytes)])}
119119
end
120120
end).
@@ -159,8 +159,8 @@ g_generichash_size() ->
159159
%% * box_afternm/3
160160
%% * box_open_afternm/3
161161
keypair_valid(PK, SK) when is_binary(PK), is_binary(SK) ->
162-
PKBytes = enacl:box_public_key_bytes(),
163-
SKBytes = enacl:box_secret_key_bytes(),
162+
PKBytes = enacl:box_PUBLICKEYBYTES(),
163+
SKBytes = enacl:box_SECRETKEYBYTES(),
164164
byte_size(PK) == PKBytes andalso byte_size(SK) == SKBytes;
165165
keypair_valid(_PK, _SK) -> false.
166166

@@ -264,11 +264,11 @@ beforenm_key() ->
264264
oneof([
265265
elements([a,b,c]),
266266
real(),
267-
?SUCHTHAT(X, binary(), byte_size(X) /= enacl:box_beforenm_bytes())
267+
?SUCHTHAT(X, binary(), byte_size(X) /= enacl:box_BEFORENMBYTES())
268268
])
269269
end).
270270

271-
v_key(K) when is_binary(K) -> byte_size(K) == enacl:box_beforenm_bytes();
271+
v_key(K) when is_binary(K) -> byte_size(K) == enacl:box_BEFORENMBYTES();
272272
v_key(_) -> false.
273273

274274
prop_beforenm_correct() ->
@@ -324,11 +324,11 @@ sign_keypair_bad() ->
324324
KP = enacl:sign_keypair(),
325325
case X of
326326
pk ->
327-
Sz = enacl:sign_keypair_public_size(),
327+
Sz = enacl:sign_PUBLICBYTES(),
328328
?LET(Wrong, oneof([a, int(), ?SUCHTHAT(B, binary(), byte_size(B) /= Sz)]),
329329
KP#{ public := Wrong });
330330
sk ->
331-
Sz = enacl:sign_keypair_secret_size(),
331+
Sz = enacl:sign_SECRETBYTES(),
332332
?LET(Wrong, oneof([a, int(), ?SUCHTHAT(B, binary(), byte_size(B) /= Sz)]),
333333
KP#{ secret := Wrong })
334334
end
@@ -342,12 +342,12 @@ sign_keypair() ->
342342

343343
sign_keypair_public_valid(#{ public := Public })
344344
when is_binary(Public) ->
345-
byte_size(Public) == enacl:sign_keypair_public_size();
345+
byte_size(Public) == enacl:sign_PUBLICBYTES();
346346
sign_keypair_public_valid(_) -> false.
347347

348348
sign_keypair_secret_valid(#{ secret := Secret })
349349
when is_binary(Secret) ->
350-
byte_size(Secret) == enacl:sign_keypair_secret_size();
350+
byte_size(Secret) == enacl:sign_SECRETBYTES();
351351
sign_keypair_secret_valid(_) -> false.
352352

353353
sign_keypair_valid(KP) ->
@@ -408,11 +408,11 @@ signed_message_good_d(M) ->
408408
end)}]).
409409

410410
signed_message_bad() ->
411-
Sz = enacl:sign_keypair_public_size(),
411+
Sz = enacl:sign_PUBLICBYTES(),
412412
{binary(), oneof([a, int(), ?SUCHTHAT(B, binary(Sz), byte_size(B) /= Sz)])}.
413413

414414
signed_message_bad_d() ->
415-
Sz = enacl:sign_keypair_public_size(),
415+
Sz = enacl:sign_PUBLICBYTES(),
416416
{binary(), oneof([a, int(), ?SUCHTHAT(B, binary(Sz), byte_size(B) /= Sz)])}.
417417

418418
signed_message(M) ->
@@ -496,19 +496,19 @@ prop_seal_box_correct() ->
496496
%% * secretbox/3
497497
%% * secretbo_open/3
498498
secret_key_good() ->
499-
Sz = enacl:secretbox_key_size(),
499+
Sz = enacl:secretbox_KEYBYTES(),
500500
binary(Sz).
501501

502502
secret_key_bad() ->
503503
oneof([return(a),
504504
nat(),
505-
?SUCHTHAT(B, binary(), byte_size(B) /= enacl:secretbox_key_size())]).
505+
?SUCHTHAT(B, binary(), byte_size(B) /= enacl:secretbox_KEYBYTES())]).
506506

507507
secret_key() ->
508508
?FAULT(secret_key_bad(), secret_key_good()).
509509

510510
secret_key_valid(SK) when is_binary(SK) ->
511-
Sz = enacl:secretbox_key_size(),
511+
Sz = enacl:secretbox_KEYBYTES(),
512512
byte_size(SK) == Sz;
513513
secret_key_valid(_SK) -> false.
514514

@@ -618,6 +618,27 @@ xor_bytes(<<A, As/binary>>, <<B, Bs/binary>>) ->
618618
[A bxor B | xor_bytes(As, Bs)];
619619
xor_bytes(<<>>, <<>>) -> [].
620620

621+
positive() ->
622+
?LET(N, nat(), N+1).
623+
624+
chacha20_nonce() ->
625+
Sz = enacl:stream_chacha20_NONCEBYTES(),
626+
binary(Sz).
627+
628+
chacha20_key() ->
629+
Sz = enacl:stream_chacha20_KEYBYTES(),
630+
binary(Sz).
631+
632+
prop_stream_chacha20_correct() ->
633+
?FORALL(Len, positive(),
634+
?FORALL({Msg, Nonce, Key}, {binary(Len), chacha20_nonce(), chacha20_key()},
635+
begin
636+
CT = enacl:stream_chacha20_xor(Msg, Nonce, Key),
637+
Stream = enacl:stream_chacha20(Len, Nonce, Key),
638+
CT2 = list_to_binary(xor_bytes(Stream, Msg)),
639+
equals(CT, CT2)
640+
end)).
641+
621642
%% CRYPTO AUTH
622643
%% ------------------------------------------------------------
623644
%% * auth/2
@@ -635,19 +656,19 @@ prop_auth_correct() ->
635656
end).
636657

637658
authenticator_bad() ->
638-
oneof([a, int(), ?SUCHTHAT(X, binary(), byte_size(X) /= enacl:auth_size())]).
659+
oneof([a, int(), ?SUCHTHAT(X, binary(), byte_size(X) /= enacl:auth_BYTES())]).
639660

640661
authenticator_good(Msg, Key) when is_binary(Key) ->
641-
Sz = enacl:secretbox_key_size(),
662+
Sz = enacl:secretbox_KEYBYTES(),
642663
case v_iodata(Msg) andalso byte_size(Key) == Sz of
643664
true ->
644-
frequency([{1, ?LAZY({invalid, binary(enacl:auth_size())})},
665+
frequency([{1, ?LAZY({invalid, binary(enacl:auth_BYTES())})},
645666
{3, return({valid, enacl:auth(Msg, Key)})}]);
646667
false ->
647-
binary(enacl:auth_size())
668+
binary(enacl:auth_BYTES())
648669
end;
649670
authenticator_good(_Msg, _Key) ->
650-
binary(enacl:auth_size()).
671+
binary(enacl:auth_BYTES()).
651672

652673
authenticator(Msg, Key) ->
653674
?FAULT(authenticator_bad(), authenticator_good(Msg, Key)).
@@ -690,19 +711,19 @@ prop_onetimeauth_correct() ->
690711
end).
691712

692713
ot_authenticator_bad() ->
693-
oneof([a, int(), ?SUCHTHAT(X, binary(), byte_size(X) /= enacl:onetime_auth_size())]).
714+
oneof([a, int(), ?SUCHTHAT(X, binary(), byte_size(X) /= enacl:onetime_auth_BYTES())]).
694715

695716
ot_authenticator_good(Msg, Key) when is_binary(Key) ->
696-
Sz = enacl:secretbox_key_size(),
717+
Sz = enacl:secretbox_KEYBYTES(),
697718
case v_iodata(Msg) andalso byte_size(Key) == Sz of
698719
true ->
699-
frequency([{1, ?LAZY({invalid, binary(enacl:onetime_auth_size())})},
720+
frequency([{1, ?LAZY({invalid, binary(enacl:onetime_auth_BYTES())})},
700721
{3, return({valid, enacl:onetime_auth(Msg, Key)})}]);
701722
false ->
702-
binary(enacl:onetime_auth_size())
723+
binary(enacl:onetime_auth_BYTES())
703724
end;
704725
ot_authenticator_good(_Msg, _Key) ->
705-
binary(enacl:auth_size()).
726+
binary(enacl:auth_BYTES()).
706727

707728
ot_authenticator(Msg, Key) ->
708729
?FAULT(ot_authenticator_bad(), ot_authenticator_good(Msg, Key)).

0 commit comments

Comments
 (0)