]> Cypherpunks.ru repositories - nncp.git/commitdiff
Counter increment optimization
authorSergey Matveev <stargrave@stargrave.org>
Tue, 29 Jun 2021 10:39:27 +0000 (13:39 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 29 Jun 2021 19:00:24 +0000 (22:00 +0300)
src/pkt.go

index c2715e4928d585f7fb9541af01802f87d530f337..2049b6354fb8bf0210eb3e61558afe9a76ee9236 100644 (file)
@@ -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 {