Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
72 changes: 0 additions & 72 deletions src/crypto/dilithium_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,78 +204,6 @@ std::vector<unsigned char> CDilithiumKey::Serialize() const
return std::vector<unsigned char>(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<unsigned char>& vchSig,
const std::vector<unsigned char>& context) const
{
if (!IsValid() || vchSig.empty()) {
return false;
}

// Use the hash as the message to verify
return VerifyMessage(Span<const unsigned char>(hash.begin(), hash.size()), vchSig, context);
}

bool CDilithiumPubKey::VerifyMessage(Span<const unsigned char> message, const std::vector<unsigned char>& vchSig,
const std::vector<unsigned char>& 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<unsigned char> CDilithiumPubKey::GetAddress() const
{
if (!IsValid()) {
return {};
}

// For Bitcoin-style addresses, we typically use Hash160 of the public key
uint160 hash = GetID();
return std::vector<unsigned char>(hash.begin(), hash.end());
}

// Global initialization functions

void DilithiumInit()
Expand Down
76 changes: 76 additions & 0 deletions src/crypto/dilithium_pubkey.cpp
Original file line number Diff line number Diff line change
@@ -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 <crypto/dilithium_key.h>

#include <hash.h>

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<unsigned char>& vchSig,
const std::vector<unsigned char>& context) const
{
if (!IsValid() || vchSig.empty()) {
return false;
}

return VerifyMessage(Span<const unsigned char>(hash.begin(), hash.size()), vchSig, context);
}

bool CDilithiumPubKey::VerifyMessage(Span<const unsigned char> message, const std::vector<unsigned char>& vchSig,
const std::vector<unsigned char>& 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<unsigned char> CDilithiumPubKey::GetAddress() const
{
if (!IsValid()) {
return {};
}

uint160 hash = GetID();
return std::vector<unsigned char>(hash.begin(), hash.end());
}
Loading