packages feed

secp256k1-haskell-3.1.1: src/Crypto/Secp256k1/EllSwift.hs

-- | ElligatorSwift encoding of public keys and the x-only Diffie-Hellman
-- exchange of BIP 324, through the @ellswift@ module of
-- <https://github.com/bitcoin-core/secp256k1 libsecp256k1>.
--
-- == What it is for
--
-- A public key on the wire is easy to spot: an x coordinate is a field
-- element for which a square root exists, and a compressed key starts with 2
-- or 3. A censor that looks for such bytes can tell an encrypted Bitcoin
-- connection from other traffic. ElligatorSwift (Chávez-Saab,
-- Rodríguez-Henríquez and Tibouchi, <https://eprint.iacr.org/2022/759>)
-- encodes a point as 64 bytes that are indistinguishable from uniformly
-- random bytes. The encoding is not unique: it is chosen at random among the
-- many that decode to the point. BIP 324, the encrypted transport of the
-- Bitcoin network, sends each side's key this way.
--
-- 'ellSwiftCreate' makes the encoded public key of a secret key,
-- 'ellSwiftEncode' encodes a public key one already has, and
-- 'ellSwiftDecode' gives back the point. 'ellSwiftXdhBip324' is the
-- Diffie-Hellman exchange of BIP 324 on two encodings, which is all a
-- transport needs: it never has to decode a key.
--
-- == What it does not do
--
-- The encoding hides that a key is a key; it does not say whose key it is.
-- A Diffie-Hellman exchange on keys that nobody vouches for is
-- unauthenticated: a man in the middle can run one exchange with each side.
-- BIP 324 accepts that and says so; it only makes passive observation
-- impossible and active interception detectable afterwards, by comparing the
-- session ids out of band.
--
-- == The ellswift module of libsecp256k1
--
-- The module is in libsecp256k1 from version 0.4.0 on, and the build turns
-- it on by default. Ubuntu 24.04 ships 0.2.0, which does not have it; Fedora,
-- Arch, Homebrew, Nixpkgs and Ubuntu 26.04 ship versions that do. This
-- package binds it when its @ellswift@ flag is on, which is the default,
-- and then asks pkg-config for libsecp256k1 0.4.0 or later: with an older
-- library the build stops at the configure step and names the version it
-- found. Build the package with @-f -ellswift@ then, and this module is not
-- available. A library of 0.4.0 or later built without the module (it is
-- on by default) fails later, at the link, with an undefined reference to
-- @secp256k1_ellswift_create@.
--
-- == Values
--
-- t'EllSwift' holds the 64 bytes of an encoding, which are standard and
-- portable, unlike the @get@ field of a 'Crypto.Secp256k1.PubKey'. Any 64
-- bytes are valid, so 'ellSwift' only checks the length. The raw constructor
-- is in "Crypto.Secp256k1.Internal.EllSwift", which has no API stability
-- guarantee.
module Crypto.Secp256k1.EllSwift
  ( -- * Encodings
    EllSwift (get),
    ellSwift,
    ellSwiftCreate,
    ellSwiftCreateIO,
    ellSwiftEncode,
    ellSwiftDecode,

    -- * Diffie-Hellman exchange of BIP 324
    Party (..),
    ellSwiftXdhBip324,
  )
where

import Crypto.Secp256k1.Internal.EllSwift