SPB Git

Repository Public

MLKEMCryptoKitTests.swift 6.2 KB 132 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraMLKEMTests/MLKEMCryptoKitTests.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 CryptoKit42import Foundation43import QyvoraMath44import Testing4546@testable import QyvoraMLKEM4748/// Differential tests against Apple CryptoKit (an independent implementation, used as an oracle49/// in tests only). CryptoKit offers ML-KEM-768 and ML-KEM-1024; ML-KEM-512 has no counterpart.50@Suite struct MLKEMCryptoKitTests {51    static let rounds = 205253    @Test func mlkem768KeyGenerationFromSeed() throws {54        for _ in 0..<Self.rounds {55            let seed = SystemRandom.bytes(64)56            let ours = try MLKEM.keyPair(.mlkem768, seed: seed)57            let theirs = try MLKEM768.PrivateKey(seedRepresentation: Data(seed), publicKey: nil)58            #expect(Bytes(theirs.publicKey.rawRepresentation) == ours.encapsulationKey)59            #expect(Bytes(theirs.seedRepresentation) == seed)60        }61    }6263    @Test func mlkem1024KeyGenerationFromSeed() throws {64        for _ in 0..<Self.rounds {65            let seed = SystemRandom.bytes(64)66            let ours = try MLKEM.keyPair(.mlkem1024, seed: seed)67            let theirs = try MLKEM1024.PrivateKey(seedRepresentation: Data(seed), publicKey: nil)68            #expect(Bytes(theirs.publicKey.rawRepresentation) == ours.encapsulationKey)69        }70    }7172    @Test func mlkem768Interop() throws {73        for _ in 0..<Self.rounds {74            // our encapsulation, CryptoKit decapsulation (CryptoKit-generated key)75            let theirs = try MLKEM768.PrivateKey.generate()76            let enc = try MLKEM.encapsulate(.mlkem768, encapsulationKey: Bytes(theirs.publicKey.rawRepresentation))77            let k1 = try theirs.decapsulate(Data(enc.ciphertext))78            #expect(k1.withUnsafeBytes { Bytes($0) } == enc.sharedSecret)79            // CryptoKit encapsulation, our decapsulation (our key)80            let ours = MLKEM.generateKeyPair(.mlkem768)81            let pk = try MLKEM768.PublicKey(rawRepresentation: Data(ours.encapsulationKey))82            let r = try pk.encapsulate()83            let k2 = try MLKEM.decapsulate(.mlkem768, decapsulationKey: ours.decapsulationKey, ciphertext: Bytes(r.encapsulated))84            #expect(k2 == r.sharedSecret.withUnsafeBytes { Bytes($0) })85            // implicit rejection agrees too: same key on both sides, corrupted ciphertext86            let seed = SystemRandom.bytes(64)87            let a = try MLKEM.keyPair(.mlkem768, seed: seed)88            let b = try MLKEM768.PrivateKey(seedRepresentation: Data(seed), publicKey: nil)89            var bad = try MLKEM.encapsulate(.mlkem768, encapsulationKey: a.encapsulationKey).ciphertext90            bad[Int(seed[0]) % bad.count] ^= 0x4091            let k3 = try MLKEM.decapsulate(.mlkem768, decapsulationKey: a.decapsulationKey, ciphertext: bad)92            let k4 = try b.decapsulate(Data(bad))93            #expect(k3 == k4.withUnsafeBytes { Bytes($0) })94        }95    }9697    @Test func mlkem1024Interop() throws {98        for _ in 0..<Self.rounds {99            let theirs = try MLKEM1024.PrivateKey.generate()100            let enc = try MLKEM.encapsulate(.mlkem1024, encapsulationKey: Bytes(theirs.publicKey.rawRepresentation))101            let k1 = try theirs.decapsulate(Data(enc.ciphertext))102            #expect(k1.withUnsafeBytes { Bytes($0) } == enc.sharedSecret)103            let ours = MLKEM.generateKeyPair(.mlkem1024)104            let pk = try MLKEM1024.PublicKey(rawRepresentation: Data(ours.encapsulationKey))105            let r = try pk.encapsulate()106            let k2 = try MLKEM.decapsulate(.mlkem1024, decapsulationKey: ours.decapsulationKey, ciphertext: Bytes(r.encapsulated))107            #expect(k2 == r.sharedSecret.withUnsafeBytes { Bytes($0) })108            let seed = SystemRandom.bytes(64)109            let a = try MLKEM.keyPair(.mlkem1024, seed: seed)110            let b = try MLKEM1024.PrivateKey(seedRepresentation: Data(seed), publicKey: nil)111            var bad = try MLKEM.encapsulate(.mlkem1024, encapsulationKey: a.encapsulationKey).ciphertext112            bad[Int(seed[0]) % bad.count] ^= 0x40113            let k3 = try MLKEM.decapsulate(.mlkem1024, decapsulationKey: a.decapsulationKey, ciphertext: bad)114            let k4 = try b.decapsulate(Data(bad))115            #expect(k3 == k4.withUnsafeBytes { Bytes($0) })116        }117    }118119    /// Both implementations must refuse an encapsulation key with a coefficient >= q.120    @Test func modulusCheckAgreement() throws {121        let ours = MLKEM.generateKeyPair(.mlkem768)122        var ek = ours.encapsulationKey123        ek[0] = 0xff124        ek[1] |= 0x0f125        #expect(!MLKEM.isValidEncapsulationKey(.mlkem768, ek))126        #expect(throws: (any Error).self) {127            let pk = try MLKEM768.PublicKey(rawRepresentation: Data(ek))128            _ = try pk.encapsulate()129        }130    }131}132
Files main
Menu

Theme