From 6b665d5b0c4fbffb4c8155db77fcd2889a61c000 Mon Sep 17 00:00:00 2001 From: oskii Date: Mon, 23 Feb 2026 13:04:44 +0100 Subject: [PATCH] fix v2 --- src/Makefile.am | 4 +- src/crypto/dilithium_key.cpp | 72 ------------------------------- src/crypto/dilithium_pubkey.cpp | 76 +++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 74 deletions(-) create mode 100644 src/crypto/dilithium_pubkey.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 2ab252393..12ec0e317 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -586,8 +586,7 @@ crypto_libbtq_crypto_base_la_SOURCES = \ crypto/dilithium/ref/config.h \ crypto/dilithium_wrapper.c \ crypto/dilithium_wrapper.h \ - crypto/dilithium_key.cpp \ - crypto/dilithium_key.h \ + crypto/dilithium_pubkey.cpp \ crypto/hkdf_sha256_32.cpp \ crypto/hkdf_sha256_32.h \ crypto/hmac_sha256.cpp \ @@ -718,6 +717,7 @@ libbtq_common_a_SOURCES = \ external_signer.cpp \ init/common.cpp \ kernel/chainparams.cpp \ + crypto/dilithium_key.cpp \ key.cpp \ key_io.cpp \ merkleblock.cpp \ diff --git a/src/crypto/dilithium_key.cpp b/src/crypto/dilithium_key.cpp index f0f2ea1b6..87460d028 100644 --- a/src/crypto/dilithium_key.cpp +++ b/src/crypto/dilithium_key.cpp @@ -204,78 +204,6 @@ std::vector CDilithiumKey::Serialize() const return std::vector(keydata->begin(), keydata->end()); } -// CDilithiumPubKey implementation - -uint256 CDilithiumPubKey::GetHash() const -{ - return Hash(Span{vch}); -} - -uint160 CDilithiumPubKey::GetID() const -{ - return Hash160(Span{vch}); -} - -bool CDilithiumPubKey::IsValid() const -{ - // A Dilithium public key is valid if it's not all zeros - for (size_t i = 0; i < SIZE; ++i) { - if (vch[i] != 0) { - return true; - } - } - return false; -} - -bool CDilithiumPubKey::IsFullyValid() const -{ - // For now, we use the same validation as IsValid() - // In a more complete implementation, we might want to validate - // the mathematical structure of the public key - return IsValid(); -} - -bool CDilithiumPubKey::Verify(const uint256& hash, const std::vector& vchSig, - const std::vector& context) const -{ - if (!IsValid() || vchSig.empty()) { - return false; - } - - // Use the hash as the message to verify - return VerifyMessage(Span(hash.begin(), hash.size()), vchSig, context); -} - -bool CDilithiumPubKey::VerifyMessage(Span message, const std::vector& vchSig, - const std::vector& context) const -{ - if (!IsValid() || vchSig.empty()) { - return false; - } - - // Verify signature using Dilithium - int result = btq_dilithium_verify( - vchSig.data(), vchSig.size(), - message.data(), message.size(), - context.data(), context.size(), - vch.data() - ); - - - return result == 0; // 0 means success in Dilithium -} - -std::vector CDilithiumPubKey::GetAddress() const -{ - if (!IsValid()) { - return {}; - } - - // For Bitcoin-style addresses, we typically use Hash160 of the public key - uint160 hash = GetID(); - return std::vector(hash.begin(), hash.end()); -} - // Global initialization functions void DilithiumInit() diff --git a/src/crypto/dilithium_pubkey.cpp b/src/crypto/dilithium_pubkey.cpp new file mode 100644 index 000000000..7570b4f96 --- /dev/null +++ b/src/crypto/dilithium_pubkey.cpp @@ -0,0 +1,76 @@ +// Copyright (c) 2024 The BTQ Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +// CDilithiumPubKey implementation — consensus-safe (no LockedPoolManager dependency). +// CDilithiumKey and extended key implementations live in dilithium_key.cpp. + +#include + +#include + +extern "C" { +#include "dilithium_wrapper.h" +} + +uint256 CDilithiumPubKey::GetHash() const +{ + return Hash(Span{vch}); +} + +uint160 CDilithiumPubKey::GetID() const +{ + return Hash160(Span{vch}); +} + +bool CDilithiumPubKey::IsValid() const +{ + for (size_t i = 0; i < SIZE; ++i) { + if (vch[i] != 0) { + return true; + } + } + return false; +} + +bool CDilithiumPubKey::IsFullyValid() const +{ + return IsValid(); +} + +bool CDilithiumPubKey::Verify(const uint256& hash, const std::vector& vchSig, + const std::vector& context) const +{ + if (!IsValid() || vchSig.empty()) { + return false; + } + + return VerifyMessage(Span(hash.begin(), hash.size()), vchSig, context); +} + +bool CDilithiumPubKey::VerifyMessage(Span message, const std::vector& vchSig, + const std::vector& context) const +{ + if (!IsValid() || vchSig.empty()) { + return false; + } + + int result = btq_dilithium_verify( + vchSig.data(), vchSig.size(), + message.data(), message.size(), + context.data(), context.size(), + vch.data() + ); + + return result == 0; +} + +std::vector CDilithiumPubKey::GetAddress() const +{ + if (!IsValid()) { + return {}; + } + + uint160 hash = GetID(); + return std::vector(hash.begin(), hash.end()); +}