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

wallet-core: Remove u64 cast when generating the seed #2217

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
400 changes: 200 additions & 200 deletions rusk/tests/config/bench.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rusk/tests/config/contract_deployment.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [10_000_000_000]
4 changes: 2 additions & 2 deletions rusk/tests/config/gas-behavior.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [10_000_000_000]

[[phoenix_balance]]
address = "3MoVQ6VfGNu8fJ5GeHPRDVUfxcsDEmGXpWhvKhXY7F2dKCp7QWRw8RqPcbuJGdRqeTtxpuiwETnGAJLnhT4Kq4e8"
address = "aTzucx7GCB74UqahpL4VKB8ZqeMwC6W9ETVx12LU4E5UKzJeg4PVwsja5STiZojy9xAZAP8XqrgwVRwc6jdQk8H"
seed = 57005
notes = [10_000_000_000]

6 changes: 3 additions & 3 deletions rusk/tests/config/multi_transfer.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [10_000_000_000]

[[phoenix_balance]]
address = "3MoVQ6VfGNu8fJ5GeHPRDVUfxcsDEmGXpWhvKhXY7F2dKCp7QWRw8RqPcbuJGdRqeTtxpuiwETnGAJLnhT4Kq4e8"
address = "aTzucx7GCB74UqahpL4VKB8ZqeMwC6W9ETVx12LU4E5UKzJeg4PVwsja5STiZojy9xAZAP8XqrgwVRwc6jdQk8H"
seed = 57005
notes = [10_000_000_000]

[[phoenix_balance]]
address = "4RyaodGmN8MyUDmpRrtRxJJhrVW2HsY2ycRUnRUXR97JCN1GHraQT9Ygb8yYo7oKzyZg2EXXCGkHBwoeNb96BKtQ"
address = "546nHTptE8dy18tZcR6TBVXVTnRQQVJbnBMWtFm2AHzZfdQBKqEodFgTGGnvmd6ii7mpVdVkY4vDDGGCpZ5Wm3e9"
seed = 57005
notes = [10_000_000_000]
2 changes: 1 addition & 1 deletion rusk/tests/config/rusk-state.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[[phoenix_balance]]
address = "29ENJqLtHJRSZdghZxiGuzQTe3F4t1bv35zM7mEMS142e5QdxkknokMALrBEFUnyav9NfeXLNvfjx4sfTtJN9WCB"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
notes = [10_000_000_000]
4 changes: 2 additions & 2 deletions rusk/tests/config/slash.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [
10_000_000_000_000,
Expand All @@ -15,6 +15,6 @@ notes = [
]

[[stake]]
address = "qe1FbZxf6YaCAeFNSvL1G82cBhG4Q4gBf4vKYo527Vws3b23jdbBuzKSFsdUHnZeBgsTnyNJLkApEpRyJw87sdzR9g9iESJrG5ZgpCs9jq88m6d4qMY5txGpaXskRQmkzE3"
address = "zryQ5mBuLvhj3bypKnykqoZH2cmoboWL36q64oCWpxzpBFygCFRqwnAiy3ajPpVCqJqY64LY1RXLUFdSk82zENHEVsHsKS2yNoDXWdCcae3r9qbTwkxxXaAi3gUZakSeqnH"
amount = 20_000_000_000
reward = 3_000_000_000
6 changes: 3 additions & 3 deletions rusk/tests/config/stake.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [
10_000_000_000_000,
Expand All @@ -15,10 +15,10 @@ notes = [
]

[[stake]]
address = "qe1FbZxf6YaCAeFNSvL1G82cBhG4Q4gBf4vKYo527Vws3b23jdbBuzKSFsdUHnZeBgsTnyNJLkApEpRyJw87sdzR9g9iESJrG5ZgpCs9jq88m6d4qMY5txGpaXskRQmkzE3"
address = "zryQ5mBuLvhj3bypKnykqoZH2cmoboWL36q64oCWpxzpBFygCFRqwnAiy3ajPpVCqJqY64LY1RXLUFdSk82zENHEVsHsKS2yNoDXWdCcae3r9qbTwkxxXaAi3gUZakSeqnH"
amount = 1_000_000_000_000

[[stake]]
address = "25omWWRyfcMjYNbQZVyc3rypYLi8UqZuthoJHqbCEriRX3z2EmnBaXWZLFL2NvzvDnkoYoHLGiSYQpmupNJj1sSdWNstqzfFEpiqvSNYw7gqvoEiU9FsEHUMG1ZyG3XgL8Rv"
address = "ugaSrdetkhaDekhvJRR8RP17FHYhEwEUq3CAx6Nem5V6ShENsHYPkLiNtdKsELr3PAZcML2ZAm5MFwxdwS7j42UwXttYhUgywN6KA4CZw9z9hi17Zx55NBf8KzPdnQaiN5g"
amount = 1_000_000_000_000
reward = 1_000_000_000_000
4 changes: 2 additions & 2 deletions rusk/tests/config/transfer.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [
10_000_000_000,
Expand All @@ -15,5 +15,5 @@ notes = [
]

[[moonlight_account]]
address = "qe1FbZxf6YaCAeFNSvL1G82cBhG4Q4gBf4vKYo527Vws3b23jdbBuzKSFsdUHnZeBgsTnyNJLkApEpRyJw87sdzR9g9iESJrG5ZgpCs9jq88m6d4qMY5txGpaXskRQmkzE3"
address = "zryQ5mBuLvhj3bypKnykqoZH2cmoboWL36q64oCWpxzpBFygCFRqwnAiy3ajPpVCqJqY64LY1RXLUFdSk82zENHEVsHsKS2yNoDXWdCcae3r9qbTwkxxXaAi3gUZakSeqnH"
balance = 10_000_000_000
6 changes: 3 additions & 3 deletions rusk/tests/config/unspendable.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[[phoenix_balance]]
address = "ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN"
address = "5gZ7msthCUNu2YhpQQz51Fj3L4ppYW58yG7mzZGkdzAp8iLHxkMvQAQhweJ6jhTrBZwhpTkC9ubbi135ynakNj3z"
seed = 57005
notes = [10_000_000_000]

[[phoenix_balance]]
address = "3MoVQ6VfGNu8fJ5GeHPRDVUfxcsDEmGXpWhvKhXY7F2dKCp7QWRw8RqPcbuJGdRqeTtxpuiwETnGAJLnhT4Kq4e8"
address = "aTzucx7GCB74UqahpL4VKB8ZqeMwC6W9ETVx12LU4E5UKzJeg4PVwsja5STiZojy9xAZAP8XqrgwVRwc6jdQk8H"
seed = 57005
notes = [10_000_000_000]

[[phoenix_balance]]
address = "4RyaodGmN8MyUDmpRrtRxJJhrVW2HsY2ycRUnRUXR97JCN1GHraQT9Ygb8yYo7oKzyZg2EXXCGkHBwoeNb96BKtQ"
address = "546nHTptE8dy18tZcR6TBVXVTnRQQVJbnBMWtFm2AHzZfdQBKqEodFgTGGnvmd6ii7mpVdVkY4vDDGGCpZ5Wm3e9"
seed = 57005
notes = [10_000_000_000]
75 changes: 75 additions & 0 deletions rusk/tests/rusk-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,78 @@ async fn generate_moonlight_txs() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

// This code is used to generate the addresses for the config-toml files.
// To generate:
// - uncomment the `#[test]' line
// - adjust the variables accordingly ("bench.toml" has each 100 phoenix and
// moonlight accounts)
// - run the test 'generate_bench_toml'
// - move the resulting "bench.toml" file under "tests/config/bench.toml"
// The first phoenix and moonlight addresses in "bench.toml" are the same
// across all the toml config files (moonlight addresses are equivalent to stake
// addresses).
#[allow(dead_code)]
// #[test]
fn generate_bench_toml() -> Result<(), Box<dyn std::error::Error>> {
use common::wallet::{TestStateClient, TestStore};
use dusk_bytes::Serializable;
use rusk_recovery_tools::state::Snapshot;
use std::io::Write;

const MAX_INDEX: u8 = 100;
const PHOENIX: bool = true;
const MOONLIGHT: bool = true;
let file_name = "bench.toml";

// create a dummy wallet for keys generation
let tmp = tempdir()?;
let rusk = new_state(&tmp, &Snapshot::default(), 0)?;
let cache =
Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));
let wallet =
test_wallet::Wallet::new(TestStore, TestStateClient { rusk, cache });

// write the toml file
let mut toml_file = std::fs::File::create(file_name)?;
toml_file.write(b"[acl.stake]\nowners = []\nallowlist = []\n\n")?;

if PHOENIX {
for index in 0..MAX_INDEX {
let phoenix_pk = wallet
.phoenix_public_key(index)
.expect("key-generation should work");
toml_file.write(b"[[phoenix_balance]]\n")?;
toml_file.write(b"address = \"")?;
toml_file.write(
bs58::encode(phoenix_pk.to_bytes()).into_string().as_bytes(),
)?;
toml_file.write(b"\"\n")?;
toml_file.write(b"seed = 57005\n")?;
toml_file.write(b"notes = [10_000_000_000]\n")?;
if index < MAX_INDEX - 1 || MOONLIGHT {
toml_file.write(b"\n")?;
}
}
}

if MOONLIGHT {
for index in 0..MAX_INDEX {
let account_pk = wallet
.account_public_key(index)
.expect("key-generation should work");
toml_file.write(b"[[moonlight_account]]\n")?;
toml_file.write(b"address = \"")?;
toml_file.write(
bs58::encode(account_pk.to_bytes()).into_string().as_bytes(),
)?;
toml_file.write(b"\"\n")?;
toml_file.write(b"balance = 10_000_000_000\n")?;
if index < MAX_INDEX - 1 {
toml_file.write(b"\n")?;
}
}
}

Ok(())
}
10 changes: 2 additions & 8 deletions wallet-core/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ use crate::RNG_SEED;
pub fn derive_bls_sk(seed: &[u8; RNG_SEED], index: u8) -> BlsSecretKey {
// note that if we change the string used for the rng, all previously
// generated keys will become invalid
// NOTE: When breaking the keys, we will want to change the string too
BlsSecretKey::random(&mut rng_with_index(seed, index, b"SK"))
BlsSecretKey::random(&mut rng_with_index(seed, index, b"BSK"))
}

/// Generates a [`PhoenixSecretKey`] from a seed and index.
Expand All @@ -41,8 +40,7 @@ pub fn derive_bls_sk(seed: &[u8; RNG_SEED], index: u8) -> BlsSecretKey {
pub fn derive_phoenix_sk(seed: &[u8; RNG_SEED], index: u8) -> PhoenixSecretKey {
// note that if we change the string used for the rng, all previously
// generated keys will become invalid
// NOTE: When breaking the keys, we will want to change the string too
PhoenixSecretKey::random(&mut rng_with_index(seed, index, b"SSK"))
PhoenixSecretKey::random(&mut rng_with_index(seed, index, b"PSK"))
}

/// Generates multiple [`PhoenixSecretKey`] from a seed and a range of indices.
Expand Down Expand Up @@ -98,10 +96,6 @@ pub fn rng_with_index(
index: u8,
termination: &[u8],
) -> ChaCha12Rng {
// NOTE: to not break the test-keys, we cast to a u64 here. Once we are
// ready to use the new keys, the index should not be cast to a u64
// anymore.
let index = u64::from(index);
let mut hash = Sha256::new();

hash.update(seed);
Expand Down
57 changes: 28 additions & 29 deletions wallet-core/tests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const INDEX: u8 = 42;
fn test_derive_phoenix_sk() {
// it is important that we always derive the same key from a fixed seed
let sk_bytes = [
12, 16, 72, 188, 33, 76, 44, 178, 86, 123, 107, 153, 230, 149, 238,
131, 87, 30, 94, 88, 52, 129, 247, 167, 30, 167, 163, 246, 68, 254, 14,
9, 218, 135, 245, 104, 11, 190, 143, 129, 83, 202, 64, 179, 157, 248,
175, 120, 157, 220, 98, 211, 141, 50, 224, 8, 1, 125, 29, 180, 206,
195, 34, 0,
160, 210, 234, 8, 94, 23, 76, 60, 130, 143, 137, 225, 37, 83, 68, 218,
207, 192, 171, 235, 252, 130, 133, 62, 18, 232, 6, 49, 245, 123, 220,
12, 250, 111, 39, 88, 24, 41, 156, 174, 241, 14, 118, 173, 11, 53, 192,
126, 7, 119, 70, 69, 212, 230, 124, 79, 223, 140, 93, 153, 33, 147,
163, 0,
];
assert_eq!(derive_phoenix_sk(&SEED, INDEX).to_bytes(), sk_bytes);
}
Expand All @@ -31,21 +31,20 @@ fn test_derive_phoenix_sk() {
fn test_derive_multiple_phoenix_sk() {
// it is important that we always derive the same key from a fixed seed
let sk_bytes_0 = [
12, 16, 72, 188, 33, 76, 44, 178, 86, 123, 107, 153, 230, 149, 238,
131, 87, 30, 94, 88, 52, 129, 247, 167, 30, 167, 163, 246, 68, 254, 14,
9, 218, 135, 245, 104, 11, 190, 143, 129, 83, 202, 64, 179, 157, 248,
175, 120, 157, 220, 98, 211, 141, 50, 224, 8, 1, 125, 29, 180, 206,
195, 34, 0,
184, 26, 20, 142, 31, 215, 134, 183, 100, 232, 94, 101, 206, 208, 164,
59, 72, 52, 42, 195, 106, 148, 124, 219, 47, 13, 145, 73, 227, 209,
140, 2, 27, 66, 207, 96, 97, 88, 228, 153, 4, 43, 36, 106, 159, 80, 80,
173, 145, 49, 116, 46, 8, 8, 95, 220, 115, 6, 30, 4, 100, 15, 89, 4,
];
let sk_bytes_1 = [
185, 163, 40, 99, 29, 14, 67, 189, 145, 215, 252, 61, 146, 211, 135,
55, 80, 69, 220, 183, 145, 4, 252, 186, 244, 79, 124, 177, 227, 35,
209, 5, 100, 181, 254, 1, 111, 180, 155, 211, 140, 23, 252, 248, 103,
44, 132, 14, 19, 18, 204, 101, 4, 200, 125, 185, 143, 68, 157, 251,
129, 238, 137, 5,
39, 29, 134, 8, 24, 45, 211, 176, 210, 104, 110, 168, 65, 57, 41, 187,
77, 165, 30, 222, 130, 179, 204, 85, 252, 152, 73, 218, 50, 97, 245, 8,
252, 26, 233, 156, 237, 162, 138, 31, 83, 196, 188, 60, 69, 76, 44, 59,
251, 111, 182, 35, 247, 210, 66, 108, 46, 117, 92, 183, 16, 104, 162,
9,
];

let keys = derive_multiple_phoenix_sk(&SEED, INDEX..INDEX + 2);
let keys = derive_multiple_phoenix_sk(&SEED, 0..2);
assert_eq!(keys[0].to_bytes(), sk_bytes_0,);
assert_eq!(keys[1].to_bytes(), sk_bytes_1,);
}
Expand All @@ -54,11 +53,11 @@ fn test_derive_multiple_phoenix_sk() {
fn test_derive_phoenix_pk() {
// it is important that we always derive the same key from a fixed seed
let pk_bytes = [
51, 204, 45, 112, 212, 44, 118, 183, 148, 176, 254, 135, 253, 117, 230,
62, 177, 139, 2, 57, 21, 150, 41, 86, 118, 239, 75, 194, 148, 129, 225,
38, 132, 140, 106, 77, 181, 217, 196, 50, 135, 177, 158, 153, 43, 147,
159, 217, 0, 160, 89, 95, 67, 160, 42, 74, 19, 1, 221, 216, 126, 204,
206, 209,
59, 192, 170, 209, 99, 97, 60, 124, 218, 81, 61, 102, 25, 235, 14, 87,
219, 234, 56, 102, 10, 111, 22, 189, 171, 101, 180, 168, 17, 70, 72,
101, 135, 243, 55, 243, 138, 103, 185, 26, 196, 219, 84, 126, 33, 115,
84, 60, 38, 41, 79, 104, 232, 222, 105, 2, 60, 185, 149, 50, 207, 43,
89, 100,
];
assert_eq!(derive_phoenix_pk(&SEED, INDEX).to_bytes(), pk_bytes);
}
Expand All @@ -67,11 +66,11 @@ fn test_derive_phoenix_pk() {
fn test_derive_phoenix_vk() {
// it is important that we always derive the same key from a fixed seed
let vk_bytes = [
12, 16, 72, 188, 33, 76, 44, 178, 86, 123, 107, 153, 230, 149, 238,
131, 87, 30, 94, 88, 52, 129, 247, 167, 30, 167, 163, 246, 68, 254, 14,
9, 132, 140, 106, 77, 181, 217, 196, 50, 135, 177, 158, 153, 43, 147,
159, 217, 0, 160, 89, 95, 67, 160, 42, 74, 19, 1, 221, 216, 126, 204,
206, 209,
160, 210, 234, 8, 94, 23, 76, 60, 130, 143, 137, 225, 37, 83, 68, 218,
207, 192, 171, 235, 252, 130, 133, 62, 18, 232, 6, 49, 245, 123, 220,
12, 135, 243, 55, 243, 138, 103, 185, 26, 196, 219, 84, 126, 33, 115,
84, 60, 38, 41, 79, 104, 232, 222, 105, 2, 60, 185, 149, 50, 207, 43,
89, 100,
];
assert_eq!(derive_phoenix_vk(&SEED, INDEX).to_bytes(), vk_bytes);
}
Expand All @@ -80,9 +79,9 @@ fn test_derive_phoenix_vk() {
fn test_derive_bls_sk() {
// it is important that we always derive the same key from a fixed seed
let sk_bytes = [
95, 35, 167, 191, 106, 171, 71, 158, 159, 39, 84, 1, 132, 238, 152,
235, 154, 5, 250, 158, 255, 195, 79, 95, 193, 58, 36, 189, 0, 99, 230,
86,
130, 180, 24, 224, 131, 143, 97, 18, 120, 53, 37, 39, 251, 44, 121,
168, 4, 248, 29, 176, 142, 136, 224, 188, 159, 246, 73, 6, 112, 174, 6,
7,
];
assert_eq!(derive_bls_sk(&SEED, INDEX).to_bytes(), sk_bytes);
}
Loading