-
Notifications
You must be signed in to change notification settings - Fork 302
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
feat(avm): constrain sha256 #11048
base: master
Are you sure you want to change the base?
feat(avm): constrain sha256 #11048
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
edd1c61
to
1f4a5bf
Compare
1f4a5bf
to
87cef77
Compare
sel * (1 - sel) = 0; | ||
// For the XOR lookups | ||
pol commit xor_sel; | ||
perform_round * (xor_sel - 2) = 0; |
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.
We should introduce constant for op_id of OR, AND, XOR and use it instead of hardcoded '2'.
perform_round * (xor_sel - 2) = 0; | ||
// For the AND lookups | ||
pol commit and_sel; | ||
and_sel = 0; |
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.
Same as above.
Maybe add a note that, once we support constant in a tuple member of a lookup, we will not need to define this column anymore.
|
||
// Counter | ||
pol commit rounds_remaining; | ||
start * (rounds_remaining - 64) + perform_round * (rounds_remaining - rounds_remaining' - 1) = 0; |
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.
Please define a constant for 64.
}; | ||
|
||
template <typename DestRow> | ||
void merge_into(DestRow& dest, FixedSha256ConstantsTable::Sha256ConstantsRow const& src, uint32_t multiplicity) |
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.
void merge_into(DestRow& dest, FixedSha256ConstantsTable::Sha256ConstantsRow const& src, uint32_t multiplicity) | |
void merge_into(DestRow& dest, const FixedSha256ConstantsTable::Sha256ConstantsRow& src, uint32_t multiplicity) |
}; | ||
|
||
template <typename DestRow> | ||
void merge_into(DestRow& dest, FixedSha256ConstantsTable::Sha256ConstantsRow const& src, uint32_t multiplicity) |
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.
void merge_into(DestRow& dest, FixedSha256ConstantsTable::Sha256ConstantsRow const& src, uint32_t multiplicity) | |
void merge_into(DestRow& dest, const FixedSha256ConstantsTable::Sha256ConstantsRow& src, uint32_t multiplicity) |
return output; | ||
} | ||
|
||
static std::tuple<uint32_t, uint32_t> bit_limbs(uint32_t const& a, uint8_t const b) |
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.
static std::tuple<uint32_t, uint32_t> bit_limbs(uint32_t const& a, uint8_t const b) | |
static std::tuple<uint32_t, uint32_t> bit_limbs(const uint32_t a, const uint8_t b) |
{ | ||
|
||
for (size_t i = 0; i < sha256_trace.size(); i++) { | ||
auto const& src = sha256_trace.at(i); |
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.
auto const& src = sha256_trace.at(i); | |
const auto& src = sha256_trace.at(i); |
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.
LGTM but have a look at my feedback. Cool work!
87cef77
to
eb03f46
Compare
eb03f46
to
8c6e5a8
Compare
1f891fb
to
c6ffba9
Compare
c6ffba9
to
c3f7087
Compare
c3f7087
to
6f9fbd0
Compare
|
||
// SHA256 Round Params Lookup | ||
pol constant sel_sha256_compression; |
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.
I think you need to do tracegen for these
- add to the precomputed trace
- add it in the tracegen helper
builder.process(sha256_event_container, trace); | ||
|
||
TestTraceContainer::RowTraceContainer rows = trace.as_rows(); | ||
rows[0].precomputed_first_row = 1; |
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.
do you need this? doesnt seem used in the pil file?
using C = Column; | ||
using sha256 = bb::avm2::sha256<FF>; | ||
|
||
// This test imports a bunch of external code since hand-generating the sha256 trace is a bit laborious atm. |
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.
Please add an "empty row test": just a single row with precomputed_first_row = 1
and run the whole relation. This is to check that the relation passes on empty rows (and kind of test skippable). See David's PR.
|
||
TestTraceContainer::RowTraceContainer rows = trace.as_rows(); | ||
rows[0].precomputed_first_row = 1; | ||
rows[0].sha256_latch = 1; |
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.
why isnt tracegen doing this?
|
||
check_relation<sha256>(rows); | ||
} | ||
|
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.
No negative tests now or no negative tests ever? :)
} | ||
|
||
// Computes and returns the message schedule (w) value for that round, and inserts witness data into the trace. | ||
uint32_t Sha256TraceBuilder::compute_w_with_witness(std::array<uint32_t, 16> prev_w_helpers, TraceContainer& trace) |
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.
I will consider taking the trace in the constructor so that we can eventually avoid passing it everywhere.
state[6], /*h = g*/ | ||
}; | ||
} | ||
|
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.
I will trust you for most of this. But indeed you effort paid off. It's looking much better now!
@@ -0,0 +1,375 @@ | |||
include "precomputed.pil"; | |||
namespace sha256; |
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.
newline before
namespace sha256; | ||
|
||
// Memory ops | ||
pol commit state_offset; |
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.
We are not indenting in the other files, since there is only 1 namespace expected, does it make sense?
// binary.start {binary.acc_ia, binary.acc_ib, binary.acc_ic, binary.op_id}; | ||
|
||
pol commit ch; | ||
// #[LOOKUP_CH_XOR] |
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.
(not this one)
You can add lookups into precomputed I think. See how David did it.
// Load 8 elements representing the state from memory. | ||
for (uint32_t i = 0; i < 8; ++i) { | ||
auto memory_value = memory.get(state_addr + i).value; | ||
/*Check Tag is U32 */ |
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.
missing " ", same below in the other check tag
// Load 16 elements representing the input from memory. | ||
for (uint32_t i = 0; i < 16; ++i) { | ||
auto memory_value = memory.get(input_addr + i).value; | ||
/*Check Tag is U32 */ |
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.
Is this an error handling TODO? Do you need to check the tags here? if yes please make explicit
MemoryAddress output_addr) override; | ||
|
||
private: | ||
EventEmitterInterface<Sha256CompressionEvent>& events; |
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.
Please add a TODO that we should be using "deduplicating events" here. I'll build the infra at some point.
Sorry, that was wrong. This is memory aware.
Most of the sha256 constraints. The main things missing are:
We'll delay this until vm2 re-design