Skip to content

Commit

Permalink
docs(frontend): reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
bcm-at-zama committed Jul 5, 2024
1 parent b753c49 commit 4e835f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# Hamming weight computation
def hw(x):
# Hamming Weight table for 8b entries
"""Return the Hamming Weight table for 8b entries."""
hw_table_ref = [np.binary_repr(x).count("1") for x in range(2**8)]

# fmt: off
Expand All @@ -30,17 +30,17 @@ def hw(x):


def mapme(x):
# Map 0 to -1, and keep 1 as 1
"""Map 0 to -1, and keep 1 as 1."""
return 2 * x - 1


# Reference function for tests
def dist_in_clear(x, y):
"""Compute the distance in the clear."""
return np.sum(hw(x ^ y))


# In FHE
def dist_in_fhe(x_mapped, y_mapped):
"""Compute the distance in FHE."""

# x is a line tensor, whose 0's have been replaced by -1
# y_clear is a column tensor, whose 0's have been replaced by -1
Expand All @@ -59,8 +59,8 @@ def dist_in_fhe(x_mapped, y_mapped):
return final_result


# Manage user args
def manage_args():
"""Manage user args."""
parser = argparse.ArgumentParser(
description="Hamming weight (aka XOR) distance in Concrete, between an encrypted vector and a clear vector."
)
Expand Down Expand Up @@ -91,6 +91,7 @@ def manage_args():


def main():
"""Main function."""
print()

# Options by the user
Expand Down Expand Up @@ -125,8 +126,10 @@ def main():

total_time = 0

nb_samples_for_warmup = 10

# Then use
for _i in range(args.repeat):
for i in range(nb_samples_for_warmup + args.repeat):
# Take a random input pair
x, y = (
np.random.randint(2**1, size=(1, nb_bits)),
Expand All @@ -144,7 +147,9 @@ def main():
encrypted_result = circuit.run(encrypted_input)
end_time = time.time()

total_time += end_time - begin_time
# Don't count the warmup samples
if i >= nb_samples_for_warmup:
total_time += end_time - begin_time

# Decrypt
result = circuit.decrypt(encrypted_result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ And finally, for 12804-bit vectors, execution times should be:
dist_in_fhe_with_multivariate_tables on 4 bits: 40.89 seconds
```

## Distance Between Two Encrypted Tensors
## Distance Between One Encrypted Tensor and One Clear Tensor

In [this code](hamming_distance_to_clear.py), we propose a simple implementation for the special case
where one of the vectors (here, `y`) is not encrypted. The function `dist_in_fhe` is based on the
Expand Down

0 comments on commit 4e835f4

Please sign in to comment.