xrpld
Loading...
Searching...
No Matches
include/xrpl/protocol/ConfidentialTransfer.h
1#pragma once
2
3#include <xrpl/basics/Buffer.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/protocol/AccountID.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STInteger.h> // IWYU pragma: keep
9#include <xrpl/protocol/STObject.h>
10#include <xrpl/protocol/TER.h>
11#include <xrpl/protocol/UintTypes.h>
12#include <xrpl/protocol/detail/secp256k1.h>
13
14#include <secp256k1.h>
15
16#include <cstdint>
17#include <limits>
18#include <optional>
19
20namespace xrpl {
21
42
47struct EcPair
48{
52 secp256k1_pubkey c1;
53
57 secp256k1_pubkey c2;
58};
59
69inline void
71{
72 // Retrieve current version and increment, wrapping back to 0 at UINT32_MAX.
73 // The wrap is computed explicitly rather than relying on unsigned overflow
74 // of `+ 1u`, as it trips the unsigned-integer-overflow sanitizer in the UBSan CI build.
75 auto const current = mptoken[~sfConfidentialBalanceVersion].valueOr(0u);
76 mptoken[sfConfidentialBalanceVersion] =
77 current == std::numeric_limits<std::uint32_t>::max() ? 0u : current + 1u;
78}
79
95 AccountID const& account,
96 uint192 const& issuanceID,
97 std::uint32_t sequence,
98 AccountID const& destination,
99 std::uint32_t version);
100
115 AccountID const& account,
116 uint192 const& issuanceID,
117 std::uint32_t sequence,
118 AccountID const& holder);
119
132getConvertContextHash(AccountID const& account, uint192 const& issuanceID, std::uint32_t sequence);
133
148 AccountID const& account,
149 uint192 const& issuanceID,
150 std::uint32_t sequence,
151 std::uint32_t version);
152
165makeEcPair(Slice const& buffer);
166
179serializeEcPair(EcPair const& pair);
180
187bool
188isValidCiphertext(Slice const& buffer);
189
200bool
201isValidCompressedECPoint(Slice const& buffer);
202
214homomorphicAdd(Slice const& a, Slice const& b);
215
227homomorphicSubtract(Slice const& a, Slice const& b);
228
244rerandomizeCiphertext(Slice const& ciphertext, Slice const& pubKeySlice, Slice const& randomness);
245
259encryptAmount(uint64_t const amt, Slice const& pubKeySlice, Slice const& blindingFactor);
260
274encryptCanonicalZeroAmount(Slice const& pubKeySlice, AccountID const& account, MPTID const& mptId);
275
287TER
288verifySchnorrProof(Slice const& pubKeySlice, Slice const& proofSlice, uint256 const& contextHash);
289
301NotTEC
302checkEncryptedAmountFormat(STObject const& object);
303
318TER
320 uint64_t const amount,
321 Slice const& blindingFactor,
322 ConfidentialRecipient const& holder,
323 ConfidentialRecipient const& issuer,
325
335constexpr uint8_t
337{
338 return hasAuditor ? 4 : 3;
339}
340
356TER
358 uint64_t const amount,
359 Slice const& proof,
360 Slice const& pubKeySlice,
361 Slice const& ciphertext,
362 uint256 const& contextHash);
363
374Buffer
376
399TER
401 Slice const& proof,
402 ConfidentialRecipient const& sender,
403 ConfidentialRecipient const& destination,
404 ConfidentialRecipient const& issuer,
406 Slice const& spendingBalance,
407 Slice const& amountCommitment,
408 Slice const& balanceCommitment,
409 uint256 const& contextHash);
410
427TER
429 Slice const& proof,
430 Slice const& pubKeySlice,
431 Slice const& spendingBalance,
432 Slice const& balanceCommitment,
433 uint64_t amount,
434 uint256 const& contextHash);
435
436} // namespace xrpl
An immutable linear range of bytes.
Definition Slice.h:28
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 192 > uint192
Definition base_uint.h:581
NotTEC checkEncryptedAmountFormat(STObject const &object)
Validates the format of encrypted amount fields in a transaction.
std::optional< Buffer > rerandomizeCiphertext(Slice const &ciphertext, Slice const &pubKeySlice, Slice const &randomness)
Re-randomizes an ElGamal ciphertext without changing its plaintext.
TER verifySchnorrProof(Slice const &pubKeySlice, Slice const &proofSlice, uint256 const &contextHash)
Verifies a Schnorr proof of knowledge of an ElGamal private key.
std::optional< Buffer > encryptCanonicalZeroAmount(Slice const &pubKeySlice, AccountID const &account, MPTID const &mptId)
Generates the canonical zero encryption for a specific MPToken.
std::optional< Buffer > encryptAmount(uint64_t const amt, Slice const &pubKeySlice, Slice const &blindingFactor)
Encrypts an amount using ElGamal encryption.
bool isValidCompressedECPoint(Slice const &buffer)
Verifies that a buffer contains a valid, parsable compressed EC point.
constexpr uint8_t getConfidentialRecipientCount(bool hasAuditor)
Returns the number of recipients in a confidential transfer.
std::optional< EcPair > makeEcPair(Slice const &buffer)
Parses an ElGamal ciphertext into two secp256k1 public key components.
std::optional< Buffer > serializeEcPair(EcPair const &pair)
Serializes an EcPair into compressed form.
TER verifyRevealedAmount(uint64_t const amount, Slice const &blindingFactor, ConfidentialRecipient const &holder, ConfidentialRecipient const &issuer, std::optional< ConfidentialRecipient > const &auditor)
Verifies revealed amount encryptions for all recipients.
uint256 getConvertBackContextHash(AccountID const &account, uint192 const &issuanceID, std::uint32_t sequence, std::uint32_t version)
Generates the context hash for ConfidentialMPTConvertBack transactions.
bool isValidCiphertext(Slice const &buffer)
Verifies that a buffer contains two valid, parsable EC public keys.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
std::optional< Buffer > homomorphicSubtract(Slice const &a, Slice const &b)
Homomorphically subtracts two ElGamal ciphertexts.
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:54
uint256 getConvertContextHash(AccountID const &account, uint192 const &issuanceID, std::uint32_t sequence)
Generates the context hash for ConfidentialMPTConvert transactions.
uint256 getClawbackContextHash(AccountID const &account, uint192 const &issuanceID, std::uint32_t sequence, AccountID const &holder)
Generates the context hash for ConfidentialMPTClawback transactions.
Buffer generateBlindingFactor()
Generates a cryptographically secure blinding factor (size=xrpl::kEcBlindingFactorLength).
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
TER verifyConvertBackProof(Slice const &proof, Slice const &pubKeySlice, Slice const &spendingBalance, Slice const &balanceCommitment, uint64_t amount, uint256 const &contextHash)
Verifies all zero-knowledge proofs for a ConfidentialMPTConvertBack transaction.
uint256 getSendContextHash(AccountID const &account, uint192 const &issuanceID, std::uint32_t sequence, AccountID const &destination, std::uint32_t version)
Generates the context hash for ConfidentialMPTSend transactions.
TERSubset< CanCvtToTER > TER
Definition TER.h:647
void incrementConfidentialVersion(STObject &mptoken)
Increments the confidential balance version counter on an MPToken.
BaseUInt< 256 > uint256
Definition base_uint.h:580
std::optional< Buffer > homomorphicAdd(Slice const &a, Slice const &b)
Homomorphically adds two ElGamal ciphertexts.
TER verifySendProof(Slice const &proof, ConfidentialRecipient const &sender, ConfidentialRecipient const &destination, ConfidentialRecipient const &issuer, std::optional< ConfidentialRecipient > const &auditor, Slice const &spendingBalance, Slice const &amountCommitment, Slice const &balanceCommitment, uint256 const &contextHash)
Verifies all zero-knowledge proofs for a ConfidentialMPTSend transaction.
TER verifyClawbackProof(uint64_t const amount, Slice const &proof, Slice const &pubKeySlice, Slice const &ciphertext, uint256 const &contextHash)
Verifies a compact sigma clawback proof.
Bundles an ElGamal public key with its associated encrypted amount.
Slice encryptedAmount
The encrypted amount ciphertext (size=xrpl::kEcGamalEncryptedTotalLength).
Slice publicKey
The recipient's ElGamal public key (size=xrpl::kEcPubKeyLength).
Holds two secp256k1 public key components representing an ElGamal ciphertext (C1, C2).
secp256k1_pubkey c2
Second ElGamal ciphertext component.
secp256k1_pubkey c1
First ElGamal ciphertext component.