PHP Classes

File: tests/XWingTest.php

Recommend this page to a friend!
  Packages of Scott Arciszewski   ext-pqcrypto   tests/XWingTest.php   Download  
File: tests/XWingTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: ext-pqcrypto
PHP extension to encrypt data with FIPS algorithms
Author: By
Last change:
Date: 8 days ago
Size: 1,750 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
PQCrypto\Tests;

use
PHPUnit\Framework\TestCase;
use
PQCrypto\XWing;

final class
XWingTest extends TestCase
{
    public function
testKeygenSizes(): void
   
{
        [
$sk, $pk] = XWing::generateKeypair();
       
$this->assertSame(32, strlen($sk->bytes()));
       
$this->assertSame(1216, strlen($pk->bytes()));
    }

    public function
testEncapsulateDecapsulateRoundTrip(): void
   
{
        [
$sk, $pk] = XWing::generateKeypair();
        [
$ss, $ct] = $pk->encapsulate();
       
$this->assertSame(32, strlen($ss));
       
$this->assertSame(1120, strlen($ct));
       
$rss = $sk->decapsulate($ct);
       
$this->assertTrue(hash_equals($rss, $ss));
    }

    public function
testDecapsulationKeyFromBytesRoundTrip(): void
   
{
        [
$sk, $pk] = XWing::generateKeypair();
       
$restored = XWing\DecapsulationKey::fromBytes($sk->bytes());
        [
$ss, $ct] = $pk->encapsulate();
       
$rss = $restored->decapsulate($ct);
       
$this->assertTrue(hash_equals($rss, $ss));
    }

    public function
testEncapsulationKeyFromBytesRoundTrip(): void
   
{
        [
$sk, $pk] = XWing::generateKeypair();
       
$restored = XWing\EncapsulationKey::fromBytes($pk->bytes());
        [
$ss, $ct] = $restored->encapsulate();
       
$rss = $sk->decapsulate($ct);
       
$this->assertTrue(hash_equals($rss, $ss));
    }

    public function
testRejectsWrongSeedLength(): void
   
{
       
$this->expectException(\Exception::class);
       
XWing\DecapsulationKey::fromBytes('short');
    }

    public function
testRejectsWrongCiphertextLength(): void
   
{
        [
$sk, $_] = XWing::generateKeypair();
       
$this->expectException(\Exception::class);
       
$sk->decapsulate('wrong');
    }
}