Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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#include <crypto/common.h>6#include <crypto/poly1305.h>8namespace poly1305_donna {10// Based on the public domain implementation by Andrew Moon11// poly1305-donna-32.h from https://github.com/floodyberry/poly1305-donna13void poly1305_init(poly1305_context *st, const unsigned char key[32]) noexcept {14/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */15st->r[0] = (ReadLE32(&key[ 0]) ) & 0x3ffffff;16st->r[1] = (ReadLE32(&key[ 3]) >> 2) & 0x3ffff03;17st->r[2] = (ReadLE32(&key[ 6]) >> 4) & 0x3ffc0ff;18st->r[3] = (ReadLE32(&key[ 9]) >> 6) & 0x3f03fff;19st->r[4] = (ReadLE32(&key[12]) >> 8) & 0x00fffff;21/* h = 0 */22st->h[0] = 0;23st->h[1] = 0;24st->h[2] = 0;25st->h[3] = 0;26st->h[4] = 0;28/* save pad for later */29st->pad[0] = ReadLE32(&key[16]);30st->pad[1] = ReadLE32(&key[20]);31st->pad[2] = ReadLE32(&key[24]);32st->pad[3] = ReadLE32(&key[28]);34st->leftover = 0;35st->final = 0;36}38static void poly1305_blocks(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept {39const uint32_t hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */40uint32_t r0,r1,r2,r3,r4;41uint32_t s1,s2,s3,s4;42uint32_t h0,h1,h2,h3,h4;43uint64_t d0,d1,d2,d3,d4;44uint32_t c;46r0 = st->r[0];47r1 = st->r[1];48r2 = st->r[2];49r3 = st->r[3];50r4 = st->r[4];52s1 = r1 * 5;53s2 = r2 * 5;54s3 = r3 * 5;55s4 = r4 * 5;57h0 = st->h[0];58h1 = st->h[1];59h2 = st->h[2];60h3 = st->h[3];61h4 = st->h[4];63while (bytes >= POLY1305_BLOCK_SIZE) {64/* h += m[i] */65h0 += (ReadLE32(m+ 0) ) & 0x3ffffff;66h1 += (ReadLE32(m+ 3) >> 2) & 0x3ffffff;67h2 += (ReadLE32(m+ 6) >> 4) & 0x3ffffff;68h3 += (ReadLE32(m+ 9) >> 6) & 0x3ffffff;69h4 += (ReadLE32(m+12) >> 8) | hibit;71/* h *= r */72d0 = ((uint64_t)h0 * r0) + ((uint64_t)h1 * s4) + ((uint64_t)h2 * s3) + ((uint64_t)h3 * s2) + ((uint64_t)h4 * s1);73d1 = ((uint64_t)h0 * r1) + ((uint64_t)h1 * r0) + ((uint64_t)h2 * s4) + ((uint64_t)h3 * s3) + ((uint64_t)h4 * s2);74d2 = ((uint64_t)h0 * r2) + ((uint64_t)h1 * r1) + ((uint64_t)h2 * r0) + ((uint64_t)h3 * s4) + ((uint64_t)h4 * s3);75d3 = ((uint64_t)h0 * r3) + ((uint64_t)h1 * r2) + ((uint64_t)h2 * r1) + ((uint64_t)h3 * r0) + ((uint64_t)h4 * s4);76d4 = ((uint64_t)h0 * r4) + ((uint64_t)h1 * r3) + ((uint64_t)h2 * r2) + ((uint64_t)h3 * r1) + ((uint64_t)h4 * r0);78/* (partial) h %= p */79c = (uint32_t)(d0 >> 26); h0 = (uint32_t)d0 & 0x3ffffff;80d1 += c; c = (uint32_t)(d1 >> 26); h1 = (uint32_t)d1 & 0x3ffffff;81d2 += c; c = (uint32_t)(d2 >> 26); h2 = (uint32_t)d2 & 0x3ffffff;82d3 += c; c = (uint32_t)(d3 >> 26); h3 = (uint32_t)d3 & 0x3ffffff;83d4 += c; c = (uint32_t)(d4 >> 26); h4 = (uint32_t)d4 & 0x3ffffff;84h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;85h1 += c;87m += POLY1305_BLOCK_SIZE;88bytes -= POLY1305_BLOCK_SIZE;89}91st->h[0] = h0;92st->h[1] = h1;93st->h[2] = h2;94st->h[3] = h3;95st->h[4] = h4;96}98void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept {99uint32_t h0,h1,h2,h3,h4,c;100uint32_t g0,g1,g2,g3,g4;101uint64_t f;102uint32_t mask;104/* process the remaining block */105if (st->leftover) {106size_t i = st->leftover;107st->buffer[i++] = 1;108for (; i < POLY1305_BLOCK_SIZE; i++) {109st->buffer[i] = 0;110}111st->final = 1;112poly1305_blocks(st, st->buffer, POLY1305_BLOCK_SIZE);113}115/* fully carry h */116h0 = st->h[0];117h1 = st->h[1];118h2 = st->h[2];119h3 = st->h[3];120h4 = st->h[4];122c = h1 >> 26; h1 = h1 & 0x3ffffff;123h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;124h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;125h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;126h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;127h1 += c;129/* compute h + -p */130g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;131g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;132g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;133g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;134g4 = h4 + c - (1UL << 26);136/* select h if h < p, or h + -p if h >= p */137mask = (g4 >> ((sizeof(uint32_t) * 8) - 1)) - 1;138g0 &= mask;139g1 &= mask;140g2 &= mask;141g3 &= mask;142g4 &= mask;143mask = ~mask;144h0 = (h0 & mask) | g0;145h1 = (h1 & mask) | g1;146h2 = (h2 & mask) | g2;147h3 = (h3 & mask) | g3;148h4 = (h4 & mask) | g4;150/* h = h % (2^128) */151h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;152h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;153h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;154h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;156/* mac = (h + pad) % (2^128) */157f = (uint64_t)h0 + st->pad[0] ; h0 = (uint32_t)f;158f = (uint64_t)h1 + st->pad[1] + (f >> 32); h1 = (uint32_t)f;159f = (uint64_t)h2 + st->pad[2] + (f >> 32); h2 = (uint32_t)f;160f = (uint64_t)h3 + st->pad[3] + (f >> 32); h3 = (uint32_t)f;162WriteLE32(mac + 0, h0);163WriteLE32(mac + 4, h1);164WriteLE32(mac + 8, h2);165WriteLE32(mac + 12, h3);167/* zero out the state */168st->h[0] = 0;169st->h[1] = 0;170st->h[2] = 0;171st->h[3] = 0;172st->h[4] = 0;173st->r[0] = 0;174st->r[1] = 0;175st->r[2] = 0;176st->r[3] = 0;177st->r[4] = 0;178st->pad[0] = 0;179st->pad[1] = 0;180st->pad[2] = 0;181st->pad[3] = 0;182}184void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept {185size_t i;187/* handle leftover */188if (st->leftover) {189size_t want = (POLY1305_BLOCK_SIZE - st->leftover);190if (want > bytes) {191want = bytes;192}193for (i = 0; i < want; i++) {194st->buffer[st->leftover + i] = m[i];195}196bytes -= want;197m += want;198st->leftover += want;199if (st->leftover < POLY1305_BLOCK_SIZE) return;200poly1305_blocks(st, st->buffer, POLY1305_BLOCK_SIZE);201st->leftover = 0;202}204/* process full blocks */205if (bytes >= POLY1305_BLOCK_SIZE) {206size_t want = (bytes & ~(POLY1305_BLOCK_SIZE - 1));207poly1305_blocks(st, m, want);208m += want;209bytes -= want;210}212/* store leftover */213if (bytes) {214for (i = 0; i < bytes; i++) {215st->buffer[st->leftover + i] = m[i];216}217st->leftover += bytes;218}219}221} // namespace poly1305_donna
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.