From 7ff58c28200b42a5ce4ee50b1fef09369ad11d59 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 5 Sep 2024 10:13:22 -0700 Subject: [PATCH] Hashing over owned/borrowed data without explicit type parameters --- ipa-core/src/helpers/hashing.rs | 46 +++++++++++-------- .../src/protocol/basics/share_validation.rs | 4 +- .../ipa_prf/malicious_security/prover.rs | 4 +- .../ipa_prf/validation_protocol/validation.rs | 8 +--- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/ipa-core/src/helpers/hashing.rs b/ipa-core/src/helpers/hashing.rs index b76acc0a9..bdf684c8f 100644 --- a/ipa-core/src/helpers/hashing.rs +++ b/ipa-core/src/helpers/hashing.rs @@ -1,4 +1,4 @@ -use std::{borrow::Borrow, convert::Infallible}; +use std::convert::Infallible; use generic_array::GenericArray; use sha2::{ @@ -29,6 +29,22 @@ impl Serializable for Hash { } } +pub trait SerializeAs { + fn serialize(self, buf: &mut GenericArray); +} + +impl SerializeAs for T { + fn serialize(self, buf: &mut GenericArray::Size>) { + ::serialize(&self, buf); + } +} + +impl<'a, T: Serializable> SerializeAs for &'a T { + fn serialize(self, buf: &mut GenericArray::Size>) { + ::serialize(self, buf); + } +} + impl MpcMessage for Hash {} /// Computes Hash of serializable values from an iterator @@ -38,7 +54,7 @@ impl MpcMessage for Hash {} pub fn compute_hash(input: I) -> Hash where I: IntoIterator, - T: Borrow, + T: SerializeAs, S: Serializable, { // set up hash @@ -49,7 +65,7 @@ where // set state for x in input { is_empty = false; - x.borrow().serialize(&mut buf); + x.serialize(&mut buf); sha.update(&buf); } @@ -91,7 +107,7 @@ where ); // set state - let combine = compute_hash::<_, _, Hash>([left, right]); + let combine = compute_hash([left, right]); let mut buf = GenericArray::default(); combine.serialize(&mut buf); @@ -122,7 +138,7 @@ mod test { let mut rng = thread_rng(); let list: GenericArray = GenericArray::generate(|_| rng.gen::()); - let hash: Hash = compute_hash::<_, _, Fp32BitPrime>(&list); + let hash: Hash = compute_hash(list); let mut buf: GenericArray = GenericArray::default(); hash.serialize(&mut buf); let deserialized_hash = Hash::deserialize(&buf); @@ -139,7 +155,7 @@ mod test { for _ in 0..LIST_LENGTH { list.push(rng.gen::()); } - let hash_1 = compute_hash::<_, _, Fp31>(&list); + let hash_1 = compute_hash(&list); // modify one, randomly selected element in the list let random_index = rng.gen::() % LIST_LENGTH; @@ -149,7 +165,7 @@ mod test { } list[random_index] = different_field_element; - let hash_2 = compute_hash::<_, _, Fp31>(&list); + let hash_2 = compute_hash(&list); assert_ne!( hash_1, hash_2, @@ -171,7 +187,7 @@ mod test { } list.swap(index_1, index_2); - let hash_3 = compute_hash::<_, _, Fp31>(&list); + let hash_3 = compute_hash(&list); assert_ne!( hash_2, hash_3, @@ -192,11 +208,7 @@ mod test { left.push(rng.gen::()); right.push(rng.gen::()); } - let r1: Fp32BitPrime = hash_to_field( - &compute_hash::<_, _, Fp32BitPrime>(&left), - &compute_hash::<_, _, Fp32BitPrime>(&right), - EXCLUDE, - ); + let r1: Fp32BitPrime = hash_to_field(&compute_hash(&left), &compute_hash(&right), EXCLUDE); // modify one, randomly selected element in the list let random_index = rng.gen::() % LIST_LENGTH; @@ -208,11 +220,7 @@ mod test { right[random_index] = modified_value; } - let r2: Fp32BitPrime = hash_to_field( - &compute_hash::<_, _, Fp32BitPrime>(&left), - &compute_hash::<_, _, Fp32BitPrime>(&right), - EXCLUDE, - ); + let r2: Fp32BitPrime = hash_to_field(&compute_hash(&left), &compute_hash(&right), EXCLUDE); assert_ne!( r1, r2, @@ -224,6 +232,6 @@ mod test { fn check_hash_from_owned_values() { let mut rng = thread_rng(); let vec = (0..100).map(|_| rng.gen::()).collect::>(); - assert_eq!(compute_hash::<_, _, Fp31>(&vec), compute_hash(vec)); + assert_eq!(compute_hash(&vec), compute_hash(vec)); } } diff --git a/ipa-core/src/protocol/basics/share_validation.rs b/ipa-core/src/protocol/basics/share_validation.rs index f0add711b..43d1f34e6 100644 --- a/ipa-core/src/protocol/basics/share_validation.rs +++ b/ipa-core/src/protocol/basics/share_validation.rs @@ -35,7 +35,7 @@ where S: SharedValue, { // compute hash of `left` - let hash_left = compute_hash::<_, _, S>(input_left); + let hash_left = compute_hash(input_left); // set up context let ctx_new = &(ctx.set_total_records(TotalRecords::ONE)); @@ -45,7 +45,7 @@ where let ((), hash_received) = try_join( // send hash - send_channel.send(RecordId::FIRST, compute_hash::<_, _, S>(input_right)), + send_channel.send(RecordId::FIRST, compute_hash(input_right)), receive_channel.receive(RecordId::FIRST), ) .await?; diff --git a/ipa-core/src/protocol/ipa_prf/malicious_security/prover.rs b/ipa-core/src/protocol/ipa_prf/malicious_security/prover.rs index 332c92c0b..780eabcf2 100644 --- a/ipa-core/src/protocol/ipa_prf/malicious_security/prover.rs +++ b/ipa-core/src/protocol/ipa_prf/malicious_security/prover.rs @@ -157,8 +157,8 @@ impl ProofGenerat B: Borrow<([F; L], [F; L])>, { let r: F = hash_to_field( - &compute_hash::<_, _, F>(proof_left), - &compute_hash::<_, _, F>(proof_right), + &compute_hash(proof_left), + &compute_hash(proof_right), L.try_into().unwrap(), ); diff --git a/ipa-core/src/protocol/ipa_prf/validation_protocol/validation.rs b/ipa-core/src/protocol/ipa_prf/validation_protocol/validation.rs index 899c9abf1..456c7cdf0 100644 --- a/ipa-core/src/protocol/ipa_prf/validation_protocol/validation.rs +++ b/ipa-core/src/protocol/ipa_prf/validation_protocol/validation.rs @@ -305,12 +305,8 @@ impl ProofHashes { }; Self { - hashes: once(compute_hash::<_, _, Fp61BitPrime>(first_proof)) - .chain( - other_proofs - .iter() - .map(|proof| compute_hash::<_, _, Fp61BitPrime>(proof.iter())), - ) + hashes: once(compute_hash(first_proof)) + .chain(other_proofs.iter().map(|proof| compute_hash(proof.iter()))) .collect::>(), } }