From ec58279424469a373e373623fd5c264422b5ee84 Mon Sep 17 00:00:00 2001 From: rvcas Date: Thu, 9 Feb 2023 09:44:06 -0500 Subject: [PATCH] test(machine): integer overflow --- Cargo.lock | 12 ++++++++++++ crates/uplc/Cargo.toml | 1 + crates/uplc/src/machine.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b294ff5a..9c9a3006 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1380,6 +1380,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.45" @@ -2657,6 +2668,7 @@ dependencies = [ "indexmap", "itertools", "k256", + "num-bigint", "pallas-addresses", "pallas-codec", "pallas-crypto", diff --git a/crates/uplc/Cargo.toml b/crates/uplc/Cargo.toml index 4747839a..4b1e9398 100644 --- a/crates/uplc/Cargo.toml +++ b/crates/uplc/Cargo.toml @@ -33,6 +33,7 @@ itertools = "0.10.5" indexmap = "1.9.2" secp256k1 = { version = "0.26.0", optional = true } k256 = { version = "0.12.0", optional = true } +num-bigint = "0.4.3" [dev-dependencies] hex = "0.4.3" diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index f45220b6..713d3ae4 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -730,3 +730,33 @@ impl From<&Constant> for Type { } } } + +#[cfg(test)] +mod test { + use crate::{ + ast::{Constant, NamedDeBruijn, Program, Term}, + builtins::DefaultFunction, + machine::Error, + }; + + use super::cost_model::ExBudget; + + #[test] + fn add_big_ints() { + let program: Program = Program { + version: (0, 0, 0), + term: Term::Apply { + function: Term::Apply { + function: Term::Builtin(DefaultFunction::AddInteger).into(), + argument: Term::Constant(Constant::Integer(i128::MAX).into()).into(), + } + .into(), + argument: Term::Constant(Constant::Integer(i128::MAX).into()).into(), + }, + }; + + let (eval_result, _, _) = program.eval(ExBudget::default()); + + assert!(!matches!(eval_result, Err(Error::OverflowError))); + } +}