Skip to content

Commit de3d723

Browse files
committed
Problem: rust-secp256k1 fork diverged from upstream (fixes crypto-com#757)
Solution: - created a branch off upstream rust-secp256k1 https://github.com/crypto-com/rust-secp256k1-zkp/tree/upstream-catchup - use the vendor script to update the secp256k1 library from PR to upstream with changes to Schnorr signatures (the vendor script applies custom patches to have mem allocation in Rust etc.) - updated the code against the upstream changes (one notable change is that signing uses "synthentic nonces" -- https://moderncrypto.org/mail-archive/curves/2017/000925.html -- so needs some fresh randomness) NOTE: MuSig hasn't been ported up to the latest upstream changes yet, so its related functionality is currently disabled (when required, it can later be fixed and enabled via "experimental" feature flag)
1 parent 74c0ab3 commit de3d723

File tree

40 files changed

+301
-156
lines changed

40 files changed

+301
-156
lines changed

Cargo.lock

+31-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain-abci/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ hex = "0.4"
3434
protobuf = "2.7.0"
3535
integer-encoding = "1.1.5"
3636
structopt = "0.3"
37-
secp256k1zkp = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", rev = "f8759809f6e3fed793b37166f7cd91c57cdb2eab", features = ["recovery", "endomorphism"] }
37+
secp256k1 = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", rev = "535790e91fac1b3b00c770cb339a06feadc5f48d", features = ["recovery", "endomorphism"] }
3838
parity-scale-codec = { features = ["derive"], version = "1.3" }
3939
thiserror = "1.0"
4040

@@ -43,6 +43,7 @@ enclave-u-common = { path = "../chain-tx-enclave/enclave-u-common" }
4343
sgx_types = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
4444
sgx_urts = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
4545
zmq = "0.9"
46+
rand = "0.7"
4647

4748
[build-dependencies]
4849
cc = "1.0"
@@ -58,6 +59,7 @@ base64 = "0.12"
5859
kvdb = "0.7"
5960
kvdb-memorydb = "0.7"
6061
test-common = { path = "../test-common" }
62+
rand = "0.7"
6163

6264
# TODO: currently not maintained benchmarks
6365
# [[bench]]

chain-abci/src/enclave_bridge/real/test/seal.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ pub fn test_sealing() {
196196
tx1.add_output(TxOut::new(eaddr.clone(), Coin::one()));
197197
let txid1 = tx1.id();
198198
let witness1 = vec![TxInWitness::TreeSig(
199-
schnorr_sign(&secp, &Message::from_slice(&txid1).unwrap(), &secret_key),
199+
schnorr_sign(
200+
&secp,
201+
&Message::from_slice(&txid1).unwrap(),
202+
&secret_key,
203+
&mut rand::thread_rng(),
204+
),
200205
merkle_tree
201206
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
202207
.unwrap(),
@@ -228,7 +233,12 @@ pub fn test_sealing() {
228233
tx2.add_output(TxOut::new(eaddr.clone(), Coin::zero()));
229234
let txid2 = tx2.id();
230235
let witness2 = vec![TxInWitness::TreeSig(
231-
schnorr_sign(&secp, &Message::from_slice(&txid2).unwrap(), &secret_key),
236+
schnorr_sign(
237+
&secp,
238+
&Message::from_slice(&txid2).unwrap(),
239+
&secret_key,
240+
&mut rand::thread_rng(),
241+
),
232242
merkle_tree
233243
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
234244
.unwrap(),

chain-abci/tests/abci_app.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,12 @@ fn all_valid_tx_types_should_commit() {
908908
tx1.add_output(TxOut::new(eaddr, Coin::from(99999700u32)));
909909
let txid1 = tx1.id();
910910
let witness1 = vec![TxInWitness::TreeSig(
911-
schnorr_sign(&secp, &Message::from_slice(&txid1).unwrap(), &secret_key),
911+
schnorr_sign(
912+
&secp,
913+
&Message::from_slice(&txid1).unwrap(),
914+
&secret_key,
915+
&mut rand::thread_rng(),
916+
),
912917
merkle_tree
913918
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
914919
.unwrap(),
@@ -934,7 +939,12 @@ fn all_valid_tx_types_should_commit() {
934939
let utxo2 = TxoPointer::new(*txid, 1);
935940
let tx2 = DepositBondTx::new(vec![utxo2], addr.into(), StakedStateOpAttributes::new(0));
936941
let witness2 = vec![TxInWitness::TreeSig(
937-
schnorr_sign(&secp, &Message::from_slice(&tx2.id()).unwrap(), &secret_key),
942+
schnorr_sign(
943+
&secp,
944+
&Message::from_slice(&tx2.id()).unwrap(),
945+
&secret_key,
946+
&mut rand::thread_rng(),
947+
),
938948
merkle_tree
939949
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
940950
.unwrap(),
@@ -964,7 +974,12 @@ fn all_valid_tx_types_should_commit() {
964974
let utxo3 = TxoPointer::new(*txid, 2);
965975
let tx3 = DepositBondTx::new(vec![utxo3], addr2.into(), StakedStateOpAttributes::new(0));
966976
let witness3 = vec![TxInWitness::TreeSig(
967-
schnorr_sign(&secp, &Message::from_slice(&tx3.id()).unwrap(), &secret_key),
977+
schnorr_sign(
978+
&secp,
979+
&Message::from_slice(&tx3.id()).unwrap(),
980+
&secret_key,
981+
&mut rand::thread_rng(),
982+
),
968983
merkle_tree
969984
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
970985
.unwrap(),

chain-abci/tests/tx_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn get_tx_witness<C: Signing>(
100100
let proof = merkle_tree
101101
.generate_proof(RawXOnlyPubkey::from(public_key.serialize()))
102102
.unwrap();
103-
let signature = schnorr_sign(&secp, &message, secret_key);
103+
let signature = schnorr_sign(&secp, &message, secret_key, &mut rand::thread_rng());
104104

105105
TxInWitness::TreeSig(signature, proof)
106106
}

chain-core/Cargo.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ readme = "../README.md"
77
edition = "2018"
88

99
[features]
10-
default = ["serde", "bech32", "hex", "base64", "secp256k1zkp/serde", "secp256k1zkp/std", "mls", "ra-client"]
11-
edp = ["secp256k1zkp/edp"]
12-
mesalock_sgx = ["sgx_tstd", "secp256k1zkp/sgx"]
10+
default = ["serde", "bech32", "hex", "base64", "secp256k1/serde", "secp256k1/std", "mls", "ra-client"]
11+
edp = ["secp256k1/lowmemory"]
12+
mesalock_sgx = ["secp256k1/lowmemory", "sgx_tstd"]
1313

1414
[dependencies]
1515
mls = { path = "../chain-tx-enclave-next/mls", optional = true }
@@ -18,7 +18,7 @@ digest = { version = "0.8", default-features = false}
1818
tiny-keccak = { version = "2.0", features = ["keccak"] }
1919
sha2 = { version = "0.8", default-features = false }
2020
hex = { version = "0.4", optional = true }
21-
secp256k1zkp = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "f8759809f6e3fed793b37166f7cd91c57cdb2eab", features = ["recovery", "endomorphism"] }
21+
secp256k1 = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "535790e91fac1b3b00c770cb339a06feadc5f48d", features = ["recovery", "endomorphism", "schnorrsig"] }
2222
serde = { version = "1.0", features = ["derive"], optional = true }
2323
blake3 = { version = "0.3.5", default-features = false }
2424
parity-scale-codec = { features = ["derive"], default-features = false, version = "1.3" }
@@ -34,3 +34,4 @@ quickcheck = "0.9"
3434
serde_json = "1.0"
3535
fixed = "1.0.0"
3636
test-common = { path = "../test-common" }
37+
rand = "0.7"

chain-core/src/tx/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ pub mod tests {
654654
let merkle = MerkleTree::new(raw_public_keys.clone());
655655

656656
let w1 = TxInWitness::TreeSig(
657-
schnorr_sign(&secp, &msg, &sk1),
657+
schnorr_sign(&secp, &msg, &sk1, &mut rand::thread_rng()),
658658
merkle.generate_proof(raw_public_keys[0].clone()).unwrap(),
659659
);
660660
let txa = PlainTxAux::TransferTx(tx, vec![w1].into());

chain-tx-enclave-next/tx-query-next/enclave-app/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ parity-scale-codec = "1.3"
1313
rand = "0.7"
1414
rs-libc = "0.2"
1515
rustls = "0.18"
16-
secp256k1zkp = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "f8759809f6e3fed793b37166f7cd91c57cdb2eab", features = ["edp"] }
16+
secp256k1 = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "535790e91fac1b3b00c770cb339a06feadc5f48d", features = ["lowmemory"] }
1717
thread-pool = "0.1"
1818
zeroize = "1.1"
1919

chain-tx-enclave-next/tx-query-next/enclave-app/src/sgx_module/handler/decryption_request.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ pub fn get_random_challenge() -> H256 {
2525
}
2626

2727
pub fn verify_decryption_request(decryption_request: &DecryptionRequest, challenge: H256) -> bool {
28-
decryption_request
29-
.verify(&Secp256k1::verification_only(), challenge)
30-
.is_ok()
28+
// FIXME: provide secp as ref
29+
let mut buf_vfy = vec![0u8; Secp256k1::preallocate_verification_size()];
30+
if let Some(secp) = Secp256k1::preallocated_verification_only(&mut buf_vfy) {
31+
decryption_request.verify(&secp, challenge).is_ok()
32+
} else {
33+
eprintln!("allocation failed");
34+
false
35+
}
3136
}
3237

3338
pub fn handle_decryption_request(

chain-tx-enclave-next/tx-validation-next/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2018"
1010
enclave-macro = { path = "../../chain-tx-enclave/enclave-macro" }
1111
chain-tx-validation = { path = "../../chain-tx-validation" }
1212
chain-core = { path = "../../chain-core" }
13-
secp256k1zkp = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", rev = "f8759809f6e3fed793b37166f7cd91c57cdb2eab", features = ["recovery", "endomorphism", "edp"] }
13+
secp256k1 = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", rev = "535790e91fac1b3b00c770cb339a06feadc5f48d", features = ["recovery", "endomorphism", "lowmemory", "schnorrsig"] }
1414
parity-scale-codec = { version = "1.3" }
1515
enclave-protocol = { path = "../../enclave-protocol" }
1616
chain-tx-filter = { path = "../../chain-tx-filter" }

chain-tx-enclave-next/tx-validation-next/src/sgx_module.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,12 @@ mod tests {
346346
tx1.add_output(TxOut::new(eaddr.clone(), Coin::one()));
347347
let txid1 = tx1.id();
348348
let witness1: TxWitness = vec![TxInWitness::TreeSig(
349-
schnorr_sign(&secp, &Message::from_slice(&txid1).unwrap(), &secret_key),
349+
schnorr_sign(
350+
&secp,
351+
&Message::from_slice(&txid1).unwrap(),
352+
&secret_key,
353+
&mut rand::thread_rng(),
354+
),
350355
merkle_tree
351356
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
352357
.unwrap(),
@@ -386,7 +391,12 @@ mod tests {
386391
tx2.add_output(TxOut::new(eaddr.clone(), Coin::zero()));
387392
let txid2 = tx2.id();
388393
let witness2: TxWitness = vec![TxInWitness::TreeSig(
389-
schnorr_sign(&secp, &Message::from_slice(&txid2).unwrap(), &secret_key),
394+
schnorr_sign(
395+
&secp,
396+
&Message::from_slice(&txid2).unwrap(),
397+
&secret_key,
398+
&mut rand::thread_rng(),
399+
),
390400
merkle_tree
391401
.generate_proof(RawXOnlyPubkey::from(x_public_key.serialize()))
392402
.unwrap(),

chain-tx-enclave/enclave-t-common/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2018"
99
[dependencies]
1010
sgx_tstd = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
1111
chain-core = { path = "../../chain-core", default-features = false, features = ["mesalock_sgx"] }
12-
secp256k1zkp = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "f8759809f6e3fed793b37166f7cd91c57cdb2eab", features = ["recovery", "endomorphism", "sgx"] }
12+
secp256k1 = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "535790e91fac1b3b00c770cb339a06feadc5f48d", features = ["recovery", "endomorphism", "lowmemory", "schnorrsig"] }
1313
zeroize = { version = "1.0", default-features = false }
1414
sgx_tseal = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
1515
parity-scale-codec = { default-features = false, version = "1.0" }

chain-tx-enclave/tx-validation/enclave/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sgx_tcrypto = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-
2424
enclave-macro = { path = "../../enclave-macro" }
2525
chain-tx-validation = { path = "../../../chain-tx-validation", default-features = false, features = ["mesalock_sgx"] }
2626
chain-core = { path = "../../../chain-core", default-features = false, features = ["mesalock_sgx"] }
27-
secp256k1zkp = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "f8759809f6e3fed793b37166f7cd91c57cdb2eab", features = ["recovery", "endomorphism", "sgx"] }
27+
secp256k1 = { git = "https://github.com/crypto-com/rust-secp256k1-zkp.git", default-features = false, rev = "535790e91fac1b3b00c770cb339a06feadc5f48d", features = ["recovery", "endomorphism", "lowmemory", "schnorrsig"] }
2828
parity-scale-codec = { default-features = false, version = "1.3" }
2929
enclave-protocol = { path = "../../../enclave-protocol", default-features = false, features = ["mesalock_sgx"] }
3030
chain-tx-filter = { path = "../../../chain-tx-filter", default-features = false, features = ["mesalock_sgx"] }

0 commit comments

Comments
 (0)