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

feat(avm): constrain sha256 #11048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

IlyasRidhuan
Copy link
Contributor

@IlyasRidhuan IlyasRidhuan commented Jan 3, 2025

Most of the sha256 constraints. The main things missing are:

  1. Lookup constraints for XOR / AND
  2. Memory loading constraints

We'll delay this until vm2 re-design

Copy link
Contributor Author

IlyasRidhuan commented Jan 3, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch from edd1c61 to 1f4a5bf Compare January 9, 2025 10:38
@IlyasRidhuan IlyasRidhuan marked this pull request as ready for review January 9, 2025 10:57
@IlyasRidhuan IlyasRidhuan changed the title feat: constrain sha256 feat(avm): constrain sha256 Jan 9, 2025
@IlyasRidhuan IlyasRidhuan requested review from fcarreiro and dbanks12 and removed request for fcarreiro and Maddiaa0 January 9, 2025 10:58
@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch from 1f4a5bf to 87cef77 Compare January 9, 2025 12:51
sel * (1 - sel) = 0;
// For the XOR lookups
pol commit xor_sel;
perform_round * (xor_sel - 2) = 0;
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto const& src = sha256_trace.at(i);
const auto& src = sha256_trace.at(i);

Copy link
Contributor

@jeanmon jeanmon left a 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!

@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch from 87cef77 to eb03f46 Compare January 10, 2025 11:54
@IlyasRidhuan IlyasRidhuan marked this pull request as draft January 15, 2025 12:54
@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch from eb03f46 to 8c6e5a8 Compare January 23, 2025 12:38
@IlyasRidhuan IlyasRidhuan changed the base branch from master to ir/01-21-fix_hardcode_value_in_constants January 23, 2025 12:38
@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch 3 times, most recently from 1f891fb to c6ffba9 Compare January 23, 2025 13:53
@IlyasRidhuan IlyasRidhuan marked this pull request as ready for review January 23, 2025 13:54
@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch from c6ffba9 to c3f7087 Compare January 23, 2025 14:16
Base automatically changed from ir/01-21-fix_hardcode_value_in_constants to master January 23, 2025 14:52
@IlyasRidhuan IlyasRidhuan force-pushed the ir/01-03-feat_constrain_sha256 branch from c3f7087 to 6f9fbd0 Compare January 23, 2025 15:34

// SHA256 Round Params Lookup
pol constant sel_sha256_compression;
Copy link
Contributor

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

  1. add to the precomputed trace
  2. add it in the tracegen helper

builder.process(sha256_event_container, trace);

TestTraceContainer::RowTraceContainer rows = trace.as_rows();
rows[0].precomputed_first_row = 1;
Copy link
Contributor

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.
Copy link
Contributor

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;
Copy link
Contributor

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);
}

Copy link
Contributor

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)
Copy link
Contributor

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*/
};
}

Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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]
Copy link
Contributor

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 */
Copy link
Contributor

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 */
Copy link
Contributor

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;
Copy link
Contributor

@fcarreiro fcarreiro Jan 24, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants