SPB Git

Repository Public

KeccakBasicTests.swift 3.5 KB 76 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraKeccakTests/KeccakBasicTests.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 Testing44import QyvoraMath45@testable import QyvoraKeccak4647@Suite struct KeccakBasicTests {48    @Test func sha3KnownAnswers() {49        #expect(SHA3.sha3_256([]).hex == "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")50        #expect(SHA3.sha3_256(Array("abc".utf8)).hex == "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532")51        #expect(SHA3.sha3_224(Array("abc".utf8)).hex == "e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf")52        #expect(SHA3.sha3_512([]).hex == "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")53        #expect(SHA3.keccak256([]).hex == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")54        #expect(SHAKE.shake128([], outputLength: 32).hex == "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26")55        #expect(SHAKE.shake256([], outputLength: 32).hex == "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f")56    }5758    @Test func differentialAgainstCryptoKit() {59        var rng = SystemRandomNumberGenerator()60        for n in [0, 1, 71, 72, 73, 135, 136, 137, 167, 168, 169, 500, 4096] {61            let m = (0..<n).map { _ in UInt8.random(in: 0...255, using: &rng) }62            #expect(SHA3.sha3_256(m) == Array(CryptoKit.SHA3_256.hash(data: Data(m))))63            #expect(SHA3.sha3_384(m) == Array(CryptoKit.SHA3_384.hash(data: Data(m))))64            #expect(SHA3.sha3_512(m) == Array(CryptoKit.SHA3_512.hash(data: Data(m))))65        }66    }6768    @Test func incrementalEqualsOneShot() {69        let m = (0..<1000).map { UInt8(truncatingIfNeeded: $0 &* 7) }70        var x = SHAKE(256)71        x.absorb(Array(m[0..<3])); x.absorb(Array(m[3..<140])); x.absorb(Array(m[140...]))72        let a = x.squeeze(10) + x.squeeze(300)73        #expect(a == SHAKE.shake256(m, outputLength: 310))74    }75}76
Files main
Menu

Theme