Skip to content

Commit

Permalink
Use rayon in get_script_to_secret_map
Browse files Browse the repository at this point in the history
  • Loading branch information
cygnet3 committed Jun 5, 2024
1 parent 2c35ead commit 1b71c6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bitcoin = { version = "0.31.1", features = ["serde", "rand", "base64"] }
bip39 = { version = "2.0.0", features = ["rand"] }
serde_with = "3.6.0"
log = "0.4.20"
rayon = "1.10.0"
24 changes: 16 additions & 8 deletions src/spclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,21 +742,29 @@ impl SpClient {
tweak_data_vec: Vec<PublicKey>,
secp: &Secp256k1<All>,
) -> Result<HashMap<[u8; 34], PublicKey>> {
use rayon::prelude::*;
let scan_key_scalar: Scalar = self.scan_sk.into();

let mut res = HashMap::new();
let shared_secrets: Result<Vec<PublicKey>> = tweak_data_vec
.into_iter()
.into_par_iter()
.map(|tweak| tweak.mul_tweak(secp, &scan_key_scalar).map_err(Error::new))
.collect();

for shared_secret in shared_secrets? {
let spks = self
.sp_receiver
.get_spks_from_shared_secret(&shared_secret)?;
let shared_secrets = shared_secrets?;

for spk in spks.into_values() {
res.insert(spk, shared_secret);
let items: Result<Vec<_>> = shared_secrets
.into_par_iter()
.map(|secret| {
let spks = self.sp_receiver.get_spks_from_shared_secret(&secret)?;

Ok((secret, spks.into_values()))
})
.collect();

let mut res = HashMap::new();
for (secret, spks) in items? {
for spk in spks {
res.insert(spk, secret);
}
}
Ok(res)
Expand Down

0 comments on commit 1b71c6f

Please sign in to comment.