Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraSLHDSA/Address.swift8// Module : Qyvora SLHDSA9// 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/// SLH-DSA hash address ADRS (FIPS 205, section 4.2).44///45/// The 32-byte address is `layer (4) ‖ tree (12) ‖ type (4) ‖ three type-dependent words (4 each)`.46/// The tree address never exceeds 64 bits for the approved parameter sets, so its upper four bytes47/// are always zero and the field is held in a `UInt64`.48struct ADRS: Equatable, Sendable {49 /// Address types (FIPS 205, Table 1).50 enum AddressType: UInt32, Sendable {51 case wotsHash = 052 case wotsPK = 153 case tree = 254 case forsTree = 355 case forsRoots = 456 case wotsPRF = 557 case forsPRF = 658 }5960 var layer: UInt32 = 061 var tree: UInt64 = 062 var type: UInt32 = 063 /// Bytes 20..<24: key pair address (zero padding for `tree` and unused types).64 var word1: UInt32 = 065 /// Bytes 24..<28: chain address (WOTS) or tree height (TREE, FORS_TREE).66 var word2: UInt32 = 067 /// Bytes 28..<32: hash address (WOTS_HASH) or tree index (TREE, FORS_TREE, FORS_PRF).68 var word3: UInt32 = 06970 init() {}7172 init(layer: UInt32, tree: UInt64, type: AddressType, keyPair: UInt32 = 0) {73 self.layer = layer74 self.tree = tree75 self.type = type.rawValue76 self.word1 = keyPair77 }7879 @inline(__always) mutating func setLayerAddress(_ l: UInt32) { layer = l }80 @inline(__always) mutating func setTreeAddress(_ t: UInt64) { tree = t }8182 /// Sets the type and zeroes the final 12 bytes (FIPS 205: `setTypeAndClear`).83 @inline(__always) mutating func setTypeAndClear(_ t: AddressType) {84 type = t.rawValue85 word1 = 086 word2 = 087 word3 = 088 }8990 @inline(__always) mutating func setKeyPairAddress(_ i: UInt32) { word1 = i }91 @inline(__always) mutating func setChainAddress(_ i: UInt32) { word2 = i }92 @inline(__always) mutating func setTreeHeight(_ i: UInt32) { word2 = i }93 @inline(__always) mutating func setHashAddress(_ i: UInt32) { word3 = i }94 @inline(__always) mutating func setTreeIndex(_ i: UInt32) { word3 = i }95 @inline(__always) func getKeyPairAddress() -> UInt32 { word1 }96 @inline(__always) func getTreeIndex() -> UInt32 { word3 }9798 /// The full 32-byte encoding.99 var bytes: Bytes {100 var out = Bytes(repeating: 0, count: 32)101 out.withUnsafeMutableBufferPointer { write(to: $0.baseAddress!) }102 return out103 }104105 /// The 22-byte compressed encoding used by the SHA-2 instantiation (FIPS 205, section 11.2):106 /// `ADRS[3] ‖ ADRS[8:16] ‖ ADRS[19] ‖ ADRS[20:32]`.107 var compressedBytes: Bytes {108 var out = Bytes(repeating: 0, count: 22)109 out.withUnsafeMutableBufferPointer { writeCompressed(to: $0.baseAddress!) }110 return out111 }112113 /// Writes the 32-byte encoding at `p`.114 @inline(__always) func write(to p: UnsafeMutablePointer<UInt8>) {115 let r = UnsafeMutableRawPointer(p)116 r.storeBytes(of: layer.bigEndian, toByteOffset: 0, as: UInt32.self)117 r.storeBytes(of: 0, toByteOffset: 4, as: UInt32.self)118 r.storeBytes(of: tree.bigEndian, toByteOffset: 8, as: UInt64.self)119 r.storeBytes(of: type.bigEndian, toByteOffset: 16, as: UInt32.self)120 r.storeBytes(of: word1.bigEndian, toByteOffset: 20, as: UInt32.self)121 r.storeBytes(of: word2.bigEndian, toByteOffset: 24, as: UInt32.self)122 r.storeBytes(of: word3.bigEndian, toByteOffset: 28, as: UInt32.self)123 }124125 /// Writes the 22-byte compressed encoding at `p`.126 @inline(__always) func writeCompressed(to p: UnsafeMutablePointer<UInt8>) {127 let r = UnsafeMutableRawPointer(p)128 r.storeBytes(of: UInt8(truncatingIfNeeded: layer), toByteOffset: 0, as: UInt8.self)129 r.storeBytes(of: tree.bigEndian, toByteOffset: 1, as: UInt64.self)130 r.storeBytes(of: UInt8(truncatingIfNeeded: type), toByteOffset: 9, as: UInt8.self)131 r.storeBytes(of: word1.bigEndian, toByteOffset: 10, as: UInt32.self)132 r.storeBytes(of: word2.bigEndian, toByteOffset: 14, as: UInt32.self)133 r.storeBytes(of: word3.bigEndian, toByteOffset: 18, as: UInt32.self)134 }135136 /// Writes the 32-byte encoding as four little-endian Keccak lanes at `lanes`.137 @inline(__always) func writeLanes(to lanes: UnsafeMutablePointer<UInt64>) {138 lanes[0] = UInt64(layer.byteSwapped)139 lanes[1] = tree.byteSwapped140 lanes[2] = UInt64(type.byteSwapped) | UInt64(word1.byteSwapped) << 32141 lanes[3] = UInt64(word2.byteSwapped) | UInt64(word3.byteSwapped) << 32142 }143}144