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

feat: Add precompiled contracts for alt_bn128 curve [rebased] #3971

Merged
merged 38 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
32f3aeb
add support for alt_bn128
snjax Nov 2, 2020
f5a7e54
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn128
Feb 17, 2021
f8c5a9b
Merge
Feb 17, 2021
373515f
Wrapping some usage under features
Feb 17, 2021
2a49ed0
Updating param estimator. Re-format
Feb 18, 2021
40056a5
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 22, 2021
9230405
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 22, 2021
5e9425b
Update param estimator
Feb 22, 2021
280c40a
Upgrade bn
Feb 22, 2021
369076c
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 22, 2021
b3115e8
Fix default for config
Feb 23, 2021
ad4d769
Add protocol_version fix to wasmer1 during rebase
Feb 23, 2021
846d599
Remove res dependencies
Feb 23, 2021
ab76546
Adding tests for alt-bn128
Feb 24, 2021
5aa4b94
Make used_gas test feature dependent
Feb 24, 2021
5597fb9
Compile contracts before param estimator
Feb 24, 2021
529fe4d
Fix target
Feb 24, 2021
2c94fe0
Remove nightly toolchain from build
Feb 24, 2021
dbcd949
Add stop on failure into build contract for param estimator
Feb 24, 2021
bef3d4f
Switch to cross-platform filesize
Feb 24, 2021
83c3cb8
Fixing evil_deep_recursion test. The contract was optimized (I guess)
Feb 24, 2021
38e1ff9
Obscure recursion
Feb 24, 2021
5339790
Fix used_gas test to be binary independent
Feb 24, 2021
0467955
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 24, 2021
16ee715
Merge branch 'master' into feature/alt_bn128_fork
Feb 24, 2021
6dd1f39
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 24, 2021
c47ea3f
Merge branch 'feature/alt_bn128_fork' of github.com:near/nearcore int…
Feb 24, 2021
122482c
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 24, 2021
afa279f
Merge branch 'master' into feature/alt_bn128_fork
Feb 26, 2021
8cb3aec
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 26, 2021
690ba81
Fix nightly protocol version and add nightly protocol to param estimator
Feb 26, 2021
f670142
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Feb 26, 2021
3db43a4
Merge branch 'feature/alt_bn128_fork' of github.com:near/nearcore int…
Feb 26, 2021
b61e614
Fix bash estimate.sh
Feb 26, 2021
de054e8
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Mar 1, 2021
56bfa56
Fix nightly_protocol_features
Mar 1, 2021
d02a523
Fix param estimator nightly + update the config
Mar 2, 2021
1bf0b1e
Merge branch 'master' of github.com:near/nearcore into feature/alt_bn…
Mar 2, 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
41 changes: 22 additions & 19 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ steps:
command: |
source ~/.cargo/env
RUSTFLAGS='-D warnings' cargo test --locked --workspace

timeout: 60
agents:
- "distro=amazonlinux"
branches: "!master"

- label: "cargo test nightly"
command: |
source ~/.cargo/env
RUSTFLAGS='-D warnings' cargo test --workspace --features nightly_protocol,nightly_protocol_features,protocol_feature_evm

timeout: 60
agents:
- "distro=amazonlinux"
branches: "!master"

- label: "sanity checks"
command: |
source ~/.cargo/env
Expand Down Expand Up @@ -73,7 +73,7 @@ steps:
timeout: 30
agents:
- "distro=amazonlinux"

- label: "db migration"
command: |
source ~/.cargo/env
Expand All @@ -84,57 +84,60 @@ steps:
timeout: 30
agents:
- "distro=amazonlinux"

- label: "runtime params estimate"
command: |
source ~/.cargo/env
cd runtime/runtime-params-estimator
cd runtime/runtime-params-estimator/test-contract
rustup target add wasm32-unknown-unknown
./build.sh
cd ..
RUSTFLAGS='-D warnings' cargo run --release --package neard --bin neard -- --home /tmp/data init --chain-id= --test-seed=alice.near --account-id=test.near --fast
RUSTFLAGS='-D warnings' cargo run --release --package genesis-populate --bin genesis-populate -- --additional-accounts-num=200000 --home /tmp/data
RUSTFLAGS='-D warnings' cargo run --release --package runtime-params-estimator --bin runtime-params-estimator -- --home /tmp/data --accounts-num 20000 --iters 1 --warmup-iters 1 --metric time

branches: "!master !beta !stable"
timeout: 60
agents:
- "distro=amazonlinux"

#- label: "near-sdk-rs tests"
# trigger: "near-sdk-rs"
# branches: "!master"

# Disable rainbow bridge tests as they are temporary broken
# - label: "rainbow-bridge test"
# command: |
# source ~/.cargo/env
# source ~/.nvm/nvm.sh

# git clone https://github.com/near/rainbow-bridge
# cd rainbow-bridge
# source ci/e2e_ci_prepare_env.sh
# source ci/e2e_ci_prepare_log.sh

# LOCAL_CORE_SRC=.. ci/e2e.sh

# timeout: 60
# agents:
# - "queue=default"
# branches: "!master"
# artifact_paths:
# - "rainbow-bridge/logs/**/*.log"

- label: "cargo release check"
command: |
source ~/.cargo/env
rm Cargo.lock
RUSTFLAGS='-D warnings' cargo build

timeout: 30
agents:
- "distro=amazonlinux"


- wait

#- label: "coverage"
# command: |
# source ~/.cargo/env
Expand All @@ -151,11 +154,11 @@ steps:
# soft_fail:
# - exit_status: 1
# branches: "master"

- label: "Trigger Nearcore Release"
trigger: nearcore-release
branches: "master"

- label: "Trigger Nearcore Nighlty Release"
trigger: nearcore-nightly-release
branches: "master"
Expand Down
17 changes: 17 additions & 0 deletions Cargo.lock

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

21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@ no_cache = ["neard/no_cache"]
metric_recorder = ["neard/metric_recorder"]
delay_detector = ["neard/delay_detector"]
rosetta_rpc = ["neard/rosetta_rpc"]
protocol_feature_forward_chunk_parts = ["neard/protocol_feature_forward_chunk_parts"]
nightly_protocol = ["near-primitives/nightly_protocol", "near-jsonrpc/nightly_protocol"]
nightly_protocol_features = ["nightly_protocol", "neard/nightly_protocol_features", "protocol_feature_evm", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128"]
protocol_feature_forward_chunk_parts = ["neard/protocol_feature_forward_chunk_parts"]
protocol_feature_evm = ["neard/protocol_feature_evm", "testlib/protocol_feature_evm", "runtime-params-estimator/protocol_feature_evm"]
protocol_feature_alt_bn128 = ["neard/protocol_feature_alt_bn128", "testlib/protocol_feature_alt_bn128", "runtime-params-estimator/protocol_feature_alt_bn128"]
protocol_feature_block_header_v3 = ["near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "neard/protocol_feature_block_header_v3"]
costs_counting = [
"near-primitives/costs_counting",
"neard/costs_counting",
"node-runtime/costs_counting",
"testlib/costs_counting"
]
# enable this to build neard with wasmer 1.0 runner
# now if none of wasmer0_default, wasmer1_default or wasmtime_default is enabled, wasmer0 would be default
wasmer1_default = ["node-runtime/wasmer1_default"]
Expand All @@ -118,12 +128,3 @@ wasmtime_default = ["node-runtime/wasmtime_default"]
wasmer1_vm = ["node-runtime/wasmer1_vm"]
wasmer0_vm = ["node-runtime/wasmer0_vm"]
wasmtime_vm = ["node-runtime/wasmtime_vm"]
nightly_protocol_features = ["nightly_protocol", "neard/nightly_protocol_features", "protocol_feature_evm", "protocol_feature_block_header_v3"]
protocol_feature_evm = ["neard/protocol_feature_evm", "testlib/protocol_feature_evm", "runtime-params-estimator/protocol_feature_evm"]
protocol_feature_block_header_v3 = ["near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "neard/protocol_feature_block_header_v3"]
costs_counting = [
"near-primitives/costs_counting",
"neard/costs_counting",
"node-runtime/costs_counting",
"testlib/costs_counting"
]
1 change: 1 addition & 0 deletions core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ lazy_static = "1.4"
default = []
costs_counting = []
protocol_feature_evm = []
protocol_feature_alt_bn128 = []
96 changes: 96 additions & 0 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl Default for VMLimitConfig {
}

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
#[serde(default)]
pub struct ExtCostsConfig {
/// Base cost for calling a host function.
pub base: Gas,
Expand Down Expand Up @@ -290,6 +291,31 @@ pub struct ExtCostsConfig {
pub validator_stake_base: Gas,
/// Cost of calling `validator_total_stake`.
pub validator_total_stake_base: Gas,

// #############
// # Alt BN128 #
// #############
/// Base cost for multiexp
#[cfg(feature = "protocol_feature_alt_bn128")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be addressed in a refactor PR, but @matklad might know a way we can wrap all of these fields additions with just one flag instead of each.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just have an inner struct under feature?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, given that all these start with alt_bn128_g1_, it really wants to be

pub alt_bn128_g1: AltBn128g1

regardless of a feature flags.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might break cost counter right now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed that we'll refactor it later

pub alt_bn128_g1_multiexp_base: Gas,
/// byte cost for multiexp
#[cfg(feature = "protocol_feature_alt_bn128")]
pub alt_bn128_g1_multiexp_byte: Gas,
/// Base cost for sum
#[cfg(feature = "protocol_feature_alt_bn128")]
pub alt_bn128_g1_sum_base: Gas,
/// byte cost for sum
#[cfg(feature = "protocol_feature_alt_bn128")]
pub alt_bn128_g1_sum_byte: Gas,
/// sublinear cost for items
#[cfg(feature = "protocol_feature_alt_bn128")]
pub alt_bn128_g1_multiexp_sublinear: Gas,
/// Base cost for pairing check
#[cfg(feature = "protocol_feature_alt_bn128")]
pub alt_bn128_pairing_check_base: Gas,
/// Cost for pairing check per byte
#[cfg(feature = "protocol_feature_alt_bn128")]
pub alt_bn128_pairing_check_byte: Gas,
}

// We multiply the actual computed costs by the fixed factor to ensure we
Expand Down Expand Up @@ -348,6 +374,20 @@ impl Default for ExtCostsConfig {
promise_return: SAFETY_MULTIPLIER * 186717462,
validator_stake_base: SAFETY_MULTIPLIER * 303944908800,
validator_total_stake_base: SAFETY_MULTIPLIER * 303944908800,
#[cfg(feature = "protocol_feature_alt_bn128")]
evgenykuzyakov marked this conversation as resolved.
Show resolved Hide resolved
alt_bn128_g1_multiexp_base: SAFETY_MULTIPLIER * 5345163514,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_byte: SAFETY_MULTIPLIER * 21665818,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_sublinear: SAFETY_MULTIPLIER * 1422687,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_base: SAFETY_MULTIPLIER * 2281902410880,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_byte: SAFETY_MULTIPLIER * 10267480326,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_base: SAFETY_MULTIPLIER * 5345163514,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_byte: SAFETY_MULTIPLIER * 21665818,
}
}
}
Expand Down Expand Up @@ -404,6 +444,20 @@ impl ExtCostsConfig {
promise_return: 0,
validator_stake_base: 0,
validator_total_stake_base: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
evgenykuzyakov marked this conversation as resolved.
Show resolved Hide resolved
alt_bn128_g1_multiexp_base: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_byte: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_sublinear: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_base: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_byte: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_base: 0,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_byte: 0,
}
}
}
Expand Down Expand Up @@ -461,6 +515,20 @@ pub enum ExtCosts {
promise_return,
validator_stake_base,
validator_total_stake_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_byte,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_sublinear,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_byte,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_byte,
}

// Type of an action, used in fees logic.
Expand Down Expand Up @@ -565,6 +633,20 @@ impl ExtCosts {
promise_return => config.promise_return,
validator_stake_base => config.validator_stake_base,
validator_total_stake_base => config.validator_total_stake_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_base => config.alt_bn128_g1_multiexp_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_byte => config.alt_bn128_g1_multiexp_byte,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_multiexp_sublinear => config.alt_bn128_g1_multiexp_sublinear,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_base => config.alt_bn128_pairing_check_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_pairing_check_byte => config.alt_bn128_pairing_check_byte,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_base => config.alt_bn128_g1_sum_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_byte => config.alt_bn128_g1_sum_byte,
}
}

Expand Down Expand Up @@ -623,6 +705,20 @@ impl ExtCosts {
"promise_return",
"validator_stake_base",
"validator_total_stake_base",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_g1_multiexp_base",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_g1_multiexp_byte",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_g1_multiexp_sublinear",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_pairing_check_base",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_pairing_check_byte",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_g1_sum_base",
#[cfg(feature = "protocol_feature_alt_bn128")]
"alt_bn128_g1_sum_byte",
][index]
}
}
3 changes: 2 additions & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protocol_feature_forward_chunk_parts = []
protocol_feature_rectify_inflation = []
protocol_feature_evm = ["near-primitives-core/protocol_feature_evm"]
protocol_feature_block_header_v3 = []
nightly_protocol_features = ["nightly_protocol", "protocol_feature_forward_chunk_parts", "protocol_feature_rectify_inflation", "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"]
nightly_protocol_features = ["nightly_protocol", "protocol_feature_forward_chunk_parts", "protocol_feature_rectify_inflation", "protocol_feature_evm", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128"]
nightly_protocol = []
costs_counting = ["near-primitives-core/costs_counting"]

Expand Down
4 changes: 4 additions & 0 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub enum ProtocolFeature {
/// Decreases the storage cost of 1 byte by 10X.
#[cfg(feature = "protocol_feature_lower_storage_cost")]
LowerStorageCost,
#[cfg(feature = "protocol_feature_alt_bn128")]
AltBn128,
}

/// Current latest stable version of the protocol.
Expand Down Expand Up @@ -134,6 +136,8 @@ lazy_static! {
(ProtocolFeature::EVM, 103),
#[cfg(feature = "protocol_feature_block_header_v3")]
(ProtocolFeature::BlockHeaderV3, 104),
#[cfg(feature = "protocol_feature_alt_bn128")]
(ProtocolFeature::AltBn128, 105),
]
.into_iter()
.collect();
Expand Down
3 changes: 2 additions & 1 deletion neard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ rosetta_rpc = ["near-rosetta-rpc"]
protocol_feature_forward_chunk_parts = ["near-client/protocol_feature_forward_chunk_parts"]
protocol_feature_rectify_inflation = ["near-epoch-manager/protocol_feature_rectify_inflation"]
protocol_feature_evm = ["near-primitives/protocol_feature_evm", "node-runtime/protocol_feature_evm", "near-chain-configs/protocol_feature_evm", "near-chain/protocol_feature_evm"]
protocol_feature_alt_bn128 = ["near-primitives/protocol_feature_alt_bn128", "node-runtime/protocol_feature_alt_bn128"]
protocol_feature_block_header_v3 = ["near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "near-client/protocol_feature_block_header_v3"]
nightly_protocol_features = ["nightly_protocol", "near-primitives/nightly_protocol_features", "near-client/nightly_protocol_features", "near-epoch-manager/nightly_protocol_features", "near-store/nightly_protocol_features", "protocol_feature_forward_chunk_parts", "protocol_feature_rectify_inflation", "protocol_feature_evm", "protocol_feature_block_header_v3"]
nightly_protocol_features = ["nightly_protocol", "near-primitives/nightly_protocol_features", "near-client/nightly_protocol_features", "near-epoch-manager/nightly_protocol_features", "near-store/nightly_protocol_features", "protocol_feature_forward_chunk_parts", "protocol_feature_rectify_inflation", "protocol_feature_evm", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128"]
nightly_protocol = ["near-primitives/nightly_protocol", "near-jsonrpc/nightly_protocol"]
costs_counting = ["near-primitives/costs_counting", "node-runtime/costs_counting"]

Expand Down
8 changes: 8 additions & 0 deletions runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Pending

- Introduce `alt_bn128_g1_multiexp`, `alt_bn128_g1_sum` and `alt_bn128_pairing_check` host functions to `near-vm-logic`.

## 3.0.0

- Dependency on `near-core-primitives`

## 2.3.0

- Added disk cache to `near-vm-runner`
Expand Down
Loading