Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bb): more concise namespaces, plookup => bb::plookup #4146

Merged
merged 13 commits into from
Jan 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using namespace benchmark;
using namespace bb;
using namespace bb;

namespace {
void goblin_full(State& state) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using namespace benchmark;
using namespace bb;
using namespace bb;
using namespace bb::honk::pcs::ipa;
namespace {
using Curve = curve::Grumpkin;
Expand Down
29 changes: 14 additions & 15 deletions barretenberg/cpp/src/barretenberg/crypto/aes128/aes128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <cstdint>

#include <iostream>
namespace crypto {
namespace aes128 {

namespace {

Expand All @@ -34,7 +32,7 @@ void sub_bytes(uint8_t* input)
uint8_t i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
input[j * 4 + i] = sbox[input[j * 4 + i]];
input[j * 4 + i] = bb::crypto::aes128_sbox[input[j * 4 + i]];
}
}
}
Expand All @@ -43,7 +41,7 @@ void inverse_sub_bytes(uint8_t* input)
{
for (size_t i = 0; i < 4; ++i) {
for (size_t j = 0; j < 4; ++j) {
input[j * 4 + i] = sbox_inverse[input[j * 4 + i]];
input[j * 4 + i] = bb::crypto::aes128_sbox_inverse[input[j * 4 + i]];
}
}
}
Expand Down Expand Up @@ -151,7 +149,9 @@ void inverse_mix_columns(uint8_t* input)
}
} // namespace

void expand_key(const uint8_t* key, uint8_t* round_key)
namespace bb::crypto {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only aes128 is done this way, more are a followup but we can comment on the pattern


void aes128_expand_key(const uint8_t* key, uint8_t* round_key)
{
uint8_t temp[4]{};

Expand All @@ -176,10 +176,10 @@ void expand_key(const uint8_t* key, uint8_t* round_key)
temp[2] = temp[3];
temp[3] = t;

temp[0] = sbox[temp[0]];
temp[1] = sbox[temp[1]];
temp[2] = sbox[temp[2]];
temp[3] = sbox[temp[3]];
temp[0] = aes128_sbox[temp[0]];
temp[1] = aes128_sbox[temp[1]];
temp[2] = aes128_sbox[temp[2]];
temp[3] = aes128_sbox[temp[3]];

temp[0] = temp[0] ^ round_constants[i >> 2];
}
Expand Down Expand Up @@ -224,10 +224,10 @@ void aes128_cipher(uint8_t* state, const uint8_t* round_key)
add_round_key(state, round_key, 10);
}

void encrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const size_t length)
void aes128_encrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const size_t length)
{
uint8_t round_key[176];
expand_key(key, round_key);
aes128_expand_key(key, round_key);

uint8_t block_state[16]{};

Expand All @@ -244,10 +244,10 @@ void encrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const
}
}

void decrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const size_t length)
void aes128_decrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const size_t length)
{
uint8_t round_key[176];
expand_key(key, round_key);
aes128_expand_key(key, round_key);
uint8_t block_state[16]{};
const size_t num_blocks = (length / 16);

Expand All @@ -262,5 +262,4 @@ void decrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const
}
}

} // namespace aes128
} // namespace crypto
} // namespace bb::crypto
18 changes: 8 additions & 10 deletions barretenberg/cpp/src/barretenberg/crypto/aes128/aes128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
#include <cstdint>

#include <iostream>
namespace crypto {
namespace aes128 {
namespace bb::crypto {

void expand_key(const uint8_t* key, uint8_t* round_key);
void aes128_expand_key(const uint8_t* key, uint8_t* round_key);
void aes128_inverse_cipher(uint8_t* state, const uint8_t* round_key);
void aes128_cipher(uint8_t* state, const uint8_t* round_key);

// n.b. these methods will update the initialization vector
void encrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const size_t length);
void decrypt_buffer_cbc(uint8_t* buf, uint8_t* iv, const uint8_t* key, const size_t length);
void aes128_encrypt_buffer_cbc(uint8_t* buffer, uint8_t* iv, const uint8_t* key, const size_t length);
void aes128_decrypt_buffer_cbc(uint8_t* buf, uint8_t* iv, const uint8_t* key, const size_t length);

constexpr uint64_t sparse_base = 9;
static constexpr uint8_t sbox[256] = {
constexpr uint64_t aes128_sparse_base = 9;
static constexpr uint8_t aes128_sbox[256] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9,
0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f,
Expand All @@ -44,7 +43,7 @@ static constexpr uint8_t sbox[256] = {
0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
};

static constexpr uint8_t sbox_inverse[256] = {
static constexpr uint8_t aes128_sbox_inverse[256] = {
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39,
0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2,
0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76,
Expand All @@ -60,5 +59,4 @@ static constexpr uint8_t sbox_inverse[256] = {
0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6,
0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
};
} // namespace aes128
} // namespace crypto
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ TEST(aes128, verify_cipher)
uint8_t state[16]{ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };

uint8_t round_key[176];
crypto::aes128::expand_key(key, round_key);
crypto::aes128::aes128_cipher(state, round_key);
bb::crypto::aes128_expand_key(key, round_key);
bb::crypto::aes128_cipher(state, round_key);

for (size_t i = 0; i < 16; ++i) {
EXPECT_EQ(state[i], expected[i]);
Expand All @@ -33,7 +33,7 @@ TEST(aes128, encrypt_buffer_cbc)
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };

crypto::aes128::encrypt_buffer_cbc(in, iv, key, 64);
bb::crypto::aes128_encrypt_buffer_cbc(in, iv, key, 64);

for (size_t i = 0; i < 64; ++i) {
EXPECT_EQ(in[i], out[i]);
Expand All @@ -53,7 +53,7 @@ TEST(aes128, decrypt_buffer_cbc)
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };

crypto::aes128::decrypt_buffer_cbc(in, iv, key, 64);
bb::crypto::aes128_decrypt_buffer_cbc(in, iv, key, 64);

for (size_t i = 0; i < 64; ++i) {
EXPECT_EQ(in[i], out[i]);
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/crypto/aes128/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WASM_EXPORT void aes_encrypt_buffer_cbc(
uint8_t const* in, uint8_t const* iv, uint8_t const* key, uint32_t const* length, uint8_t** r)
{
auto len = ntohl(*length);
crypto::aes128::encrypt_buffer_cbc((uint8_t*)in, (uint8_t*)iv, key, len);
bb::crypto::aes128_encrypt_buffer_cbc((uint8_t*)in, (uint8_t*)iv, key, len);
std::vector<uint8_t> result(in, in + len);
*r = to_heap_buffer(result);
}
Expand All @@ -15,7 +15,7 @@ WASM_EXPORT void aes_decrypt_buffer_cbc(
uint8_t const* in, uint8_t const* iv, uint8_t const* key, uint32_t const* length, uint8_t** r)
{
auto len = ntohl(*length);
crypto::aes128::decrypt_buffer_cbc((uint8_t*)in, (uint8_t*)iv, key, len);
bb::crypto::aes128_decrypt_buffer_cbc((uint8_t*)in, (uint8_t*)iv, key, len);
std::vector<uint8_t> result(in, in + len);
*r = to_heap_buffer(result);
}
12 changes: 6 additions & 6 deletions barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message,
using serialize::write;
auto priv_key = from_buffer<secp256k1::fr>(private_key);
secp256k1::g1::affine_element pub_key = secp256k1::g1::one * priv_key;
crypto::ecdsa::key_pair<secp256k1::fr, secp256k1::g1> key_pair = { priv_key, pub_key };
bb::crypto::ecdsa::key_pair<secp256k1::fr, secp256k1::g1> key_pair = { priv_key, pub_key };

auto sig = crypto::ecdsa::construct_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
auto sig = bb::crypto::ecdsa::construct_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), key_pair);
write(output_sig_r, sig.r);
write(output_sig_s, sig.s);
Expand All @@ -39,9 +39,9 @@ WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message
std::copy(sig_s, sig_s + 32, s.begin());
const uint8_t v = *sig_v;

crypto::ecdsa::signature sig = { r, s, v };
bb::crypto::ecdsa::signature sig = { r, s, v };
auto recovered_pub_key =
crypto::ecdsa::recover_public_key<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
bb::crypto::ecdsa::recover_public_key<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), sig);
serialize::write(output_pub_key, recovered_pub_key);
}
Expand All @@ -59,7 +59,7 @@ WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
std::copy(sig_s, sig_s + 32, s.begin());
const uint8_t v = *sig_v;

crypto::ecdsa::signature sig = { r, s, v };
return crypto::ecdsa::verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
bb::crypto::ecdsa::signature sig = { r, s, v };
return bb::crypto::ecdsa::verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), pubk, sig);
}
6 changes: 2 additions & 4 deletions barretenberg/cpp/src/barretenberg/crypto/ecdsa/ecdsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <array>
#include <string>

namespace crypto {
namespace ecdsa {
namespace bb::crypto::ecdsa {
template <typename Fr, typename G1> struct key_pair {
Fr private_key;
typename G1::affine_element public_key;
Expand Down Expand Up @@ -47,7 +46,6 @@ inline std::ostream& operator<<(std::ostream& os, signature const& sig)
return os;
}

} // namespace ecdsa
} // namespace crypto
} // namespace bb::crypto::ecdsa

#include "./ecdsa_impl.hpp"
6 changes: 2 additions & 4 deletions barretenberg/cpp/src/barretenberg/crypto/ecdsa/ecdsa_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"

namespace crypto {
namespace ecdsa {
namespace bb::crypto::ecdsa {

template <typename Hash, typename Fq, typename Fr, typename G1>
signature construct_signature(const std::string& message, const key_pair<Fr, G1>& account)
Expand Down Expand Up @@ -169,5 +168,4 @@ bool verify_signature(const std::string& message, const typename G1::affine_elem
Fr result(Rx);
return result == r;
}
} // namespace ecdsa
} // namespace crypto
} // namespace bb::crypto::ecdsa
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <map>
#include <optional>

namespace crypto {
namespace bb::crypto {
/**
* @brief class that stores precomputed generators used for Pedersen commitments and Pedersen hashes
*
Expand Down Expand Up @@ -143,4 +143,4 @@ template <typename Curve> struct GeneratorContext {
, domain_separator(_domain_separator)
{}
};
} // namespace crypto
} // namespace bb::crypto
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/crypto/hmac/hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string>
#include <vector>

namespace crypto {
namespace bb::crypto {
/**
* @brief Compute an HMAC given a secret key and a message
*
Expand Down Expand Up @@ -126,4 +126,4 @@ Fr get_unbiased_field_from_hmac(const MessageContainer& message, const KeyContai
Fr result((field_as_u512 % Fr::modulus).lo);
return result;
}
} // namespace crypto
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TEST(hmac, ValidateHMAC)
};

for (const auto& [key_string, message, expected] : test_vectors) {
std::array<uint8_t, 32> result = crypto::hmac<Sha256Hasher>(message, key_string);
std::array<uint8_t, 32> result = bb::crypto::hmac<Sha256Hasher>(message, key_string);

EXPECT_EQ(result, hex_to_bytes(expected));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <omp.h>
#endif

namespace crypto {
namespace bb::crypto {

/**
* @brief Given a vector of fields, generate a pedersen commitment using the indexed generators.
Expand All @@ -30,4 +30,4 @@ typename Curve::AffineElement pedersen_commitment_base<Curve>::commit_native(con
return result.normalize();
}
template class pedersen_commitment_base<curve::Grumpkin>;
} // namespace crypto
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include <array>

namespace crypto {
namespace bb::crypto {

/**
* @brief Performs pedersen commitments!
Expand All @@ -31,4 +31,4 @@ template <typename Curve> class pedersen_commitment_base {
};

using pedersen_commitment = pedersen_commitment_base<curve::Grumpkin>;
} // namespace crypto
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "barretenberg/crypto/generators/generator_data.hpp"
#include <gtest/gtest.h>

namespace crypto {
namespace bb::crypto {

using bb::fr;

Expand Down Expand Up @@ -51,4 +51,4 @@ TEST(Pedersen, GeneratorPrinter)
}
}

}; // namespace crypto
}; // namespace bb::crypto
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "./pedersen.hpp"
#include "../pedersen_commitment/pedersen.hpp"

namespace crypto {
namespace bb::crypto {

/**
* @brief Converts input uint8_t buffers into vector of field elements. Used to hash the Transcript in a
Expand Down Expand Up @@ -80,4 +80,4 @@ typename Curve::BaseField pedersen_hash_base<Curve>::hash_buffer(const std::vect
}

template class pedersen_hash_base<curve::Grumpkin>;
} // namespace crypto
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../generators/generator_data.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
namespace crypto {
namespace bb::crypto {
/**
* @brief Performs pedersen hashes!
*
Expand Down Expand Up @@ -37,4 +37,4 @@ template <typename Curve> class pedersen_hash_base {
};

using pedersen_hash = pedersen_hash_base<curve::Grumpkin>;
} // namespace crypto
} // namespace bb::crypto
Loading