Skip to content

Commit

Permalink
Allocate less in Fortuna (#188)
Browse files Browse the repository at this point in the history
* Fortuna.add: don't allocate a 2 byte cstruct on each call

Instead, use a temporary buffer. Contradicts #186

* minor fix
  • Loading branch information
hannesm authored Feb 3, 2024
1 parent 8e8f836 commit 24dff0d
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions rng/fortuna.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ let generate ~g bytes =
chunk (generate_rekey ~g n' :: acc) (n - n') in
Cstruct.concat @@ chunk [] bytes

let _buf = Cstruct.create_unsafe 2

let add ~g (source, _) ~pool data =
let pool = pool land (pools - 1)
and source = source land 0xff in
let header = Cs.of_bytes [ source ; Cstruct.length data ] in
g.pools.(pool) <- SHAd256.feedi g.pools.(pool) (iter2 header data);
if pool = 0 then g.pool0_size <- g.pool0_size + Cstruct.length data
let pool = pool land (pools - 1)
and source = source land 0xff in
Cstruct.set_uint8 _buf 0 source;
Cstruct.set_uint8 _buf 1 (Cstruct.length data);
g.pools.(pool) <- SHAd256.feedi g.pools.(pool) (iter2 _buf data);
if pool = 0 then g.pool0_size <- g.pool0_size + Cstruct.length data

(* XXX
* Schneier recommends against using generator-imposed pool-seeding schedule
Expand Down

0 comments on commit 24dff0d

Please sign in to comment.