Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Crypto/Blake2b.swift3// Description: BLAKE2b (RFC 7693) in pure Swift, unkeyed, digest length 1…64 bytes. Needed by the SS58 checksum of Polkadot4// addresses (BLAKE2b-512); the 224/256-bit lengths are the ones Cardano and Sui use.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public enum Blake2b {11 private static let iv: [UInt64] = [12 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,13 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,14 ]15 private static let sigma: [[Int]] = [16 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],17 [11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], [7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8],18 [9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], [2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9],19 [12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], [13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10],20 [6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], [10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0],21 ]2223 @inline(__always) private static func rotr(_ x: UInt64, _ n: UInt64) -> UInt64 { (x >> n) | (x << (64 - n)) }2425 private static func compress(_ h: inout [UInt64], block: ArraySlice<UInt8>, counter: UInt64, last: Bool) {26 var m = [UInt64](repeating: 0, count: 16)27 var i = block.startIndex28 for w in 0..<16 {29 var x: UInt64 = 030 for k in 0..<8 { x |= UInt64(block[i + k]) << UInt64(8 * k) }31 m[w] = x; i += 832 }33 var v = h + iv34 v[12] ^= counter // the high 64 bits of the 128-bit counter stay zero: inputs here are far below 2^64 bytes35 if last { v[14] = ~v[14] }36 func g(_ a: Int, _ b: Int, _ c: Int, _ d: Int, _ x: UInt64, _ y: UInt64) {37 v[a] = v[a] &+ v[b] &+ x; v[d] = rotr(v[d] ^ v[a], 32)38 v[c] = v[c] &+ v[d]; v[b] = rotr(v[b] ^ v[c], 24)39 v[a] = v[a] &+ v[b] &+ y; v[d] = rotr(v[d] ^ v[a], 16)40 v[c] = v[c] &+ v[d]; v[b] = rotr(v[b] ^ v[c], 63)41 }42 for round in 0..<12 {43 let s = sigma[round % 10]44 g(0, 4, 8, 12, m[s[0]], m[s[1]]); g(1, 5, 9, 13, m[s[2]], m[s[3]])45 g(2, 6, 10, 14, m[s[4]], m[s[5]]); g(3, 7, 11, 15, m[s[6]], m[s[7]])46 g(0, 5, 10, 15, m[s[8]], m[s[9]]); g(1, 6, 11, 12, m[s[10]], m[s[11]])47 g(2, 7, 8, 13, m[s[12]], m[s[13]]); g(3, 4, 9, 14, m[s[14]], m[s[15]])48 }49 for k in 0..<8 { h[k] ^= v[k] ^ v[k + 8] }50 }5152 /** Unkeyed BLAKE2b of `message` with a digest of `outputLength` bytes (1…64). */53 public static func hash(_ message: [UInt8], outputLength: Int = 64) -> [UInt8] {54 precondition((1...64).contains(outputLength), "BLAKE2b digest length must be 1…64 bytes")55 var h = iv56 h[0] ^= 0x0101_0000 ^ UInt64(outputLength)57 var offset = 058 while message.count - offset > 128 {59 compress(&h, block: message[offset..<(offset + 128)], counter: UInt64(offset + 128), last: false)60 offset += 12861 }62 var lastBlock = [UInt8](message[offset...])63 lastBlock.append(contentsOf: [UInt8](repeating: 0, count: 128 - lastBlock.count))64 compress(&h, block: lastBlock[...], counter: UInt64(message.count), last: true)65 var out = [UInt8](); out.reserveCapacity(64)66 for word in h { for k in 0..<8 { out.append(UInt8(truncatingIfNeeded: word >> UInt64(8 * k))) } }67 return [UInt8](out.prefix(outputLength))68 }69}70