1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-26 09:21:28 +00:00
pqcrypto/test/ut.cpp

96 lines
3.5 KiB
C++
Raw Normal View History

2021-03-24 10:25:45 +00:00
#include <algorithm>
2021-12-17 16:10:15 +00:00
#include <sstream>
2021-03-24 10:25:45 +00:00
#include <vector>
#include <gtest/gtest.h>
2021-03-24 23:34:34 +00:00
#include <pqc/pqc.h>
2021-06-21 09:20:48 +01:00
#include <random>
2022-07-11 18:19:21 +01:00
extern "C" {
#include "sign/dilithium/dilithium2/clean/reduce.h"
#include "sign/dilithium/dilithium2/clean/params.h"
}
2021-03-24 10:25:45 +00:00
2021-06-29 23:41:13 +01:00
TEST(KEM,OneOff) {
2021-03-24 10:25:45 +00:00
2021-07-10 00:33:09 +01:00
for (int i=0; i<PQC_ALG_KEM_MAX; i++) {
const pqc_ctx_t *p = pqc_kem_alg_by_id(i);
std::vector<uint8_t> ct(pqc_ciphertext_bsz(p));
std::vector<uint8_t> ss1(pqc_shared_secret_bsz(p));
std::vector<uint8_t> ss2(pqc_shared_secret_bsz(p));
std::vector<uint8_t> sk(pqc_private_key_bsz(p));
std::vector<uint8_t> pk(pqc_public_key_bsz(p));
ASSERT_TRUE(
pqc_keygen(p, pk.data(), sk.data()));
ASSERT_TRUE(
pqc_kem_encapsulate(p, ct.data(), ss1.data(), pk.data()));
ASSERT_TRUE(
pqc_kem_decapsulate(p, ss2.data(), ct.data(), sk.data()));
ASSERT_TRUE(
std::equal(ss1.begin(), ss1.end(), ss2.begin()));
}
2021-03-24 10:25:45 +00:00
}
2021-06-29 23:41:13 +01:00
TEST(SIGN,OneOff) {
2021-03-24 10:25:45 +00:00
2021-06-21 09:20:48 +01:00
std::random_device rd;
std::uniform_int_distribution<uint8_t> dist(0, 0xFF);
uint8_t msg[1234] = {0};
2021-07-10 00:33:09 +01:00
for (int i=0; i<PQC_ALG_SIG_MAX; i++) {
const pqc_ctx_t *p = pqc_sig_alg_by_id(i);
// generate some random msg
for (auto &x : msg) {x = dist(rd);}
std::vector<uint8_t> sig(pqc_signature_bsz(p));
std::vector<uint8_t> sk(pqc_private_key_bsz(p));
std::vector<uint8_t> pk(pqc_public_key_bsz(p));
ASSERT_TRUE(
pqc_keygen(p, pk.data(), sk.data()));
uint64_t sigsz = sig.size();
ASSERT_TRUE(
pqc_sig_create(p, sig.data(), &sigsz, msg, 1234, sk.data()));
ASSERT_TRUE(
pqc_sig_verify(p, sig.data(), sigsz, msg, 1234, pk.data()));
}
2021-03-24 10:25:45 +00:00
}
2021-12-17 16:10:15 +00:00
TEST(KEMSIG,PrintSizes) {
for (int i=0; i<PQC_ALG_SIG_MAX; i++) {
std::stringstream out;
const pqc_ctx_t *p = pqc_sig_alg_by_id(i);
out << std::setw(30) << std::left << p->alg_name
<< " :pk: " << std::setw(15) << pqc_public_key_bsz(p)
<< " :sign: " << std::setw(15) << pqc_signature_bsz(p);
std::cout << out.str() << std::endl;
}
for (int i=0; i<PQC_ALG_KEM_MAX; i++) {
std::stringstream out;
const pqc_ctx_t *p = pqc_kem_alg_by_id(i);
out << std::setw(30) << std::left << p->alg_name
<< " :pk: " << std::setw(15) << pqc_public_key_bsz(p)
<< " :ct: " << std::setw(15) << pqc_ciphertext_bsz(p);
std::cout << out.str() << std::endl;
}
}
2022-07-11 18:19:21 +01:00
TEST(Dilithium, MontREDC) {
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(0), 0);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(Q), 0);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(Q*100), 0);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(1), -114592);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(-1), 114592);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(((uint64_t)Q<<31)-1), 114592);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(-((int64_t)Q<<31)),0);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(-((1ULL<<31)*(int64_t)Q)+1), -114592);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(-(((int64_t)Q)<<31)+1), -114592);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce((uint64_t)1<<15), -523840);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce((uint64_t)1<<31), 4190209);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(3347556), 2070606);
ASSERT_EQ(PQCLEAN_DILITHIUM2_CLEAN_montgomery_reduce(-2581810), 910169);
}