Skip to content

Commit

Permalink
docs(frontend): save eval keys only in TFHE-rs examples
Browse files Browse the repository at this point in the history
  • Loading branch information
youben11 committed Dec 24, 2024
1 parent 426a261 commit 66b5c05
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
33 changes: 15 additions & 18 deletions frontends/concrete-python/examples/tfhers-ml/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ def keygen(output_secret_key: str, secret_key: str, concrete_keyset_path: str):
print("full keygen")
circuit.keygen()

print("saving Concrete keyset")
circuit.client.keys.save(concrete_keyset_path)
print(f"saved Concrete keyset to '{concrete_keyset_path}'")
print("saving Concrete Evaluation Keys")
with open(concrete_keyset_path, "wb") as f:
f.write(circuit.client.evaluation_keys.serialize())
print(f"saved Concrete Evaluation Keys to '{concrete_keyset_path}'")

sk: bytes = tfhers_bridge.serialize_input_secret_key(input_idx=0)
print(f"writing secret key of size {len(sk)} to '{output_secret_key}'")
Expand All @@ -142,7 +143,7 @@ def keygen(output_secret_key: str, secret_key: str, concrete_keyset_path: str):

@cli.command()
@click.option("-c", "--rust-ct", type=str, required=True)
@click.option("-o", "--output-rust-ct", type=str, required=False)
@click.option("-o", "--output-rust-ct", type=str, required=True)
@click.option("-k", "--concrete-keyset-path", type=str, required=True)
def run(rust_ct: str, output_rust_ct: str, concrete_keyset_path: str):
"""Run circuit"""
Expand All @@ -151,7 +152,8 @@ def run(rust_ct: str, output_rust_ct: str, concrete_keyset_path: str):
if not os.path.exists(concrete_keyset_path):
raise RuntimeError("cannot find keys, you should run keygen before")
print(f"loading keys from '{concrete_keyset_path}'")
circuit.client.keys.load(concrete_keyset_path)
with open(concrete_keyset_path, "rb") as f:
eval_keys = fhe.EvaluationKeys.deserialize(f.read())

# read tfhers int from file
with open(rust_ct, "rb") as f:
Expand All @@ -160,19 +162,14 @@ def run(rust_ct: str, output_rust_ct: str, concrete_keyset_path: str):
tfhers_uint8_x = tfhers_bridge.import_value(buff, input_idx=0)

print("Homomorphic evaluation...")
encrypted_result = circuit.run(tfhers_uint8_x)

if output_rust_ct:
print("exporting Rust ciphertexts")
# export fheuint8
buff = tfhers_bridge.export_value(encrypted_result, output_idx=0)
# write it to file
with open(output_rust_ct, "wb") as f:
f.write(buff)
else:
result = circuit.decrypt(encrypted_result)
decoded = tfhers_type.decode(result)
print(f"Concrete decryption result: raw({result}), decoded({decoded})")
encrypted_result = circuit.server.run(tfhers_uint8_x, evaluation_keys=eval_keys)

print("exporting Rust ciphertexts")
# export fheuint8
buff = tfhers_bridge.export_value(encrypted_result, output_idx=0)
# write it to file
with open(output_rust_ct, "wb") as f:
f.write(buff)


if __name__ == "__main__":
Expand Down
35 changes: 16 additions & 19 deletions frontends/concrete-python/examples/tfhers/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import click
import numpy as np

from concrete import fhe
import concrete.fhe as fhe
from concrete.fhe import tfhers

### Options ###########################
Expand Down Expand Up @@ -87,9 +87,10 @@ def keygen(output_secret_key: str, secret_key: str, concrete_keyset_path: str):
print("full keygen")
circuit.keygen()

print(f"saving Concrete keyset")
circuit.client.keys.save(concrete_keyset_path)
print(f"saved Concrete keyset to '{concrete_keyset_path}'")
print("saving Concrete Evaluation Keys")
with open(concrete_keyset_path, "wb") as f:
f.write(circuit.client.evaluation_keys.serialize())
print(f"saved Concrete Evaluation Keys to '{concrete_keyset_path}'")

sk: bytes = tfhers_bridge.serialize_input_secret_key(input_idx=0)
print(f"writing secret key of size {len(sk)} to '{output_secret_key}'")
Expand All @@ -100,7 +101,7 @@ def keygen(output_secret_key: str, secret_key: str, concrete_keyset_path: str):
@cli.command()
@click.option("-c1", "--rust-ct-1", type=str, required=True)
@click.option("-c2", "--rust-ct-2", type=str, required=True)
@click.option("-o", "--output-rust-ct", type=str, required=False)
@click.option("-o", "--output-rust-ct", type=str, required=True)
@click.option("-k", "--concrete-keyset-path", type=str, required=True)
def run(rust_ct_1: str, rust_ct_2: str, output_rust_ct: str, concrete_keyset_path: str):
"""Run circuit"""
Expand All @@ -109,7 +110,8 @@ def run(rust_ct_1: str, rust_ct_2: str, output_rust_ct: str, concrete_keyset_pat
if not os.path.exists(concrete_keyset_path):
raise RuntimeError("cannot find keys, you should run keygen before")
print(f"loading keys from '{concrete_keyset_path}'")
circuit.client.keys.load(concrete_keyset_path)
with open(concrete_keyset_path, "rb") as f:
eval_keys = fhe.EvaluationKeys.deserialize(f.read())

# read tfhers int from file
with open(rust_ct_1, "rb") as f:
Expand All @@ -125,20 +127,15 @@ def run(rust_ct_1: str, rust_ct_2: str, output_rust_ct: str, concrete_keyset_pat

encrypted_x, encrypted_y = tfhers_uint8_x, tfhers_uint8_y

print(f"Homomorphic evaluation...")
encrypted_result = circuit.run(encrypted_x, encrypted_y)
print("Homomorphic evaluation...")
encrypted_result = circuit.server.run(encrypted_x, encrypted_y, evaluation_keys=eval_keys)

if output_rust_ct:
print("exporting Rust ciphertexts")
# export fheuint8
buff = tfhers_bridge.export_value(encrypted_result, output_idx=0)
# write it to file
with open(output_rust_ct, "wb") as f:
f.write(buff)
else:
result = circuit.decrypt(encrypted_result)
decoded = tfhers_type.decode(result)
print(f"Concrete decryption result: raw({result}), decoded({decoded})")
print("exporting Rust ciphertexts")
# export fheuint8
buff = tfhers_bridge.export_value(encrypted_result, output_idx=0)
# write it to file
with open(output_rust_ct, "wb") as f:
f.write(buff)


if __name__ == "__main__":
Expand Down

0 comments on commit 66b5c05

Please sign in to comment.