Skip to content

nonce byte array allocations #30

@nbrownus

Description

@nbrownus

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions