SPB Git

spb/os-vault Public

Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.

Swift 96% Shell 3.4% Makefile 0.6%
7.2 KB · 192 lines swift
Raw Blame History
1public extension RIPEMD {2    // FIXME: Make struct and all functions framework-only as soon as tests support that3    struct Block {4        public init() {}56        var message: [UInt32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]78        // Initial values9        var h₀: UInt32 = 0x6745_230110        var h₁: UInt32 = 0xEFCD_AB8911        var h₂: UInt32 = 0x98BA_DCFE12        var h₃: UInt32 = 0x1032_547613        var h₄: UInt32 = 0xC3D2_E1F01415        public var hash: [UInt32] {16            [h₀, h₁, h₂, h₃, h₄]17        }1819        // FIXME: Make private as soon as tests support that20        public mutating func compress(_ message: [UInt32]) {21            assert(message.count == 16, "Wrong message size")2223            var Aᴸ = h₀24            var Bᴸ = h₁25            var Cᴸ = h₂26            var Dᴸ = h₃27            var Eᴸ = h₄2829            var Aᴿ = h₀30            var Bᴿ = h₁31            var Cᴿ = h₂32            var Dᴿ = h₃33            var Eᴿ = h₄3435            for j in 0 ... 79 {36                // Left side37                let wordᴸ = message[r.left[j]]38                let functionᴸ = f(j)3940                let Tᴸ: UInt32 = ((Aᴸ &+ functionᴸ(Bᴸ, Cᴸ, Dᴸ) &+ wordᴸ &+ K.left[j]) ~<< s.left[j]) &+ Eᴸ4142                Aᴸ = Eᴸ43                Eᴸ = Dᴸ44                Dᴸ = Cᴸ ~<< 1045                Cᴸ = Bᴸ46                Bᴸ = Tᴸ4748                // Right side49                let wordᴿ = message[r.right[j]]50                let functionᴿ = f(79 - j)5152                let Tᴿ: UInt32 = ((Aᴿ &+ functionᴿ(Bᴿ, Cᴿ, Dᴿ) &+ wordᴿ &+ K.right[j]) ~<< s.right[j]) &+ Eᴿ5354                Aᴿ = Eᴿ55                Eᴿ = Dᴿ56                Dᴿ = Cᴿ ~<< 1057                Cᴿ = Bᴿ58                Bᴿ = Tᴿ59            }6061            let T = h₁ &+ Cᴸ &+ Dᴿ62            h₁ = h₂ &+ Dᴸ &+ Eᴿ63            h₂ = h₃ &+ Eᴸ &+ Aᴿ64            h₃ = h₄ &+ Aᴸ &+ Bᴿ65            h₄ = h₀ &+ Bᴸ &+ Cᴿ66            h₀ = T67        }6869        public func f(_ j: Int) -> ((UInt32, UInt32, UInt32) -> UInt32) {70            switch j {71            case _ where j < 0:72                assertionFailure("Invalid j")73                return { _, _, _ in 0 }74            case _ where j <= 15:75                return { x, y, z in x ^ y ^ z }76            case _ where j <= 31:77                return { x, y, z in (x & y) | (~x & z) }78            case _ where j <= 47:79                return { x, y, z in (x | ~y) ^ z }80            case _ where j <= 63:81                return { x, y, z in (x & z) | (y & ~z) }82            case _ where j <= 79:83                return { x, y, z in x ^ (y | ~z) }84            default:85                assertionFailure("Invalid j")86                return { _, _, _ in 0 }87            }88        }8990        public enum K {91            case left, right9293            public subscript(j: Int) -> UInt32 {94                switch j {95                case _ where j < 0:96                    assertionFailure("Invalid j")97                    return 098                case _ where j <= 15:99                    return self == .left ? 0x0000_0000 : 0x50A2_8BE6100                case _ where j <= 31:101                    return self == .left ? 0x5A82_7999 : 0x5C4D_D124102                case _ where j <= 47:103                    return self == .left ? 0x6ED9_EBA1 : 0x6D70_3EF3104                case _ where j <= 63:105                    return self == .left ? 0x8F1B_BCDC : 0x7A6D_76E9106                case _ where j <= 79:107                    return self == .left ? 0xA953_FD4E : 0x0000_0000108                default:109                    assertionFailure("Invalid j")110                    return 0111                }112            }113        }114115        public enum r {116            case left, right117118            public subscript(j: Int) -> Int {119                switch j {120                case _ where j < 0:121                    assertionFailure("Invalid j")122                    return 0123                case let index where j <= 15:124                    if self == .left {125                        return index126                    } else {127                        return [5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12][index]128                    }129                case let index where j <= 31:130                    if self == .left {131                        return [7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8][index - 16]132                    } else {133                        return [6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2][index - 16]134                    }135                case let index where j <= 47:136                    if self == .left {137                        return [3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12][index - 32]138                    } else {139                        return [15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13][index - 32]140                    }141                case let index where j <= 63:142                    if self == .left {143                        return [1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2][index - 48]144                    } else {145                        return [8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14][index - 48]146                    }147                case let index where j <= 79:148                    if self == .left {149                        return [4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13][index - 64]150                    } else {151                        return [12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11][index - 64]152                    }153154                default:155                    assertionFailure("Invalid j")156                    return 0157                }158            }159        }160161        public enum s {162            case left, right163164            public subscript(j: Int) -> Int {165                switch j {166                case _ where j < 0:167                    assertionFailure("Invalid j")168                    return 0169                case _ where j <= 15:170                    return (self == .left ? [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8] :171                        [8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6])[j]172                case _ where j <= 31:173                    return (self == .left ? [7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12] :174                        [9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11])[j - 16]175                case _ where j <= 47:176                    return (self == .left ? [11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5] :177                        [9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5])[j - 32]178                case _ where j <= 63:179                    return (self == .left ? [11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12] :180                        [15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8])[j - 48]181                case _ where j <= 79:182                    return (self == .left ? [9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6] :183                        [8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11])[j - 64]184                default:185                    assertionFailure("Invalid j")186                    return 0187                }188            }189        }190    }191}192