Skip to content

Commit

Permalink
Libspats cleanup refacor (#1517)
Browse files Browse the repository at this point in the history
* Cleanup and refactor

* More cleanup

* Reverted some changes, bug fixes

* More fixes
  • Loading branch information
levonpetrosyan93 authored Jan 12, 2025
1 parent e8d39a8 commit 983dc27
Show file tree
Hide file tree
Showing 54 changed files with 337 additions and 3,374 deletions.
28 changes: 27 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ LIBBITCOIN_CLI=libbitcoin_cli.a
LIBBITCOIN_UTIL=libbitcoin_util.a
LIBLELANTUS=liblelantus.a
LIBSPARK=libspark.a
LIBSPATS=libspats.a
LIBBITCOIN_CRYPTO=crypto/libbitcoin_crypto.a
LIBBITCOINQT=qt/libfiroqt.a
LIBSECP256K1=secp256k1/libsecp256k1.la
Expand Down Expand Up @@ -88,7 +89,8 @@ EXTRA_LIBRARIES += \
$(LIBBITCOIN_ZMQ) \
$(LIBFIRO_SIGMA) \
$(LIBLELANTUS) \
$(LIBSPARK)
$(LIBSPARK) \
$(LIBSPATS)

lib_LTLIBRARIES = $(LIBBITCOINCONSENSUS)

Expand Down Expand Up @@ -693,6 +695,29 @@ libspark_a_SOURCES = \
libspark/bech32.h \
libspark/bech32.cpp

libspats_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
libspats_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libspats_a_SOURCES = \
libspats/bpplus.cpp \
libspats/bpplus.h \
libspats/bpplus_proof.h \
libspats/coin.cpp \
libspats/coin.h \
libspats/base_asset.h \
libspats/base_asset.cpp \
libspats/base_asset_proof.h \
libspats/type_proof.h \
libspats/type.cpp \
libspats/type.h \
libspats/balance.h \
libspats/balance.cpp \
libspats/balance_proof.h \
libspats/mint_transaction.cpp \
libspats/mint_transaction.h \
libspats/spend_transaction.cpp \
libspats/spend_transaction.h \
libspats/util.h

liblelantus_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
liblelantus_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
liblelantus_a_SOURCES = \
Expand Down Expand Up @@ -799,6 +824,7 @@ firod_LDADD = \
$(LIBFIRO_SIGMA) \
$(LIBLELANTUS) \
$(LIBSPARK) \
$(LIBSPATS) \
$(LIBBITCOIN_ZMQ) \
$(LIBBITCOIN_CONSENSUS) \
$(LIBBITCOIN_CRYPTO) \
Expand Down
48 changes: 2 additions & 46 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -197,51 +197,6 @@ BITCOIN_TESTS = \
test/evo_simplifiedmns_tests.cpp \
test/progpow_tests.cpp \
test/bls_tests.cpp \
libspats/aead.cpp \
libspats/aead.h \
libspats/bech32.cpp \
libspats/bech32.h \
libspats/bpplus.cpp \
libspats/bpplus.h \
libspats/bpplus_proof.h \
libspats/chaum.cpp \
libspats/chaum.h \
libspats/chaum_proof.h \
libspats/coin.cpp \
libspats/coin.h \
libspats/f4grumble.cpp \
libspats/f4grumble.h \
libspats/grootle.cpp \
libspats/grootle.h \
libspats/grootle_proof.h \
libspats/hash.cpp \
libspats/hash.h \
libspats/kdf.cpp \
libspats/kdf.h \
libspats/keys.cpp \
libspats/keys.h \
libspats/base_asset.h \
libspats/base_asset.cpp \
libspats/base_asset_proof.h \
libspats/type_proof.h \
libspats/type.cpp \
libspats/type.h \
libspats/balance.h \
libspats/balance.cpp \
libspats/balance_proof.h \
libspats/mint_transaction.cpp \
libspats/mint_transaction.h \
libspats/params.cpp \
libspats/params.h \
libspats/schnorr.cpp \
libspats/schnorr.h \
libspats/schnorr_proof.h \
libspats/spend_transaction.cpp \
libspats/spend_transaction.h \
libspats/transcript.cpp \
libspats/transcript.h \
libspats/util.cpp \
libspats/util.h \
libspats/test/bpplus_test.cpp \
libspats/test/coin_test.cpp \
libspats/test/type_test.cpp \
Expand Down Expand Up @@ -269,7 +224,7 @@ test_test_bitcoin_LDADD = $(LIBBITCOIN_SERVER) -ltor

test_test_bitcoin_SOURCES = $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -I$(builddir)/test/ $(TESTDEFS) $(EVENT_CFLAGS)
test_test_bitcoin_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBFIRO_SIGMA) $(LIBLELANTUS) $(LIBSPARK) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBLEVELDB_SSE42) $(LIBMEMENV) \
test_test_bitcoin_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBFIRO_SIGMA) $(LIBLELANTUS) $(LIBSPARK) $(LIBSPATS) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBLEVELDB_SSE42) $(LIBMEMENV) \
$(BACKTRACE_LIB) $(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(LIBSECP256K1) $(EVENT_PTHREADS_LIBS) $(ZMQ_LIBS) $(ZLIB_LIBS)
test_test_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
if ENABLE_WALLET
Expand Down Expand Up @@ -297,6 +252,7 @@ test_test_bitcoin_fuzzy_LDADD = \
$(LIBBITCOIN_SERVER) \
$(LIBLELANTUS) \
$(LIBSPARK) \
$(LIBSPATS) \
$(LIBBITCOIN_COMMON) \
$(LIBBITCOIN_UTIL) \
$(LIBBITCOIN_CONSENSUS) \
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ bool Coin::validate(
// Recover a coin
RecoveredCoinData Coin::recover(const FullViewKey& full_view_key, const IdentifiedCoinData& data) {
RecoveredCoinData recovered_data;
recovered_data.s = SparkUtils::hash_ser(data.k, this->serial_context) + SparkUtils::hash_Q2(full_view_key.get_s1(), data.i) + full_view_key.get_s2();
recovered_data.s = SparkUtils::hash_ser(data.k, this->serial_context) + SparkUtils::hash_Q2(full_view_key.get_s1(), data.i) + full_view_key.get_s2();
recovered_data.T = (this->params->get_U() + full_view_key.get_D().inverse())*recovered_data.s.inverse();

return recovered_data;
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/grootle.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Grootle {
const std::vector<GroupElement>& Hi,
const std::size_t n,
const std::size_t m
);
);

void prove(const std::size_t l,
const Scalar& s,
Expand Down
30 changes: 30 additions & 0 deletions src/libspark/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Params::Params(
)
{
// Global generators
this->E = SparkUtils::hash_generator(LABEL_GENERATOR_E);
this->F = SparkUtils::hash_generator(LABEL_GENERATOR_F);
this->G.set_base_g();
this->H = SparkUtils::hash_generator(LABEL_GENERATOR_H);
Expand All @@ -65,9 +66,13 @@ Params::Params(

// Range proof parameters
this->max_M_range = max_M_range;
this->E_range.resize(64*max_M_range);
this->F_range.resize(64*max_M_range);
this->G_range.resize(64*max_M_range);
this->H_range.resize(64*max_M_range);
for (std::size_t i = 0; i < 64*max_M_range; i++) {
this->E_range[i] = SparkUtils::hash_generator(LABEL_GENERATOR_E_RANGE + " " + std::to_string(i));
this->F_range[i] = SparkUtils::hash_generator(LABEL_GENERATOR_F_RANGE + " " + std::to_string(i));
this->G_range[i] = SparkUtils::hash_generator(LABEL_GENERATOR_G_RANGE + " " + std::to_string(i));
this->H_range[i] = SparkUtils::hash_generator(LABEL_GENERATOR_H_RANGE + " " + std::to_string(i));
}
Expand All @@ -78,9 +83,13 @@ Params::Params(
}
this->n_grootle = n_grootle;
this->m_grootle = m_grootle;
this->E_grootle.resize(n_grootle * m_grootle);
this->F_grootle.resize(n_grootle * m_grootle);
this->G_grootle.resize(n_grootle * m_grootle);
this->H_grootle.resize(n_grootle * m_grootle);
for (std::size_t i = 0; i < n_grootle * m_grootle; i++) {
this->E_grootle[i] = SparkUtils::hash_generator(LABEL_GENERATOR_E_GROOTLE + " " + std::to_string(i));
this->F_grootle[i] = SparkUtils::hash_generator(LABEL_GENERATOR_F_GROOTLE + " " + std::to_string(i));
this->G_grootle[i] = SparkUtils::hash_generator(LABEL_GENERATOR_G_GROOTLE + " " + std::to_string(i));
this->H_grootle[i] = SparkUtils::hash_generator(LABEL_GENERATOR_H_GROOTLE + " " + std::to_string(i));
}
Expand All @@ -102,10 +111,22 @@ const GroupElement& Params::get_U() const {
return this->U;
}

const GroupElement& Params::get_E() const {
return this->E;
}

const std::size_t Params::get_memo_bytes() const {
return this->memo_bytes;
}

const std::vector<GroupElement>& Params::get_E_range() const {
return this->E_range;
}

const std::vector<GroupElement>& Params::get_F_range() const {
return this->F_range;
}

const std::vector<GroupElement>& Params::get_G_range() const {
return this->G_range;
}
Expand All @@ -114,10 +135,19 @@ const std::vector<GroupElement>& Params::get_H_range() const {
return this->H_range;
}

const std::vector<GroupElement>& Params::get_E_grootle() const {
return this->E_grootle;
}

const std::vector<GroupElement>& Params::get_G_grootle() const {
return this->G_grootle;
}


const std::vector<GroupElement>& Params::get_F_grootle() const {
return this->F_grootle;
}

const std::vector<GroupElement>& Params::get_H_grootle() const {
return this->H_grootle;
}
Expand Down
10 changes: 9 additions & 1 deletion src/libspark/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ class Params {
const GroupElement& get_G() const;
const GroupElement& get_H() const;
const GroupElement& get_U() const;
const GroupElement& get_E() const;

const std::size_t get_memo_bytes() const;

std::size_t get_max_M_range() const;
const std::vector<GroupElement>& get_E_range() const;
const std::vector<GroupElement>& get_F_range() const;
const std::vector<GroupElement>& get_G_range() const;
const std::vector<GroupElement>& get_H_range() const;

std::size_t get_n_grootle() const;
std::size_t get_m_grootle() const;
const std::vector<GroupElement>& get_E_grootle() const;
const std::vector<GroupElement>& get_F_grootle() const;
const std::vector<GroupElement>& get_G_grootle() const;
const std::vector<GroupElement>& get_H_grootle() const;

Expand All @@ -44,6 +49,7 @@ class Params {
static std::unique_ptr<Params> instance;

// Global generators
GroupElement E;
GroupElement F;
GroupElement G;
GroupElement H;
Expand All @@ -54,12 +60,14 @@ class Params {

// Range proof parameters
std::size_t max_M_range;
std::vector<GroupElement> G_range, H_range;
std::vector<GroupElement> G_range, H_range, E_range, F_range;

// One-of-many parameters
std::size_t n_grootle, m_grootle;
std::vector<GroupElement> G_grootle;
std::vector<GroupElement> H_grootle;
std::vector<GroupElement> E_grootle;
std::vector<GroupElement> F_grootle;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/spend_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ bool SpendTransaction::verify(
params->get_H_grootle(),
params->get_n_grootle(),
params->get_m_grootle()
);
);
for (auto grootle_bucket : grootle_buckets) {
std::size_t cover_set_id = grootle_bucket.first;
std::vector<std::pair<std::size_t, std::size_t>> proof_indexes = grootle_bucket.second;
Expand Down
7 changes: 7 additions & 0 deletions src/libspark/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ const std::string LABEL_GENERATOR_H_RANGE = "H_RANGE";
const std::string LABEL_GENERATOR_G_GROOTLE = "G_GROOTLE";
const std::string LABEL_GENERATOR_H_GROOTLE = "H_GROOTLE";

// Spark assets Generator labels
const std::string LABEL_GENERATOR_E = "E";
const std::string LABEL_GENERATOR_E_RANGE = "E_RANGE";
const std::string LABEL_GENERATOR_F_RANGE = "F_RANGE";
const std::string LABEL_GENERATOR_E_GROOTLE = "E_GROOTLE";
const std::string LABEL_GENERATOR_F_GROOTLE = "F_GROOTLE";

// Hash function labels
const std::string LABEL_HASH_DIV = "DIV";
const std::string LABEL_HASH_Q2 = "Q2";
Expand Down
92 changes: 0 additions & 92 deletions src/libspats/aead.cpp

This file was deleted.

Loading

0 comments on commit 983dc27

Please sign in to comment.