Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Crypto/Keccak256.swift3// Description: Keccak-f[1600] sponge in pure Swift. `Keccak.keccak256` is the ORIGINAL Keccak (domain padding 0x01) used by4// Ethereum and TRON addresses; `Keccak.sha3_256` is FIPS 202 SHA3-256 (padding 0x06). They are different hashes:5// EIP-55 needs the first one. Used only to validate and display addresses, never to sign.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation1011public enum Keccak {12 private static let roundConstants: [UInt64] = [13 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,14 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,15 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,16 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,17 ]18 private static let rotations: [UInt64] = [1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44]19 private static let piLanes: [Int] = [10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1]2021 private static func permute(_ a: inout [UInt64]) {22 var c = [UInt64](repeating: 0, count: 5)23 for round in 0..<24 {24 for x in 0..<5 { c[x] = a[x] ^ a[x + 5] ^ a[x + 10] ^ a[x + 15] ^ a[x + 20] }25 for x in 0..<5 {26 let d = c[(x + 4) % 5] ^ ((c[(x + 1) % 5] << 1) | (c[(x + 1) % 5] >> 63))27 for y in stride(from: 0, to: 25, by: 5) { a[y + x] ^= d }28 }29 var t = a[1]30 for i in 0..<24 {31 let j = piLanes[i], next = a[j], r = rotations[i]32 a[j] = (t << r) | (t >> (64 - r))33 t = next34 }35 for y in stride(from: 0, to: 25, by: 5) {36 for x in 0..<5 { c[x] = a[y + x] }37 for x in 0..<5 { a[y + x] = c[x] ^ (~c[(x + 1) % 5] & c[(x + 2) % 5]) }38 }39 a[0] ^= roundConstants[round]40 }41 }4243 /** Sponge with capacity 512 (rate 136 bytes) and a 32-byte output. `suffix` is the domain byte: 0x01 Keccak, 0x06 SHA-3. */44 private static func sponge256(_ message: [UInt8], suffix: UInt8) -> [UInt8] {45 let rate = 13646 var block = message47 block.append(suffix)48 while block.count % rate != 0 { block.append(0) }49 block[block.count - 1] |= 0x8050 var state = [UInt64](repeating: 0, count: 25)51 var offset = 052 while offset < block.count {53 for lane in 0..<(rate / 8) {54 var v: UInt64 = 055 for k in 0..<8 { v |= UInt64(block[offset + lane * 8 + k]) << UInt64(8 * k) }56 state[lane] ^= v57 }58 permute(&state)59 offset += rate60 }61 var out = [UInt8](); out.reserveCapacity(32)62 for lane in 0..<4 { for k in 0..<8 { out.append(UInt8(truncatingIfNeeded: state[lane] >> UInt64(8 * k))) } }63 return out64 }6566 /** Original Keccak-256 (Ethereum, TRON). NOT SHA3-256. */67 public static func keccak256(_ message: [UInt8]) -> [UInt8] { sponge256(message, suffix: 0x01) }68 public static func keccak256(_ message: Data) -> Data { Data(sponge256([UInt8](message), suffix: 0x01)) }69 /** FIPS 202 SHA3-256 (Aptos authentication keys). */70 public static func sha3_256(_ message: [UInt8]) -> [UInt8] { sponge256(message, suffix: 0x06) }71}72