Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraSecp256k1Tests/CarryChainTests.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 QyvoraMath42import Testing4344@testable import QyvoraSecp256k14546/// Regression tests of finding QYV-AUD-002: the carry chain of the 256-bit arithmetic is computed with bit47/// arithmetic (no Boolean, hence no conditional branch on secret limbs in the compiled code). These tests48/// check that the branch-free forms compute exactly what the overflow-reporting operations compute; the49/// absence of conditional branches in the machine code is checked by the audit engine50/// (`security/audit/runners/timing.py`, machine-code check) on every audit run.51@Suite struct CarryChainTests {52 static let edges: [UInt64] = [0, 1, 2, 0x7fff_ffff_ffff_ffff, 0x8000_0000_0000_0000, 0x8000_0000_0000_0001, .max - 1, .max]5354 static func reference(adc x: UInt64, _ y: UInt64, _ c: UInt64) -> (UInt64, UInt64) {55 let (s1, o1) = x.addingReportingOverflow(y)56 let (s2, o2) = s1.addingReportingOverflow(c)57 return (s2, (o1 ? 1 : 0) + (o2 ? 1 : 0))58 }5960 static func reference(sbb x: UInt64, _ y: UInt64, _ b: UInt64) -> (UInt64, UInt64) {61 let (d1, o1) = x.subtractingReportingOverflow(y)62 let (d2, o2) = d1.subtractingReportingOverflow(b)63 return (d2, (o1 ? 1 : 0) + (o2 ? 1 : 0))64 }6566 @Test func addWithCarryAndSubtractWithBorrowMatchTheReportingForms() {67 var rng = SystemRandomNumberGenerator()68 var values = Self.edges69 for _ in 0..<5000 { values.append(rng.next()) }70 for (i, x) in values.enumerated() {71 for y in Self.edges + [values[(i * 31 + 7) % values.count]] {72 for c: UInt64 in [0, 1] {73 #expect(U256.adc(x, y, c) == Self.reference(adc: x, y, c), "adc(\(x), \(y), \(c))")74 #expect(U256.sbb(x, y, c) == Self.reference(sbb: x, y, c), "sbb(\(x), \(y), \(c))")75 }76 }77 }78 }7980 /// 256-bit addition and subtraction against a limb-by-limb reference, including full carry propagation.81 @Test func wideAdditionAndSubtraction() {82 var rng = SystemRandomNumberGenerator()83 func limbs(_ v: U256) -> [UInt64] { [v.a0, v.a1, v.a2, v.a3] }84 var cases: [(U256, U256)] = [85 (U256(.max, .max, .max, .max), .one), (.zero, .one), (U256(.max, .max, .max, .max), U256(.max, .max, .max, .max)),86 (U256(.max, 0, .max, 0), U256(1, .max, 0, .max)), (.zero, .zero), (U256(0, 0, 0, 1 << 63), U256(0, 0, 0, 1 << 63)),87 ]88 for _ in 0..<5000 {89 cases.append((U256(rng.next(), rng.next(), rng.next(), rng.next()), U256(rng.next(), rng.next(), rng.next(), rng.next())))90 }91 for (a, b) in cases {92 var carry: UInt64 = 0, borrow: UInt64 = 093 var sum = [UInt64](), difference = [UInt64]()94 for (x, y) in zip(limbs(a), limbs(b)) {95 let s = Self.reference(adc: x, y, carry)96 sum.append(s.0)97 carry = s.198 let d = Self.reference(sbb: x, y, borrow)99 difference.append(d.0)100 borrow = d.1101 }102 let (s, c) = U256.add(a, b), (d, bo) = U256.sub(a, b)103 #expect(limbs(s) == sum && c == carry)104 #expect(limbs(d) == difference && bo == borrow)105 #expect(U256.lessThan(a, b) == (borrow == 1))106 }107 }108109 /// Montgomery multiplication on operands that drive every carry of the inner loop (all-ones limbs, p − 1, n − 1).110 @Test func montgomeryProductOnCarryHeavyOperands() {111 for m in [Secp256k1.field, Secp256k1.order] {112 let top = U256.sub(m.m, .one).0113 // (m − 1)² ≡ 1, (m − 1)·2 ≡ m − 2, and a·1 = a through the Montgomery form114 #expect(m.mul(top, top) == .one)115 #expect(m.mul(top, U256(2, 0, 0, 0)) == U256.sub(m.m, U256(2, 0, 0, 0)).0)116 #expect(m.fromMont(m.toMont(top)) == top)117 #expect(m.mul(m.inverse(top), top) == .one)118 let allOnes = m.reduce(U256(.max, .max, .max, .max))119 #expect(m.mul(allOnes, m.inverse(allOnes)) == .one)120 }121 // the generator still has order n, and the known public key of the private key 1 is unchanged122 #expect(Secp256k1.Point.multiply(Secp256k1.n, Secp256k1.generator).isIdentity)123 let one = Bytes(repeating: 0, count: 31) + [1]124 #expect(try! Secp256k1.publicKey(privateKey: one).hex == "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")125 }126}127