Skip to content

Commit

Permalink
Upgrade sha2 and digest
Browse files Browse the repository at this point in the history
Motivated by private-attribution#993 and the main reason is to bring `sha2` closer to latest `generic-array` version. `0.13` uses 1.0
  • Loading branch information
akoshelev committed Mar 27, 2024
1 parent 9314153 commit 9c85ecc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ipa-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions ipa-core/src/hpke/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down
36 changes: 20 additions & 16 deletions ipa-core/src/protocol/ipa_prf/malicious_security/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -54,7 +56,7 @@ where
{
// set up hash
let mut sha = Sha256::new();

// set state
let mut buf = GenericArray::default();
left.serialize(&mut buf);
Expand All @@ -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<Fp31> = Vec::with_capacity(LIST_LENGTH);
for _ in 0..LIST_LENGTH {
list.push(rng.gen::<Fp31>());
Expand All @@ -102,25 +106,25 @@ 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]
fn field_element_changes() {
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::<Fp32BitPrime>());
right.push(rng.gen::<Fp32BitPrime>());
}
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::<usize>() % LIST_LENGTH;
Expand All @@ -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"
);
}
}

0 comments on commit 9c85ecc

Please sign in to comment.