| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637 |
- // SPDX-License-Identifier: Apache-2.0
- use crate::{build_solidity, BorshToken};
- use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt};
- use num_traits::{One, ToPrimitive, Zero};
- use rand::seq::SliceRandom;
- use rand::Rng;
- use std::ops::BitAnd;
- use std::ops::Div;
- use std::ops::Mul;
- use std::ops::Rem;
- use std::ops::Shl;
- use std::ops::Shr;
- use std::ops::Sub;
- use std::ops::{Add, BitOr, BitXor, MulAssign, ShlAssign, ShrAssign, SubAssign};
- #[test]
- #[should_panic]
- fn assert_false() {
- // without a working assert, this is not going to work
- let mut vm = build_solidity(
- r#"
- contract foo {
- function assert_fails() public {
- require(false, "humpty-dumpty");
- }
- }"#,
- );
- vm.constructor(&[]);
- vm.function("assert_fails", &[]);
- }
- #[test]
- fn assert_true() {
- // without a working assert, this is not going to work
- let mut vm = build_solidity(
- r#"
- contract foo {
- function assert_fails() public {
- require(true, "humpty-dumpty");
- }
- }"#,
- );
- vm.constructor(&[]);
- vm.function("assert_fails", &[]);
- }
- #[test]
- fn boolean() {
- // we need to test: literals
- // passing address around
- // abi encoding/decoding address
- // comparing address to another
- let mut vm = build_solidity(
- r#"
- contract foo {
- function return_true() public returns (bool) {
- return true;
- }
- function return_false() public returns (bool) {
- return false;
- }
- function true_arg(bool b) public {
- assert(b);
- }
- function false_arg(bool b) public {
- assert(!b);
- }
- }"#,
- );
- vm.constructor(&[]);
- let returns = vm.function("return_true", &[]).unwrap();
- assert_eq!(returns, BorshToken::Bool(true));
- let returns = vm.function("return_false", &[]).unwrap();
- assert_eq!(returns, BorshToken::Bool(false));
- vm.function("true_arg", &[BorshToken::Bool(true)]);
- vm.function("false_arg", &[BorshToken::Bool(false)]);
- }
- #[test]
- fn address() {
- // we need to test: literals
- // passing address around
- // abi encoding/decoding address
- // comparing address to another
- let mut vm = build_solidity(
- r#"
- contract foo {
- function return_address() public returns (address) {
- return address'CXQw5tfeRKKzV4hk6PcdyKyANSvFxoZCKwHkVXAhAYSJ';
- }
- function address_arg(address a) public {
- assert(a == address'66Eh1STPJgabub73TP8YbN7VNCwjaVTEJGHRxCLeBJ4A');
- }
- }"#,
- );
- vm.constructor(&[]);
- let returns = vm.function("return_address", &[]).unwrap();
- assert_eq!(
- returns,
- BorshToken::Address([
- 171, 59, 10, 127, 211, 122, 217, 123, 53, 213, 159, 40, 54, 36, 50, 52, 196, 144, 17,
- 226, 97, 168, 69, 213, 79, 14, 6, 232, 165, 44, 58, 31
- ]),
- );
- vm.function(
- "address_arg",
- &[BorshToken::Address([
- 75, 161, 209, 89, 47, 84, 50, 13, 23, 127, 94, 21, 50, 249, 250, 185, 117, 49, 186,
- 134, 82, 130, 112, 97, 218, 24, 157, 198, 40, 105, 118, 27,
- ])],
- );
- }
- #[test]
- fn test_enum() {
- // we need to test enum literals
- // abi encoding/decode literals
- // comparing enums
- let mut vm = build_solidity(
- r#"
- contract foo {
- enum bar { bar0, bar1, bar2, bar3, bar4, bar5, bar6, bar7, bar8, bar9, bar10 }
- function return_enum() public returns (bar) {
- return bar.bar9;
- }
- function enum_arg(bar a) public {
- assert(a == bar.bar6);
- }
- }"#,
- );
- vm.constructor(&[]);
- let returns = vm.function("return_enum", &[]).unwrap();
- assert_eq!(
- returns,
- BorshToken::Uint {
- width: 8,
- value: BigInt::from(9u8)
- }
- );
- vm.function(
- "enum_arg",
- &[BorshToken::Uint {
- width: 8,
- value: BigInt::from(6u8),
- }],
- );
- }
- #[test]
- fn bytes() {
- let mut rng = rand::thread_rng();
- for width in 1..32 {
- let src = r#"
- contract test {
- function return_literal() public returns (bytes7) {
- return hex"01020304050607";
- }
- function return_arg(bytes7 x) public returns (bytes7) {
- return x;
- }
- function or(bytesN a, bytesN b) public returns (bytesN) {
- return a | b;
- }
- function and(bytesN a, bytesN b) public returns (bytesN) {
- return a & b;
- }
- function xor(bytesN a, bytesN b) public returns (bytesN) {
- return a ^ b;
- }
- function shift_left(bytesN a, uint32 r) public returns (bytesN) {
- return a << r;
- }
- function shift_right(bytesN a, uint32 r) public returns (bytesN) {
- return a >> r;
- }
- }"#
- .replace("bytesN", &format!("bytes{width}"));
- let mut vm = build_solidity(&src);
- vm.constructor(&[]);
- let returns = vm.function("return_literal", &[]).unwrap();
- assert_eq!(
- returns,
- BorshToken::uint8_fixed_array(vec![1, 2, 3, 4, 5, 6, 7])
- );
- let returns = vm
- .function(
- "return_arg",
- &[BorshToken::FixedBytes(vec![1, 2, 3, 4, 5, 6, 7])],
- )
- .unwrap();
- assert_eq!(
- returns,
- BorshToken::uint8_fixed_array(vec![1, 2, 3, 4, 5, 6, 7])
- );
- for _ in 0..10 {
- let mut a = Vec::new();
- let mut b = Vec::new();
- a.resize(width, 0);
- b.resize(width, 0);
- rng.fill(&mut a[..]);
- rng.fill(&mut b[..]);
- let or = vm
- .function(
- "or",
- &[
- BorshToken::FixedBytes(a.to_vec()),
- BorshToken::FixedBytes(b.to_vec()),
- ],
- )
- .unwrap();
- let res: Vec<u8> = a.iter().zip(b.iter()).map(|(a, b)| a | b).collect();
- println!(
- "{} | {} = {}",
- hex::encode(&a),
- hex::encode(&b),
- hex::encode(&res)
- );
- assert_eq!(or, BorshToken::uint8_fixed_array(res));
- let and = vm
- .function(
- "and",
- &[
- BorshToken::FixedBytes(a.to_vec()),
- BorshToken::FixedBytes(b.to_vec()),
- ],
- )
- .unwrap();
- let res: Vec<u8> = a.iter().zip(b.iter()).map(|(a, b)| a & b).collect();
- assert_eq!(and, BorshToken::uint8_fixed_array(res));
- let xor = vm
- .function(
- "xor",
- &[
- BorshToken::FixedBytes(a.to_vec()),
- BorshToken::FixedBytes(b.to_vec()),
- ],
- )
- .unwrap();
- let res: Vec<u8> = a.iter().zip(b.iter()).map(|(a, b)| a ^ b).collect();
- assert_eq!(xor, BorshToken::uint8_fixed_array(res));
- let r = rng.gen::<u32>() % (width as u32 * 8);
- println!("w = {width} r = {r}");
- let shl = vm
- .function(
- "shift_left",
- &[
- BorshToken::FixedBytes(a.to_vec()),
- BorshToken::Uint {
- width: 32,
- value: BigInt::from(r),
- },
- ],
- )
- .unwrap();
- let mut res = (BigUint::from_bytes_be(&a) << r).to_bytes_be();
- while res.len() > width {
- res.remove(0);
- }
- while res.len() < width {
- res.insert(0, 0);
- }
- assert_eq!(shl, BorshToken::uint8_fixed_array(res));
- let shr = vm
- .function(
- "shift_right",
- &[
- BorshToken::FixedBytes(a.to_vec()),
- BorshToken::Uint {
- width: 32,
- value: BigInt::from(r),
- },
- ],
- )
- .unwrap();
- let mut res = (BigUint::from_bytes_be(&a) >> r).to_bytes_be();
- while res.len() < width {
- res.insert(0, 0);
- }
- assert_eq!(shr, BorshToken::uint8_fixed_array(res));
- }
- }
- }
- #[test]
- fn uint() {
- let mut rng = rand::thread_rng();
- for width in (8u16..=256).step_by(8) {
- let src = r#"
- contract test {
- function pass(uintN a) public returns (uintN) {
- print("x:{:x}".format(uint64(a)));
- return 0x7f;
- }
- function add(uintN a, uintN b) public returns (uintN) {
- unchecked {
- return a + b;
- }
- }
- function sub(uintN a, uintN b) public returns (uintN) {
- unchecked {
- return a - b;
- }
- }
- function mul(uintN a, uintN b) public returns (uintN) {
- unchecked {
- return a * b;
- }
- }
- function div(uintN a, uintN b) public returns (uintN) {
- unchecked {
- return a / b;
- }
- }
- function mod(uintN a, uintN b) public returns (uintN) {
- unchecked {
- return a % b;
- }
- }
- function pow(uintN a, uintN b) public returns (uintN) {
- unchecked {
- return a ** b;
- }
- }
- function or(uintN a, uintN b) public returns (uintN) {
- return a | b;
- }
- function and(uintN a, uintN b) public returns (uintN) {
- return a & b;
- }
- function xor(uintN a, uintN b) public returns (uintN) {
- return a ^ b;
- }
- function shift_left(uintN a, uint32 r) public returns (uintN) {
- return a << r;
- }
- function shift_right(uintN a, uint32 r) public returns (uintN) {
- return a >> r;
- }
- }"#
- .replace("uintN", &format!("uint{width}"));
- let mut vm = build_solidity(&src);
- vm.constructor(&[]);
- println!("width:{width}");
- let returned_width = width.next_power_of_two();
- for _ in 0..10 {
- let mut a = rng.gen_biguint(width as u64);
- let mut b = rng.gen_biguint(width as u64);
- if b > a {
- std::mem::swap(&mut a, &mut b);
- }
- let res = vm.function(
- "pass",
- &[BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- }],
- );
- println!("{a:x} = {res:?} o");
- let add = vm
- .function(
- "add",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().add(&b);
- truncate_biguint(&mut res, width);
- println!("{a:x} + {b:x} = {add:?} or {res:x}");
- assert_eq!(
- add,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let sub = vm
- .function(
- "sub",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().sub(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- sub,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let mul = vm
- .function(
- "mul",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().mul(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- mul,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- if let Some(mut n) = b.to_u32() {
- n %= 65536;
- let pow = vm
- .function(
- "pow",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: BigInt::from(n),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().pow(n);
- truncate_biguint(&mut res, width);
- assert_eq!(
- pow,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- }
- if b != BigUint::zero() {
- let div = vm
- .function(
- "div",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().div(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- div,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let add = vm
- .function(
- "mod",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().rem(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- add,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- }
- let or = vm
- .function(
- "or",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().bitor(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- or,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let and = vm
- .function(
- "and",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().bitand(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- and,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let xor = vm
- .function(
- "xor",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width,
- value: b.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().bitxor(&b);
- truncate_biguint(&mut res, width);
- assert_eq!(
- xor,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let r = rng.gen::<u32>() % (width as u32);
- let shl = vm
- .function(
- "shift_left",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width: 32,
- value: BigInt::from(r),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone();
- res.shl_assign(r);
- truncate_biguint(&mut res, width);
- assert_eq!(
- shl,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- let shr = vm
- .function(
- "shift_right",
- &[
- BorshToken::Uint {
- width,
- value: a.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width: 32,
- value: BigInt::from(r),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone();
- res.shr_assign(&r);
- truncate_biguint(&mut res, width);
- assert_eq!(
- shr,
- BorshToken::Uint {
- width: returned_width,
- value: res.to_bigint().unwrap(),
- }
- );
- }
- }
- }
- fn truncate_biguint(n: &mut BigUint, width: u16) {
- let mut bytes = n.to_bytes_le();
- let byte_width = (width / 8) as usize;
- if bytes.len() < byte_width {
- return;
- }
- for item in bytes.iter_mut().skip(byte_width) {
- *item = 0;
- }
- *n = BigUint::from_bytes_le(&bytes);
- }
- #[test]
- fn test_power_overflow_boundaries() {
- for width in (8u16..=256).step_by(8) {
- let src = r#"
- contract test {
- function pow(uintN a, uintN b) public returns (uintN) {
- return a ** b;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut contract = build_solidity(&src);
- contract.constructor(&[]);
- let return_value = contract
- .function(
- "pow",
- &[
- BorshToken::Uint {
- width,
- value: BigInt::from(2u8),
- },
- BorshToken::Uint {
- width,
- value: BigInt::from(width - 1),
- },
- ],
- )
- .unwrap();
- let res = BigUint::from(2_u32).pow((width - 1) as u32);
- assert_eq!(
- return_value,
- BorshToken::Uint {
- width: width.next_power_of_two(),
- value: res.to_bigint().unwrap(),
- }
- );
- let sesa = contract.function_must_fail(
- "pow",
- &[
- BorshToken::Uint {
- width,
- value: BigInt::from(2u8),
- },
- BorshToken::Uint {
- width,
- value: BigInt::from(width + 1),
- },
- ],
- );
- assert_ne!(sesa.unwrap(), 0);
- }
- }
- #[test]
- fn test_overflow_boundaries() {
- for width in (8..=256).step_by(8) {
- let src = r#"
- contract test {
- function mul(intN a, intN b) public returns (intN) {
- return a * b;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut contract = build_solidity(&src);
- // The range of values that can be held in signed N bits is [-2^(N-1), 2^(N-1)-1]. We generate these boundaries:
- let mut upper_boundary: BigInt = BigInt::from(2_u32).pow((width - 1) as u32);
- upper_boundary.sub_assign(1);
- let mut lower_boundary: BigInt = BigInt::from(2_u32).pow((width - 1) as u32);
- lower_boundary.mul_assign(-1);
- let second_op = BigInt::from(1_u32);
- let returned_width = (width as u16).next_power_of_two();
- // Multiply the boundaries by 1.
- contract.constructor(&[]);
- let return_value = contract
- .function(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: upper_boundary.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: second_op.clone(),
- },
- ],
- )
- .unwrap();
- assert_eq!(
- return_value,
- BorshToken::Int {
- width: returned_width,
- value: upper_boundary.clone(),
- }
- );
- let return_value = contract
- .function(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: lower_boundary.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: second_op.clone(),
- },
- ],
- )
- .unwrap();
- assert_eq!(
- return_value,
- BorshToken::Int {
- width: returned_width,
- value: lower_boundary.clone(),
- },
- );
- let upper_boundary_plus_one: BigInt = BigInt::from(2_u32).pow((width - 1) as u32);
- // We subtract 2 instead of one to make the number even, so that no rounding occurs when we divide by 2 later on.
- let mut lower_boundary_minus_two: BigInt = BigInt::from(2_u32).pow((width - 1) as u32);
- lower_boundary_minus_two.mul_assign(-1_i32);
- lower_boundary_minus_two.sub_assign(2_i32);
- let upper_second_op = upper_boundary_plus_one.div(2);
- let lower_second_op = lower_boundary_minus_two.div(2);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: upper_second_op,
- },
- BorshToken::Int {
- width: width as u16,
- value: BigInt::from(2u8),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: lower_second_op,
- },
- BorshToken::Int {
- width: width as u16,
- value: BigInt::from(2),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: upper_boundary.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: upper_boundary.clone(),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: lower_boundary.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: lower_boundary.clone(),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: upper_boundary.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: lower_boundary.clone(),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- }
- }
- #[test]
- fn test_mul_within_range_signed() {
- let mut rng = rand::thread_rng();
- for width in (8..=256).step_by(8) {
- let src = r#"
- contract test {
- function mul(intN a, intN b) public returns (intN) {
- print("{}*{}".format(a, b));
- return a * b;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut contract = build_solidity(&src);
- // The range of values that can be held in signed N bits is [-2^(N-1), 2^(N-1)-1]. Here we generate a random number within this range and multiply it by -1, 1 or 0.
- let first_operand_rand = rng.gen_bigint(width - 1).sub(1_u32);
- println!("First op : {first_operand_rand:?}");
- let side = vec![-1, 0, 1];
- // -1, 1 or 0
- let second_op = BigInt::from(*side.choose(&mut rng).unwrap());
- println!("second op : {second_op:?}");
- contract.constructor(&[]);
- let return_value = contract
- .function(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: first_operand_rand.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: second_op.clone(),
- },
- ],
- )
- .unwrap();
- let res = first_operand_rand.mul(second_op);
- assert_eq!(
- return_value,
- BorshToken::Int {
- width: width.next_power_of_two() as u16,
- value: res,
- }
- );
- }
- }
- #[test]
- fn test_mul_within_range() {
- let mut rng = rand::thread_rng();
- for width in (8..=256).step_by(8) {
- let src = r#"
- contract test {
- function mul(uintN a, uintN b) public returns (uintN) {
- return a * b;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut contract = build_solidity(&src);
- contract.constructor(&[]);
- for _ in 0..10 {
- // Max number to fit unsigned N bits is (2^N)-1
- let mut limit: BigUint = BigUint::from(2_u32).pow(width as u32);
- limit.sub_assign(1u8);
- // Generate a random number within the the range [0, 2^N -1]
- let first_operand_rand = rng.gen_biguint_range(&BigUint::from(1usize), &limit);
- // Calculate a number that when multiplied by first_operand_rand, the result will not overflow N bits (the result of this division will cast the float result to int result, therefore lowering it. The result of multiplication will never overflow).
- let second_operand_rand = limit.div(&first_operand_rand);
- let return_value = contract
- .function(
- "mul",
- &[
- BorshToken::Uint {
- width: width as u16,
- value: first_operand_rand.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width: width as u16,
- value: second_operand_rand.to_bigint().unwrap(),
- },
- ],
- )
- .unwrap();
- let res = first_operand_rand * second_operand_rand;
- assert_eq!(
- return_value,
- BorshToken::Uint {
- width: (width as u16).next_power_of_two(),
- value: res.to_bigint().unwrap(),
- }
- );
- }
- }
- }
- #[test]
- fn test_overflow_detect_signed() {
- let mut rng = rand::thread_rng();
- for width in (8..=256).step_by(8) {
- let src = r#"
- contract test {
- function mul(intN a, intN b) public returns (intN) {
- return a * b;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut contract = build_solidity(&src);
- contract.constructor(&[]);
- // The range of values that can be held in signed N bits is [-2^(N-1), 2^(N-1)-1] .
- let mut limit: BigInt = BigInt::from(2_u32).pow((width - 1) as u32);
- limit.sub_assign(1u8);
- // Generate a random number within the the range [(2^N-1)/2, (2^N-1) -1]
- let first_operand_rand =
- rng.gen_bigint_range(&(limit.clone().div(2usize)).add(1usize), &limit);
- // Calculate a number that when multiplied by first_operand_rand, the result will overflow N bits
- let second_operand_rand = rng.gen_bigint_range(&BigInt::from(2usize), &limit);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: first_operand_rand.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: second_operand_rand.clone(),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- // The range of values that can be held in signed N bits is [-2^(N-1), 2^(N-1)-1] .
- let mut lower_limit: BigInt = BigInt::from(2_u32).pow((width - 1) as u32);
- lower_limit.sub_assign(1usize);
- lower_limit.mul_assign(-1_i32);
- // Generate a random number within the the range [-(2^N-1), -(2^N-1)/2]
- let first_operand_rand =
- rng.gen_bigint_range(&lower_limit, &(lower_limit.clone().div(2usize)).add(1usize));
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: first_operand_rand.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: second_operand_rand.clone(),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- }
- }
- #[test]
- fn test_overflow_detect_unsigned() {
- let mut rng = rand::thread_rng();
- for width in (8..=256).step_by(8) {
- let src = r#"
- contract test {
- function mul(uintN a, uintN b) public returns (uintN) {
- return a * b;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut contract = build_solidity(&src);
- contract.constructor(&[]);
- for _ in 0..10 {
- // N bits can hold the range [0, (2^N)-1]. Generate a value that overflows N bits
- let mut limit: BigUint = BigUint::from(2_u32).pow(width as u32);
- limit.sub_assign(1u8);
- // Generate a random number within the the range [(2^N-1)/2, 2^N -1]
- let first_operand_rand =
- rng.gen_biguint_range(&(limit.clone().div(2usize)).add(1usize), &limit);
- // Calculate a number that when multiplied by first_operand_rand, the result will overflow N bits
- let second_operand_rand = rng.gen_biguint_range(&BigUint::from(2usize), &limit);
- let res = contract.function_must_fail(
- "mul",
- &[
- BorshToken::Uint {
- width: width as u16,
- value: first_operand_rand.to_bigint().unwrap(),
- },
- BorshToken::Uint {
- width: width as u16,
- value: second_operand_rand.to_bigint().unwrap(),
- },
- ],
- );
- assert_ne!(res.unwrap(), 0);
- }
- }
- }
- #[test]
- fn int() {
- let mut rng = rand::thread_rng();
- for width in (8..=256).step_by(8) {
- let src = r#"
- contract test {
- function add(intN a, intN b) public returns (intN) {
- unchecked {
- return a + b;
- }
- }
- function sub(intN a, intN b) public returns (intN) {
- unchecked {
- return a - b;
- }
- }
- function mul(intN a, intN b) public returns (intN) {
- unchecked {
- return a * b;
- }
- }
- function div(intN a, intN b) public returns (intN) {
- unchecked {
- return a / b;
- }
- }
- function mod(intN a, intN b) public returns (intN) {
- return a % b;
- }
- function or(intN a, intN b) public returns (intN) {
- return a | b;
- }
- function and(intN a, intN b) public returns (intN) {
- return a & b;
- }
- function xor(intN a, intN b) public returns (intN) {
- return a ^ b;
- }
- function shift_left(intN a, uint32 r) public returns (intN) {
- return a << r;
- }
- function shift_right(intN a, uint32 r) public returns (intN) {
- return a >> r;
- }
- }"#
- .replace("intN", &format!("int{width}"));
- let mut vm = build_solidity(&src);
- vm.constructor(&[]);
- let returned_width = (width as u16).next_power_of_two();
- for _ in 0..10 {
- let a = rng.gen_bigint(width - 1);
- let b = rng.gen_bigint(width - 1);
- let add = vm
- .function(
- "add",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().add(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- add,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let sub = vm
- .function(
- "sub",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().sub(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- sub,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let mul = vm
- .function(
- "mul",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().mul(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- mul,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- if b != BigInt::zero() {
- let div = vm
- .function(
- "div",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().div(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- div,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let add = vm
- .function(
- "mod",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().rem(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- add,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- }
- let or = vm
- .function(
- "or",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().bitor(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- or,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let and = vm
- .function(
- "and",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().bitand(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- and,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let xor = vm
- .function(
- "xor",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Int {
- width: width as u16,
- value: b.clone(),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().bitxor(&b);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- xor,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let r = rng.gen::<u32>() % (width as u32);
- let shl = vm
- .function(
- "shift_left",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Uint {
- width: 32,
- value: BigInt::from(r),
- },
- ],
- )
- .unwrap();
- let mut res = a.clone().shl(r);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- shl,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- let shr = vm
- .function(
- "shift_right",
- &[
- BorshToken::Int {
- width: width as u16,
- value: a.clone(),
- },
- BorshToken::Uint {
- width: 32,
- value: BigInt::from(r),
- },
- ],
- )
- .unwrap();
- let mut res = a.shr(r);
- truncate_bigint(&mut res, width as usize);
- assert_eq!(
- shr,
- BorshToken::Int {
- width: returned_width,
- value: res,
- }
- );
- }
- }
- }
- fn truncate_bigint(n: &mut BigInt, width: usize) {
- let mut bytes_le = n.to_signed_bytes_le();
- let bytes_width = width / 8;
- if bytes_le.len() < bytes_width {
- return;
- }
- while bytes_le.len() > bytes_width {
- bytes_le.pop();
- }
- *n = BigInt::from_signed_bytes_le(&bytes_le);
- }
- #[test]
- fn bytes_cast() {
- let mut vm = build_solidity(
- r#"
- contract foo {
- function to_bytes(bytes4 b) public returns (bytes) {
- return b;
- }
- function to_bytes5(bytes b) public returns (bytes5) {
- return b;
- }
- }
- "#,
- );
- vm.constructor(&[]);
- let returns = vm
- .function("to_bytes", &[BorshToken::FixedBytes(b"abcd".to_vec())])
- .unwrap();
- assert_eq!(returns, BorshToken::Bytes(b"abcd".to_vec()));
- let returns = vm
- .function("to_bytes5", &[BorshToken::Bytes(b"abcde".to_vec())])
- .unwrap();
- assert_eq!(returns, BorshToken::uint8_fixed_array(b"abcde".to_vec()));
- }
- #[test]
- fn shift_after_load() {
- let mut vm = build_solidity(
- r#"
- contract OneSwapToken {
- function testIt(uint256[] calldata mixedAddrVal) public pure returns (uint256, uint256) {
- uint256 a = mixedAddrVal[0]<<2;
- uint256 b = mixedAddrVal[1]>>2;
- return (a, b);
- }
- }
- "#,
- );
- vm.constructor(&[]);
- let args = BorshToken::Array(vec![
- BorshToken::Uint {
- width: 256,
- value: BigInt::one(),
- },
- BorshToken::Uint {
- width: 256,
- value: BigInt::from(4u8),
- },
- ]);
- let returns = vm.function("testIt", &[args]).unwrap().unwrap_tuple();
- assert_eq!(returns.len(), 2);
- assert_eq!(
- returns[0],
- BorshToken::Uint {
- width: 256,
- value: BigInt::from(4u8)
- }
- );
- assert_eq!(
- returns[1],
- BorshToken::Uint {
- width: 256,
- value: BigInt::one(),
- }
- );
- }
|