Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_CRYPTO_POLY1305_H6#define BITCOIN_CRYPTO_POLY1305_H8#include <span.h>10#include <cassert>11#include <cstddef>12#include <cstdint>13#include <span>15#define POLY1305_BLOCK_SIZE 1617namespace poly1305_donna {19// Based on the public domain implementation by Andrew Moon20// poly1305-donna-32.h from https://github.com/floodyberry/poly1305-donna22typedef struct {23uint32_t r[5];24uint32_t h[5];25uint32_t pad[4];26size_t leftover;27unsigned char buffer[POLY1305_BLOCK_SIZE];28unsigned char final;29} poly1305_context;31void poly1305_init(poly1305_context *st, const unsigned char key[32]) noexcept;32void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept;33void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept;35} // namespace poly1305_donna37/** C++ wrapper with std::byte span interface around poly1305_donna code. */38class Poly130539{40poly1305_donna::poly1305_context m_ctx;42public:43/** Length of the output produced by Finalize(). */44static constexpr unsigned TAGLEN{16};46/** Length of the keys expected by the constructor. */47static constexpr unsigned KEYLEN{32};49/** Construct a Poly1305 object with a given 32-byte key. */50Poly1305(std::span<const std::byte> key) noexcept51{52assert(key.size() == KEYLEN);53poly1305_donna::poly1305_init(&m_ctx, UCharCast(key.data()));54}56/** Process message bytes. */57Poly1305& Update(std::span<const std::byte> msg) noexcept58{59poly1305_donna::poly1305_update(&m_ctx, UCharCast(msg.data()), msg.size());60return *this;61}63/** Write authentication tag to 16-byte out. */64void Finalize(std::span<std::byte> out) noexcept65{66assert(out.size() == TAGLEN);67poly1305_donna::poly1305_finish(&m_ctx, UCharCast(out.data()));68}69};71#endif // BITCOIN_CRYPTO_POLY1305_H