feat(runtime): implement byteStringToInteger and add conformance tests
This commit is contained in:
parent
da6e5ec6d1
commit
028528899c
|
@ -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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1151407
|
||||
| mem: 602})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con integer 0))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1101095
|
||||
| mem: 601})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con integer 20016001699311))
|
|
@ -0,0 +1,4 @@
|
|||
-- The empty bytestring decodes to 0
|
||||
(program 1.0.0
|
||||
[(builtin byteStringToInteger) (con bool True) (con bytestring #)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1101095
|
||||
| mem: 601})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con integer 0))
|
|
@ -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)]
|
||||
]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 2530335
|
||||
| mem: 1404})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bool True))
|
|
@ -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)]
|
||||
]
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 2581068
|
||||
| mem: 1405})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bool True))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1151407
|
||||
| mem: 602})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con integer 0))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1101095
|
||||
| mem: 601})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con integer 263666621887506))
|
|
@ -0,0 +1,4 @@
|
|||
-- The empty bytestring decodes to 0
|
||||
(program 1.0.0
|
||||
[(builtin byteStringToInteger) (con bool False) (con bytestring #)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1101095
|
||||
| mem: 601})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con integer 0))
|
|
@ -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)]
|
||||
]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 2530335
|
||||
| mem: 1404})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bool True))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 801})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #123456abcdef))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 802})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #000000000000123456abcdef))
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,2 @@
|
|||
({cpu: 104104055
|
||||
| mem: 1824})
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 1824})
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,4 @@
|
|||
-- Negative inputs give an error
|
||||
(program 1.0.0
|
||||
[(builtin integerToByteString) (con bool True) (con integer 20) (con integer -5)]
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1,4 @@
|
|||
-- Negative widths give an error
|
||||
(program 1.0.0
|
||||
[(builtin integerToByteString) (con bool True) (con integer -20) (con integer 5)]
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 802})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #000000000000000000000000))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 801})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #123456abcdef))
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,2 @@
|
|||
({cpu: 104104055
|
||||
| mem: 1824})
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,4 @@
|
|||
-- Negative inputs cause an error.
|
||||
(program 1.0.0
|
||||
[(builtin integerToByteString) (con bool True) (con integer 0) (con integer -5)]
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 801})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 801})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #efcdab563412))
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 802})
|
|
@ -0,0 +1 @@
|
|||
(program 1.0.0 (con bytestring #efcdab563412000000000000))
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,2 @@
|
|||
({cpu: 104104055
|
||||
| mem: 1824})
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -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)]
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
({cpu: 1477718
|
||||
| mem: 1824})
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,5 @@
|
|||
-- Negative inputs give an error
|
||||
(program 1.0.0
|
||||
[(builtin integerToByteString) (con bool False) (con integer 20) (con integer -5)]
|
||||
)
|
||||
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1,5 @@
|
|||
-- Negative widths give an error
|
||||
(program 1.0.0
|
||||
[(builtin integerToByteString) (con bool False) (con integer -20) (con integer 5)]
|
||||
)
|
||||
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
|
@ -0,0 +1 @@
|
|||
evaluation failure
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue