feat(runtime): implement byteStringToInteger and add conformance tests

This commit is contained in:
rvcas 2024-02-19 20:47:19 -05:00 committed by Lucas
parent da6e5ec6d1
commit 028528899c
124 changed files with 349 additions and 4 deletions

View File

@ -1342,10 +1342,10 @@ impl DefaultFunction {
return Ok(Value::Con(constant.into()));
}
let mut bytes = if *endianness {
input.to_be_bytes()
let (_, mut bytes) = if *endianness {
input.to_bytes_be()
} else {
input.to_le_bytes()
input.to_bytes_le()
};
if !size.is_zero() && bytes.len() > size_unwrapped {
@ -1374,7 +1374,18 @@ impl DefaultFunction {
Ok(Value::Con(constant.into()))
}
DefaultFunction::ByteStringToInteger => {
todo!("do it not live")
let endianness = args[0].unwrap_bool()?;
let bytes = args[1].unwrap_byte_string()?;
let number = if *endianness {
BigInt::from_bytes_be(num_bigint::Sign::Plus, bytes)
} else {
BigInt::from_bytes_le(num_bigint::Sign::Plus, bytes)
};
let constant = Constant::Integer(number);
Ok(Value::Con(constant.into()))
}
}
}

View File

@ -0,0 +1,4 @@
-- A bytestring consisting entirely of zeros decodes to 0.
(program 1.0.0
[(builtin byteStringToInteger) (con bool True) (con bytestring #00000000000000000000000000)]
)

View File

@ -0,0 +1,4 @@
-- Check that a particular bytestring decodes to the expected integer.
(program 1.0.0
[(builtin byteStringToInteger) (con bool True) (con bytestring #123456abcdef)]
)

View File

@ -0,0 +1,4 @@
-- The empty bytestring decodes to 0
(program 1.0.0
[(builtin byteStringToInteger) (con bool True) (con bytestring #)]
)

View File

@ -0,0 +1,7 @@
-- Check that leading zeros don't affect the result of a big-endian decoding.
(program 1.0.0
[(builtin equalsInteger)
[(builtin byteStringToInteger) (con bool True) (con bytestring #123456abcdef)]
[(builtin byteStringToInteger) (con bool True) (con bytestring #0000000000000000123456abcdef)]
]
)

View File

@ -0,0 +1,9 @@
-- Check that the big-endian decoding of a bytestring is the same as the
-- little-endian decoding of its reverse.
(program 1.0.0
[(builtin equalsInteger)
[(builtin byteStringToInteger) (con bool False) (con bytestring #92828b9d9e097a23ef34ba5522ee67)]
[(builtin byteStringToInteger) (con bool True) (con bytestring #67ee2255ba34ef237a099e9d8b8292)]
]
)

View File

@ -0,0 +1,4 @@
-- A bytestring consisting entirely of zeros decodes to 0.
(program 1.0.0
[(builtin byteStringToInteger) (con bool False) (con bytestring #00000000000000000000000000)]
)

View File

@ -0,0 +1,4 @@
-- Check that a particular bytestring decodes to the expected integer.
(program 1.0.0
[(builtin byteStringToInteger) (con bool False) (con bytestring #123456abcdef)]
)

View File

@ -0,0 +1,4 @@
-- The empty bytestring decodes to 0
(program 1.0.0
[(builtin byteStringToInteger) (con bool False) (con bytestring #)]
)

View File

@ -0,0 +1,7 @@
-- Check that trailing zeros don't affect the result of a little-endian decoding.
(program 1.0.0
[(builtin equalsInteger)
[(builtin byteStringToInteger) (con bool False) (con bytestring #123456abcdef)]
[(builtin byteStringToInteger) (con bool False) (con bytestring #123456abcdef0000000000000000)]
]
)

View File

@ -0,0 +1,4 @@
-- Check that a particular integer encodes to the expected bytestring using exactly the right width.
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 6) (con integer 20016001699311)]
)

View File

@ -0,0 +1,5 @@
-- Check that a particular integer encodes to the expected bytestring with some
-- extra padding on the left when the width is greater than the minimal required width
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 12) (con integer 20016001699311)]
)

View File

@ -0,0 +1,4 @@
-- Check that we can encode zero using the maximum width (8192).
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 8192) (con integer 0)]
)

View File

@ -0,0 +1,4 @@
-- Negative inputs give an error
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 20) (con integer -5)]
)

View File

@ -0,0 +1,4 @@
-- Negative widths give an error
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer -20) (con integer 5)]
)

View File

@ -0,0 +1,4 @@
-- Check that trying to encode an integer to a bytestring using too narrow a width fails.
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 5) (con integer 20016001699311)]
)

View File

@ -0,0 +1,4 @@
-- Check that a width greater than the maximum of 8192 is rejected when encoding zero.
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 8193) (con integer 0)]
)

View File

@ -0,0 +1,4 @@
-- Zero should encode to a bytestring consisting entirely of zero bytes.
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 12) (con integer 0)]
)

View File

@ -0,0 +1 @@
(program 1.0.0 (con bytestring #000000000000000000000000))

View File

@ -0,0 +1,4 @@
-- Check that a particular integer encodes to the expected bytestring
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 0) (con integer 20016001699311)]
)

View File

@ -0,0 +1,4 @@
-- Negative inputs cause an error.
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 0) (con integer -5)]
)

View File

@ -0,0 +1,4 @@
-- Zero should encode to the empty bytestring.
(program 1.0.0
[(builtin integerToByteString) (con bool True) (con integer 0) (con integer 0)]
)

View File

@ -0,0 +1,4 @@
-- Check that a particular integer encodes to the expected bytestring using exactly the right width.
(program 1.0.0
[(builtin integerToByteString) (con bool False) (con integer 6) (con integer 20016001699311)]
)

View File

@ -0,0 +1,6 @@
-- Check that a particular integer encodes to the expected bytestring with some
-- extra padding on the right when the width is greater than the minimal
-- required width.
(program 1.0.0
[(builtin integerToByteString) (con bool False) (con integer 12) (con integer 20016001699311)]
)

View File

@ -0,0 +1,4 @@
-- Check that we can encode zero using the maximum width (8192).
(program 1.0.0
[(builtin integerToByteString) (con bool False) (con integer 8192) (con integer 0)]
)

View File

@ -0,0 +1,5 @@
-- Negative inputs give an error
(program 1.0.0
[(builtin integerToByteString) (con bool False) (con integer 20) (con integer -5)]
)

View File

@ -0,0 +1,5 @@
-- Negative widths give an error
(program 1.0.0
[(builtin integerToByteString) (con bool False) (con integer -20) (con integer 5)]
)

Some files were not shown because too many files have changed in this diff Show More