fix: reject non-canonical field elements in FieldArray SSZ decoding#56
Open
aryaethn wants to merge 1 commit into
Open
fix: reject non-canonical field elements in FieldArray SSZ decoding#56aryaethn wants to merge 1 commit into
aryaethn wants to merge 1 commit into
Conversation
FieldArray::from_ssz_bytes decoded each limb with F::new(u32), which reduces its argument mod p. Because 2p - 1 < 2^32, every canonical limb value v has a non-canonical alias v + p that fits in a u32 and decodes to the same element, while encoding always emits the canonical form. This made the SSZ encoding of signatures, public keys, and secret keys malleable, contradicting the canonical encoding contract required for consensus-layer compatibility. Reject any limb >= F::ORDER_U32 with DecodeError::BytesInvalid and add a regression test. Mirrors the canonical-deserialization check already enforced on the field type in leanVM (audit finding F-01).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
FieldArray::from_ssz_bytesdecoded each 4-byte limb withF::new(u32::from_le_bytes(..)).F::newreduces its argument mod p, so a limbvand its aliasv + pdecode to the same field element. For KoalaBear (p = 2130706433),2p - 1 < 2^32, so every canonical limb has such an alias that fits in au32, while encoding always emits the canonicalas_canonical_u32().The result: the SSZ encoding of signatures, public keys, and secret keys is malleable — a distinct byte string decodes to the same object and still verifies — contradicting the canonical-encoding contract the code documents and that consensus-layer SSZ requires.
Fix
Reject any limb
>= F::ORDER_U32withDecodeError::BytesInvalid, and add a regression test covering the modulus boundary and a concrete alias. This mirrors the canonical-deserialization check already enforced on the field type in leanVM (audit finding F-01).Tested