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

Change BLSCache to use the augmented message for its lookup #744

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
11 changes: 3 additions & 8 deletions crates/chia-bls/src/bls_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ impl BlsCache {
let iter = pks_msgs.into_iter().map(|(pk, msg)| -> GTElement {
// Hash pubkey + message
let mut hasher = Sha256::new();
hasher.update(pk.borrow().to_bytes());
hasher.update(msg.as_ref());
let mut aug_msg = pk.borrow().to_bytes().to_vec();
aug_msg.extend_from_slice(msg.as_ref());
hasher.update(&aug_msg);
let hash: [u8; 32] = hasher.finalize();

// If the pairing is in the cache, we don't need to recalculate it.
Expand All @@ -62,14 +63,8 @@ impl BlsCache {
}

// Otherwise, we need to calculate the pairing and add it to the cache.
let mut aug_msg = pk.borrow().to_bytes().to_vec();
aug_msg.extend_from_slice(msg.as_ref());
let aug_hash = hash_to_g2(&aug_msg);

let mut hasher = Sha256::new();
hasher.update(&aug_msg);
let hash: [u8; 32] = hasher.finalize();

let pairing = aug_hash.pair(pk.borrow());
self.cache.put(hash, pairing.clone());
pairing
Expand Down
24 changes: 10 additions & 14 deletions crates/chia-consensus/src/spendbundle_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub fn validate_clvm_and_signature(
let mut pairs = Vec::new();

let mut aug_msg = Vec::<u8>::new();
let mut final_msg = Vec::<u8>::new();

for spend in &conditions.spends {
let condition_items_pairs = [
Expand All @@ -54,14 +53,15 @@ pub fn validate_clvm_and_signature(
for (condition, items) in condition_items_pairs {
for (pk, msg) in items {
aug_msg.clear();
final_msg.clear();
final_msg.extend_from_slice(msg.as_slice());
aug_msg.extend_from_slice(&pk.to_bytes());
make_aggsig_final_message(condition, &mut final_msg, spend, constants);
aug_msg.extend(&final_msg);
aug_msg.extend_from_slice(msg.as_slice());
make_aggsig_final_message(condition, &mut aug_msg, spend, constants);
let aug_hash = hash_to_g2(&aug_msg);
let pairing = aug_hash.pair(pk);
pairs.push((hash_pk_and_msg(&pk.to_bytes(), &final_msg), pairing));
let mut hasher = Sha256::new();
hasher.update(&aug_msg);
let aug_msg_hash = hasher.finalize();
pairs.push((aug_msg_hash, pairing));
}
}
}
Expand All @@ -72,7 +72,10 @@ pub fn validate_clvm_and_signature(
aug_msg.extend_from_slice(msg.as_ref());
let aug_hash = hash_to_g2(&aug_msg);
let pairing = aug_hash.pair(pk);
pairs.push((hash_pk_and_msg(&pk.to_bytes(), msg), pairing));
let mut hasher = Sha256::new();
hasher.update(&aug_msg);
let aug_msg_hash = hasher.finalize();
pairs.push((aug_msg_hash, pairing));
}

// Verify aggregated signature
Expand All @@ -88,13 +91,6 @@ pub fn validate_clvm_and_signature(
Ok((conditions, pairs, start_time.elapsed()))
}

fn hash_pk_and_msg(pk: &[u8], msg: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(pk);
hasher.update(msg);
hasher.finalize()
}

pub fn get_flags_for_height_and_constants(height: u32, constants: &ConsensusConstants) -> u32 {
let mut flags: u32 = 0;

Expand Down