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) 2014-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/ripemd160.h>7#include <crypto/common.h>9#include <cstring>11// Internal implementation code.12namespace13{14/// Internal RIPEMD-160 implementation.15namespace ripemd16016{17uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }18uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); }19uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; }20uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); }21uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); }23/** Initialize RIPEMD-160 state. */24void inline Initialize(uint32_t* s)25{26s[0] = 0x67452301ul;27s[1] = 0xEFCDAB89ul;28s[2] = 0x98BADCFEul;29s[3] = 0x10325476ul;30s[4] = 0xC3D2E1F0ul;31}33uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); }35void inline Round(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r)36{37a = rol(a + f + x + k, r) + e;38c = rol(c, 10);39}41void inline R11(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, c, d), x, 0, r); }42void inline R21(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r); }43void inline R31(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r); }44void inline R41(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r); }45void inline R51(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r); }47void inline R12(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r); }48void inline R22(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r); }49void inline R32(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r); }50void inline R42(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r); }51void inline R52(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, c, d), x, 0, r); }53/** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */54void Transform(uint32_t* s, const unsigned char* chunk)55{56uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];57uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;58uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4), w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12);59uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20), w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28);60uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36), w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44);61uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52), w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60);63R11(a1, b1, c1, d1, e1, w0, 11);64R12(a2, b2, c2, d2, e2, w5, 8);65R11(e1, a1, b1, c1, d1, w1, 14);66R12(e2, a2, b2, c2, d2, w14, 9);67R11(d1, e1, a1, b1, c1, w2, 15);68R12(d2, e2, a2, b2, c2, w7, 9);69R11(c1, d1, e1, a1, b1, w3, 12);70R12(c2, d2, e2, a2, b2, w0, 11);71R11(b1, c1, d1, e1, a1, w4, 5);72R12(b2, c2, d2, e2, a2, w9, 13);73R11(a1, b1, c1, d1, e1, w5, 8);74R12(a2, b2, c2, d2, e2, w2, 15);75R11(e1, a1, b1, c1, d1, w6, 7);76R12(e2, a2, b2, c2, d2, w11, 15);77R11(d1, e1, a1, b1, c1, w7, 9);78R12(d2, e2, a2, b2, c2, w4, 5);79R11(c1, d1, e1, a1, b1, w8, 11);80R12(c2, d2, e2, a2, b2, w13, 7);81R11(b1, c1, d1, e1, a1, w9, 13);82R12(b2, c2, d2, e2, a2, w6, 7);83R11(a1, b1, c1, d1, e1, w10, 14);84R12(a2, b2, c2, d2, e2, w15, 8);85R11(e1, a1, b1, c1, d1, w11, 15);86R12(e2, a2, b2, c2, d2, w8, 11);87R11(d1, e1, a1, b1, c1, w12, 6);88R12(d2, e2, a2, b2, c2, w1, 14);89R11(c1, d1, e1, a1, b1, w13, 7);90R12(c2, d2, e2, a2, b2, w10, 14);91R11(b1, c1, d1, e1, a1, w14, 9);92R12(b2, c2, d2, e2, a2, w3, 12);93R11(a1, b1, c1, d1, e1, w15, 8);94R12(a2, b2, c2, d2, e2, w12, 6);96R21(e1, a1, b1, c1, d1, w7, 7);97R22(e2, a2, b2, c2, d2, w6, 9);98R21(d1, e1, a1, b1, c1, w4, 6);99R22(d2, e2, a2, b2, c2, w11, 13);100R21(c1, d1, e1, a1, b1, w13, 8);101R22(c2, d2, e2, a2, b2, w3, 15);102R21(b1, c1, d1, e1, a1, w1, 13);103R22(b2, c2, d2, e2, a2, w7, 7);104R21(a1, b1, c1, d1, e1, w10, 11);105R22(a2, b2, c2, d2, e2, w0, 12);106R21(e1, a1, b1, c1, d1, w6, 9);107R22(e2, a2, b2, c2, d2, w13, 8);108R21(d1, e1, a1, b1, c1, w15, 7);109R22(d2, e2, a2, b2, c2, w5, 9);110R21(c1, d1, e1, a1, b1, w3, 15);111R22(c2, d2, e2, a2, b2, w10, 11);112R21(b1, c1, d1, e1, a1, w12, 7);113R22(b2, c2, d2, e2, a2, w14, 7);114R21(a1, b1, c1, d1, e1, w0, 12);115R22(a2, b2, c2, d2, e2, w15, 7);116R21(e1, a1, b1, c1, d1, w9, 15);117R22(e2, a2, b2, c2, d2, w8, 12);118R21(d1, e1, a1, b1, c1, w5, 9);119R22(d2, e2, a2, b2, c2, w12, 7);120R21(c1, d1, e1, a1, b1, w2, 11);121R22(c2, d2, e2, a2, b2, w4, 6);122R21(b1, c1, d1, e1, a1, w14, 7);123R22(b2, c2, d2, e2, a2, w9, 15);124R21(a1, b1, c1, d1, e1, w11, 13);125R22(a2, b2, c2, d2, e2, w1, 13);126R21(e1, a1, b1, c1, d1, w8, 12);127R22(e2, a2, b2, c2, d2, w2, 11);129R31(d1, e1, a1, b1, c1, w3, 11);130R32(d2, e2, a2, b2, c2, w15, 9);131R31(c1, d1, e1, a1, b1, w10, 13);132R32(c2, d2, e2, a2, b2, w5, 7);133R31(b1, c1, d1, e1, a1, w14, 6);134R32(b2, c2, d2, e2, a2, w1, 15);135R31(a1, b1, c1, d1, e1, w4, 7);136R32(a2, b2, c2, d2, e2, w3, 11);137R31(e1, a1, b1, c1, d1, w9, 14);138R32(e2, a2, b2, c2, d2, w7, 8);139R31(d1, e1, a1, b1, c1, w15, 9);140R32(d2, e2, a2, b2, c2, w14, 6);141R31(c1, d1, e1, a1, b1, w8, 13);142R32(c2, d2, e2, a2, b2, w6, 6);143R31(b1, c1, d1, e1, a1, w1, 15);144R32(b2, c2, d2, e2, a2, w9, 14);145R31(a1, b1, c1, d1, e1, w2, 14);146R32(a2, b2, c2, d2, e2, w11, 12);147R31(e1, a1, b1, c1, d1, w7, 8);148R32(e2, a2, b2, c2, d2, w8, 13);149R31(d1, e1, a1, b1, c1, w0, 13);150R32(d2, e2, a2, b2, c2, w12, 5);151R31(c1, d1, e1, a1, b1, w6, 6);152R32(c2, d2, e2, a2, b2, w2, 14);153R31(b1, c1, d1, e1, a1, w13, 5);154R32(b2, c2, d2, e2, a2, w10, 13);155R31(a1, b1, c1, d1, e1, w11, 12);156R32(a2, b2, c2, d2, e2, w0, 13);157R31(e1, a1, b1, c1, d1, w5, 7);158R32(e2, a2, b2, c2, d2, w4, 7);159R31(d1, e1, a1, b1, c1, w12, 5);160R32(d2, e2, a2, b2, c2, w13, 5);162R41(c1, d1, e1, a1, b1, w1, 11);163R42(c2, d2, e2, a2, b2, w8, 15);164R41(b1, c1, d1, e1, a1, w9, 12);165R42(b2, c2, d2, e2, a2, w6, 5);166R41(a1, b1, c1, d1, e1, w11, 14);167R42(a2, b2, c2, d2, e2, w4, 8);168R41(e1, a1, b1, c1, d1, w10, 15);169R42(e2, a2, b2, c2, d2, w1, 11);170R41(d1, e1, a1, b1, c1, w0, 14);171R42(d2, e2, a2, b2, c2, w3, 14);172R41(c1, d1, e1, a1, b1, w8, 15);173R42(c2, d2, e2, a2, b2, w11, 14);174R41(b1, c1, d1, e1, a1, w12, 9);175R42(b2, c2, d2, e2, a2, w15, 6);176R41(a1, b1, c1, d1, e1, w4, 8);177R42(a2, b2, c2, d2, e2, w0, 14);178R41(e1, a1, b1, c1, d1, w13, 9);179R42(e2, a2, b2, c2, d2, w5, 6);180R41(d1, e1, a1, b1, c1, w3, 14);181R42(d2, e2, a2, b2, c2, w12, 9);182R41(c1, d1, e1, a1, b1, w7, 5);183R42(c2, d2, e2, a2, b2, w2, 12);184R41(b1, c1, d1, e1, a1, w15, 6);185R42(b2, c2, d2, e2, a2, w13, 9);186R41(a1, b1, c1, d1, e1, w14, 8);187R42(a2, b2, c2, d2, e2, w9, 12);188R41(e1, a1, b1, c1, d1, w5, 6);189R42(e2, a2, b2, c2, d2, w7, 5);190R41(d1, e1, a1, b1, c1, w6, 5);191R42(d2, e2, a2, b2, c2, w10, 15);192R41(c1, d1, e1, a1, b1, w2, 12);193R42(c2, d2, e2, a2, b2, w14, 8);195R51(b1, c1, d1, e1, a1, w4, 9);196R52(b2, c2, d2, e2, a2, w12, 8);197R51(a1, b1, c1, d1, e1, w0, 15);198R52(a2, b2, c2, d2, e2, w15, 5);199R51(e1, a1, b1, c1, d1, w5, 5);200R52(e2, a2, b2, c2, d2, w10, 12);201R51(d1, e1, a1, b1, c1, w9, 11);202R52(d2, e2, a2, b2, c2, w4, 9);203R51(c1, d1, e1, a1, b1, w7, 6);204R52(c2, d2, e2, a2, b2, w1, 12);205R51(b1, c1, d1, e1, a1, w12, 8);206R52(b2, c2, d2, e2, a2, w5, 5);207R51(a1, b1, c1, d1, e1, w2, 13);208R52(a2, b2, c2, d2, e2, w8, 14);209R51(e1, a1, b1, c1, d1, w10, 12);210R52(e2, a2, b2, c2, d2, w7, 6);211R51(d1, e1, a1, b1, c1, w14, 5);212R52(d2, e2, a2, b2, c2, w6, 8);213R51(c1, d1, e1, a1, b1, w1, 12);214R52(c2, d2, e2, a2, b2, w2, 13);215R51(b1, c1, d1, e1, a1, w3, 13);216R52(b2, c2, d2, e2, a2, w13, 6);217R51(a1, b1, c1, d1, e1, w8, 14);218R52(a2, b2, c2, d2, e2, w14, 5);219R51(e1, a1, b1, c1, d1, w11, 11);220R52(e2, a2, b2, c2, d2, w0, 15);221R51(d1, e1, a1, b1, c1, w6, 8);222R52(d2, e2, a2, b2, c2, w3, 13);223R51(c1, d1, e1, a1, b1, w15, 5);224R52(c2, d2, e2, a2, b2, w9, 11);225R51(b1, c1, d1, e1, a1, w13, 6);226R52(b2, c2, d2, e2, a2, w11, 11);228uint32_t t = s[0];229s[0] = s[1] + c1 + d2;230s[1] = s[2] + d1 + e2;231s[2] = s[3] + e1 + a2;232s[3] = s[4] + a1 + b2;233s[4] = t + b1 + c2;234}236} // namespace ripemd160238} // namespace240////// RIPEMD160242CRIPEMD160::CRIPEMD160()243{244ripemd160::Initialize(s);245}247CRIPEMD160& CRIPEMD160::Write(const unsigned char* data, size_t len)248{249const unsigned char* end = data + len;250size_t bufsize = bytes % 64;
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.