From: Sergey Matveev Date: Tue, 29 Jun 2021 10:39:27 +0000 (+0300) Subject: Counter increment optimization X-Git-Tag: v7.0.0^2~6 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=116c4cf07862ff127054fca98b1fdac9451fb3a0;p=nncp.git Counter increment optimization --- diff --git a/src/pkt.go b/src/pkt.go index c2715e4..2049b63 100644 --- a/src/pkt.go +++ b/src/pkt.go @@ -128,6 +128,16 @@ func NewPkt(typ PktType, nice uint8, path []byte) (*Pkt, error) { return &pkt, nil } +func ctrIncr(b []byte) { + for i := len(b) - 1; i >= 0; i-- { + b[i]++ + if b[i] != 0 { + return + } + } + panic("counter overflow") +} + func aeadProcess( aead cipher.AEAD, nonce []byte, @@ -135,7 +145,6 @@ func aeadProcess( r io.Reader, w io.Writer, ) (int, error) { - var blkCtr uint64 ciphCtr := nonce[len(nonce)-8:] buf := make([]byte, EncBlkSize+aead.Overhead()) var toRead []byte @@ -159,8 +168,7 @@ func aeadProcess( } } readBytes += n - blkCtr++ - binary.BigEndian.PutUint64(ciphCtr, blkCtr) + ctrIncr(ciphCtr) if doEncrypt { toWrite = aead.Seal(buf[:0], nonce, buf[:n], nil) } else {