-
Notifications
You must be signed in to change notification settings - Fork 68
Open
Description
nonce preparation in Encrypt and Decrypt causes an allocation. In an application encrypting/decrypting at 200k+ times a second this becomes rather noticeable in the garbage collector.
One way I've been thinking of working around this is to provide a function similar to this:
func (c aeadCipher) EncryptNB(out []byte, n uint64, ad, plaintext []byte, nb []byte) []byte {
nb[0] = 0
nb[1] = 0
nb[2] = 0
nb[3] = 0
binary.BigEndian.PutUint64(nb[4:], n)
return c.Seal(out, nb, plaintext, ad)
}
Obviously there are a handful of issues here, is the byte array big enough, too big, being used by another thread? Adding (some of) those checks would still likely be less overhead though.
I can bring crypto into my part of the program, but this feels bad:
nb[0] = 0
nb[1] = 0
nb[2] = 0
nb[3] = 0
binary.BigEndian.PutUint64(nb[4:], n)
return s.c.(cipher.AEAD).Open(out, nb, ciphertext, ad)
Other thoughts would be to internalize a heap for storing nonce byte arrays.
Wanted to get your thoughts on the matter before I sent any PRs your way, thanks!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels