Skip to content

Commit 318c244

Browse files
committed
move test
1 parent bb11a7c commit 318c244

File tree

4 files changed

+62
-58
lines changed

4 files changed

+62
-58
lines changed

Cargo.lock

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

pallets/subtensor/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ tle = { workspace = true, default-features = false }
5050
ark-bls12-381 = { workspace = true, default-features = false }
5151
ark-serialize = { workspace = true, default-features = false }
5252
w3f-bls = { workspace = true, default-features = false }
53+
sha2 = { workspace = true }
54+
rand_chacha = { workspace = true }
5355

5456
[dev-dependencies]
5557
pallet-balances = { workspace = true, features = ["std"] }

pallets/subtensor/tests/weights.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,3 +4188,61 @@ fn test_commit_weights_rate_limit() {
41884188
));
41894189
});
41904190
}
4191+
4192+
use ark_serialize::CanonicalDeserialize;
4193+
use rand_chacha::rand_core::SeedableRng;
4194+
use rand_chacha::ChaCha20Rng;
4195+
use sha2::Digest;
4196+
use tle::ibe::fullident::Identity;
4197+
use tle::tlock::tld;
4198+
use tle::tlock::tle;
4199+
use tle::{curves::drand::TinyBLS381, stream_ciphers::AESGCMStreamCipherProvider};
4200+
use w3f_bls::EngineBLS;
4201+
4202+
#[test]
4203+
pub fn tlock_encrypt_decrypt_drand_quicknet_works() {
4204+
// using a pulse from drand's QuickNet
4205+
// https://api.drand.sh/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/public/1000
4206+
// the beacon public key
4207+
let pk_bytes =
4208+
b"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a"
4209+
; // a round number that we know a signature for
4210+
let round: u64 = 1000;
4211+
// the signature produced in that round
4212+
let signature =
4213+
b"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39"
4214+
;
4215+
4216+
// Convert hex string to bytes
4217+
let pub_key_bytes = hex::decode(pk_bytes).expect("Decoding failed");
4218+
// Deserialize to G1Affine
4219+
let pub_key =
4220+
<TinyBLS381 as EngineBLS>::PublicKeyGroup::deserialize_compressed(&*pub_key_bytes)
4221+
.unwrap();
4222+
4223+
// then we tlock a message for the pubkey
4224+
let plaintext = b"this is a test".as_slice();
4225+
let esk = [2; 32];
4226+
4227+
let sig_bytes = hex::decode(signature).expect("The signature should be well formatted");
4228+
let sig =
4229+
<TinyBLS381 as EngineBLS>::SignatureGroup::deserialize_compressed(&*sig_bytes).unwrap();
4230+
4231+
let message = {
4232+
let mut hasher = sha2::Sha256::new();
4233+
hasher.update(round.to_be_bytes());
4234+
hasher.finalize().to_vec()
4235+
};
4236+
4237+
let identity = Identity::new(b"", vec![message]);
4238+
4239+
let rng = ChaCha20Rng::seed_from_u64(0);
4240+
let ct = tle::<TinyBLS381, AESGCMStreamCipherProvider, ChaCha20Rng>(
4241+
pub_key, esk, plaintext, identity, rng,
4242+
)
4243+
.unwrap();
4244+
4245+
// then we can decrypt the ciphertext using the signature
4246+
let result = tld::<TinyBLS381, AESGCMStreamCipherProvider>(ct, sig).unwrap();
4247+
assert!(result == plaintext);
4248+
}

runtime/src/lib.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,64 +2067,6 @@ impl_runtime_apis! {
20672067

20682068
#[cfg(test)]
20692069
mod tests {
2070-
use ark_serialize::CanonicalDeserialize;
2071-
use rand_chacha::rand_core::SeedableRng;
2072-
use rand_chacha::ChaCha20Rng;
2073-
use sha2::Digest;
2074-
use tle::ibe::fullident::Identity;
2075-
use tle::tlock::tld;
2076-
use tle::tlock::tle;
2077-
use tle::{curves::drand::TinyBLS381, stream_ciphers::AESGCMStreamCipherProvider};
2078-
use w3f_bls::EngineBLS;
2079-
2080-
#[test]
2081-
pub fn tlock_encrypt_decrypt_drand_quicknet_works() {
2082-
// using a pulse from drand's QuickNet
2083-
// https://api.drand.sh/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/public/1000
2084-
// the beacon public key
2085-
let pk_bytes =
2086-
b"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a"
2087-
; // a round number that we know a signature for
2088-
let round: u64 = 1000;
2089-
// the signature produced in that round
2090-
let signature =
2091-
b"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39"
2092-
;
2093-
2094-
// Convert hex string to bytes
2095-
let pub_key_bytes = hex::decode(pk_bytes).expect("Decoding failed");
2096-
// Deserialize to G1Affine
2097-
let pub_key =
2098-
<TinyBLS381 as EngineBLS>::PublicKeyGroup::deserialize_compressed(&*pub_key_bytes)
2099-
.unwrap();
2100-
2101-
// then we tlock a message for the pubkey
2102-
let plaintext = b"this is a test".as_slice();
2103-
let esk = [2; 32];
2104-
2105-
let sig_bytes = hex::decode(signature).expect("The signature should be well formatted");
2106-
let sig =
2107-
<TinyBLS381 as EngineBLS>::SignatureGroup::deserialize_compressed(&*sig_bytes).unwrap();
2108-
2109-
let message = {
2110-
let mut hasher = sha2::Sha256::new();
2111-
hasher.update(round.to_be_bytes());
2112-
hasher.finalize().to_vec()
2113-
};
2114-
2115-
let identity = Identity::new(b"", vec![message]);
2116-
2117-
let rng = ChaCha20Rng::seed_from_u64(0);
2118-
let ct = tle::<TinyBLS381, AESGCMStreamCipherProvider, ChaCha20Rng>(
2119-
pub_key, esk, plaintext, identity, rng,
2120-
)
2121-
.unwrap();
2122-
2123-
// then we can decrypt the ciphertext using the signature
2124-
let result = tld::<TinyBLS381, AESGCMStreamCipherProvider>(ct, sig).unwrap();
2125-
assert!(result == plaintext);
2126-
}
2127-
21282070
#[test]
21292071
fn check_whitelist() {
21302072
use crate::*;

0 commit comments

Comments
 (0)