Skip to main content

A quantization-based technique for privacy preserving distributed learning

Project description

HashComb Python

HashComb is a quantization-based hashing technique for privacy‑preserving distributed learning. It builds a balanced binary tree over a numeric range and maps values to hash tokens derived from tree nodes. Tokens can be used as compact, privacy‑preserving representations for aggregation, statistics, and clustering, as described in the HashComb paper.

Reference: This implementation follows the HashComb paper (see the PDF in the repository root).


Why HashComb

  • Quantization via tree: values are mapped to bins defined by a balanced binary tree.
  • Hash tokens: each tree node can be hashed into a compact token.
  • Three signatures:
    • Leaf hash: single token for the leaf bin.
    • Prefix multihash: first $k$ tokens from the path.
    • Full‑path multihash: tokens from root to leaf.
  • Aggregation‑ready: server can aggregate counts without seeing raw values.

Installation

pip install -e .

Optional dependencies for notebooks:

pip install -e .[notebooks]

Quickstart

from hashcomb import Encoder, Decoder

enc = Encoder(channels=4, maxValue=10.0, minValue=0.0, configPath="artifacts/config.pkl")
dec = Decoder(configPath="artifacts/config.pkl")

v = 3.7
leaf = enc.encode(v)
center = dec.decode(leaf)

leaf, center

Core concepts (from the paper)

  1. Quantization tree: splits the range into $2^L$ bins (leaf nodes), where $L$ is channels.
  2. Tokenization: each node is hashed into a compact token (optionally salted).
  3. Encoding: a value maps to a path of tokens (root→leaf).
  4. Decoding: tokens map to bin centers for approximate reconstruction.

Public API (classes + methods)

Encoder

Deterministic encoder using a fixed tree.

Constructor

from hashcomb import Encoder
enc = Encoder(channels=4, maxValue=10.0, minValue=0.0, configPath="artifacts/enc.pkl")

Methods

leaf = enc.encode(3.7)                 # leaf token
path = enc.encodePath(3.7)             # full path tokens
prefix = enc.encodePrefix(3.7, 2)      # first k tokens

arr_leaf = enc.encodeArray([1.0, 2.0])
arr_path = enc.encodePathArray([1.0, 2.0])
arr_pref = enc.encodePrefixArray([1.0, 2.0], length=2)

Factory methods

from hashcomb import PklIO
enc2 = Encoder.from_pkl("artifacts/enc.pkl")
enc3 = Encoder.from_config(PklIO.loadConfig("artifacts/enc.pkl"))

RandomizedEncoder

Randomized encoder (paper mode) using the “last‑head in $L$ tosses” rule.

Constructor

from hashcomb import RandomizedEncoder, RoundContext
ctx = RoundContext(salt="roundA", seed=123)
enc = RandomizedEncoder(
    channels=4,
    maxValue=10.0,
    minValue=0.0,
    selectionProbability=0.6,
    roundContext=ctx,
    configPath="artifacts/rand.pkl",
)

Methods

leaf = enc.encode(3.7)
path = enc.encodePath(3.7)
prefix = enc.encodePrefix(3.7, 2)

arr_leaf = enc.encodeArray([1.0, 2.0])
arr_path = enc.encodePathArray([1.0, 2.0])
arr_pref = enc.encodePrefixArray([1.0, 2.0], length=2)

Probability helpers

p = RandomizedEncoder.compute_selection_probability(channels=4, targetLevel=2.5)
exp = RandomizedEncoder.expected_level(channels=4, selectionProbability=p)

Factory methods

enc2 = RandomizedEncoder.from_pkl("artifacts/rand.pkl")

Decoder

Decode tokens to bin centers.

from hashcomb import Decoder

dec = Decoder(configPath="artifacts/enc.pkl")
center = dec.decode(leaf)
center2 = dec.decodePath(prefix)

centers = dec.decodeArray([leaf])
centers2 = dec.decodePathArray([path])

Tree

Balanced binary tree defining the quantization bins.

from hashcomb import Tree

tr = Tree(channels=3, maxValue=10.0, minValue=0.0)
path_tokens = tr.getHValues(3.7, True)
rounded = Tree.round(1.2345, 2)

Node

Tree node with interval and helpers.

from hashcomb import Node

n = Node(0.0, 1.0, 0)
center = n.getCenter
is_leaf = n.isLeaf
as_str = str(n)
node_token = n.getValue(True)
node_path = n.getValue(0.2, True)

Hash

Tokenization helpers.

from hashcomb.core.hash import Hash

hmap = Hash.buildHashTable(tr, include_internal=True)
sha = Hash.sha3_256_int64("abc")
tok = Hash.hash_token("abc", "salt")

RoundContext

Per‑round shared salt and RNG seed.

from hashcomb import RoundContext

ctx = RoundContext.generate(salt_bytes=2, seed=7)

PklIO

Read/write configs and pickles.

from hashcomb import PklIO

PklIO.savePickle("artifacts/obj.pkl", {"a": 1})
obj = PklIO.loadPickle("artifacts/obj.pkl")

PklIO.saveConfig("artifacts/config.pkl", {"schema": "hashcomb.config.v1", "params": {}})
cfg = PklIO.loadConfig("artifacts/config.pkl")

CsvIO

Encode/decode a CSV column with HashComb.

from hashcomb import CsvIO

# input CSV has header with a "value" column
CsvIO.encodeCsv("data.csv", "data_encoded.csv", enc, valueCol="value", hashCol="hash")
CsvIO.decodeCsv("data_encoded.csv", "data_decoded.csv", dec, hashCol="hash", decodedValueCol="decoded_value")

Add‑ons

Optional utilities (not required for core usage).

from hashcomb.addons import aggregate_ciphertexts, serialize_path, deserialize_path

agg = aggregate_ciphertexts([("a", 1), ("a", 2), ("b", 5)])
ser = serialize_path(["x", "y"])
rest = deserialize_path(ser)

CLI

Minimal CLI wrapper:

# encode leaf
python -m hashcomb.cli encode --channels 4 --min 0 --max 10 --value 3.7

# encode full path
python -m hashcomb.cli encode --channels 4 --min 0 --max 10 --value 3.7 --mode path

# encode prefix
python -m hashcomb.cli encode --channels 4 --min 0 --max 10 --value 3.7 --mode prefix --prefix-length 2

# decode leaf
python -m hashcomb.cli decode --config artifacts/enc.pkl --hash <token>

# decode path/prefix
python -m hashcomb.cli decode --config artifacts/enc.pkl --path token1,token2

Notebooks

  • 01_basics.ipynb: API basics, signatures, randomization.
  • 02_statistics.ipynb: mean estimation and error.
  • 03_clustering.ipynb: clustering + K‑means demos.
  • 04_mpc_addons.ipynb: add‑on utilities.
  • hashcomb.ipynb: this reference notebook (overview + API examples).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hashcomb-0.1.1.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hashcomb-0.1.1-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file hashcomb-0.1.1.tar.gz.

File metadata

  • Download URL: hashcomb-0.1.1.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for hashcomb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1dce85bd59e8b09aeb34ef9d35b9f3af4183e8de45c1631f65156917be252993
MD5 515a07d9986527325464b05274019dff
BLAKE2b-256 691265aa82beb69554b908b3e90e8485a81d143e185aff7a2154fcadb1a84a7e

See more details on using hashes here.

File details

Details for the file hashcomb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hashcomb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for hashcomb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 73c8657bb2dc88815bebf9af032277dbaa87359c3606b7621f841522227c2b68
MD5 c70d068445fec33fa5e002c78037c978
BLAKE2b-256 7e4e4e132e0263c74d504a1ec8214aaceca7488d7ce5d282cfe6a885485270f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page