NoNoncense (NoNoncense v1.0.2)

View Source

Generate locally unique nonces (number-only-used-once) in distributed Elixir.

Nonces are unique values that are generated once and never repeated within your system. They have many practical uses including:

  • ID Generation: Create unique identifiers for database records, API requests, or any other resource in distributed systems
  • Cryptographic Operations: Serve as initialization vectors (IVs) for encryption algorithms, ensuring security in block cipher modes
  • Deduplication: Identify and prevent duplicate operations or messages in distributed systems

Locally unique means that the nonces are unique within your application/database/domain, as opposed to globally unique nonces that are unique across applications/databases/domains, like UUIDs.

Read the migration guide

If you're upgrading from v0.x.x and you use encrypted nonces, please read the Migration Guide carefully - there are breaking changes that require attention to preserve uniqueness guarantees.

Nonce types

Several types of nonces can be generated, although they share their basic composition. The first 42 bits are a millisecond-precision timestamp (allows for ~139 years of operation), relative to the NoNoncense epoch (2025-01-01 00:00:00 UTC) by default. The next 9 bits are the machine ID (allows for 512 machines). The remaining bits are a per-machine counter.

Counter nonces

  • Features: unique.
  • Generation rate: very high.
  • Info leak: medium (machine init time, creation order).
  • Crypto: technically suitable for block ciphers in modes that require a nonce that is unique but not necessarily unpredictable (like CTR, OFB, CCM, and GCM), and some streaming ciphers. Only when some info leak is acceptable.

Counter nonces are basically a counter that is initialized with the machine (node) start time. An overflow of a nonce's counter bits will trigger a timestamp increase by 1ms, implying that the timestamp effectively functions as an extended counter. Because the timestamp can't exceed the actual time (that would break the uniqueness guarantee), new nonce generation throttles if the timestamp catches up to the actual time.

That means that the maximum sustained rate is 8M/s per machine for 64-bits nonces (which have 13 counter bits). In practice it is unlikely that nonces are generated at such an extreme sustained rate, and the timestamp will lag behind the actual time. This creates "saved up seconds" that can be used to burst to even higher rates. For example, if the first nonce is generated 10 seconds after initialization, 10K milliseconds have been "saved up" to generate 80M nonces as quickly as hardware will allow. Benchmarking shows rates in the tens of millions per second are attainable this way.

96/128 bits counter nonces have such large counters that they can be generated at a practically unlimited sustained rate of >= 2^45 nonces per ms per machine, meaning they will never catch the actual time, and the practical rate is only limited by hardware.

Sortable nonces (Snowflake IDs)

  • Features: unique, time-sortable.
  • Generation rate: high.
  • Info leak: high (creation time, creation order).
  • Crypto: not recommended. They leak more info than counter nonces but are slightly slower to generate.

Sortable nonces have an accurate creation timestamp (as opposed to counter nonces). This makes them equivalent to Snowflake IDs, apart from the slightly altered bit distribution of NoNoncense nonces (42 instead of 41 timestamp bits, 9 instead of 10 ID bits, no unused bit).

This has some implications. Again, 96/128-bits sortable nonces can be generated as quickly as your hardware can go. However, the 64-bits variant can be generated at 8M/s per machine and can't ever burst beyond that (the "saved-up-seconds" mechanic of counter nonces does not apply here). This should of course be plenty for most applications.

Encrypted nonces

  • Features: unique, unpredictable.
  • Generation rate: medium (scales well with CPU cores).
  • Info leak: none.
  • Crypto: same as counter nonces, but no info leaks. Additionally, suitable for block cipher modes that require unpredictable IVs, like CBC and CFB.

These nonces are encrypted in a way that preserves their uniqueness, but they are unpredictable and don't leak information. For more info, see nonce encryption.

Don't change the key or cipher

Once you are using a cipher and a key, you must never change them. Doing so breaks the uniqueness guarantees of all encrypted nonces of the affected NoNoncense instance. The only way to change the key or the cipher is by regenerating / invalidating all previously generated encrypted nonces.

Usage

Note that NoNoncense is not a GenServer. Instead it stores its initial state using :persistent_term and its internal counter using :atomics. Because :persistent_term triggers a garbage collection cycle on writes, it is recommended to initialize your NoNoncense instance(s) at application start, when there is hardly any garbage to collect.

# lib/my_app/application.ex
# generate a machine ID, start conflict guard and initialize a NoNoncense instance
defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    machine_id = NoNoncense.MachineId.id!(node_list: [:"myapp@127.0.0.1"])
    # base_key is required for encrypted nonces
    :ok = NoNoncense.init(machine_id: machine_id, base_key: System.get_env("BASE_KEY"))

    children =
      [
        # optional but recommended
        {NoNoncense.MachineId.ConflictGuard, [machine_id: machine_id]}
      ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Then you can generate nonces.

# generate counter nonces
iex> <<_::64>> = NoNoncense.nonce(64)
iex> <<_::96>> = NoNoncense.nonce(96)
iex> <<_::128>> = NoNoncense.nonce(128)

# generate sortable nonces
iex> <<_::64>> = NoNoncense.sortable_nonce(64)
iex> <<_::96>> = NoNoncense.sortable_nonce(96)
iex> <<_::128>> = NoNoncense.sortable_nonce(128)

# generate encrypted nonces
# be sure to read the NoNoncense docs before using 64/96 bits encrypted nonces
iex> <<_::64>> = NoNoncense.encrypted_nonce(64)
iex> <<_::96>> = NoNoncense.encrypted_nonce(96)
iex> <<_::128>> = NoNoncense.encrypted_nonce(128)

Uniqueness guarantees

Nonces are guaranteed to be unique if:

  • Machine IDs are unique for each node (NoNoncense.MachineId and NoNoncense.MachineId.ConflictGuard can help there).
  • Individual machines maintain a somewhat accurate clock (specifically, the UTC clock has to have progressed between node restarts).
  • (Sortable nonces only) the machine clock has to be accurate.

Nonce encryption

By encrypting a nonce, the timestamp, machine ID and message ordering information leak can be prevented. However, we wish to encrypt in a way that maintains the uniqueness guarantee of the input counter nonce. So 2^64 unique inputs should generate 2^64 unique outputs, same for the other sizes.

IETF has some wisdom to share on the topic of nonce encryption (in the context of ChaCha20 / Poly1305 nonces):

Counters and LFSRs are both acceptable ways of generating unique nonces, as is encrypting a counter using a block cipher with a 64-bit block size such as DES. Note that it is not acceptable to use a truncation of a counter encrypted with block ciphers with 128-bit or 256-bit blocks, because such a truncation may repeat after a short time.

There are some interesting things to unpick there. Why can't we use higher ciphers with a larger block size? As it turns out, block ciphers only generate unique outputs for inputs of at least their block size (128 bits for most modern ciphers, notably AES). For example, encrypting a 64-bit nonce with AES would produce a unique 128-bit ciphertext, but that ciphertext can't be reduced back to 64 bits without losing the uniqueness property. Sadly, this also holds for the streaming modes of these ciphers, which still use blocks internally to generate the keystream. That means we can just use AES256 ECB (we only encrypt unique blocks) for 128-bit nonces.

128-bit encrypted nonces

We have AES256-encrypted 128-bit nonces that are unique and indistinguishable from random noise.

For 64/96 bits nonces we need a block cipher that operates on matching block sizes, which are exceedingly rare. One such cipher is Speck, designed by the NSA in 2013 for lightweight encryption. The optional dependency SpeckEx, backed by (precompiled) Rust crate speck_cipher, enables support for it. It is very fast; in line with hardware-accelerated AES. Be aware that SpeckEx should be considered experimental right now; it has not been reviewed or audited; although the primitive block cipher mode used by NoNoncense matches official test vectors.

If you only want to use OTP ciphers, we are limited to DES, 3DES, and BlowFish. DES is broken and can merely be considered obfuscation at this point, despite the IETF quote (from 2018). 3DES is slow (it is still offered for backwards compatibility). Blowfish performs well after initial key expansion, and is secure since we don't have to worry about the birthday attack (all of our input blocks are unique, so all of our output blocks are unique, so there will be no collisions).

For 96-bit nonces there are no block ciphers whatsoever to choose from in OTP. All we can do is generate a 64-bits encrypted nonce and postfix 32 zero-bits. That way the whole nonce is unique, despite the predictable tail. You should determine for yourself if you can live with that. The only other option, and the main reason it was added, is using SpeckEx, because Speck has a 96-bits variant that can encrypt a full 96-bits counter nonce, without needing any padding.

64/96-bit encrypted nonces

We have either Speck, Blowfish or 3DES encrypted nonces. Speck offers the best security and performance, but is experimental right now. Of the OTP ciphers, the default Blowfish is fast and secure. For 96-bits nonces, using OTP's Blowfish or 3DES results in a padded 64-bits encrypted nonce, which may or may not be good enough for your use case. If it is not, your only option is using Speck.

Summary

Functions

Generate a new counter nonce and encrypt it. This creates an unpredictable but still unique nonce.

Get the timestamp of the nonce as a DateTime, given the epoch of the instance. This should only be used for sortable_nonce/2 nonces.

Initialize a nonce factory. Multiple instances with different names, epochs and even machine IDs are supported.

Generate a new 64/96/128-bits counter-like nonce.

Generate a nonce that is sortable by generation time, like a Snowflake ID. The first 42 bits contain the timestamp.

Types

init_opt()

@type init_opt() ::
  {:epoch, non_neg_integer()}
  | {:name, atom()}
  | {:machine_id, non_neg_integer()}
  | {:base_key, binary()}
  | {:key64, binary()}
  | {:key96, binary()}
  | {:key128, binary()}
  | {:cipher64, :blowfish | :des3 | :speck}
  | {:cipher96, :blowfish | :des3 | :speck}
  | {:cipher128, :aes | :speck}
  • :machine_id (required) - machine ID of the node
  • :name - The name of the nonce factory (default: module name).
  • :epoch - Override the configured epoch for this factory instance. Defaults to the NoNoncense epoch (2025-01-01 00:00:00Z).
  • :base_key - A key of at least 256 bits (32 bytes) used to derive encryption keys for all nonce sizes.
  • :key64 - Override the derived key for 64-bit nonces.
  • :key96 - Override the derived key for 96-bit nonces.
  • :key128 - Override the derived key for 128-bit nonces.
  • :cipher64 - The cipher for 64-bit nonces (:blowfish, :speck, or :des3). Defaults to :blowfish.
  • :cipher96 - The cipher for 96-bit nonces (:blowfish, :speck, or :des3). Defaults to :blowfish.
  • :cipher128 - The cipher for 128-bit nonces (:aes or :speck). Defaults to :aes.

The encryption-related options only affect encrypted_nonce/2 nonces.

nonce()

@type nonce() :: <<_::64>> | <<_::96>> | <<_::128>>

nonce_size()

@type nonce_size() :: 64 | 96 | 128

Functions

encrypted_nonce(name \\ __MODULE__, bit_size)

@spec encrypted_nonce(atom(), nonce_size()) :: nonce()

Generate a new counter nonce and encrypt it. This creates an unpredictable but still unique nonce.

For more info, see nonce encryption.

iex> NoNoncense.init(machine_id: 1, base_key: :crypto.strong_rand_bytes(32))
:ok
iex> NoNoncense.encrypted_nonce(64)
<<50, 231, 215, 98, 233, 96, 157, 205>>
iex> NoNoncense.encrypted_nonce(96)
<<6, 138, 218, 96, 131, 136, 51, 242, 0, 0, 0, 0>>
iex> NoNoncense.encrypted_nonce(128)
<<162, 10, 94, 4, 91, 56, 147, 198, 46, 87, 142, 197, 128, 41, 79, 209>>

get_datetime(name \\ __MODULE__, nonce)

@spec get_datetime(atom(), nonce()) :: DateTime.t()

Get the timestamp of the nonce as a DateTime, given the epoch of the instance. This should only be used for sortable_nonce/2 nonces.

init(opts \\ [])

@spec init([init_opt()]) :: :ok

Initialize a nonce factory. Multiple instances with different names, epochs and even machine IDs are supported.

Options

  • :machine_id (required) - machine ID of the node
  • :name - The name of the nonce factory (default: module name).
  • :epoch - Override the configured epoch for this factory instance. Defaults to the NoNoncense epoch (2025-01-01 00:00:00Z).
  • :base_key - A key of at least 256 bits (32 bytes) used to derive encryption keys for all nonce sizes.
  • :key64 - Override the derived key for 64-bit nonces.
  • :key96 - Override the derived key for 96-bit nonces.
  • :key128 - Override the derived key for 128-bit nonces.
  • :cipher64 - The cipher for 64-bit nonces (:blowfish, :speck, or :des3). Defaults to :blowfish.
  • :cipher96 - The cipher for 96-bit nonces (:blowfish, :speck, or :des3). Defaults to :blowfish.
  • :cipher128 - The cipher for 128-bit nonces (:aes or :speck). Defaults to :aes.

The encryption-related options only affect encrypted_nonce/2 nonces.

Examples

iex> NoNoncense.init(machine_id: 1)
:ok

iex> NoNoncense.init(machine_id: 1, name: :custom, epoch: 1609459200000)
:ok

nonce(name \\ __MODULE__, bit_size)

@spec nonce(atom(), nonce_size()) :: nonce()

Generate a new 64/96/128-bits counter-like nonce.

Examples

iex> nonce(64)
<<101, 6, 25, 181, 192, 128, 32, 17>>

iex> nonce(96)
<<101, 6, 25, 181, 192, 128, 32, 0, 0, 0, 0, 18>>

iex> nonce(128)
<<101, 6, 25, 181, 192, 128, 32, 0, 0, 0, 0, 0, 0, 0, 0, 19>>

sortable_nonce(name \\ __MODULE__, bit_size)

@spec sortable_nonce(atom(), nonce_size()) :: nonce()

Generate a nonce that is sortable by generation time, like a Snowflake ID. The first 42 bits contain the timestamp.

These nonces are not suitable for cryptographic purposes because they are predictable and leak their generation timestamp.

Examples

iex> NoNoncense.sortable_nonce(64)
<<0, 15, 27, 213, 143, 128, 0, 0>>
iex> NoNoncense.sortable_nonce(96)
<<0, 15, 27, 215, 172, 0, 0, 0, 0, 0, 0, 0>>
iex> NoNoncense.sortable_nonce(128)
<<0, 15, 27, 217, 161, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>

# the generation time can be extracted
iex> <<ts::42, _::22>> = <<0, 15, 27, 213, 143, 128, 0, 0>>
iex> epoch = ~U[2025-01-01T00:00:00Z] |> DateTime.to_unix(:millisecond)
iex> DateTime.from_unix!(ts + epoch, :millisecond)
~U[2025-01-12 17:38:49.534Z]