Fix unmarshalling of Integer values in TTC

Switch (un)marshalling of Integer values to Bits8 instead of Int.

Previously, in `fromLimbs`, the read values arrived as a list of
(32-bit) Ints. If any of the limbs had its MSB set, it would be
interpreted as a negative number and corrupt the result.

This commit fixes this by using a `List Bits8` instead, which has an
explicit (and direct) mapping to Bytes in the Buffer, where it's
eventually stored. This way no awkard casts need to happen.
This commit is contained in:
Johann Rudloff 2020-05-11 17:02:45 +02:00
parent 735395f403
commit 17b01b807e

View File

@ -383,17 +383,16 @@ count : List.Elem x xs -> Int
count Here = 0
count (There p) = 1 + count p
toLimbs : Integer -> List Int
toLimbs : Integer -> List Bits8
toLimbs x
= if x == 0
then []
else if x == -1 then [-1]
else fromInteger (prim__andBigInt x 0xffffffff) ::
toLimbs (prim__ashrBigInt x 32)
else fromInteger (prim__andBigInt x 0xff) ::
toLimbs (prim__ashrBigInt x 8)
fromLimbs : List Int -> Integer
fromLimbs : List Bits8 -> Integer
fromLimbs [] = 0
fromLimbs (x :: xs) = cast x + prim__shlBigInt (fromLimbs xs) 32
fromLimbs (x :: xs) = prim__zextB8_BigInt x + prim__shlBigInt (fromLimbs xs) 8
export
TTC Integer where