-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Do not remove prefix zeros when RLP encoding bytes32 values #6167
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
Do not remove prefix zeros when RLP encoding bytes32 values #6167
Conversation
🦋 Changeset detectedLatest commit: e1bbdf0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughThe pull request modifies the RLP utility contract and its test suite. The encode(bytes32) function is reimplemented as a dedicated assembly-based encoding that preserves prefix zeros and uses a fixed-size array representation, instead of delegating to encode(uint256). Docstrings for encode(address), encode(uint256), and encode(bytes32) are updated to clarify their encoding behaviors. The test file is refactored to compute expected RLP encodings using ethers.encodeRlp() and adds explicit test cases for non-canonical encodings and zero-value handling. Possibly related PRs
Suggested labels
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
test/utils/RLP.test.js (1)
117-120: Minor: Clarify the inline comments for zero-value tests.The comment on line 117 ("Encoding for the value") is unclear about which value it refers to. Consider making these comments more descriptive.
- // Encoding for the value + // Canonical empty string encoding → decodes to zero await expect(this.mock.$decodeBytes32('0x80')).to.eventually.equal(ethers.ZeroHash); - // Encoding for two zeros (and nothing else) + // Non-canonical encoding with two zero bytes → decodes to zero await expect(this.mock.$decodeBytes32('0x820000')).to.eventually.equal(ethers.ZeroHash);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
contracts/utils/RLP.sol(3 hunks)test/utils/RLP.test.js(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/utils/RLP.test.js (1)
test/helpers/random.js (1)
generators(3-10)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: tests
- GitHub Check: slither
- GitHub Check: tests-upgradeable
- GitHub Check: tests-foundry
- GitHub Check: coverage
- GitHub Check: halmos
🔇 Additional comments (4)
contracts/utils/RLP.sol (3)
154-158: LGTM!The updated documentation accurately describes the fixed-size encoding behavior for addresses and provides clear guidance for users who want scalar encoding via casting.
168-172: LGTM!The documentation clearly distinguishes scalar encoding (uint256) from fixed-size encoding (bytes32), which helps users choose the appropriate encoding for their use case.
193-206: LGTM!The assembly implementation is correct. Memory layout and arithmetic are verified:
- Prefix 0xa0 = SHORT_OFFSET (0x80) + 32 correctly indicates a 32-byte string
- Full 32-byte input is preserved, including leading zeros
- Free memory pointer is properly updated to
result + 0x41This aligns with the PR objective of matching ethers.js behavior for bytes32 encoding.
test/utils/RLP.test.js (1)
94-103: LGTM!The test inputs provide good coverage of edge cases:
- Zero hash (all leading zeros)
- Trailing non-zero byte with leading zeros
- Leading non-zero byte
- Random value for broader coverage
Using
ethers.encodeRlp(input)as the expected value correctly validates that the Solidity implementation matches the ethers.js reference behavior.
Co-authored-by: Luiz Vasconcelos Júnior <[email protected]>
ernestognw
left a comment
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.
Added a small call to action in the changelog to make it clear how to preserve the previous behavior if someone is currently relying on encode(bytes32)
Followup on a discution started by #5826
[...]
PR Checklist
npx changeset add)