SPB Git

Repository Public

RIPEMD160.swift 6.8 KB 167 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraSymmetric/RIPEMD160.swift8// Module           : Qyvora Symmetric9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import QyvoraMath4243/// RIPEMD-160 (Dobbertin, Bosselaers, Preneel, 1996; ISO/IEC 10118-3). Used by Bitcoin addresses (HASH160).44public struct RIPEMD160: HashFunction {45    public static let blockSize = 6446    public static let digestSize = 204748    var h: [UInt32] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]49    var buffer = Bytes()50    var length: UInt64 = 05152    public init() { buffer.reserveCapacity(64) }5354    public mutating func update(_ data: Bytes) {55        length &+= UInt64(data.count)56        var offset = 057        if !buffer.isEmpty {58            let take = min(64 - buffer.count, data.count)59            buffer.append(contentsOf: data[0..<take])60            offset = take61            if buffer.count == 64 {62                buffer.withUnsafeBufferPointer { compress($0.baseAddress!) }63                buffer.removeAll(keepingCapacity: true)64            }65        }66        data.withUnsafeBufferPointer { d in67            while data.count - offset >= 64 {68                compress(d.baseAddress! + offset)69                offset += 6470            }71        }72        if offset < data.count { buffer.append(contentsOf: data[offset...]) }73    }7475    public mutating func finalize() -> Bytes {76        let bitLength = length &* 877        var tail = buffer78        tail.append(0x80)79        while tail.count % 64 != 56 { tail.append(0) }80        tail.append(contentsOf: Endian.le64(bitLength))81        tail.withUnsafeBufferPointer { t in82            var o = 083            while o < t.count {84                compress(t.baseAddress! + o)85                o += 6486            }87        }88        var out = Bytes(repeating: 0, count: 20)89        for i in 0..<20 { out[i] = UInt8(truncatingIfNeeded: h[i >> 2] >> UInt32(8 * (i & 3))) }90        return out91    }9293    // Message word order and rotation amounts of the left and right lines.94    private static let rl: [UInt8] = [95        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,96        7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,97        3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,98        1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,99        4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13,100    ]101    private static let rr: [UInt8] = [102        5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,103        6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,104        15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,105        8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,106        12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11,107    ]108    private static let sl: [UInt8] = [109        11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,110        7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,111        11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,112        11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,113        9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6,114    ]115    private static let sr: [UInt8] = [116        8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,117        9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,118        9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,119        15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,120        8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11,121    ]122    private static let kl: [UInt32] = [0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e]123    private static let kr: [UInt32] = [0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000]124125    @inline(__always) private static func rotl(_ x: UInt32, _ n: UInt32) -> UInt32 { (x << n) | (x >> (32 &- n)) }126127    /// The five round functions; the right line uses them in reverse order.128    @inline(__always) private static func f(_ round: Int, _ x: UInt32, _ y: UInt32, _ z: UInt32) -> UInt32 {129        switch round {130        case 0: return x ^ y ^ z131        case 1: return (x & y) | (~x & z)132        case 2: return (x | ~y) ^ z133        case 3: return (x & z) | (y & ~z)134        default: return x ^ (y | ~z)135        }136    }137138    private mutating func compress(_ block: UnsafePointer<UInt8>) {139        withUnsafeTemporaryAllocation(of: UInt32.self, capacity: 16) { x in140            for i in 0..<16 { x[i] = Endian.loadLE32(block + 4 * i) }141            var al = h[0], bl = h[1], cl = h[2], dl = h[3], el = h[4]142            var ar = h[0], br = h[1], cr = h[2], dr = h[3], er = h[4]143            for j in 0..<80 {144                let round = j >> 4145                var t = Self.rotl(al &+ Self.f(round, bl, cl, dl) &+ x[Int(Self.rl[j])] &+ Self.kl[round], UInt32(Self.sl[j])) &+ el146                al = el; el = dl; dl = Self.rotl(cl, 10); cl = bl; bl = t147                t = Self.rotl(ar &+ Self.f(4 - round, br, cr, dr) &+ x[Int(Self.rr[j])] &+ Self.kr[round], UInt32(Self.sr[j])) &+ er148                ar = er; er = dr; dr = Self.rotl(cr, 10); cr = br; br = t149            }150            let t = h[1] &+ cl &+ dr151            h[1] = h[2] &+ dl &+ er152            h[2] = h[3] &+ el &+ ar153            h[3] = h[4] &+ al &+ br154            h[4] = h[0] &+ bl &+ cr155            h[0] = t156            for i in 0..<16 { x[i] = 0 }157        }158    }159}160161/// HASH160 of Bitcoin: `RIPEMD160(SHA256(data))` (public-key hashes, script hashes).162public enum Hash160 {163    public static func hash(_ data: Bytes) -> Bytes {164        RIPEMD160.hash(SHA256.hash(data))165    }166}167
Files main
Menu

Theme