pallas/pallas-codec/src/flat/decode/mod.rs
Matthias Benkort 7cb1ffe100
fix(codec): Fix flat encoding and decoding of arbitrarily size integers (#378)
This commits fixes the flat encoding and decoding (and consequently,
  the zigzag) for large integers in the following ways:

  - It removes support for encoding and decoding i128 values.

  - It optionally (feature = "num-bigint") introduces encoding and
    decoding of large sized integers through the num-bigint::BigInt
    type.

  Without the feature enabled, it is still possible to encode and decode
  isize values; but the use of i128 is now prohibited (as it would
  overflow on boundaries) in favor of arbitrarily sized integers.

  The commit also introduces a missing property roundtrip for encoding
  and decoding large integers, which was missing and thus, failed to
  identify the overflow problem.

  See related issue: https://github.com/aiken-lang/aiken/issues/796
2024-01-13 10:09:16 -03:00

71 lines
1.3 KiB
Rust

mod decoder;
mod error;
use crate::flat::filler::Filler;
#[cfg(feature = "num-bigint")]
use num_bigint::BigInt;
pub use decoder::Decoder;
pub use error::Error;
pub trait Decode<'b>: Sized {
fn decode(d: &mut Decoder) -> Result<Self, Error>;
}
impl Decode<'_> for Filler {
fn decode(d: &mut Decoder) -> Result<Filler, Error> {
d.filler()?;
Ok(Filler::FillerEnd)
}
}
impl Decode<'_> for Vec<u8> {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
d.bytes()
}
}
impl Decode<'_> for u8 {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
d.u8()
}
}
impl Decode<'_> for isize {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
d.integer()
}
}
#[cfg(feature = "num-bigint")]
impl Decode<'_> for BigInt {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
Ok(d.big_integer()?.into())
}
}
impl Decode<'_> for usize {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
d.word()
}
}
impl Decode<'_> for char {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
d.char()
}
}
impl Decode<'_> for String {
fn decode(d: &mut Decoder) -> Result<Self, Error> {
d.utf8()
}
}
impl Decode<'_> for bool {
fn decode(d: &mut Decoder) -> Result<bool, Error> {
d.bool()
}
}