Skip to content

Commit

Permalink
mldsa: fix indexing bug in hint bit unpack #184
Browse files Browse the repository at this point in the history
Changes the type of the index `i` to an integer to avoid overflow when
indexing into the larger array.
  • Loading branch information
marsella committed Nov 27, 2024
1 parent 873fff0 commit 6a918da
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Primitive/Asymmetric/Signature/ML_DSA/Specification.cry
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ HintBitPack h = yFinal where
* - To simplify updating `h`, we treat it as a single array of size `256k`.
* We separate it into the correct `[k]R2` representation in the final step.
* We access the array in "the natural way" -- that is, in Step 12, the
* element `h[i]_y[Index]` is at index `i * k + y[Index]` in our array.
* element `h[i]_y[Index]` is at index `i * 256 + y[Index]` in our array.
*/
HintBitUnpack : [ω + k]Byte -> Option ([k]R2)
HintBitUnpack y = h___ where
Expand All @@ -183,20 +183,20 @@ HintBitUnpack y = h___ where
]

// Steps 4 - 5.
Step4_5 : Byte -> Pair -> Option Pair
Step4_5 : Integer -> Pair -> Option Pair
Step4_5 i (h, Index) = if (y@(`ω + i) < Index) || (y@(`ω + i) > `ω) then
None
else Step6_15 (h, Index) i

// Steps 6 -15
Step6_15 : Pair -> Byte -> Option Pair
Step6_15 : Pair -> Integer -> Option Pair
Step6_15 (h, Index) i = result15 where
// Step 6.
First = Index
result15 = Step7_14 (h, Index) i First

// Steps 7 - 14.
Step7_14 : Pair -> Byte -> Byte -> Option Pair
Step7_14 : Pair -> Integer -> Byte -> Option Pair
Step7_14 (h, Index) i First =
if Index < (y@(`ω + i)) then
// The `/\` is a short-cutting `and`, equivalent to the nested `if`
Expand All @@ -205,7 +205,7 @@ HintBitUnpack y = h___ where
// Recursive call is equivalent to continuing the loop.
// The constants `i` and `First` do not change between iterations.
else Step7_14
(update h (i*`k + y@Index) 1,
(update h (i*256 + (toInteger (y@Index))) 1,
Index + 1)
i First
// If the loop condition is no longer true, return the current values
Expand Down

0 comments on commit 6a918da

Please sign in to comment.