mirage / mirage-crypto

Cryptographic primitives for OCaml, in OCaml (also used in MirageOS)
ISC License
77 stars 43 forks source link

Use xor_into3 in CBC #206

Closed reynir closed 8 months ago

reynir commented 9 months ago

This adds xor_into3 which takes two input buffers and stores the xor in a destination buffer. Then in CBC this is used so as to not copy the cleartext buffer. I can observe a small speedup on aes-128-cbc of 0-10%. On my laptop it is mostly in the range of 2-5% while on a server with a Intel(R) Xeon(R) CPU E3-1275 v6 CPU with little load I see a more consistent ~8% speedup.

I tried to use this function in chacha20 and in CTR-mode, but it seemed to result in a slowdown.

I am not sure a small speedup is worth adding more code. Maybe xor_into could be implemented with xor_into3 but maybe the compiler can take advantage of knowing one of the sources is the same as the destination(?)

hannesm commented 8 months ago

so, what should we do here? is it worth to rebase on top of our string/bytes code?

reynir commented 8 months ago

Since this function only gave a small speedup in cbc mode and seemed to slow down ctr mode and chacha20 I'm not so convinced it's worth adding more C code only to use it in some places. Maybe things look different with string/bytes. I am not too motivated to look into it right now so I will close for now.