Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
Simplify App state computation (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
YaronWittenstein committed Mar 25, 2020
1 parent 8fb4898 commit ac779e4
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 849 deletions.
131 changes: 1 addition & 130 deletions crates/svm-common/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,7 @@
use std::ops::Add;

use crate::{
helpers::{u32_to_le_array, u8_pair_add, u8_triple_add},
impl_bytes_primitive,
};
use crate::impl_bytes_primitive;

impl_bytes_primitive!(Address, 20);

/// Assumption: `Address` internal bytes are laid out in *Big-Endian* order.
impl Add<u32> for Address {
type Output = [u8; 21];

fn add(self, b: u32) -> Self::Output {
// `self.0` is an address (Big-Endian order) `a20 (msb), a19, a18, a17, a16, ..., a1, a0 (lsb)`
// `b` is a 4-byte number (Big-Endian order): `b3 (msb), b2, b1, b0 (lsb)`
//
// output (marked as `c`) will consist of `21 bytes` and not `20 bytes` since a carry might be turned-on
// `c20 (msb), c19, c18, c17, c16, ..., c3, c2, c1, c0 (lsb)`

let mut c = [0; 21];
c[0..20].copy_from_slice(&self.0);

let a0 = self.0[19];
let a1 = self.0[18];
let a2 = self.0[17];
let a3 = self.0[16];
let a4 = self.0[15];

let [b0, b1, b2, b3] = u32_to_le_array(b);

let (carry0, c0) = u8_pair_add(a0, b0);
let (carry1, c1) = u8_triple_add(carry0, a1, b1);
let (carry2, c2) = u8_triple_add(carry1, a2, b2);
let (carry3, c3) = u8_triple_add(carry2, a3, b3);
let (carry4, c4) = u8_pair_add(carry3, a4); // we use `u8_pair_add` and not `u8_triple_add` since `b4 = 0`

c[20] = c0;
c[19] = c1;
c[18] = c2;
c[17] = c3;
c[16] = c4;
c[15] = carry4;

c
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -168,91 +124,6 @@ mod tests {
assert_eq!(expected, actual);
}

#[test]
fn address_from_u32() {
let expected = Address([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
]);

let actual = Address::from(0x11_22_33_44 as u32);

assert_eq!(expected, actual);
}

#[test]
fn address_add_u32_no_carry() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x15, 0x25, 0x35, 0x45,
];

let actual = Address::from(0x11_22_33_44 as u32).add(0x04_03_02_01);

// `assert_eq!` isn't implemented for an `Array` so we compare slices
assert_eq!(expected[..], actual[..]);
}

#[test]
fn address_add_u32_carry_0() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
];

let actual = Address::from(0x00_00_00_FF as u32).add(0x00_00_00_03);

assert_eq!(expected[..], actual[..]);
}

#[test]
fn address_add_u32_carry_1() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
];

let actual = Address::from(0x00_00_FF_00 as u32).add(0x00_00_03_00);

assert_eq!(expected[..], actual[..]);
}

#[test]
fn address_add_u32_carry_2() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00,
];

let actual = Address::from(0x00_FF_00_00 as u32).add(0x00_03_00_00);

assert_eq!(expected[..], actual[..]);
}

#[test]
fn address_add_u32_carry_3() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00,
];

let actual = Address::from(0xFF_00_00_00 as u32).add(0x03_00_00_00);

assert_eq!(expected[..], actual[..]);
}

#[test]
fn address_add_u32_carry_4() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
];

let actual = Address::from(0xFF_FF_00_00_00 as u64).add(0x03_00_00_00);

assert_eq!(expected[..], actual[..]);
}

#[test]
fn address_fmt_hex() {
let addr = Address([
Expand Down
124 changes: 0 additions & 124 deletions crates/svm-common/src/helpers.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/svm-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod address_of;

mod default_key_hasher;
mod ffi;
mod helpers;
mod key_hasher;
mod macros;
mod state;
Expand Down
47 changes: 0 additions & 47 deletions crates/svm-common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,52 +124,5 @@ macro_rules! impl_bytes_primitive {
$primitive(buf)
}
}

/// Should be used **only** for tests
#[doc(hidden)]
impl From<u32> for $primitive {
fn from(n: u32) -> $primitive {
let mut buf = [0; $byte_count];

let [n3, n2, n1, n0] = $crate::helpers::u32_to_be_array(n);

buf[$byte_count - 4] = n3;
buf[$byte_count - 3] = n2;
buf[$byte_count - 2] = n1;
buf[$byte_count - 1] = n0;

$primitive(buf)
}
}

/// Should be used **only** for tests
#[doc(hidden)]
impl From<i32> for $primitive {
#[inline]
fn from(n: i32) -> $primitive {
$primitive::from(n as u32)
}
}

/// Should be used **only** for tests
#[doc(hidden)]
impl From<u64> for $primitive {
fn from(n: u64) -> $primitive {
let mut buf = [0; $byte_count];

let [n7, n6, n5, n4, n3, n2, n1, n0] = $crate::helpers::u64_to_be_array(n);

buf[$byte_count - 8] = n7;
buf[$byte_count - 7] = n6;
buf[$byte_count - 6] = n5;
buf[$byte_count - 5] = n4;
buf[$byte_count - 4] = n3;
buf[$byte_count - 3] = n2;
buf[$byte_count - 2] = n1;
buf[$byte_count - 1] = n0;

$primitive(buf)
}
}
};
}
Loading

0 comments on commit ac779e4

Please sign in to comment.