Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : security/tests/Tests/ConformanceAuditTests/Ed25519Vectors.swift8// Module : Tests9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0512// Last Modified : 2026-12-0513//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 QyvoraMath44import Testing4546@testable import QyvoraEd255194748// Ed25519 conformance from files fetched at pinned revisions (security/vectors/ed25519, MANIFEST.json):49// RFC 8032 section 7.1 (read from the RFC text), the SLIP-0010 ed25519 vectors (read from slip-0010.md)50// and the twelve edge cases of ed25519-speccheck ("Taming the many EdDSAs", Chalkias, Garillot, Nikolaenko).5152@Suite struct Ed25519Conformance {53 static let dir = AuditPaths.vectors.appendingPathComponent("ed25519")5455 static func vectors(_ file: String) throws -> [[String: Any]] {56 let root = try AuditVectors.json(dir.appendingPathComponent(file)) as? [String: Any]57 return try #require(root?["vectors"] as? [[String: Any]])58 }5960 @Test func rfc8032Section71() throws {61 var board = Board(source: "RFC 8032 section 7.1 (rfc8032.txt, pinned SHA-256)")62 for v in try Self.vectors("rfc8032-ed25519-vectors.json") {63 let name = v.str("name")64 let seed = try #require(v.hex("secret_key")), pk = try #require(v.hex("public_key"))65 let message = try #require(v.hex("message")), signature = try #require(v.hex("signature"))66 board.add("Ed25519", "", "rfc8032-public-key", ok: (try? Ed25519.publicKey(seed: seed)) == pk, id: name)67 board.add("Ed25519", "", "rfc8032-sign", ok: (try? Ed25519.sign(message: message, seed: seed)) == signature, id: name)68 board.add("Ed25519", "", "rfc8032-verify", ok: Ed25519.verify(signature: signature, message: message, publicKey: pk), id: name)69 }70 #expect(board.cases == 15)71 board.finish(minimum: 15)72 }7374 @Test func slip0010() throws {75 var board = Board(source: "SLIP-0010 (satoshilabs/slips, pinned commit), test vectors 1 and 2 for ed25519")76 for v in try Self.vectors("slip-0010-ed25519-vectors.json") {77 let seed = try #require(v.hex("seed"))78 let chains = try #require(v["chains"] as? [[String: Any]])79 for c in chains {80 let path = c.str("path")81 let indices: [UInt32] = path.split(separator: "/").dropFirst().compactMap {82 UInt32($0.replacingOccurrences(of: "'", with: ""))83 }84 #expect(indices.count == path.split(separator: "/").count - 1)85 let node = try? Ed25519.SLIP10.derive(seed: seed, path: indices)86 board.add("SLIP-0010", "ed25519", "slip10-private-key", ok: node?.privateKey == c.hex("private"), id: path)87 board.add("SLIP-0010", "ed25519", "slip10-chain-code", ok: node?.chainCode == c.hex("chain_code"), id: path)88 // the vectors print the public key with a leading 00 byte89 let pk = (try? node?.publicKey()).map { [0x00] + $0 }90 board.add("SLIP-0010", "ed25519", "slip10-public-key", ok: pk == c.hex("public"), id: path)91 }92 }93 #expect(board.cases == 36)94 board.finish(minimum: 36)95 }9697 /// The twelve speccheck cases under the documented strict policy: which rule refuses each one.98 ///99 /// | case | structure | expected | rule |100 /// | 0, 1 | small-order A | refused | rule 3 (small-order public key) |101 /// | 2 | mixed-order A, small-order R | refused | rule 4 (small-order R) |102 /// | 3 | mixed-order A and R, cofactorless equation holds | **accepted** | — (as ed25519-dalek verify_strict and libsodium) |103 /// | 4, 5 | only the cofactored equation holds | refused | rule 5 (cofactorless equation) |104 /// | 6, 7 | S ≥ ℓ | refused | rule 2 (canonical S) |105 /// | 8, 9 | non-canonical encoding of a small-order R | refused | rules 4 and 5 (strict decoding of R) |106 /// | 10, 11 | non-canonical encoding of a small-order A | refused | rule 3 (strict decoding of A) |107 @Test func speccheckEdgeCases() throws {108 let cases = try #require(try AuditVectors.json(Self.dir.appendingPathComponent("speccheck-cases.json")) as? [[String: Any]])109 #expect(cases.count == 12)110 var board = Board(source: "ed25519-speccheck cases.json (novifinancial, pinned commit)")111 let expectedAccept: Set<Int> = [3]112 for (i, c) in cases.enumerated() {113 let pk = try #require(c.hex("pub_key")), message = try #require(c.hex("message")), sig = try #require(c.hex("signature"))114 board.add("Ed25519", "strict", "speccheck-verdict", ok: Ed25519.verify(signature: sig, message: message, publicKey: pk) == expectedAccept.contains(i), id: i)115116 // the structure of each case, read with the engine's own predicates: the reason for the verdict117 let r = Bytes(sig[0..<32])118 let sCanonical = Sc(littleEndian: Bytes(sig[32..<64])).isCanonical119 let aStrict = EdPoint.decode(pk, strict: true), aLenient = EdPoint.decode(pk, strict: false)120 let rStrict = EdPoint.decode(r, strict: true), rLenient = EdPoint.decode(r, strict: false)121 let reason: Bool122 switch i {123 case 0, 1: reason = sCanonical && aStrict?.isSmallOrder == true && Ed25519.isValidPublicKey(pk) == false124 case 2: reason = sCanonical && aStrict?.isSmallOrder == false && rStrict?.isSmallOrder == true125 case 3, 4, 5: reason = sCanonical && aStrict?.isSmallOrder == false && rStrict?.isSmallOrder == false126 case 6, 7: reason = !sCanonical && aStrict?.isSmallOrder == false && rStrict?.isSmallOrder == false127 case 8, 9: reason = sCanonical && aStrict != nil && rStrict == nil && rLenient?.isSmallOrder == true128 default: reason = sCanonical && aStrict == nil && aLenient?.isSmallOrder == true && Ed25519.isCanonicalPoint(pk) == false129 }130 board.add("Ed25519", "strict", "speccheck-structure", ok: reason, id: i)131 }132 board.finish(minimum: 24, note: "strict policy: only case 3 is accepted (same verdicts as ed25519-dalek verify_strict and libsodium, compared in the differential tests)")133 }134}135