Skip to content

Commit

Permalink
Merge branch 'paritytech:master' into optimizations/upgraded-version-…
Browse files Browse the repository at this point in the history
…of-common-part
  • Loading branch information
xgreenx authored Dec 1, 2021
2 parents c8e18c7 + 44c51d1 commit 4551a13
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 133 deletions.
166 changes: 86 additions & 80 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ default:
- api_failure

stages:
- lint
- check
- workspace
- examples
Expand Down Expand Up @@ -107,6 +108,91 @@ workflow:
vault: cicd/gitlab/$CI_PROJECT_PATH/GITHUB_SSH_PRIV_KEY@kv
file: false

#### stage: lint
#
# Note: For all of these lints we `allow_failure` so that the rest of the build can
# continue running despite them not passing. Merging is still disallowed since (most) of
# the lint steps are marked as "Required" in GitHub.
spellcheck:
stage: lint
<<: *docker-env
<<: *test-refs
script:
- cargo spellcheck check -vvvv --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive .
- cargo spellcheck check -vvvv --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive examples/
allow_failure: true

fmt:
stage: lint
<<: *docker-env
<<: *test-refs
script:
- cargo fmt --verbose --all -- --check
# For the UI tests we need to disable the license check
- cargo fmt --verbose --all -- --check --config=license_template_path="" crates/lang/tests/ui/contract/{pass,fail}/*.rs
- cargo fmt --verbose --all -- --check --config=license_template_path="" crates/lang/tests/ui/trait_def/{pass,fail}/*.rs
allow_failure: true

examples-fmt:
stage: lint
<<: *docker-env
<<: *test-refs
script:
# Note that we disable the license header check for the examples, since they are unlicensed.
- for example in examples/*/; do
cargo fmt --verbose --manifest-path ${example}/Cargo.toml -- --check --config=license_template_path="";
done
- for contract in ${DELEGATOR_SUBCONTRACTS}; do
cargo fmt --verbose --manifest-path examples/delegator/${contract}/Cargo.toml -- --check --config=license_template_path="";
done
allow_failure: true

clippy-std:
stage: lint
<<: *docker-env
<<: *test-refs
script:
- for crate in ${ALL_CRATES}; do
cargo clippy --verbose --all-targets --all-features --manifest-path crates/${crate}/Cargo.toml -- -D warnings;
done

clippy-wasm:
stage: lint
<<: *docker-env
<<: *test-refs
script:
- for crate in ${ALSO_WASM_CRATES}; do
cargo clippy --verbose --no-default-features --manifest-path crates/${crate}/Cargo.toml --target wasm32-unknown-unknown -- -D warnings;
done
allow_failure: true

examples-clippy-std:
stage: lint
<<: *docker-env
<<: *test-refs
script:
- for example in examples/*/; do
cargo clippy --verbose --all-targets --manifest-path ${example}/Cargo.toml -- -D warnings;
done
- for contract in ${DELEGATOR_SUBCONTRACTS}; do
cargo clippy --verbose --all-targets --manifest-path examples/delegator/${contract}/Cargo.toml -- -D warnings;
done
allow_failure: true

examples-clippy-wasm:
stage: lint
<<: *docker-env
<<: *test-refs
script:
- for example in examples/*/; do
cargo clippy --verbose --manifest-path ${example}/Cargo.toml --no-default-features --target wasm32-unknown-unknown -- -D warnings;
done
- for contract in ${DELEGATOR_SUBCONTRACTS}; do
cargo clippy --verbose --manifest-path examples/delegator/${contract}/Cargo.toml --no-default-features --target wasm32-unknown-unknown -- -D warnings;
done
allow_failure: true


#### stage: check

check-std:
Expand Down Expand Up @@ -199,13 +285,6 @@ docs:
# FIXME: remove me after CI image gets nonroot
- chown -R nonroot:nonroot ./crate-docs

spellcheck:
stage: workspace
<<: *docker-env
<<: *test-refs
script:
- cargo spellcheck check -vvvv --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive .
- cargo spellcheck check -vvvv --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive examples/

codecov:
stage: workspace
Expand Down Expand Up @@ -256,40 +335,6 @@ codecov:
- rust-covfix lcov-lines.info --output lcov-lines-fixed.info
- codecov --token "$CODECOV_TOKEN" --file lcov-lines-fixed.info --nonZero

clippy-std:
stage: workspace
<<: *docker-env
<<: *test-refs
needs:
- job: check-std
artifacts: false
script:
- for crate in ${ALL_CRATES}; do
cargo clippy --verbose --all-targets --all-features --manifest-path crates/${crate}/Cargo.toml -- -D warnings;
done

clippy-wasm:
stage: workspace
<<: *docker-env
<<: *test-refs
needs:
- job: check-wasm
artifacts: false
script:
- for crate in ${ALSO_WASM_CRATES}; do
cargo clippy --verbose --no-default-features --manifest-path crates/${crate}/Cargo.toml --target wasm32-unknown-unknown -- -D warnings;
done

fmt:
stage: workspace
<<: *docker-env
<<: *test-refs
script:
- cargo fmt --verbose --all -- --check
# For the UI tests we need to disable the license check
- cargo fmt --verbose --all -- --check --config=license_template_path="" crates/lang/tests/ui/contract/{pass,fail}/*.rs
- cargo fmt --verbose --all -- --check --config=license_template_path="" crates/lang/tests/ui/trait_def/{pass,fail}/*.rs


#### stage: examples

Expand Down Expand Up @@ -323,45 +368,6 @@ examples-test-experimental-engine:
- cargo test --no-default-features --features std, ink-experimental-engine --verbose --manifest-path examples/contract-terminate/Cargo.toml
- cargo test --no-default-features --features std, ink-experimental-engine --verbose --manifest-path examples/contract-transfer/Cargo.toml

examples-fmt:
stage: examples
<<: *docker-env
<<: *test-refs
script:
# Note that we disable the license header check for the examples, since they are unlicensed.
- for example in examples/*/; do
cargo fmt --verbose --manifest-path ${example}/Cargo.toml -- --check --config=license_template_path="";
done
- for contract in ${DELEGATOR_SUBCONTRACTS}; do
cargo fmt --verbose --manifest-path examples/delegator/${contract}/Cargo.toml -- --check --config=license_template_path="";
done

examples-clippy-std:
stage: examples
<<: *docker-env
<<: *test-refs
needs:
- job: clippy-std
artifacts: false
script:
- for example in examples/*/; do
cargo clippy --verbose --all-targets --manifest-path ${example}/Cargo.toml -- -D warnings;
done
- for contract in ${DELEGATOR_SUBCONTRACTS}; do
cargo clippy --verbose --all-targets --manifest-path examples/delegator/${contract}/Cargo.toml -- -D warnings;
done

examples-clippy-wasm:
stage: examples
<<: *docker-env
<<: *test-refs
script:
- for example in examples/*/; do
cargo clippy --verbose --manifest-path ${example}/Cargo.toml --no-default-features --target wasm32-unknown-unknown -- -D warnings;
done
- for contract in ${DELEGATOR_SUBCONTRACTS}; do
cargo clippy --verbose --manifest-path examples/delegator/${contract}/Cargo.toml --no-default-features --target wasm32-unknown-unknown -- -D warnings;
done

examples-contract-build:
stage: examples
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sha3 = { version = "0.9" }
blake2 = { version = "0.9" }

# ECDSA for the off-chain environment.
libsecp256k1 = { version = "0.7.0" }
secp256k1 = { version = "0.20.3", features = ["recovery"] }

[features]
default = ["std"]
Expand Down
29 changes: 19 additions & 10 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,13 @@ impl Engine {
message_hash: &[u8; 32],
output: &mut [u8; 33],
) -> Result {
use libsecp256k1::{
recover,
use secp256k1::{
recovery::{
RecoverableSignature,
RecoveryId,
},
Message,
RecoveryId,
Signature,
Secp256k1,
};

// In most implementations, the v is just 0 or 1 internally, but 27 was added
Expand All @@ -401,17 +403,24 @@ impl Engine {
} else {
signature[64]
};
let message = Message::parse(message_hash);
let signature = Signature::parse_standard_slice(&signature[0..64])
.unwrap_or_else(|error| panic!("Unable to parse the signature: {}", error));

let recovery_id = RecoveryId::parse(recovery_byte)
let recovery_id = RecoveryId::from_i32(recovery_byte as i32)
.unwrap_or_else(|error| panic!("Unable to parse the recovery id: {}", error));

let pub_key = recover(&message, &signature, &recovery_id);
let message = Message::from_slice(message_hash).unwrap_or_else(|error| {
panic!("Unable to create the message from hash: {}", error)
});
let signature =
RecoverableSignature::from_compact(&signature[0..64], recovery_id)
.unwrap_or_else(|error| {
panic!("Unable to parse the signature: {}", error)
});

let secp = Secp256k1::new();
let pub_key = secp.recover(&message, &signature);
match pub_key {
Ok(pub_key) => {
*output = pub_key.serialize_compressed();
*output = pub_key.serialize();
Ok(())
}
Err(_) => Err(Error::EcdsaRecoverFailed),
Expand Down
23 changes: 14 additions & 9 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use crate::ext::{
Engine,
Error,
};
use libsecp256k1::{
use secp256k1::{
recovery::RecoverableSignature,
Message,
PublicKey,
Secp256k1,
SecretKey,
};

Expand Down Expand Up @@ -236,11 +238,12 @@ fn ecdsa_recovery_test_from_contracts_pallet() {
fn ecdsa_recovery_with_secp256k1_crate() {
// given
let mut engine = Engine::new();
let secp = Secp256k1::new();
let seckey = [
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203,
174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
];
let pubkey = PublicKey::parse_compressed(&[
let pubkey = PublicKey::from_slice(&[
2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245,
41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
])
Expand All @@ -249,22 +252,24 @@ fn ecdsa_recovery_with_secp256k1_crate() {
let mut msg_hash = [0; 32];
crate::hashing::sha2_256(b"Some message", &mut msg_hash);

let msg = Message::parse(&msg_hash);
let seckey = SecretKey::parse(&seckey).expect("secret key creation failed");
let (signature, recovery_id) = libsecp256k1::sign(&msg, &seckey);
let msg = Message::from_slice(&msg_hash).expect("message creation failed");
let seckey = SecretKey::from_slice(&seckey).expect("secret key creation failed");
let recoverable_signature: RecoverableSignature =
secp.sign_recoverable(&msg, &seckey);

let mut signature = signature.serialize().to_vec();
signature.push(recovery_id.serialize());
let recovery_id = recoverable_signature.serialize_compact().0.to_i32() as u8;
let mut signature = recoverable_signature.serialize_compact().1.to_vec();
signature.push(recovery_id);
let signature_with_recovery_id: [u8; 65] = signature
.try_into()
.expect("unable to create signature with recovery id");

// when
let mut output = [0; 33];
engine
.ecdsa_recover(&signature_with_recovery_id, &msg.serialize(), &mut output)
.ecdsa_recover(&signature_with_recovery_id, msg.as_ref(), &mut output)
.expect("ecdsa recovery failed");

// then
assert_eq!(output, pubkey.serialize_compressed());
assert_eq!(output, pubkey.serialize());
}
3 changes: 1 addition & 2 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ arrayref = "0.3"
static_assertions = "1.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
libsecp256k1 = { version = "0.7.0", default-features = false }
rlibc = "1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -41,7 +40,7 @@ sha3 = { version = "0.9", optional = true }
blake2 = { version = "0.9", optional = true }

# ECDSA for the off-chain environment.
libsecp256k1 = { version = "0.7.0" }
secp256k1 = { version = "0.20.3", features = ["recovery"] }

# Only used in the off-chain environment.
#
Expand Down
31 changes: 19 additions & 12 deletions crates/env/src/engine/experimental_off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ impl EnvBackend for EnvInstance {
message_hash: &[u8; 32],
output: &mut [u8; 33],
) -> Result<()> {
use libsecp256k1::{
recover,
use secp256k1::{
recovery::{
RecoverableSignature,
RecoveryId,
},
Message,
RecoveryId,
Signature,
Secp256k1,
};

// In most implementations, the v is just 0 or 1 internally, but 27 was added
Expand All @@ -260,17 +262,22 @@ impl EnvBackend for EnvInstance {
} else {
signature[64]
};
let message = Message::parse(message_hash);
let signature = Signature::parse_standard_slice(&signature[0..64])
.unwrap_or_else(|error| panic!("Unable to parse the signature: {}", error));

let recovery_id = RecoveryId::parse(recovery_byte)
let recovery_id = RecoveryId::from_i32(recovery_byte as i32)
.unwrap_or_else(|error| panic!("Unable to parse the recovery id: {}", error));

let pub_key = recover(&message, &signature, &recovery_id);
let message = Message::from_slice(message_hash).unwrap_or_else(|error| {
panic!("Unable to create the message from hash: {}", error)
});
let signature =
RecoverableSignature::from_compact(&signature[0..64], recovery_id)
.unwrap_or_else(|error| {
panic!("Unable to parse the signature: {}", error)
});

let secp = Secp256k1::new();
let pub_key = secp.recover(&message, &signature);
match pub_key {
Ok(pub_key) => {
*output = pub_key.serialize_compressed();
*output = pub_key.serialize();
Ok(())
}
Err(_) => Err(Error::EcdsaRecoverFailed),
Expand Down
Loading

0 comments on commit 4551a13

Please sign in to comment.