  aead/xchacha20poly1305-ietf-npubbytes 24
  aead/xchacha20poly1305-ietf-abytes 16)
 
+(defn rand-bit-flip
+  "Randomly flip a bit in the byte array"
+  [^bytes bs]
+  (let [bs (aclone bs)
+        i  (rand-int (alength bs))
+        v  (aget bs i)]
+    (aset bs i (byte (bit-flip v (rand-int 7))))
+    bs))
+
 (def aead-chacha20poly1305-ietf-vector
   (comp v/hex-resource (partial str "vectors/aead/chacha20poly1305ietf/")))
 

        {:keys [c mac]} (aead/chacha20poly1305-encrypt-detached ptext ad nonce k)]
    (is (bb/bytes= ctext c))
    (is (bb/bytes= mtext mac))
    (is (bb/bytes= ptext (aead/chacha20poly1305-decrypt-detached ctext mac ad nonce k)))))

(def aead-xchacha20poly1305-ietf-vector
  (comp v/hex-resource (partial str "vectors/aead/xchacha20poly1305ietf/")))

        ad (aead-xchacha20poly1305-ietf-vector "ad")
        nonce (aead/new-xchacha20poly1305-ietf-nonce)
        {:keys [c mac]} (aead/xchacha20poly1305-ietf-encrypt-detached ptext ad nonce k)]
    (is (bb/bytes= ptext (aead/xchacha20poly1305-ietf-decrypt-detached c mac ad nonce k)))
    (is (thrown-with-msg?
         RuntimeException #"Ciphertext verification failed"
         (aead/xchacha20poly1305-ietf-decrypt-detached (rand-bit-flip c) mac ad nonce k)))))

(deftest xchacha20poly1305-ietf-keygen-test
  (let [ks (set (repeatedly 100 aead/xchacha20poly1305-ietf-keygen))]
    (is (= 100 (count ks)))
    (doseq [^ByteBuffer k ks]
      (is (= aead/xchacha20poly1305-ietf-keybytes (.limit k))))))
