Skip to content
Draft
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
13 changes: 0 additions & 13 deletions include/xrpl/protocol/ConfidentialTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,6 @@ verifyClawbackProof(
Slice const& ciphertext,
uint256 const& contextHash);

/**
* @brief Generates a cryptographically secure blinding factor
* (size=xrpl::kEcBlindingFactorLength).
*
* Produces random bytes suitable for use as an ElGamal blinding factor
* or Pedersen commitment randomness.
*
* @return A buffer containing the random blinding factor
* (size=xrpl::kEcBlindingFactorLength).
*/
Buffer
generateBlindingFactor();

/**
* @brief Verifies all zero-knowledge proofs for a ConfidentialMPTSend transaction.
*
Expand Down
4 changes: 2 additions & 2 deletions include/xrpl/protocol/detail/sfields.macro
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ TYPED_SFIELD(sfParentBatchID, UINT256, 36)
TYPED_SFIELD(sfLoanBrokerID, UINT256, 37,
SField::kSmdPseudoAccount | SField::kSmdDefault)
TYPED_SFIELD(sfLoanID, UINT256, 38)
TYPED_SFIELD(sfBlindingFactor, UINT256, 39)
TYPED_SFIELD(sfReferenceHolding, UINT256, 40)
TYPED_SFIELD(sfReferenceHolding, UINT256, 39)
TYPED_SFIELD(sfBlindingFactor, UINT256, 40)

// number (common)
TYPED_SFIELD(sfNumber, NUMBER, 1)
Expand Down
18 changes: 6 additions & 12 deletions src/libxrpl/protocol/ConfidentialTransfer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <xrpl/protocol/ConfidentialTransfer.h>

#include <xrpl/basics/Buffer.h>
#include <xrpl/basics/Log.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/contract.h>
#include <xrpl/basics/strHex.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/SField.h>
Expand Down Expand Up @@ -197,6 +199,8 @@ homomorphicAdd(Slice const& a, Slice const& b)
secp256k1Context(), &sum.c1, &sum.c2, &pairA->c1, &pairA->c2, &pairB->c1, &pairB->c2);
res != 1)
{
JLOG(debugLog().error()) << "homomorphicAdd: secp256k1_elgamal_add failed"
<< " a=" << strHex(a) << " b=" << strHex(b);
return std::nullopt;
}

Expand All @@ -220,6 +224,8 @@ homomorphicSubtract(Slice const& a, Slice const& b)
secp256k1Context(), &diff.c1, &diff.c2, &pairA->c1, &pairA->c2, &pairB->c1, &pairB->c2);
res != 1)
{
JLOG(debugLog().error()) << "homomorphicSubtract: secp256k1_elgamal_subtract failed"
<< " a=" << strHex(a) << " b=" << strHex(b);
return std::nullopt;
}

Expand All @@ -236,18 +242,6 @@ rerandomizeCiphertext(Slice const& ciphertext, Slice const& pubKeySlice, Slice c
return homomorphicAdd(ciphertext, *zero);
}

Buffer
generateBlindingFactor()
{
unsigned char blindingFactor[kEcBlindingFactorLength];

// todo: might need to be updated using another RNG
if (RAND_bytes(blindingFactor, kEcBlindingFactorLength) != 1)
Throw<std::runtime_error>("Failed to generate random number");

return Buffer(blindingFactor, kEcBlindingFactorLength);
}

std::optional<Buffer>
encryptAmount(uint64_t const amt, Slice const& pubKeySlice, Slice const& blindingFactor)
{
Expand Down
4 changes: 2 additions & 2 deletions src/test/jtx/ConfidentialTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ class ConfidentialTransferTestBase : public beast::unit_test::Suite
: sendAmount(amount)
, nRecipients(auditor ? 4 : 3)
, version(mpt.getMPTokenVersion(sender))
, blindingFactor(generateBlindingFactor())
, blindingFactor(test::jtx::generateBlindingFactor())
, amountBlindingFactor(blindingFactor)
, balanceBlindingFactor(generateBlindingFactor())
, balanceBlindingFactor(test::jtx::generateBlindingFactor())
, senderAmt(mpt.encryptAmount(sender, amount, blindingFactor))
, destAmt(mpt.encryptAmount(dest, amount, blindingFactor))
, issuerAmt(mpt.encryptAmount(issuer, amount, blindingFactor))
Expand Down
23 changes: 23 additions & 0 deletions src/test/jtx/mpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,35 @@
#include <xrpl/protocol/UintTypes.h>
#include <xrpl/protocol/XRPAmount.h>

#include <openssl/rand.h>

#include <cstddef>
#include <cstdint>
#include <cstring>

namespace xrpl::test::jtx {

/**
* @brief Generates a cryptographically secure blinding factor
* (size=xrpl::kEcBlindingFactorLength).
*
* Produces random bytes suitable for use as an ElGamal blinding factor
* or Pedersen commitment randomness.
*
* @return A buffer containing the random blinding factor
* (size=xrpl::kEcBlindingFactorLength).
*/
inline Buffer
generateBlindingFactor()
{
unsigned char blindingFactor[kEcBlindingFactorLength];

if (RAND_bytes(blindingFactor, kEcBlindingFactorLength) != 1)
Throw<std::runtime_error>("Failed to generate random number");

return Buffer(blindingFactor, kEcBlindingFactorLength);
}

class MPTTester;

auto const kMptDexFlags = tfMPTCanTrade | tfMPTCanTransfer;
Expand Down
Loading