about summary refs log tree commit diff
path: root/tvix/derivation/src/derivation.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-01-23T12·19+0300
committertazjin <tazjin@tvl.su>2023-01-23T12·55+0000
commitbe7f64ac87ee00aa13b0629ef4b927e14cc30e05 (patch)
tree74599dfa0bc957d44fb46e22ad2a66d6411e30f1 /tvix/derivation/src/derivation.rs
parent5719763fd3afaa5dd157da604069b037ca4bf79a (diff)
refactor(tvix/derivation): minor structure simplification r/5736
Fixes some module comments and embeds the `compress_hash` function in
the `derivation` module, as it was not used outside of this module
anyways.

Change-Id: I6c5c92b3f0c03c2cdcbcfc2f813909a968c4d44c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7905
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/derivation/src/derivation.rs')
-rw-r--r--tvix/derivation/src/derivation.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs
index d937bd91a7fa..74e68ce1d7c5 100644
--- a/tvix/derivation/src/derivation.rs
+++ b/tvix/derivation/src/derivation.rs
@@ -1,6 +1,6 @@
 use crate::output::{Hash, Output};
 use crate::write;
-use crate::{nix_hash, DerivationError};
+use crate::DerivationError;
 use serde::{Deserialize, Serialize};
 use sha2::{Digest, Sha256};
 use std::collections::BTreeSet;
@@ -29,6 +29,26 @@ pub struct Derivation {
     pub system: String,
 }
 
+/// compress_hash takes an arbitrarily long sequence of bytes (usually
+/// a hash digest), and returns a sequence of bytes of length
+/// output_size.
+///
+/// It's calculated by rotating through the bytes in the output buffer
+/// (zero- initialized), and XOR'ing with each byte of the passed
+/// input. It consumes 1 byte at a time, and XOR's it with the current
+/// value in the output buffer.
+///
+/// This mimics equivalent functionality in C++ Nix.
+fn compress_hash(input: &[u8], output_size: usize) -> Vec<u8> {
+    let mut output: Vec<u8> = vec![0; output_size];
+
+    for (ii, ch) in input.iter().enumerate() {
+        output[ii % output_size] ^= ch;
+    }
+
+    output
+}
+
 /// This returns a store path, either of a derivation or a regular output.
 /// The path_hash is compressed to 20 bytes, and nixbase32-encoded (32 characters)
 fn build_store_path(
@@ -36,7 +56,7 @@ fn build_store_path(
     path_hash: &[u8],
     name: &str,
 ) -> Result<StorePath, DerivationError> {
-    let compressed = nix_hash::compress_hash(path_hash, 20);
+    let compressed = compress_hash(path_hash, 20);
     if is_derivation {
         StorePath::from_string(
             format!(