Skip to content

Commit

Permalink
Merge pull request #1775 from AztecProtocol/defi-bridge-project
Browse files Browse the repository at this point in the history
Defi bridge project
  • Loading branch information
PhilWindle authored Nov 22, 2022
2 parents 3a1e5fd + 7729e71 commit 67f8781
Show file tree
Hide file tree
Showing 18 changed files with 642 additions and 224 deletions.
92 changes: 91 additions & 1 deletion src/aztec/common/log.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#pragma once
#include <env/logstr.hpp>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>

#define BENCHMARK_INFO_PREFIX "##BENCHMARK_INFO_PREFIX##"
#define BENCHMARK_INFO_SEPARATOR "#"
#define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##"
namespace {

inline void format_chain(std::ostream&) {}

template <typename T> void format_chain(std::ostream& os, T const& first)
Expand All @@ -22,6 +29,36 @@ template <typename... Args> std::string format(Args... args)
format_chain(os, args...);
return os.str();
}

template <typename T> void benchmark_format_chain(std::ostream& os, T const& first)
{
// We will be saving these values to a CSV file, so we can't tolerate commas
std::stringstream current_argument;
current_argument << first;
std::string current_argument_string = current_argument.str();
std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
os << current_argument_string << BENCHMARK_INFO_SUFFIX;
}

template <typename T, typename... Args>
void benchmark_format_chain(std::ostream& os, T const& first, Args const&... args)
{
// We will be saving these values to a CSV file, so we can't tolerate commas
std::stringstream current_argument;
current_argument << first;
std::string current_argument_string = current_argument.str();
std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
os << current_argument_string << BENCHMARK_INFO_SEPARATOR;
benchmark_format_chain(os, args...);
}

template <typename... Args> std::string benchmark_format(Args... args)
{
std::ostringstream os;
os << BENCHMARK_INFO_PREFIX;
benchmark_format_chain(os, args...);
return os.str();
}
} // namespace

#if NDEBUG
Expand All @@ -36,4 +73,57 @@ template <typename... Args> inline void debug(Args...) {}
template <typename... Args> inline void info(Args... args)
{
logstr(format(args...).c_str());
}
}

/**
* @brief Info used to store circuit statistics during CI/CD with concrete structure. Writes straight to log
*
* @details Automatically appends the necessary prefix and suffix, as well as separators.
*
* @tparam Args
* @param args
*/
#ifdef CI
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
{
logstr(benchmark_format(composer, class_name, operation, metric, value).c_str());
}
#else
template <typename... Args> inline void benchmark_info(Args...) {}
#endif

/**
* @brief A class for saving benchmarks and printing them all at once in the end of the function.
*
*/
class BenchmarkInfoCollator {

std::vector<std::string> saved_benchmarks;

public:
/**
* @brief Info used to store circuit statistics during CI/CD with concrete structure. Stores string in vector for now
* (used to flush all benchmarks at the end of test).
*
* @details Automatically appends the necessary prefix and suffix, as well as separators.
*
* @tparam Args
* @param args
*/
#ifdef CI
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
inline void benchmark_info_deferred(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
{
saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str());
}
#else
template <typename... Args> inline void benchmark_info_deferred(Args...) {}
#endif
~BenchmarkInfoCollator()
{
for (auto& x : saved_benchmarks) {
logstr(x.c_str());
}
}
};
10 changes: 9 additions & 1 deletion src/aztec/crypto/schnorr/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ WASM_EXPORT void compute_public_key(uint8_t const* private_key, uint8_t* public_
write(public_key_buf, pub_key);
}

WASM_EXPORT void negate_public_key(uint8_t const* public_key_buffer, uint8_t* output)
{
// Negate the public key (effectively negating the y-coordinate of the public key) and return the resulting public
// key.
auto account_public_key = from_buffer<grumpkin::g1::affine_element>(public_key_buffer);
barretenberg::group_elements::write(output, -account_public_key);
}

WASM_EXPORT void construct_signature(
uint8_t const* message, size_t msg_len, uint8_t const* private_key, uint8_t* s, uint8_t* e)
{
Expand Down Expand Up @@ -129,4 +137,4 @@ WASM_EXPORT bool multisig_combine_signatures(uint8_t const* message,
return false;
}
}
}
}
4 changes: 2 additions & 2 deletions src/aztec/ecc/curves/secp256k1/secp256k1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ TEST(secp256k1, neg_and_self_neg_0_cmp_regression)

TEST(secp256k1, montgomery_mul_big_bug)
{
secp256k1::fq a(uint256_t{0xfffffffe630dc02f, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff});
secp256k1::fq a(uint256_t{ 0xfffffffe630dc02f, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff });
secp256k1::fq a_sqr = a.sqr();
secp256k1::fq expected(uint256_t{0x60381e557e100000, 0x0, 0x0, 0x0});
secp256k1::fq expected(uint256_t{ 0x60381e557e100000, 0x0, 0x0, 0x0 });
EXPECT_EQ((a_sqr == expected), true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/aztec/ecc/curves/secp256r1/secp256r1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ TEST(secp256r1, montgomery_mul_big_bug)
a.data[2] = 0xAAAAAAAAAAAAAAAA;
a.data[3] = 0xFFFFFFFFE38E38E3;
secp256r1::fr a_sqr = a.sqr();
secp256r1::fr expected(uint256_t{0x57abc6aa0349c084, 0x65b21b232a4cb7a5, 0x5ba781948b0fcd6e, 0xd6e9e0644bda12f7});
secp256r1::fr expected(uint256_t{ 0x57abc6aa0349c084, 0x65b21b232a4cb7a5, 0x5ba781948b0fcd6e, 0xd6e9e0644bda12f7 });
EXPECT_EQ((a_sqr == expected), true);
}

Expand Down
50 changes: 0 additions & 50 deletions src/aztec/rollup/proofs/account/account_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,56 +42,6 @@ void account_tx::sign(key_pair<grumpkin::fr, grumpkin::g1> const& keys)
std::string(message.begin(), message.end()), keys);
}

void write(std::vector<uint8_t>& buf, account_tx const& tx)
{
using serialize::write;
write(buf, tx.merkle_root);
write(buf, tx.account_public_key);
write(buf, tx.new_account_public_key);
write(buf, tx.new_signing_pub_key_1);
write(buf, tx.new_signing_pub_key_2);
write(buf, tx.alias_hash);
write(buf, tx.create);
write(buf, tx.migrate);
write(buf, tx.account_note_index);
write(buf, tx.account_note_path);
write(buf, tx.signing_pub_key);
write(buf, tx.signature);
}

void read(uint8_t const*& buf, account_tx& tx)
{
using serialize::read;
read(buf, tx.merkle_root);
read(buf, tx.account_public_key);
read(buf, tx.new_account_public_key);
read(buf, tx.new_signing_pub_key_1);
read(buf, tx.new_signing_pub_key_2);
read(buf, tx.alias_hash);
read(buf, tx.create);
read(buf, tx.migrate);
read(buf, tx.account_note_index);
read(buf, tx.account_note_path);
read(buf, tx.signing_pub_key);
read(buf, tx.signature);
}

std::ostream& operator<<(std::ostream& os, account_tx const& tx)
{
return os << "merkle_root: " << tx.merkle_root << "\n"
<< "account_public_key: " << tx.account_public_key << "\n"
<< "new_account_public_key: " << tx.new_account_public_key << "\n"
<< "new_signing_pub_key_1: " << tx.new_signing_pub_key_1 << "\n"
<< "new_signing_pub_key_2: " << tx.new_signing_pub_key_2 << "\n"
<< "alias_hash: " << tx.alias_hash << "\n"
<< "create: " << tx.create << "\n"
<< "migrate: " << tx.migrate << "\n"
<< "account_note_index: " << tx.account_note_index << "\n"
<< "account_note_path: " << tx.account_note_path << "\n"
<< "signing_pub_key: " << tx.signing_pub_key << "\n"
<< "signature: " << tx.signature << "\n";
}

} // namespace account
} // namespace proofs
} // namespace rollup
53 changes: 50 additions & 3 deletions src/aztec/rollup/proofs/account/account_tx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,57 @@ struct account_tx {
bool operator==(account_tx const&) const = default;
};

void read(uint8_t const*& it, account_tx& tx);
void write(std::vector<uint8_t>& buf, account_tx const& tx);
template <typename B> inline void read(B& buf, account_tx& tx)
{
using serialize::read;
read(buf, tx.merkle_root);
read(buf, tx.account_public_key);
read(buf, tx.new_account_public_key);
read(buf, tx.new_signing_pub_key_1);
read(buf, tx.new_signing_pub_key_2);
read(buf, tx.alias_hash);
read(buf, tx.create);
read(buf, tx.migrate);
read(buf, tx.account_note_index);
read(buf, tx.account_note_path);
read(buf, tx.signing_pub_key);
read(buf, tx.signature.s);
read(buf, tx.signature.e);
}

std::ostream& operator<<(std::ostream& os, account_tx const& tx);
template <typename B> inline void write(B& buf, account_tx const& tx)
{
using serialize::write;
write(buf, tx.merkle_root);
write(buf, tx.account_public_key);
write(buf, tx.new_account_public_key);
write(buf, tx.new_signing_pub_key_1);
write(buf, tx.new_signing_pub_key_2);
write(buf, tx.alias_hash);
write(buf, tx.create);
write(buf, tx.migrate);
write(buf, tx.account_note_index);
write(buf, tx.account_note_path);
write(buf, tx.signing_pub_key);
write(buf, tx.signature.s);
write(buf, tx.signature.e);
}

inline std::ostream& operator<<(std::ostream& os, account_tx const& tx)
{
return os << "merkle_root: " << tx.merkle_root << "\n"
<< "account_public_key: " << tx.account_public_key << "\n"
<< "new_account_public_key: " << tx.new_account_public_key << "\n"
<< "new_signing_pub_key_1: " << tx.new_signing_pub_key_1 << "\n"
<< "new_signing_pub_key_2: " << tx.new_signing_pub_key_2 << "\n"
<< "alias_hash: " << tx.alias_hash << "\n"
<< "create: " << tx.create << "\n"
<< "migrate: " << tx.migrate << "\n"
<< "account_note_index: " << tx.account_note_index << "\n"
<< "account_note_path: " << tx.account_note_path << "\n"
<< "signing_pub_key: " << tx.signing_pub_key << "\n"
<< "signature: " << tx.signature << "\n";
}

} // namespace account
} // namespace proofs
Expand Down
3 changes: 2 additions & 1 deletion src/aztec/rollup/proofs/account/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
#include "account.hpp"
#include "c_bind.h"
#include "compute_circuit_data.hpp"
#include "create_proof.hpp"
#include "create_proof.hpp"
#include "verify.hpp"
32 changes: 32 additions & 0 deletions src/aztec/rollup/proofs/account/verify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "./verify.hpp"
#include "./account.hpp"
#include "./account_tx.hpp"

namespace rollup {
namespace proofs {
namespace account {

namespace {
verify_result<Composer> build_circuit(Composer& composer, account_tx& tx, circuit_data const&)
{
verify_result<Composer> result;
account_circuit(composer, tx);
return result;
}
} // namespace

verify_result<Composer> verify_logic(account_tx& tx, circuit_data const& cd)
{
Composer composer = Composer(cd.proving_key, cd.verification_key, cd.num_gates);
return verify_logic_internal(composer, tx, cd, "account", build_circuit);
}

verify_result<Composer> verify(account_tx& tx, circuit_data const& cd)
{
Composer composer = Composer(cd.proving_key, cd.verification_key, cd.num_gates);
return verify_internal(composer, tx, cd, "account", true, build_circuit);
}

} // namespace account
} // namespace proofs
} // namespace rollup
19 changes: 19 additions & 0 deletions src/aztec/rollup/proofs/account/verify.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "../verify.hpp"
#include "./compute_circuit_data.hpp"
#include "./account.hpp"
#include <stdlib/types/turbo.hpp>

namespace rollup {
namespace proofs {
namespace account {

using namespace plonk::stdlib::types::turbo;

verify_result<Composer> verify_logic(account_tx& tx, circuit_data const& cd);

verify_result<Composer> verify(account_tx& tx, circuit_data const& cd);

} // namespace account
} // namespace proofs
} // namespace rollup
Loading

0 comments on commit 67f8781

Please sign in to comment.