Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the Math API with EVM precompiles #3954

Closed
wants to merge 96 commits into from
Closed
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
4101d54
feat: Extend the Math API with EVM precompiles.
artob Feb 12, 2021
f1129da
feat: Add Math API stubs for ECRecover. (#3921)
artob Feb 17, 2021
55291ee
test: Add RIPEMD-160 and BLAKE2b to the params estimator.
artob Feb 17, 2021
e4aedd6
fix: Improve error output from the params estimator.
artob Feb 17, 2021
4b145d1
test: Add ECRecover stubs to the params estimator.
artob Feb 18, 2021
f8461f7
fix: Widen ecrecover() parameter type to pass CI checks.
artob Feb 18, 2021
becf003
feat(chain): Bump the protocol version.
artob Feb 18, 2021
4950ad3
feat(runtime): Implement ECRecover in the Math API. (#3921)
artob Feb 19, 2021
a5bbe0b
docs: Document the ecrecover() function.
artob Feb 19, 2021
2f88193
fix(chain): Guard protocol upgrade behind EVM feature.
artob Feb 19, 2021
e97e6c6
add crypto feature gates
joshuajbouw Mar 10, 2021
acb1440
fix nightly compile
joshuajbouw Mar 11, 2021
773d818
cargo fmt
joshuajbouw Mar 11, 2021
bdc0495
fix two ripemd160 missing feature gates
joshuajbouw Mar 11, 2021
b55fa0f
rename crypto_extras feature to protocol_feature equivalents
joshuajbouw Mar 12, 2021
e6c389f
change repo to our git
joshuajbouw Mar 19, 2021
fd0022a
put everything under protocol_feature_evm
joshuajbouw Mar 19, 2021
c1d77f0
point to alternative blake2 branch temporarily
joshuajbouw Mar 19, 2021
f7ec5b1
Add blake2b f function
joshuajbouw Mar 19, 2021
598b0c5
add blake2b_f to config
joshuajbouw Mar 23, 2021
16a32c8
post rebase fixes
joshuajbouw Mar 23, 2021
e2b5e59
wrap all ext_costs in evm feature
joshuajbouw Mar 23, 2021
ca3e329
add blake_2b_f to runtime estimator
joshuajbouw Mar 23, 2021
188b9b8
cargo fmt
joshuajbouw Mar 23, 2021
b0236f5
add new hash algos to feature
joshuajbouw Mar 24, 2021
8945af7
add vm-logic test for blake2b_f
joshuajbouw Mar 24, 2021
78ec861
fix derive formatting
joshuajbouw Mar 24, 2021
075e6be
use same blake2 library
joshuajbouw Mar 24, 2021
d53a580
change blake F tests to test 1 round
joshuajbouw Mar 24, 2021
8da8c49
fix blake f test
joshuajbouw Mar 24, 2021
9f3358c
cleanup calls_helper
joshuajbouw Mar 24, 2021
5ac0d32
add missing feature gated fields from ExtCostsConfig
joshuajbouw Mar 25, 2021
1c9ba91
add blake2b_f compression docs
joshuajbouw Mar 26, 2021
6872025
remove unused `blake2b_f_byte` as sizes are fixed
joshuajbouw Mar 26, 2021
6704cad
add missing blake2b_f to imports
joshuajbouw Mar 26, 2021
36bbc37
runtime estimator mod docs
joshuajbouw Mar 26, 2021
e971fe3
Add near-blake2 lib to near-crypto
joshuajbouw May 19, 2021
10f356f
fix test imports
joshuajbouw May 20, 2021
0fca391
add more pub methods to blake2
joshuajbouw May 21, 2021
83a1d11
add blake2 state success tests
joshuajbouw May 21, 2021
bdc0904
cargo fmt
joshuajbouw May 21, 2021
7dbcfdb
Add initial blake2 logic to vm
joshuajbouw May 21, 2021
ee2d0f6
Fix ecrecover
birchmd Apr 30, 2021
ebf64bc
Change blake2 `update` to `update_inner`
joshuajbouw May 21, 2021
bffa864
Update params estimator
joshuajbouw May 21, 2021
bce76f3
Remove duplicate evm feature
joshuajbouw May 21, 2021
6b5d85c
Merge branch 'master' into evm-math-api
joshuajbouw May 21, 2021
ac9982d
Remove pattern
joshuajbouw May 21, 2021
55dda77
Fix some compilation errors
birchmd May 21, 2021
77f1b2a
Fix runtime-params-estimator compile
birchmd May 21, 2021
d461c3b
Fix tests
joshuajbouw May 24, 2021
6207f4b
Reduce blake2s param estimator rounds from 12 to 10
joshuajbouw May 25, 2021
3b4c098
Add protocol_feature_evm feature to standalone runner
joshuajbouw May 25, 2021
6851665
Merge branch 'master' into evm-math-api
joshuajbouw May 26, 2021
355b997
Merge branch 'master' into evm-math-api
joshuajbouw May 27, 2021
9d84a33
Add estimated gas values
joshuajbouw May 27, 2021
6a57744
Merge remote-tracking branch 'upstream/evm-math-api' into evm-math-api
joshuajbouw May 27, 2021
e0cddbb
Update estimated gas value note
joshuajbouw May 27, 2021
15e9ecc
cargo fmt
joshuajbouw May 27, 2021
6c21c78
Update c2-chacha
joshuajbouw May 27, 2021
4483cd4
Remove nightly features from lib blake2
joshuajbouw May 27, 2021
4a1c26e
Add blake2 pass tests
joshuajbouw May 27, 2021
bcc7dd2
Fix up ecrecover test
joshuajbouw May 27, 2021
2f7cd2f
Fix blake2s args
joshuajbouw May 27, 2021
1dc1090
Merge branch 'master' into evm-math-api
joshuajbouw May 31, 2021
9a9fd8a
Merge remote-tracking branch 'upstream/master' into evm-math-api
joshuajbouw Jun 2, 2021
844da2e
Remove blake2 lib
joshuajbouw Jun 2, 2021
9f13b1e
Update documentation for blake2
joshuajbouw Jun 2, 2021
76e2a2e
Pin comment for blake2 lib
joshuajbouw Jun 2, 2021
ec72a48
Fix with_state
joshuajbouw Jun 4, 2021
276523e
Merge remote-tracking branch 'upstream/master' into evm-math-api
joshuajbouw Jun 11, 2021
e395b1c
Return a bool for ecrecover, not abort
joshuajbouw Jun 11, 2021
fe76336
Fix VarBlake2b -> VarBlake2s
joshuajbouw Jun 11, 2021
bda2e42
Add errors to RPC errors schema
joshuajbouw Jun 11, 2021
cf61d15
Add bool return to wrapped imports
joshuajbouw Jun 11, 2021
b61e89d
Merge remote-tracking branch 'upstream/master' into evm-math-api
joshuajbouw Jun 11, 2021
c59b121
Merge branch 'master' into evm-math-api
joshuajbouw Jun 14, 2021
23cd417
Take t0 and t1 for blake2b
joshuajbouw Jun 14, 2021
48fb8dd
Charge ripemd160 by blocks, not bytes
joshuajbouw Jun 14, 2021
f985379
Correctly send both t0 and t1 to blake2
joshuajbouw Jun 15, 2021
93240c6
Remove too many rounds error
joshuajbouw Jun 15, 2021
764f4a1
Remove any near-blake2 errors
joshuajbouw Jun 15, 2021
8be6dac
Remove hash data overflow error
joshuajbouw Jun 15, 2021
b58d765
Rename Blake2InvalidStateLength -> Blake2StateLengthExceeded
joshuajbouw Jun 15, 2021
1410e6d
Small ecrecover clarifications
joshuajbouw Jun 19, 2021
0210eab
Return public key in ecrecover
joshuajbouw Jun 19, 2021
4f82dc9
Correct blake2 message block math
joshuajbouw Jun 19, 2021
c79cd20
Correct ripemd160 message block math
joshuajbouw Jun 19, 2021
98ae102
Read blake2s as u32 not u64
joshuajbouw Jun 19, 2021
6db1324
Remove `state_len` and blake2 len error
joshuajbouw Jun 19, 2021
ed8b9c8
Add `saturating_mul` to blake2 math
joshuajbouw Jun 19, 2021
fa448b1
Don't allocate an extra vec on blake2
joshuajbouw Jun 19, 2021
72273d2
Merge branch 'master' into evm-math-api
joshuajbouw Jun 19, 2021
60a426b
Remove unused import
joshuajbouw Jun 19, 2021
c1eba41
Fix args in wrapped imports
joshuajbouw Jun 19, 2021
881aaee
Fix blake2s test
joshuajbouw Jun 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion chain/jsonrpc/res/rpc_errors_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
"subtypes": [],
"props": {}
},
"Blake2HashDataOverflow": {
"name": "Blake2HashDataOverflow",
"subtypes": [],
"props": {}
},
"Blake2InvalidStateLength": {
"name": "Blake2InvalidStateLength",
"subtypes": [],
"props": {
"length": ""
}
},
"BreakpointTrap": {
"name": "BreakpointTrap",
"subtypes": [],
Expand Down Expand Up @@ -163,7 +175,9 @@
"NumberInputDataDependenciesExceeded",
"ReturnedValueLengthExceeded",
"ContractSizeExceeded",
"Deprecated"
"Deprecated",
"Blake2InvalidStateLength",
"Blake2HashDataOverflow"
],
"props": {}
},
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
arrayref = "0.3"
blake2 = "0.9.1"
blake2 = { package = "near-blake2", git = "https://github.com/near/near-blake2.git", rev = "29a15a" }
borsh = "0.8.1"
bs58 = "0.4"
c2-chacha = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lazy_static = "1.4"

[features]
default = []
protocol_feature_add_account_versions = []
protocol_feature_evm = []
protocol_feature_add_account_versions = []
protocol_feature_alt_bn128 = []
protocol_feature_tx_size_limit = []
107 changes: 107 additions & 0 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,36 @@ pub struct ExtCostsConfig {
/// Cost of getting sha256 per byte
pub keccak512_byte: Gas,

/// Cost of getting ripemd160 base
#[cfg(feature = "protocol_feature_evm")]
pub ripemd160_base: Gas,
joshuajbouw marked this conversation as resolved.
Show resolved Hide resolved
/// Cost of getting ripemd160 per message block
#[cfg(feature = "protocol_feature_evm")]
pub ripemd160_block: Gas,

/// Cost of getting blake2b base
#[cfg(feature = "protocol_feature_evm")]
pub blake2b_base: Gas,
/// Cost of getting blake2b per 128 byte message block.
#[cfg(feature = "protocol_feature_evm")]
pub blake2b_block: Gas,
/// Cost of a single blake2b round.
#[cfg(feature = "protocol_feature_evm")]
pub blake2b_round: Gas,
/// Cost of getting blake2b base
#[cfg(feature = "protocol_feature_evm")]
pub blake2s_base: Gas,
/// Cost of getting blake2b per 128 byte message block.
#[cfg(feature = "protocol_feature_evm")]
pub blake2s_block: Gas,
/// Cost of a single blake2b round.
#[cfg(feature = "protocol_feature_evm")]
pub blake2s_round: Gas,

/// Cost of calling ecrecover
#[cfg(feature = "protocol_feature_evm")]
pub ecrecover_base: Gas,

/// Cost for calling logging.
pub log_base: Gas,
/// Cost for logging per byte
Expand Down Expand Up @@ -353,6 +383,24 @@ impl Default for ExtCostsConfig {
keccak256_byte: SAFETY_MULTIPLIER * 7157035,
keccak512_base: SAFETY_MULTIPLIER * 1937129412,
keccak512_byte: SAFETY_MULTIPLIER * 12216567,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_base: SAFETY_MULTIPLIER * 15136567500, // 10 x sha256_base
#[cfg(feature = "protocol_feature_evm")]
ripemd160_block: SAFETY_MULTIPLIER * 80391170 * 8, // 10 x sha256_byte x 8 bytes
#[cfg(feature = "protocol_feature_evm")]
blake2b_base: SAFETY_MULTIPLIER * 88256037, // same as base.
#[cfg(feature = "protocol_feature_evm")]
blake2b_block: SAFETY_MULTIPLIER * 1029006976, // sha256 per byte x 128
#[cfg(feature = "protocol_feature_evm")]
blake2b_round: SAFETY_MULTIPLIER * 25227612, // sha256 base / 60
#[cfg(feature = "protocol_feature_evm")]
blake2s_base: SAFETY_MULTIPLIER * 58837358, // blake2b * (2/3)
#[cfg(feature = "protocol_feature_evm")]
blake2s_block: SAFETY_MULTIPLIER * 686004650, // blake2b * (2/3)
#[cfg(feature = "protocol_feature_evm")]
blake2s_round: SAFETY_MULTIPLIER * 16818408, // blake2b * (2/3)
#[cfg(feature = "protocol_feature_evm")]
ecrecover_base: SAFETY_MULTIPLIER * 75682837500, // 50 x sha256_base
log_base: SAFETY_MULTIPLIER * 1181104350,
log_byte: SAFETY_MULTIPLIER * 4399597,
storage_write_base: SAFETY_MULTIPLIER * 21398912000,
Expand Down Expand Up @@ -423,6 +471,24 @@ impl ExtCostsConfig {
keccak256_byte: 0,
keccak512_base: 0,
keccak512_byte: 0,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_base: 0,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_block: 0,
#[cfg(feature = "protocol_feature_evm")]
blake2b_base: 0,
#[cfg(feature = "protocol_feature_evm")]
blake2b_block: 0,
#[cfg(feature = "protocol_feature_evm")]
blake2b_round: 0,
#[cfg(feature = "protocol_feature_evm")]
blake2s_base: 0,
#[cfg(feature = "protocol_feature_evm")]
blake2s_block: 0,
#[cfg(feature = "protocol_feature_evm")]
blake2s_round: 0,
#[cfg(feature = "protocol_feature_evm")]
ecrecover_base: 0,
log_base: 0,
log_byte: 0,
storage_write_base: 0,
Expand Down Expand Up @@ -494,6 +560,24 @@ pub enum ExtCosts {
keccak256_byte,
keccak512_base,
keccak512_byte,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_base,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_block,
#[cfg(feature = "protocol_feature_evm")]
blake2b_base,
#[cfg(feature = "protocol_feature_evm")]
blake2b_block,
#[cfg(feature = "protocol_feature_evm")]
blake2b_round,
#[cfg(feature = "protocol_feature_evm")]
blake2s_base,
#[cfg(feature = "protocol_feature_evm")]
blake2s_block,
#[cfg(feature = "protocol_feature_evm")]
blake2s_round,
#[cfg(feature = "protocol_feature_evm")]
ecrecover_base,
log_base,
log_byte,
storage_write_base,
Expand Down Expand Up @@ -618,6 +702,24 @@ impl ExtCosts {
keccak256_byte => config.keccak256_byte,
keccak512_base => config.keccak512_base,
keccak512_byte => config.keccak512_byte,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_base => config.ripemd160_base,
#[cfg(feature = "protocol_feature_evm")]
ripemd160_block => config.ripemd160_block,
#[cfg(feature = "protocol_feature_evm")]
blake2b_base => config.blake2b_base,
#[cfg(feature = "protocol_feature_evm")]
blake2b_block => config.blake2b_block,
#[cfg(feature = "protocol_feature_evm")]
blake2b_round => config.blake2b_round,
#[cfg(feature = "protocol_feature_evm")]
blake2s_base => config.blake2s_base,
#[cfg(feature = "protocol_feature_evm")]
blake2s_block => config.blake2s_block,
#[cfg(feature = "protocol_feature_evm")]
blake2s_round => config.blake2s_round,
#[cfg(feature = "protocol_feature_evm")]
ecrecover_base => config.ecrecover_base,
log_base => config.log_base,
log_byte => config.log_byte,
storage_write_base => config.storage_write_base,
Expand Down Expand Up @@ -692,6 +794,11 @@ impl ExtCosts {
"keccak256_byte",
"keccak512_base",
"keccak512_byte",
"ripemd160_base",
"ripemd160_block",
"blake2b_base",
"blake2b_byte",
"ecrecover_base",
"log_base",
"log_byte",
"storage_write_base",
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" }
default = ["jemallocator"]
dump_errors_schema = ["near-rpc-error-macro/dump_errors_schema"]
protocol_feature_add_account_versions = ["near-primitives-core/protocol_feature_add_account_versions"]
protocol_feature_evm = ["near-primitives-core/protocol_feature_evm"]
protocol_feature_evm = ["near-primitives-core/protocol_feature_evm", "near-vm-errors/protocol_feature_evm"]
protocol_feature_block_header_v3 = []
protocol_feature_alt_bn128 = ["near-primitives-core/protocol_feature_alt_bn128", "near-vm-errors/protocol_feature_alt_bn128"]
protocol_feature_tx_size_limit = ["near-primitives-core/protocol_feature_tx_size_limit"]
Expand Down
Empty file modified runtime/near-test-contracts/res/test_contract_ts.wasm
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions runtime/near-vm-errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ near-rpc-error-macro = { path = "../../tools/rpctypegen/macro", version = "0.1.0

[features]
dump_errors_schema = ["near-rpc-error-macro/dump_errors_schema"]
protocol_feature_evm = []
protocol_feature_alt_bn128 = []

[package.metadata.workspaces]
Expand Down
10 changes: 10 additions & 0 deletions runtime/near-vm-errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ pub enum HostError {
ContractSizeExceeded { size: u64, limit: u64 },
/// The host function was deprecated.
Deprecated { method_name: String },
/// The length of the state is not 64.
#[cfg(feature = "protocol_feature_evm")]
Blake2InvalidStateLength { length: u64 },
/// Blake2 hash data overflow error.
#[cfg(feature = "protocol_feature_evm")]
Blake2HashDataOverflow,
/// Deserialization error for alt_bn128 functions
#[cfg(feature = "protocol_feature_alt_bn128")]
AltBn128DeserializationError { msg: String },
Expand Down Expand Up @@ -494,6 +500,10 @@ impl std::fmt::Display for HostError {
ReturnedValueLengthExceeded { length, limit } => write!(f, "The length of a returned value {} exceeds the limit {}", length, limit),
ContractSizeExceeded { size, limit } => write!(f, "The size of a contract code in DeployContract action {} exceeds the limit {}", size, limit),
Deprecated {method_name}=> write!(f, "Attempted to call deprecated host function {}", method_name),
#[cfg(feature = "protocol_feature_evm")]
Blake2InvalidStateLength { length } => write!(f, "Invalid Blake2 state length {}, must be 8 words of 8 bytes", length),
#[cfg(feature = "protocol_feature_evm")]
Blake2HashDataOverflow => write!(f, "Blake2 hash data length overflow."),
#[cfg(feature = "protocol_feature_alt_bn128")]
AltBn128DeserializationError { msg } => write!(f, "AltBn128 deserialization error: {}", msg),
#[cfg(feature = "protocol_feature_alt_bn128")]
Expand Down
5 changes: 4 additions & 1 deletion runtime/near-vm-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ This crate implements the specification of the interface that Near blockchain ex

[dependencies]
base64 = "0.13"
blake2 = { package = "near-blake2", git = "https://github.com/near/near-blake2.git", rev = "29a15a", optional = true }
borsh = "0.8.1"
bs58 = "0.4"
byteorder = "1.2"
libsecp256k1 = { version = "0.3.5", optional = true }
ripemd160 = { version = "0.9.0", optional = true }
serde = { version = "1", features = ["derive"] }
sha2 = ">=0.8,<0.10"
sha3 = ">=0.8,<0.10"
Expand All @@ -33,7 +36,7 @@ serde_json = {version= "1", features= ["preserve_order"]}

[features]
default = []
protocol_feature_evm = ["near-primitives-core/protocol_feature_evm"]
protocol_feature_evm = ["near-primitives-core/protocol_feature_evm", "near-vm-errors/protocol_feature_evm", "blake2", "ripemd160", "libsecp256k1"]
protocol_feature_alt_bn128 = ["bn", "near-primitives-core/protocol_feature_alt_bn128", "near-vm-errors/protocol_feature_alt_bn128"]
protocol_feature_allow_create_account_on_delete = []

Expand Down
10 changes: 5 additions & 5 deletions runtime/near-vm-logic/src/gas_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,18 @@ impl GasCounter {
self.deduct_gas(value, value)
}

/// A helper function to pay per byte gas
pub fn pay_per_byte(&mut self, cost: ExtCosts, num_bytes: u64) -> Result<()> {
let use_gas = num_bytes
/// A helper function to pay a multiple of a cost.
pub fn pay_per(&mut self, cost: ExtCosts, num: u64) -> Result<()> {
let use_gas = num
.checked_mul(cost.value(&self.ext_costs_config))
.ok_or(HostError::IntegerOverflow)?;

self.inc_ext_costs_counter(cost, num_bytes);
self.inc_ext_costs_counter(cost, num);
self.update_profile_host(cost, use_gas);
self.deduct_gas(use_gas, use_gas)
}

/// A helper function to pay base cost gas
/// A helper function to pay base cost gas.
pub fn pay_base(&mut self, cost: ExtCosts) -> Result<()> {
let base_fee = cost.value(&self.ext_costs_config);
self.inc_ext_costs_counter(cost, 1);
Expand Down
Loading