-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Keccak constraints to acir_format (#393)
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
cpp/src/barretenberg/dsl/acir_format/keccak_constraint.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "keccak_constraint.hpp" | ||
#include "round.hpp" | ||
#include "barretenberg/stdlib/hash/keccak/keccak.hpp" | ||
|
||
namespace acir_format { | ||
|
||
void create_keccak_constraints(Composer& composer, const KeccakConstraint& constraint) | ||
{ | ||
|
||
// Create byte array struct | ||
byte_array_ct arr(&composer); | ||
|
||
// Get the witness assignment for each witness index | ||
// Write the witness assignment to the byte_array | ||
for (const auto& witness_index_num_bits : constraint.inputs) { | ||
auto witness_index = witness_index_num_bits.witness; | ||
auto num_bits = witness_index_num_bits.num_bits; | ||
|
||
// XXX: The implementation requires us to truncate the element to the nearest byte and not bit | ||
auto num_bytes = round_to_nearest_byte(num_bits); | ||
|
||
field_ct element = field_ct::from_witness_index(&composer, witness_index); | ||
byte_array_ct element_bytes(element, num_bytes); | ||
|
||
arr.write(element_bytes); | ||
} | ||
|
||
byte_array_ct output_bytes = proof_system::plonk::stdlib::keccak<Composer>::hash(arr); | ||
|
||
// Convert byte array to vector of field_t | ||
auto bytes = output_bytes.bytes(); | ||
|
||
for (size_t i = 0; i < bytes.size(); ++i) { | ||
composer.assert_equal(bytes[i].normalize().witness_index, constraint.result[i]); | ||
} | ||
} | ||
|
||
} // namespace acir_format |
52 changes: 52 additions & 0 deletions
52
cpp/src/barretenberg/dsl/acir_format/keccak_constraint.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
#include <cstdint> | ||
#include <vector> | ||
#include "barretenberg/dsl/types.hpp" | ||
|
||
namespace acir_format { | ||
|
||
struct HashInput { | ||
uint32_t witness; | ||
uint32_t num_bits; | ||
|
||
friend bool operator==(HashInput const& lhs, HashInput const& rhs) = default; | ||
}; | ||
|
||
struct KeccakConstraint { | ||
std::vector<HashInput> inputs; | ||
std::vector<uint32_t> result; | ||
|
||
friend bool operator==(KeccakConstraint const& lhs, KeccakConstraint const& rhs) = default; | ||
}; | ||
|
||
void create_keccak_constraints(Composer& composer, const KeccakConstraint& constraint); | ||
|
||
template <typename B> inline void read(B& buf, HashInput& constraint) | ||
{ | ||
using serialize::read; | ||
read(buf, constraint.witness); | ||
read(buf, constraint.num_bits); | ||
} | ||
|
||
template <typename B> inline void write(B& buf, HashInput const& constraint) | ||
{ | ||
using serialize::write; | ||
write(buf, constraint.witness); | ||
write(buf, constraint.num_bits); | ||
} | ||
|
||
template <typename B> inline void read(B& buf, KeccakConstraint& constraint) | ||
{ | ||
using serialize::read; | ||
read(buf, constraint.inputs); | ||
read(buf, constraint.result); | ||
} | ||
|
||
template <typename B> inline void write(B& buf, KeccakConstraint const& constraint) | ||
{ | ||
using serialize::write; | ||
write(buf, constraint.inputs); | ||
write(buf, constraint.result); | ||
} | ||
|
||
} // namespace acir_format |