feat: support varlen hex strings in digest macro #988
Merged
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.
Somewhat mutilates/extends the
digest!
macro from #984 to add support for variable length hex strings.Previously, the macro only supported hex strings of felt-length multiples. See this comment. Variable length is supported by manually unrolling the hex characters into
u64
/felts in reverse. This supports shorter lengths as the initial values will remain zero.I also moved the
Felt
import inside the macro so that callers ofdigest!
no longer have to importFelt
.I think this PR also fixes a LE/BE bug, where
digest!("0x123")' maps to
0x1230000...0instead of
0x123. Though I might just be misunderstanding the
try_from` implementation it utilised. edit - indeed my misunderstanding - no bugPossible future work
Currently this macro will give a compile time error IF we assign it to a constant, but won't if its not assigned to a constant.
We can also force the latter to a compile time error by moving the const allocation inside the macro itself i.e. by adding
to the end of the macro. This has further implications though, namely:
I can add this in if we want it.