-
Notifications
You must be signed in to change notification settings - Fork 94
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
Add Keccak constraints to acir_format #393
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we are going to be extending this to be used by other hash types it could be useful to have it in a separate file. A new
hash_input.hpp
file feels unnecessary though and it can be a part ofacir_format.hpp
. However, this is not blocking imo as it is only used by Keccak at the moment, just leaving the note for visibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I will do this when it is used by other hashes.