forked from private-attribution/ipa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::{ | ||
collections::BTreeMap, | ||
hash::{DefaultHasher, Hash, Hasher}, | ||
}; | ||
|
||
use proc_macro2::{Ident, TokenStream}; | ||
use quote::{quote, ToTokens}; | ||
use syn::parse_str; | ||
|
||
use crate::CompactGateIndex; | ||
|
||
/// Builds a map of step strings to the corresponding compact gate index. Emits an array of tuples | ||
/// containing the hash and the index, sorted by hash. [`std::str::FromStr`] implementation for | ||
/// compact gate uses the same hashing algorithm for input string and uses the provided code to | ||
/// run binary search and find the item. | ||
/// | ||
/// The complexity of this operation at compile time is O(n) and the cost is hash(str)*n. | ||
/// Runtime overhead is proportional to hash(str)+log(n) | ||
#[derive(Default)] | ||
pub(crate) struct HashingSteps { | ||
inner: BTreeMap<u64, CompactGateIndex>, | ||
} | ||
|
||
fn hash(s: &str) -> u64 { | ||
let mut hasher = DefaultHasher::default(); | ||
s.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
impl HashingSteps { | ||
/// Add a step to the map. | ||
/// ## Panics | ||
/// if the step already added or if there is a hash collision with any of the steps already | ||
/// added. | ||
pub fn hash(&mut self, step: &str, gate: CompactGateIndex) { | ||
let h = hash(step); | ||
if let Some(old_val) = self.inner.insert(h, gate) { | ||
panic!("Hash collision for {step}: {h} => {old_val} and {gate}. Check that there are no duplicate steps defined in the protocol."); | ||
} | ||
} | ||
|
||
pub fn lookup_type(&self) -> Ident { | ||
parse_str("GateLookup").unwrap() | ||
} | ||
} | ||
|
||
impl ToTokens for HashingSteps { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
let lookup_type = self.lookup_type(); | ||
let sz = self.inner.len(); | ||
let hashes = self.inner.iter().map(|(h, i)| quote! {(#h, #i)}); | ||
|
||
tokens.extend(quote! { | ||
#lookup_type { | ||
#[allow(clippy::unreadable_literal)] | ||
inner: [#(#hashes),*] | ||
}; | ||
|
||
struct #lookup_type { | ||
inner: [(u64, u32); #sz] | ||
} | ||
|
||
impl #lookup_type { | ||
fn find(&self, input: &str) -> Option<u32> { | ||
let h = Self::hash(input); | ||
self.inner.binary_search_by_key(&h, |(hash, _)| *hash).ok().map(|i| self.inner[i].1) | ||
} | ||
|
||
/// This must be kept in sync with proc-macro code that generates the hash. | ||
fn hash(s: &str) -> u64 { | ||
let mut hasher = ::std::hash::DefaultHasher::default(); | ||
::std::hash::Hash::hash(s, &mut hasher); | ||
::std::hash::Hasher::finish(&hasher) | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters