Skip to content

Commit c213d2c

Browse files
committed
etf crypto
1 parent 656f0a6 commit c213d2c

File tree

4 files changed

+150
-55
lines changed

4 files changed

+150
-55
lines changed

Cargo.lock

Lines changed: 52 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ pallet-drand = { path = "pallets/drand", default-features = false }
196196
sp-crypto-ec-utils = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", features = ["bls12-381"] }
197197
getrandom = { version = "0.2.15", features = ["custom"], default-features = false }
198198
sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
199-
w3f-bls = { git = "https://github.com/w3f/bls", default-features = false }
200-
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = [ "r1cs", "snark" ] }
199+
# w3f-bls = { git = "https://github.com/w3f/bls", default-features = false, version = "0.1.8" }
200+
w3f-bls = { version = "0.1.3", default-features = false }
201+
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = [ "r1cs", "snark" ] }
201202
ark-scale = { version = "0.0.11", default-features = false, features = ["hazmat"] }
202203
sp-ark-bls12-381 = { git = "https://github.com/paritytech/substrate-curves", default-features = false }
203204
ark-bls12-381 = { version = "0.4.0", features = ["curve"], default-features = false }
@@ -207,6 +208,8 @@ ark-ec = { version = "0.4.0", default-features = false }
207208
ark-std = { version = "0.4.0", default-features = false }
208209
anyhow = "1.0.81"
209210
sha2 = { version = "0.10.8", default-features = false }
211+
etf-crypto-primitives = { git = "https://github.com/ideal-lab5/etf-sdk", rev = "0796744c10e4bce8c6202fd6c025be409f6a4ffe", default-features = false }
212+
rand_chacha = { version = "0.3.1", default-features = false }
210213

211214
frame-metadata = "16"
212215

runtime/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ fp-account = { workspace = true }
114114
#drand
115115
pallet-drand = { workspace = true, default-features = false }
116116
getrandom = { workspace = true, default-features = false }
117-
117+
etf-crypto-primitives = { workspace = true }
118+
hex = { workspace = true }
119+
rand_chacha = { workspace = true }
120+
w3f-bls = { workspace = true }
121+
sha2 = { workspace = true }
122+
ark-serialize = { workspace = true }
118123

119124
[dev-dependencies]
120125
frame-metadata = { workspace = true }
@@ -196,7 +201,8 @@ std = [
196201
"pallet-hotfix-sufficients/std",
197202
"fp-account/std",
198203
"pallet-drand/std",
199-
"getrandom/std"
204+
"getrandom/std",
205+
"etf-crypto-primitives/std"
200206
]
201207
runtime-benchmarks = [
202208
"frame-benchmarking/runtime-benchmarks",

runtime/src/lib.rs

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,29 +2053,88 @@ impl_runtime_apis! {
20532053
}
20542054
}
20552055

2056-
// #[cfg(test)]
2057-
// mod tests {
2058-
2059-
#[test]
2060-
fn check_whitelist() {
2061-
use crate::*;
2062-
use frame_support::traits::WhitelistedStorageKeys;
2063-
use sp_core::hexdisplay::HexDisplay;
2064-
use std::collections::HashSet;
2065-
let whitelist: HashSet<String> = AllPalletsWithSystem::whitelisted_storage_keys()
2066-
.iter()
2067-
.map(|e| HexDisplay::from(&e.key).to_string())
2068-
.collect();
2069-
2070-
// Block Number
2071-
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac"));
2072-
// Total Issuance
2073-
assert!(whitelist.contains("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80"));
2074-
// Execution Phase
2075-
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a"));
2076-
// Event Count
2077-
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850"));
2078-
// System Events
2079-
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"));
2080-
}
2081-
// }
2056+
#[cfg(test)]
2057+
mod tests {
2058+
use ark_serialize::CanonicalDeserialize;
2059+
use etf_crypto_primitives::{ibe::fullident::Identity, tlock::tlock::tle};
2060+
use rand_chacha::rand_core::SeedableRng;
2061+
use rand_chacha::ChaCha20Rng;
2062+
use sha2::Digest;
2063+
use w3f_bls::{EngineBLS, Message, TinyBLS381};
2064+
2065+
#[test]
2066+
fn tlock_encrypt_decrypt_drand_works() {
2067+
// using a pulse from drand's QuickNet
2068+
// https://api.drand.sh/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/public/1000
2069+
// the beacon public key
2070+
let pk_bytes = b"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a";
2071+
// a round number that we know a signature for
2072+
let round: u64 = 1000;
2073+
// the signature produced in that round
2074+
let signature = b"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39";
2075+
// Convert hex string to bytes
2076+
let pub_key_bytes = hex::decode(pk_bytes).expect("Decoding failed");
2077+
// Deserialize to G1Affine
2078+
let pub_key =
2079+
<TinyBLS381 as EngineBLS>::PublicKeyGroup::deserialize_compressed(&*pub_key_bytes)
2080+
.unwrap();
2081+
// then we tlock a message for the pubkey
2082+
let plaintext = b"this is a test".as_slice();
2083+
let esk = [2; 32];
2084+
let id: &Vec<u8> = {
2085+
let mut hasher = sha2::Sha256::new();
2086+
hasher.update(&round.to_be_bytes());
2087+
&hasher.finalize().to_vec()
2088+
};
2089+
let sig_bytes = hex::decode(signature).expect("The signature should be well formatted");
2090+
let sig =
2091+
<TinyBLS381 as EngineBLS>::SignatureGroup::deserialize_compressed(&*sig_bytes).unwrap();
2092+
let message = {
2093+
let mut hasher = sha2::Sha256::new();
2094+
hasher.update(round.to_be_bytes());
2095+
hasher.finalize().to_vec()
2096+
};
2097+
let pk = w3f_bls::PublicKey::<TinyBLS381>(pub_key);
2098+
// Create message object and verify
2099+
let msg = Message::new(b"", &message);
2100+
let identity = Identity::new(b"", vec![msg.1.to_vec()]);
2101+
let rng = ChaCha20Rng::seed_from_u64(0);
2102+
let ct = tle::<TinyBLS381, ChaCha20Rng>(pub_key, esk, plaintext, identity, rng).unwrap();
2103+
// then we can decrypt the ciphertext using the signature
2104+
let result = ct.tld(sig).unwrap();
2105+
assert!(result.message == plaintext);
2106+
}
2107+
2108+
#[test]
2109+
fn check_whitelist() {
2110+
use crate::*;
2111+
use frame_support::traits::WhitelistedStorageKeys;
2112+
use sp_core::hexdisplay::HexDisplay;
2113+
use std::collections::HashSet;
2114+
let whitelist: HashSet<String> = AllPalletsWithSystem::whitelisted_storage_keys()
2115+
.iter()
2116+
.map(|e| HexDisplay::from(&e.key).to_string())
2117+
.collect();
2118+
2119+
// Block Number
2120+
assert!(
2121+
whitelist.contains("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac")
2122+
);
2123+
// Total Issuance
2124+
assert!(
2125+
whitelist.contains("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80")
2126+
);
2127+
// Execution Phase
2128+
assert!(
2129+
whitelist.contains("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a")
2130+
);
2131+
// Event Count
2132+
assert!(
2133+
whitelist.contains("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850")
2134+
);
2135+
// System Events
2136+
assert!(
2137+
whitelist.contains("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7")
2138+
);
2139+
}
2140+
}

0 commit comments

Comments
 (0)