-
Notifications
You must be signed in to change notification settings - Fork 609
fix!: sha256.pil missing input propagation constraints #19590
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -738,5 +738,74 @@ TEST(Sha256MemoryConstrainingTest, InputAddrTamperingIsCaughtByConstraint) | |
| "CONTINUITY_INPUT_ADDR"); | ||
| } | ||
|
|
||
| ////////////////////////////////////////// | ||
| /// Negative Test - init_* Propagation Constraint (PROP-001) | ||
| ////////////////////////////////////////// | ||
|
|
||
| // This test verifies that init_a through init_h are properly propagated across multi-row computation. | ||
| // | ||
| // BACKGROUND: SHA256 compression uses init_a-init_h values loaded from memory on row 0, and these | ||
| // values are used in the final output calculation (OUT_X = x + init_x) on the last row. | ||
| // | ||
| // The PROPAGATE_INIT_* constraints (sha256.pil lines 111-126) ensure that init values remain | ||
| // constant across all rounds. Without these constraints, a malicious prover could: | ||
| // 1. Set correct init_a on row 0 (to pass memory read constraint) | ||
| // 2. Set arbitrary init_a on later rows | ||
| // 3. Corrupt the final SHA256 output | ||
| // | ||
| // This test verifies that tampering with init_a is caught by the PROPAGATE_INIT_A constraint. | ||
| TEST(Sha256ConstrainingTest, InitStateTamperingIsCaughtByPropagationConstraint) | ||
| { | ||
| // Generate a valid SHA256 compression trace | ||
| MemoryStore mem; | ||
| StrictMock<MockExecutionIdManager> execution_id_manager; | ||
| EXPECT_CALL(execution_id_manager, get_execution_id()).WillRepeatedly(Return(1)); | ||
| PureGreaterThan gt; | ||
| PureBitwise bitwise; | ||
|
|
||
| EventEmitter<Sha256CompressionEvent> sha256_event_emitter; | ||
| Sha256 sha256_gadget(execution_id_manager, bitwise, gt, sha256_event_emitter); | ||
|
|
||
| // Set up valid memory for state and input | ||
| std::array<uint32_t, 8> state = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, | ||
| 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; | ||
| MemoryAddress state_addr = 0; | ||
| for (uint32_t i = 0; i < 8; ++i) { | ||
| mem.set(state_addr + i, MemoryValue::from<uint32_t>(state[i])); | ||
| } | ||
|
|
||
| std::array<uint32_t, 16> input = { 0x61626380, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x18 }; | ||
| MemoryAddress input_addr = 8; | ||
| for (uint32_t i = 0; i < 16; ++i) { | ||
| mem.set(input_addr + i, MemoryValue::from<uint32_t>(input[i])); | ||
| } | ||
| MemoryAddress output_addr = 25; | ||
|
|
||
| sha256_gadget.compression(mem, state_addr, input_addr, output_addr); | ||
|
|
||
| TestTraceContainer trace; | ||
| trace.set(C::precomputed_first_row, 0, 1); | ||
| Sha256TraceBuilder builder; | ||
| builder.process(sha256_event_emitter.dump_events(), trace); | ||
|
|
||
| // Verify the trace is valid before tampering | ||
| ASSERT_NO_THROW(check_relation<sha256>(trace)); | ||
|
|
||
| // Find a row where perform_round=1 (any round row works, but use row 1 for simplicity) | ||
| // Row 0 is start, rows 1-64 are rounds with perform_round=1 | ||
| constexpr uint32_t TAMPER_ROW = 1; | ||
| ASSERT_EQ(trace.get(C::sha256_perform_round, TAMPER_ROW), FF(1)) << "Row 1 should have perform_round=1"; | ||
|
|
||
| // Tamper with init_a on row 1 (making it different from row 0) | ||
| FF original_init_a = trace.get(C::sha256_init_a, TAMPER_ROW); | ||
| FF tampered_init_a = original_init_a + FF(0x12345678); | ||
| trace.set(C::sha256_init_a, TAMPER_ROW, tampered_init_a); | ||
|
|
||
| // The PROPAGATE_INIT_A constraint should catch this: | ||
| // perform_round * (init_a' - init_a) = 0 | ||
| // On row 0: perform_round=1, init_a'=tampered, init_a=original -> constraint violated | ||
| EXPECT_THROW_WITH_MESSAGE(check_relation<sha256>(trace, sha256::SR_PROPAGATE_INIT_A), "PROPAGATE_INIT_A"); | ||
|
Comment on lines
+799
to
+807
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before fix, this tampering did not fail constraints |
||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace bb::avm2::constraining | ||
14 changes: 7 additions & 7 deletions
14
barretenberg/cpp/src/barretenberg/vm2/generated/columns.hpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.