Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraEthereum/RLP.swift8// Module : Qyvora Ethereum9// 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 QyvoraMath42import QyvoraCore4344/// Recursive Length Prefix serialization (Ethereum Yellow Paper, appendix B).45public indirect enum RLP: Equatable, Sendable {46 case bytes(Bytes)47 case list([RLP])4849 public enum Error: Swift.Error, Equatable {50 case truncated51 case nonCanonical52 case trailingBytes53 case tooDeep54 }5556 public static func uint(_ v: UInt64) -> RLP { .bytes(UInt256(v).minimalBytes) }57 public static func uint(_ v: UInt256) -> RLP { .bytes(v.minimalBytes) }5859 public var encoded: Bytes {60 switch self {61 case .bytes(let b):62 if b.count == 1, b[0] < 0x80 { return b }63 return RLP.header(b.count, offset: 0x80) + b64 case .list(let items):65 let body = items.flatMap { $0.encoded }66 return RLP.header(body.count, offset: 0xc0) + body67 }68 }6970 private static func header(_ length: Int, offset: UInt8) -> Bytes {71 if length < 56 { return [offset + UInt8(length)] }72 let len = UInt256(UInt64(length)).minimalBytes73 return [offset + 55 + UInt8(len.count)] + len74 }7576 /// Strict decoder: rejects non-canonical lengths, single bytes wrapped in a string header, and trailing data.77 public static func decode(_ data: Bytes) throws -> RLP {78 var position = 079 let item = try decodeItem(data, &position, depth: 0)80 guard position == data.count else { throw Error.trailingBytes }81 return item82 }8384 private static func decodeItem(_ d: Bytes, _ i: inout Int, depth: Int) throws -> RLP {85 guard depth <= 64 else { throw Error.tooDeep }86 guard i < d.count else { throw Error.truncated }87 let p = d[i]88 func take(_ n: Int) throws -> Bytes {89 guard n >= 0, d.count - i >= n else { throw Error.truncated }90 defer { i += n }91 return Bytes(d[i..<(i + n)])92 }93 func longLength(_ n: Int) throws -> Int {94 let lb = try take(n)95 guard lb[0] != 0, n <= 8 else { throw Error.nonCanonical }96 var v: UInt64 = 097 for b in lb { v = v << 8 | UInt64(b) }98 guard v >= 56, v <= UInt64(Int32.max) else { throw Error.nonCanonical }99 return Int(v)100 }101 i += 1102 switch p {103 case 0x00...0x7f:104 return .bytes([p])105 case 0x80...0xb7:106 let b = try take(Int(p - 0x80))107 if b.count == 1, b[0] < 0x80 { throw Error.nonCanonical }108 return .bytes(b)109 case 0xb8...0xbf:110 return .bytes(try take(try longLength(Int(p - 0xb7))))111 default:112 let length = p <= 0xf7 ? Int(p - 0xc0) : try longLength(Int(p - 0xf7))113 guard d.count - i >= length else { throw Error.truncated }114 let end = i + length115 var items = [RLP]()116 while i < end { items.append(try decodeItem(d, &i, depth: depth + 1)) }117 guard i == end else { throw Error.truncated }118 return .list(items)119 }120 }121122 public var bytesValue: Bytes? {123 if case .bytes(let b) = self { return b }124 return nil125 }126127 public var listValue: [RLP]? {128 if case .list(let l) = self { return l }129 return nil130 }131132 /// Canonical unsigned integer (no leading zero byte, at most 32 bytes).133 public var uintValue: UInt256? {134 guard let b = bytesValue, b.count <= 32, b.first != 0 else { return nil }135 return UInt256(bigEndian: b)136 }137}138