Skip to content

Commit

Permalink
Merge pull request #136 from SigmaGmbH/feat/update-evm
Browse files Browse the repository at this point in the history
Feat/update evm
  • Loading branch information
MikkySnow authored Oct 8, 2024
2 parents dfdb448 + ebc0d02 commit e6ac60a
Show file tree
Hide file tree
Showing 117 changed files with 8,804 additions and 3,583 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "sgxvm/sgx-sdk"]
path = sgxvm/sgx-sdk
url = https://github.com/apache/teaclave-sgx-sdk.git
url = https://github.com/scrtlabs/incubator-teaclave-sgx-sdk.git
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := v1.0.5
VERSION := v1.0.6
COMMIT := $(shell git log -1 --format='%H')
ENCLAVE_HOME ?= $(HOME)/.swisstronik-enclave
PRODUCTION_MODE ?= false
Expand Down
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import (
"swisstronik/app/upgrades/v1_0_3"
"swisstronik/app/upgrades/v1_0_4"
"swisstronik/app/upgrades/v1_0_5"
"swisstronik/app/upgrades/v1_0_6"
"swisstronik/docs"
"swisstronik/encoding"
srvflags "swisstronik/server/flags"
Expand Down Expand Up @@ -1081,6 +1082,13 @@ func (app *App) setupUpgradeHandlers() {
),
)

app.UpgradeKeeper.SetUpgradeHandler(
v1_0_6.UpgradeName,
v1_0_6.CreateUpgradeHandler(
app.ModuleManager, app.configurator,
),
)

// When a planned update height is reached, the old binary will panic
// writing on disk the height and name of the update that triggered it
// This will read that value, and execute the preparations for the upgrade.
Expand Down
5 changes: 5 additions & 0 deletions app/upgrades/v1_0_6/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v1_0_6

const (
UpgradeName = "v1.0.6"
)
24 changes: 24 additions & 0 deletions app/upgrades/v1_0_6/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v1_0_6

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting module migrations...")

vm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return vm, err
}

ctx.Logger().Info("Upgrade complete")
return vm, err
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions crates/static-precompiles/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.'cfg(not(target_env = "sgx"))']
rustflags = ["-C", "target-feature=+avx2"]

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-cpu=skylake"]
1 change: 1 addition & 0 deletions crates/static-precompiles/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2023-03-13
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#[cfg(feature = "std")]
use std::vec::Vec;

#[cfg(not(feature = "std"))]
use sgx_tstd::vec::Vec;

use core::mem::size_of;
use crate::precompiles::{
ExitSucceed,
ExitError,
LinearCostPrecompile,
PrecompileFailure
};
use evm::interpreter::error::{ExitException, ExitResult, ExitSucceed};

use crate::LinearCostPrecompile;

const SIGMA: [[usize; 16]; 10] = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
Expand Down Expand Up @@ -88,13 +90,11 @@ impl LinearCostPrecompile for Blake2F {

/// Format of `input`:
/// [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f]
fn raw_execute(input: &[u8], _: u64) -> Result<(ExitSucceed, Vec<u8>), PrecompileFailure> {
fn raw_execute(input: &[u8], _: u64) -> (ExitResult, Vec<u8>) {
const BLAKE2_F_ARG_LEN: usize = 213;

if input.len() != BLAKE2_F_ARG_LEN {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other("input length for Blake2 F precompile should be exactly 213 bytes".into())
});
return (ExitException::Other("input length for Blake2 F precompile should be exactly 213 bytes".into()).into(), Vec::new());
}

let mut rounds_buf: [u8; 4] = [0; 4];
Expand Down Expand Up @@ -136,9 +136,7 @@ impl LinearCostPrecompile for Blake2F {
} else if input[212] == 0 {
false
} else {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other("incorrect final block indicator flag".into())
});
return (ExitException::Other("incorrect final block indicator flag".into()).into(), Vec::new());
};

compress(&mut h, m, [t_0.into(), t_1.into()], f, rounds as usize);
Expand All @@ -148,6 +146,6 @@ impl LinearCostPrecompile for Blake2F {
output_buf[i * 8..(i + 1) * 8].copy_from_slice(&state_word.to_le_bytes());
}

Ok((ExitSucceed::Returned, output_buf.to_vec()))
(ExitSucceed::Returned.into(), output_buf.to_vec())
}
}
199 changes: 199 additions & 0 deletions crates/static-precompiles/src/bn128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#[cfg(feature = "std")]
use std::vec::Vec;

#[cfg(not(feature = "std"))]
use sgx_tstd::vec::Vec;

use evm::interpreter::error::{ExitException, ExitResult, ExitSucceed};
use primitive_types::U256;
use substrate_bn as bn;

use crate::LinearCostPrecompile;

fn read_fr(input: &[u8], start_inx: usize) -> Result<bn::Fr, ExitException> {
bn::Fr::from_slice(&input[start_inx..(start_inx + 32)]).map_err(|_| ExitException::Other("Invalid field element".into()))
}

fn read_point(input: &[u8], start_inx: usize) -> Result<bn::G1, ExitException> {
use bn::{Fq, AffineG1, G1, Group};

let px = Fq::from_slice(&input[start_inx..(start_inx + 32)]).map_err(|_| ExitException::Other("Invalid point x coordinate".into()))?;
let py = Fq::from_slice(&input[(start_inx + 32)..(start_inx + 64)]).map_err(|_| ExitException::Other("Invalid point y coordinate".into()))?;
Ok(
if px == Fq::zero() && py == Fq::zero() {
G1::zero()
} else {
AffineG1::new(px, py).map_err(|_| ExitException::Other("Invalid curve point".into()))?.into()
}
)
}

/// The Bn128Add builtin
pub struct Bn128Add;

impl LinearCostPrecompile for Bn128Add {
const BASE: u64 = 15;
const WORD: u64 = 3;

fn raw_execute(
input: &[u8],
_: u64,
) -> (ExitResult, Vec<u8>) {
use bn::AffineG1;

let p1 = match read_point(input, 0) {
Ok(p) => p,
Err(err) => return (err.into(), Vec::new())
};
let p2 = match read_point(input, 64) {
Ok(p) => p,
Err(err) => return (err.into(), Vec::new())
};

let mut buf = [0u8; 64];
if let Some(sum) = AffineG1::from_jacobian(p1 + p2) {
// point not at infinity
match sum.x().to_big_endian(&mut buf[0..32]) {
Ok(_) => {},
Err(_) => return (ExitException::Other("Cannot fail since 0..32 is 32-byte length".into()).into(), Vec::new())
}

match sum.y().to_big_endian(&mut buf[32..64]) {
Ok(_) => {},
Err(_) => return (ExitException::Other("Cannot fail since 32..64 is 32-byte length".into()).into(), Vec::new())
}
}

(ExitSucceed::Returned.into(), buf.to_vec())
}
}

/// The Bn128Mul builtin
pub struct Bn128Mul;

impl LinearCostPrecompile for Bn128Mul {
const BASE: u64 = 15;
const WORD: u64 = 3;

fn raw_execute(
input: &[u8],
_: u64,
) -> (ExitResult, Vec<u8>) {
use bn::AffineG1;

let p = match read_point(input, 0) {
Ok(p) => p,
Err(err) => return (err.into(), Vec::new())
};
let fr = match read_fr(input, 64) {
Ok(fr) => fr,
Err(err) => return (err.into(), Vec::new())
};

let mut buf = [0u8; 64];
if let Some(sum) = AffineG1::from_jacobian(p * fr) {
// point not at infinity
match sum.x().to_big_endian(&mut buf[0..32]) {
Ok(_) => {},
Err(_) => return (ExitException::Other("Cannot fail since 0..32 is 32-byte length".into()).into(), Vec::new())
}

match sum.y().to_big_endian(&mut buf[32..64]) {
Ok(_) => {},
Err(_) => return (ExitException::Other("Cannot fail since 0..32 is 32-byte length".into()).into(), Vec::new())
}
}

(ExitSucceed::Returned.into(), buf.to_vec())
}
}

/// The Bn128Pairing builtin
pub struct Bn128Pairing;

impl LinearCostPrecompile for Bn128Pairing {
const BASE: u64 = 15;
const WORD: u64 = 3;

fn raw_execute(
input: &[u8],
_: u64,
) -> (ExitResult, Vec<u8>) {
use bn::{AffineG1, AffineG2, Fq, Fq2, pairing_batch, G1, G2, Gt, Group};

let ret_val = if input.is_empty() {
U256::one()
} else {
// (a, b_a, b_b - each 64-byte affine coordinates)
let elements = input.len() / 192;
let mut vals = Vec::new();
for idx in 0..elements {
let a_x = match Fq::from_slice(&input[idx*192..idx*192+32]) {
Ok(a) => a,
Err(_) => return (ExitException::Other("Invalid a argument x coordinate".into()).into(), Vec::new())
};

let a_y = match Fq::from_slice(&input[idx*192+32..idx*192+64]) {
Ok(a) => a,
Err(_) => return (ExitException::Other("Invalid a argument y coordinate".into()).into(), Vec::new())
};

let b_a_y = match Fq::from_slice(&input[idx*192+64..idx*192+96]) {
Ok(b) => b,
Err(_) => return (ExitException::Other("Invalid b argument imaginary coeff x coordinate".into()).into(), Vec::new())
};

let b_a_x = match Fq::from_slice(&input[idx*192+96..idx*192+128]) {
Ok(b) => b,
Err(_) => return (ExitException::Other("Invalid b argument imaginary coeff y coordinate".into()).into(), Vec::new())
};

let b_b_y = match Fq::from_slice(&input[idx*192+128..idx*192+160]) {
Ok(b) => b,
Err(_) => return (ExitException::Other("Invalid b argument real coeff x coordinate".into()).into(), Vec::new())
};

let b_b_x = match Fq::from_slice(&input[idx*192+160..idx*192+192]) {
Ok(b) => b,
Err(_) => return (ExitException::Other("Invalid b argument real coeff y coordinate".into()).into(), Vec::new())
};

let b_a = Fq2::new(b_a_x, b_a_y);
let b_b = Fq2::new(b_b_x, b_b_y);
let b = if b_a.is_zero() && b_b.is_zero() {
G2::zero()
} else {
let a_g2 = match AffineG2::new(b_a, b_b) {
Ok(a) => a,
Err(_) => return (ExitException::Other("Invalid b argument - not on curve".into()).into(), Vec::new())
};

G2::from(a_g2)
};
let a = if a_x.is_zero() && a_y.is_zero() {
G1::zero()
} else {
let a_g1 = match AffineG1::new(a_x, a_y) {
Ok(a) => a,
Err(_) => return (ExitException::Other("Invalid a argumant - not on curve".into()).into(), Vec::new())
};
G1::from(a_g1)
};
vals.push((a, b));
};

let mul = pairing_batch(&vals);

if mul == Gt::one() {
U256::one()
} else {
U256::zero()
}
};

let mut buf = [0u8; 32];
ret_val.to_big_endian(&mut buf);

(ExitSucceed::Returned.into(), buf.to_vec())
}
}
Loading

0 comments on commit e6ac60a

Please sign in to comment.