Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : security/tests/Sources/FuzzTargets/Targets+Ethereum.swift8// Module : Tests9// 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 AuditSupport42import Foundation43import QyvoraCore44import QyvoraEthereum45import QyvoraMath46import QyvoraSecp256k14748// Ethereum parsers: RLP, EIP-1559 payloads (unsigned and signed), addresses (EIP-55), ERC-20 call49// data and return data, personal-message signatures.5051func fixtureEIP1559(_ variant: Int) -> EIP1559Transaction {52 let to = EthereumAddress(bytes: fixtureBytes("to", 20))53 switch variant {54 case 0:55 return EIP1559Transaction(56 chainID: 11_155_111, nonce: 7, maxPriorityFeePerGas: UInt256(1_500_000_000), maxFeePerGas: UInt256(30_000_000_000),57 gasLimit: 21_000, to: to, value: UInt256(1_000_000_000_000_000))58 case 1:59 let token = EthereumAddress(bytes: fixtureBytes("token", 20)) ?? .zero60 return EIP1559Transaction(61 chainID: 1, nonce: 0, maxPriorityFeePerGas: UInt256(1), maxFeePerGas: UInt256(2), gasLimit: 65_000, to: token, value: .zero,62 data: ERC20.transfer(to: to ?? .zero, amount: UInt256(123_456)),63 accessList: [.init(address: token, storageKeys: [fixtureBytes("slot", 32), Bytes(repeating: 0, count: 32)])])64 default:65 return EIP1559Transaction(66 chainID: 1, nonce: UInt64.max, maxPriorityFeePerGas: .zero, maxFeePerGas: .zero, gasLimit: 0, to: nil, value: .zero,67 data: Bytes(repeating: 0x60, count: 70))68 }69}7071func rlpSeeds() -> [Bytes] {72 var out: [Bytes] = [73 [0x80], [0xc0], [0x7f], [0x81, 0x80], RLP.list([.bytes(Bytes("cat".utf8)), .bytes(Bytes("dog".utf8))]).encoded,74 RLP.bytes(Bytes(repeating: 0x61, count: 60)).encoded, RLP.list([.list([]), .list([.list([])]), .uint(UInt64(1024))]).encoded,75 RLP.list((0..<20).map { .bytes(Bytes(repeating: UInt8($0), count: 4)) }).encoded,76 ]77 for v in 0..<3 { out.append(Bytes(fixtureEIP1559(v).unsignedPayload.dropFirst())) }78 return out79}8081let ethereumTargets: [FuzzTarget] = [82 FuzzTarget(83 "rlp", "RLP strict decode and canonical re-encoding", maxLength: 4096,84 tokens: [[0xb8, 0x38], [0xf8, 0x38], [0xb9, 0x01, 0x00], [0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], [0xc1, 0x80]], seeds: rlpSeeds85 ) { input in86 guard let item = attempt({ try RLP.decode(input) }) else { return }87 fuzzCheck(item.encoded == input, "strict RLP decoder accepted a non-canonical encoding")88 fuzzUse(item.uintValue)89 fuzzUse(item.bytesValue)90 fuzzUse(item.listValue?.count)91 },9293 FuzzTarget(94 "eip1559-unsigned", "EIP-1559 unsigned payload decode, re-encoding, cost and call-data description", maxLength: 2048,95 seeds: { (0..<3).map { fixtureEIP1559($0).unsignedPayload } }96 ) { input in97 guard let tx = attempt({ try EIP1559Transaction.decodeUnsigned(input) }) else { return }98 fuzzCheck(tx.unsignedPayload == input, "decoded transaction re-encodes differently: what is shown is not what is signed")99 fuzzCheck(tx.signingHash.count == 32, "signing hash length")100 fuzzUse(tx.maximumCost)101 if let call = ERC20.decodeCall(tx.data) {102 FuzzOutcome.note("erc20")103 switch call {104 case .transfer(let to, let amount): fuzzCheck(ERC20.transfer(to: to, amount: amount) == tx.data, "ERC-20 transfer round trip")105 case .approve(let spender, let amount):106 fuzzCheck(ERC20.approve(spender: spender, amount: amount) == tx.data, "ERC-20 approve round trip")107 fuzzUse(ERC20.isUnlimited(amount))108 }109 }110 },111112 FuzzTarget(113 "eip1559-signed", "Signed type-2 transaction decode, signature shape, sender recovery, raw re-encoding", maxLength: 2048,114 seeds: {115 let key = fixtureBytes("ethkey", 32)116 return (0..<3).compactMap { try? fixtureEIP1559($0).sign(privateKey: key).raw }117 }118 ) { input in119 guard let signed = attempt({ try SignedEIP1559Transaction.decode(input) }) else { return }120 fuzzCheck(signed.raw == input, "signed transaction re-encodes differently")121 fuzzCheck((try? signed.sender()) != nil, "decoded signed transaction has no recoverable sender")122 fuzzUse(signed.hashHex)123 },124125 FuzzTarget(126 "ethereum-address", "EthereumAddress text parsing with EIP-55 checksum", maxLength: 64, text: true, dictionary: ["0x", "0X"],127 seeds: {128 let a = EthereumAddress(bytes: fixtureBytes("eip55", 20)) ?? .zero129 return [130 Bytes(a.checksummed.utf8), Bytes(a.checksummed.lowercased().utf8), Bytes("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed".utf8),131 Bytes("0x0000000000000000000000000000000000000000".utf8),132 ]133 }134 ) { input in135 let s = fuzzString(input)136 guard let a = EthereumAddress(s) else {137 FuzzOutcome.note("nil")138 return139 }140 FuzzOutcome.note("ok")141 fuzzCheck(a.bytes.count == 20, "address length")142 fuzzCheck(EthereumAddress(a.checksummed) == a, "checksummed form does not parse back")143 fuzzCheck(a.checksummed.lowercased() == "0x" + s.dropFirst(2).lowercased(), "address text and bytes disagree")144 let body = String(s.dropFirst(2))145 let mixed = body != body.lowercased() && body != body.uppercased()146 if mixed { fuzzCheck(a.checksummed.dropFirst(2) == body, "mixed-case address accepted with a wrong EIP-55 checksum") }147 },148149 FuzzTarget(150 "erc20-abi", "ERC-20 call-data and return-data decoding", maxLength: 512,151 seeds: {152 let a = EthereumAddress(bytes: fixtureBytes("erc20", 20)) ?? .zero153 var abiString = Bytes(repeating: 0, count: 31) + [32] + Bytes(repeating: 0, count: 31) + [4] + Bytes("USDC".utf8)154 abiString += Bytes(repeating: 0, count: 28)155 return [156 ERC20.transfer(to: a, amount: UInt256(1)), ERC20.approve(spender: a, amount: UInt256(bigEndian: Bytes(repeating: 0xff, count: 32)) ?? .zero),157 abiString, Bytes("MKR".utf8) + Bytes(repeating: 0, count: 29), UInt256(18).bytes32,158 ]159 }160 ) { input in161 if ERC20.decodeCall(input) != nil { FuzzOutcome.note("call") }162 if ERC20.decodeUInt(input) != nil { FuzzOutcome.note("uint") }163 if let s = ERC20.decodeString(input) {164 FuzzOutcome.note("string")165 fuzzCheck(!s.isEmpty && s.count <= 64, "token string out of bounds")166 }167 },168169 FuzzTarget(170 "personal-message", "EIP-191 signature recovery with arbitrary signatures", maxLength: 256,171 seeds: {172 let key = fixtureBytes("personal", 32)173 let message = Bytes("hello".utf8)174 return [(try? PersonalMessage.sign(message, privateKey: key)).map { $0 + message } ?? []]175 }176 ) { input in177 var f = FuzzInput(input)178 let signature = f.upTo(65)179 let message = f.rest()180 attempt { try PersonalMessage.recoverSigner(message, signature: signature) }181 },182]183