From 9c85ecca7aed9b6d781ec6442072e025643f2f51 Mon Sep 17 00:00:00 2001 From: Alex Koshelev Date: Wed, 27 Mar 2024 11:13:21 -0700 Subject: [PATCH] Upgrade sha2 and digest Motivated by #993 and the main reason is to bring `sha2` closer to latest `generic-array` version. `0.13` uses 1.0 --- ipa-core/Cargo.toml | 4 +-- ipa-core/src/hpke/mod.rs | 4 +-- .../ipa_prf/malicious_security/hashing.rs | 36 ++++++++++--------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/ipa-core/Cargo.toml b/ipa-core/Cargo.toml index 2eca7d8e7..7f6c20d22 100644 --- a/ipa-core/Cargo.toml +++ b/ipa-core/Cargo.toml @@ -107,7 +107,7 @@ futures = "0.3.28" futures-util = "0.3.28" generic-array = "1.0.0" hex = { version = "0.4", features = ["serde"] } -hkdf = "0.12.3" +hkdf = "0.13.0-pre.3" hpke = { version = "0.11.0", default-features = false, features = [ "std", "x25519", @@ -136,7 +136,7 @@ rustls-webpki = "^0.101.4" # TODO consider using zerocopy or serde_bytes or in-house serialization serde = { version = "1.0", optional = true, features = ["derive"] } serde_json = { version = "1.0", optional = true } -sha2 = "0.10" +sha2 = "0.11.0-pre.3" shuttle-crate = { package = "shuttle", version = "0.6.1", optional = true } thiserror = "1.0" time = { version = "0.3", optional = true } diff --git a/ipa-core/src/hpke/mod.rs b/ipa-core/src/hpke/mod.rs index 523efeef8..61cab7910 100644 --- a/ipa-core/src/hpke/mod.rs +++ b/ipa-core/src/hpke/mod.rs @@ -369,7 +369,7 @@ mod tests { let mut suite = EncryptionSuite::new(1, rng); let mut encryption = suite.seal(0, EventType::Source, &new_share(0, 0)); - encryption.ct.as_mut()[bad_byte] ^= 1 << bad_bit; + encryption.ct[bad_byte] ^= 1 << bad_bit; suite.open(0, EventType::Source, encryption).unwrap_err(); } } @@ -382,7 +382,7 @@ mod tests { let mut suite = EncryptionSuite::new(1, rng); let mut encryption = suite.seal(0, EventType::Source, &new_share(0, 0)); - encryption.enc.as_mut()[bad_byte] ^= 1 << bad_bit; + encryption.enc[bad_byte] ^= 1 << bad_bit; suite.open(0, EventType::Source, encryption).unwrap_err(); } } diff --git a/ipa-core/src/protocol/ipa_prf/malicious_security/hashing.rs b/ipa-core/src/protocol/ipa_prf/malicious_security/hashing.rs index bd7fcf7a5..7aac72e1a 100644 --- a/ipa-core/src/protocol/ipa_prf/malicious_security/hashing.rs +++ b/ipa-core/src/protocol/ipa_prf/malicious_security/hashing.rs @@ -5,7 +5,9 @@ use sha2::{Digest, Sha256}; use typenum::U32; use crate::{ - ff::{Field, Serializable}, helpers::Message, protocol::prss::FromRandomU128 + ff::{Field, Serializable}, + helpers::Message, + protocol::prss::FromRandomU128, }; #[derive(Clone, Copy, Debug, PartialEq)] @@ -54,7 +56,7 @@ where { // set up hash let mut sha = Sha256::new(); - + // set state let mut buf = GenericArray::default(); left.serialize(&mut buf); @@ -76,16 +78,18 @@ where mod test { use rand::{thread_rng, Rng}; - use crate::{ff::{Fp31, Fp32BitPrime}, protocol::ipa_prf::malicious_security::hashing::hash_to_field}; - use super::compute_hash; + use crate::{ + ff::{Fp31, Fp32BitPrime}, + protocol::ipa_prf::malicious_security::hashing::hash_to_field, + }; #[test] fn hash_changes() { const LIST_LENGTH: usize = 5; let mut rng = thread_rng(); - + let mut list: Vec = Vec::with_capacity(LIST_LENGTH); for _ in 0..LIST_LENGTH { list.push(rng.gen::()); @@ -102,7 +106,10 @@ mod test { let hash_2 = compute_hash(&list); - assert_ne!(hash_1, hash_2, "The hash should change if the input is different"); + assert_ne!( + hash_1, hash_2, + "The hash should change if the input is different" + ); } #[test] @@ -110,17 +117,14 @@ mod test { const LIST_LENGTH: usize = 5; let mut rng = thread_rng(); - + let mut left = Vec::with_capacity(LIST_LENGTH); let mut right = Vec::with_capacity(LIST_LENGTH); for _ in 0..LIST_LENGTH { left.push(rng.gen::()); right.push(rng.gen::()); } - let r1: Fp32BitPrime = hash_to_field( - compute_hash(&left), - compute_hash(&right), - ); + let r1: Fp32BitPrime = hash_to_field(compute_hash(&left), compute_hash(&right)); // modify one, randomly selected element in the list let random_index = rng.gen::() % LIST_LENGTH; @@ -132,11 +136,11 @@ mod test { right[random_index] = modified_value; } - let r2: Fp32BitPrime = hash_to_field( - compute_hash(&left), - compute_hash(&right), - ); + let r2: Fp32BitPrime = hash_to_field(compute_hash(&left), compute_hash(&right)); - assert_ne!(r1, r2, "any modification to either list should change the hashed field element"); + assert_ne!( + r1, r2, + "any modification to either list should change the hashed field element" + ); } }