feat: Use hash of name instead of random chars for long kdl names

This commit is contained in:
Nicolas Abril 2022-10-26 14:29:46 +02:00
parent 9b15b8f78b
commit 88f6ac73b0
5 changed files with 54 additions and 62 deletions

64
Cargo.lock generated
View File

@ -119,6 +119,12 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "ctor"
version = "0.1.23"
@ -237,17 +243,6 @@ dependencies = [
"slab",
]
[[package]]
name = "getrandom"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "h2"
version = "0.3.14"
@ -453,7 +448,7 @@ dependencies = [
"hvm",
"ntest",
"pretty_assertions",
"rand",
"tiny-keccak",
"walkdir",
]
@ -661,12 +656,6 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "pretty_assertions"
version = "1.3.0"
@ -732,36 +721,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
@ -996,6 +955,15 @@ dependencies = [
"syn",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "tinyvec"
version = "1.6.0"

View File

@ -14,7 +14,7 @@ hvm = "0.1.81"
#hvm = { path = "../hvm" }
highlight_error = "0.1.1"
clap = { version = "3.1.8", features = ["derive"] }
rand = "0.8.5"
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -5,8 +5,8 @@ use crate::book::name::Ident;
use crate::book::Book;
pub use crate::codegen::kdl::book::*;
use rand::Rng;
use std::collections::{HashMap, HashSet};
use tiny_keccak::Hasher;
pub const KDL_NAME_LEN: usize = 12;
@ -165,22 +165,28 @@ pub fn to_kdl_book(book: Book, namespace: &Option<String>) -> Result<String, Str
// Returns an err if any of the names can't be converted
pub fn get_kdl_names(book: &CompBook, namespace: &Option<String>) -> Result<HashMap<Ident, Ident>, String> {
// Fits a name to the max size allowed by kindelia.
// If the name is too large, truncates and replaces the last characters by random chars.
fn rand_shorten(name: &Ident, ns: &str) -> Ident {
// If the name is too large, uses the hash of the name instead
fn hash_shorten(name: &Ident, ns: &str) -> Ident {
let max_fn_name = KDL_NAME_LEN - ns.len();
// If the name doesn't fit, truncate and insert some random characters at the end
let name = if name.len() > max_fn_name {
let n_rnd_chrs = usize::min(3, max_fn_name);
let name_cut = name.0[..max_fn_name - n_rnd_chrs].to_string();
let mut rng = rand::thread_rng();
let rnd_chrs = (0..n_rnd_chrs).map(|_| rng.gen_range(0..63)).map(encode_base64).collect::<String>();
Ident(format!("{}{}", name_cut, rnd_chrs))
let name_hash = keccak128(name.0.as_bytes());
let name_hash = u128::from_le_bytes(name_hash);
let name_hash = u128_to_kdl_name(name_hash);
name_hash[..max_fn_name].to_string()
} else {
name.clone()
name.0.clone()
};
Ident(format!("{}{}", ns, name))
}
fn keccak128(data: &[u8]) -> [u8; 16] {
let mut hasher = tiny_keccak::Keccak::v256();
let mut output = [0u8; 16];
hasher.update(data);
hasher.finalize(&mut output);
output
}
fn get_kdl_name(entry: &CompEntry, ns: &str) -> Result<Ident, String> {
let kind_name = &entry.name;
// If the entry uses a kindelia name, use it
@ -202,18 +208,18 @@ pub fn get_kdl_names(book: &CompBook, namespace: &Option<String>) -> Result<Hash
Ident(format!("{}{}", ns, kdln))
} else {
// For entries created by the flattener, we shorten even the kindelia name
rand_shorten(&kdln, ns)
hash_shorten(&kdln, ns)
}
}
// Otherwise, try to fit the normal kind name
else {
let fixed_name = Ident(kind_name.0.replace('.', "_"));
rand_shorten(&fixed_name, ns)
hash_shorten(&fixed_name, ns)
};
Ok(kdln)
}
fn encode_base64(num: u8) -> char {
fn encode_base64_u8(num: u8) -> char {
match num {
0..=9 => (num + b'0') as char,
10..=35 => (num - 10 + b'A') as char,
@ -222,6 +228,15 @@ pub fn get_kdl_names(book: &CompBook, namespace: &Option<String>) -> Result<Hash
}
}
fn u128_to_kdl_name(mut num: u128) -> String {
let mut encoded = [0 as char; 12];
for i in 0..12 {
encoded[i] = encode_base64_u8((num & 0x3f) as u8);
num >>= 6;
}
encoded.into_iter().collect()
}
let mut errors = Vec::new();
let mut kdl_names = HashMap::new();
let ns = namespace.as_ref().map_or(String::new(), |ns| format!("{}.", ns));

View File

@ -0,0 +1,6 @@
// FunctionWithAVeryLongName : U60
fun (NDFMHGgkiOr2) {
(NDFMHGgkiOr2) =
#0
}

View File

@ -0,0 +1,3 @@
FunctionWithAVeryLongName : U60 {
0
}