<commit_before>(ns caesium.crypto.sign
  (:refer-clojure :exclude [bytes])
  (:require [caesium.binding :refer [defconsts sodium]]))

(defconsts [bytes seedbytes publickeybytes secretkeybytes primitive])

(defn generate-signing-keys
  "Generate a public-key and secret-key for signing with
  `crypto_sign_ed25519_seed_keypair`. If a seed is not provided, one
  is taken from `randombytes`.

  A map of the secret seed and public-key is returned."
  ([]
   (let [pk (byte-array publickeybytes)
         sk (byte-array secretkeybytes)]
     (.crypto_sign_keypair sodium pk sk)
     {:public pk
      :secret sk}))
  ([seed]
   (let [pk (byte-array publickeybytes)
         sk (byte-array secretkeybytes)]
     (.crypto_sign_seed_keypair sodium pk sk seed)
     {:public pk
      :secret sk})))

(defn signed-to-buf!
  "Puts a signed version of the given message using given secret key into the
  given out buffer."
  [out sk m]
  (let [mlen (alength ^bytes m)]
    (.crypto_sign sodium out nil m mlen sk)
    out))

(defn signed
  "Produces a signed version of the given message m using given secret key."
  [sk m]
  (let [sm (byte-array (+ bytes (alength ^bytes m)))]
    (signed-to-buf! sm sk m)))

(defn sign-to-buf!
  "Puts a signature of the given message using given secret key into the given
  out buffer."
  [out sk m]
  (let [mlen (alength ^bytes m)]
    (.crypto_sign_detached sodium out nil m mlen sk)
    out))

(defn sign
  "Produces a detached signature for a message m using given secret key."
  [sk m]
  (let [sig (byte-array bytes)]
    (sign-to-buf! sig sk m)))

(defn verify
  "Verify a signed message or a message and a detached signature.

  When given a valid signed message, returns the unsigned
  message. When given a valid signature, returns nil. When given an
  invalid signed message or signature, raises RuntimeException."
  ([pk sm]
   (let [smlen (alength ^bytes sm)
         m (byte-array (- smlen bytes))
         res (.crypto_sign_open sodium m nil sm smlen pk)]
     (if (zero? res)
       m
       (throw (RuntimeException. "Signature validation failed")))))
  ([pk msg sig]
   (let [mlen (alength ^bytes msg)
         res (.crypto_sign_verify_detached sodium sig msg mlen pk)]
     (when-not (zero? res)
       (throw (RuntimeException. "Signature validation failed"))))))<commit_msg>Use new buflen protocol<commit_after>(ns caesium.crypto.sign
  (:refer-clojure :exclude [bytes])
  (:require [caesium.binding :refer [defconsts sodium]]
            [caesium.byte-bufs :refer [buflen]]))

(defconsts [bytes seedbytes publickeybytes secretkeybytes primitive])

(defn generate-signing-keys
  "Generate a public-key and secret-key for signing with
  `crypto_sign_ed25519_seed_keypair`. If a seed is not provided, one
  is taken from `randombytes`.

  A map of the secret seed and public-key is returned."
  ([]
   (let [pk (byte-array publickeybytes)
         sk (byte-array secretkeybytes)]
     (.crypto_sign_keypair sodium pk sk)
     {:public pk
      :secret sk}))
  ([seed]
   (let [pk (byte-array publickeybytes)
         sk (byte-array secretkeybytes)]
     (.crypto_sign_seed_keypair sodium pk sk seed)
     {:public pk
      :secret sk})))

(defn signed-to-buf!
  "Puts a signed version of the given message using given secret key into the
  given out buffer."
  [out sk m]
  (.crypto_sign sodium out nil m (buflen m) sk)
  out)

(defn signed
  "Produces a signed version of the given message m using given secret key."
  [sk m]
  (let [sm (byte-array (+ bytes (buflen m)))]
    (signed-to-buf! sm sk m)))

(defn sign-to-buf!
  "Puts a signature of the given message using given secret key into the given
  out buffer."
  [out sk m]
  (.crypto_sign_detached sodium out nil m (buflen m) sk)
  out)

(defn sign
  "Produces a detached signature for a message m using given secret key."
  [sk m]
  (let [sig (byte-array bytes)]
    (sign-to-buf! sig sk m)))

(defn verify
  "Verify a signed message or a message and a detached signature.

  When given a valid signed message, returns the unsigned
  message. When given a valid signature, returns nil. When given an
  invalid signed message or signature, raises RuntimeException."
  ([pk sm]
   (let [smlen (buflen sm)
         m (byte-array (- smlen bytes))
         res (.crypto_sign_open sodium m nil sm smlen pk)]
     (if (zero? res)
       m
       (throw (RuntimeException. "Signature validation failed")))))
  ([pk msg sig]
   (let [mlen (buflen msg)
         res (.crypto_sign_verify_detached sodium sig msg mlen pk)]
     (when-not (zero? res)
       (throw (RuntimeException. "Signature validation failed"))))))
{'fraction_non_alphanumeric': 0.10286458333333333, 'fraction_numerical': 0.002170138888888889, 'mean_word_length': 3.2129798903107862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3367679', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1460, 'n_words': 578}