Skip to content

Commit

Permalink
more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
maramihali committed Apr 28, 2023
1 parent 6690b68 commit cc89b9b
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace proof_system::plonk {
namespace stdlib {

using namespace plookup;
using namespace barretenberg;

template <typename C>
Expand Down
22 changes: 12 additions & 10 deletions cpp/src/barretenberg/stdlib/encryption/aes128/aes128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ namespace proof_system::plonk {
namespace stdlib {
namespace aes128 {

using namespace plookup;

constexpr uint32_t AES128_BASE = 9;
typedef stdlib::field_t<plonk::UltraComposer> field_t;
typedef stdlib::witness_t<plonk::UltraComposer> witness_t;
typedef stdlib::field_t<UltraComposer> field_t;
typedef stdlib::witness_t<UltraComposer> witness_t;

typedef std::pair<field_t, field_t> byte_pair;

field_t normalize_sparse_form(plonk::UltraComposer*, field_t& byte)
field_t normalize_sparse_form(UltraComposer*, field_t& byte)
{
auto result = plookup_read::read_from_1_to_2_table(AES_NORMALIZE, byte);
return result;
}

byte_pair apply_aes_sbox_map(plonk::UltraComposer*, field_t& input)
byte_pair apply_aes_sbox_map(UltraComposer*, field_t& input)
{
return plookup_read::read_pair_from_table(AES_SBOX, input);
}

std::array<field_t, 16> convert_into_sparse_bytes(plonk::UltraComposer*, const field_t& block_data)
std::array<field_t, 16> convert_into_sparse_bytes(UltraComposer*, const field_t& block_data)
{
// `block_data` must be a 128 bit variable
std::array<field_t, 16> sparse_bytes;
Expand All @@ -46,7 +48,7 @@ std::array<field_t, 16> convert_into_sparse_bytes(plonk::UltraComposer*, const f
return sparse_bytes;
}

field_t convert_from_sparse_bytes(plonk::UltraComposer* ctx, field_t* sparse_bytes)
field_t convert_from_sparse_bytes(UltraComposer* ctx, field_t* sparse_bytes)
{
std::array<field_t, 16> bytes;

Expand All @@ -69,7 +71,7 @@ field_t convert_from_sparse_bytes(plonk::UltraComposer* ctx, field_t* sparse_byt
return result;
}

std::array<field_t, 176> expand_key(plonk::UltraComposer* ctx, const field_t& key)
std::array<field_t, 176> expand_key(UltraComposer* ctx, const field_t& key)
{
constexpr uint8_t round_constants[11] = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
std::array<field_t, 11> sparse_round_constants{
Expand Down Expand Up @@ -210,7 +212,7 @@ void mix_columns_and_add_round_key(byte_pair* state_pairs, field_t* round_key, u
mix_column_and_add_round_key(state_pairs + 12, round_key + 12, round);
}

void sub_bytes(plonk::UltraComposer* ctx, byte_pair* state_pairs)
void sub_bytes(UltraComposer* ctx, byte_pair* state_pairs)
{
for (size_t i = 0; i < 16; ++i) {
state_pairs[i] = apply_aes_sbox_map(ctx, state_pairs[i].first);
Expand All @@ -233,7 +235,7 @@ void xor_with_iv(byte_pair* state, field_t* iv)
}
}

void aes128_cipher(plonk::UltraComposer* ctx, byte_pair* state, field_t* sparse_round_key)
void aes128_cipher(UltraComposer* ctx, byte_pair* state, field_t* sparse_round_key)
{
add_round_key(state, sparse_round_key, 0);
for (size_t i = 0; i < 16; ++i) {
Expand All @@ -256,7 +258,7 @@ void aes128_cipher(plonk::UltraComposer* ctx, byte_pair* state, field_t* sparse_

std::vector<field_t> encrypt_buffer_cbc(const std::vector<field_t>& input, const field_t& iv, const field_t& key)
{
plonk::UltraComposer* ctx = key.get_context();
UltraComposer* ctx = key.get_context();

auto round_key = expand_key(ctx, key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace stdlib {
namespace blake2s_plookup {

using plookup::ColumnIdx;
using namespace blake_util;

constexpr uint32_t blake2s_IV[8] = { 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL };
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/stdlib/hash/blake2s/blake_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace stdlib {

namespace blake_util {

using namespace plookup;

// constants
enum blake_constant { BLAKE3_STATE_SIZE = 16 };

Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/stdlib/hash/blake3s/blake3s_plookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace stdlib {

namespace blake3s_plookup {

using namespace blake_util;

/*
* Constants and more.
*/
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace proof_system::plonk {
namespace stdlib {

using namespace plookup;

/**
* @brief Normalize a base-11 limb and left-rotate by keccak::ROTATIONS[lane_index] bits.
* This method also extracts the most significant bit of the normalised rotated limb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace proof_system::plonk {
namespace stdlib {

using namespace barretenberg;
using namespace plookup;

/**
* Add two curve points in one of the following ways:
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/barretenberg/stdlib/hash/sha256/sha256_plookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "barretenberg/plonk/composer/plookup_tables/plookup_tables.hpp"
#include "barretenberg/plonk/composer/plookup_tables/sha256.hpp"
#include "barretenberg/plonk/composer/plookup_tables/types.hpp"
#include "barretenberg/plonk/composer/ultra_composer.hpp"
#include "barretenberg/stdlib/primitives/bit_array/bit_array.hpp"
#include "barretenberg/stdlib/primitives/field/field.hpp"
Expand All @@ -14,6 +15,8 @@ namespace proof_system::plonk {
namespace stdlib {
namespace sha256_plookup {

using namespace plookup;

namespace internal {

constexpr size_t get_num_blocks(const size_t num_bits)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once
#include "barretenberg/plonk/composer/plookup_tables/types.hpp"
namespace proof_system::plonk {
namespace stdlib {

using plookup::MultiTableId;
template <typename C, class Fq, class Fr, class G>
template <size_t num_elements, typename>
std::array<twin_rom_table<C>, 5> element<C, Fq, Fr, G>::create_group_element_rom_tables(
const std::array<element, num_elements>& rom_data)
{

std::vector<std::array<field_t<C>, 2>> x_lo_limbs;
std::vector<std::array<field_t<C>, 2>> x_hi_limbs;
std::vector<std::array<field_t<C>, 2>> y_lo_limbs;
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class UltraComposer;
namespace proof_system::plonk {
namespace stdlib {

using plookup::ColumnIdx;
using plookup::MultiTableId;
using namespace barretenberg;

template <typename Composer>
Expand Down
18 changes: 9 additions & 9 deletions cpp/src/barretenberg/stdlib/primitives/plookup/plookup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
namespace proof_system::plonk {
namespace stdlib {

using namespace plookup;

template <typename Composer> class plookup_ {
typedef field_t<Composer> field_pt;

public:
static std::pair<field_pt, field_pt> read_pair_from_table(const MultiTableId id, const field_pt& key);
static std::pair<field_pt, field_pt> read_pair_from_table(const plookup::MultiTableId id, const field_pt& key);

static field_pt read_from_2_to_1_table(const MultiTableId id, const field_pt& key_a, const field_pt& key_b);
static field_pt read_from_1_to_2_table(const MultiTableId id, const field_pt& key_a);
static field_pt read_from_2_to_1_table(const plookup::MultiTableId id,
const field_pt& key_a,
const field_pt& key_b);
static field_pt read_from_1_to_2_table(const plookup::MultiTableId id, const field_pt& key_a);

static ReadData<field_pt> get_lookup_accumulators(const MultiTableId id,
const field_pt& key_a,
const field_pt& key_b = 0,
const bool is_2_to_1_lookup = false);
static plookup::ReadData<field_pt> get_lookup_accumulators(const plookup::MultiTableId id,
const field_pt& key_a,
const field_pt& key_b = 0,
const bool is_2_to_1_lookup = false);
};

extern template class plookup_<plonk::UltraComposer>;
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/stdlib/primitives/uint/plookup/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using namespace barretenberg;
namespace proof_system::plonk {
namespace stdlib {

using namespace plookup;

template <typename Composer, typename Native>
uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator&(const uint_plookup& other) const
{
Expand Down

0 comments on commit cc89b9b

Please sign in to comment.