-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
187 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ tmp_directory_for_cml_tests | |
|
||
# Temp file | ||
frontends/concrete-python/examples/sha1/tmp_sha1_test_file.txt | ||
|
||
.venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
frontends/concrete-python/examples/tfhers-ml/demo/01_setup_tempdir.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
echo "Setting up working directory..." | ||
start_time=$(date +%s) | ||
|
||
mkdir -p "$TDIR" | ||
echo "Directory created at: $TDIR" | ||
|
||
end_time=$(date +%s) | ||
echo "Setup completed in $(($end_time - $start_time)) seconds." |
11 changes: 11 additions & 0 deletions
11
frontends/concrete-python/examples/tfhers-ml/demo/02_build_tfhe_rs.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Building TFHE-rs utilities..." | ||
start_time=$(date +%s) | ||
|
||
cd ../../../tests/tfhers-utils/ || exit 1 | ||
make build || exit 1 | ||
cd - || exit 1 | ||
|
||
end_time=$(date +%s) | ||
echo "TFHE-rs utilities built in $(($end_time - $start_time)) seconds." |
60 changes: 60 additions & 0 deletions
60
frontends/concrete-python/examples/tfhers-ml/demo/03_keygen.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
|
||
# Function to generate keys starting from Concrete | ||
generate_concrete_keys() { | ||
if [[ -f $TDIR/concrete_sk && -f $TDIR/concrete_keyset && -f $TDIR/tfhers_sk && -f $TDIR/tfhers_client_key && -f $TDIR/tfhers_server_key ]]; then | ||
echo "Keys already exist. Skipping key generation for Concrete." | ||
return | ||
fi | ||
echo "Generating secret key in Concrete..." | ||
start_time=$(date +%s) | ||
|
||
python ../example.py keygen -o $TDIR/concrete_sk -k $TDIR/concrete_keyset || exit 1 | ||
../../../tests/tfhers-utils/target/release/tfhers_utils keygen \ | ||
--lwe-sk $TDIR/concrete_sk \ | ||
--output-lwe-sk $TDIR/tfhers_sk \ | ||
-c $TDIR/tfhers_client_key \ | ||
-s $TDIR/tfhers_server_key || exit 1 | ||
|
||
end_time=$(date +%s) | ||
echo "Key generation (Concrete) completed in $(($end_time - $start_time)) seconds." | ||
} | ||
|
||
# Function to generate keys starting from TFHE-rs | ||
generate_tfhers_keys() { | ||
if [[ -f $TDIR/tfhers_sk && -f $TDIR/tfhers_client_key && -f $TDIR/tfhers_server_key && -f $TDIR/concrete_sk && -f $TDIR/concrete_keyset ]]; then | ||
echo "Keys already exist. Skipping key generation for TFHE-rs." | ||
return | ||
fi | ||
echo "Generating secret key in TFHE-rs..." | ||
start_time=$(date +%s) | ||
|
||
../../../tests/tfhers-utils/target/release/tfhers_utils keygen \ | ||
--output-lwe-sk $TDIR/tfhers_sk \ | ||
-c $TDIR/tfhers_client_key \ | ||
-s $TDIR/tfhers_server_key || exit 1 | ||
python ../example.py keygen -s $TDIR/tfhers_sk -o $TDIR/concrete_sk -k $TDIR/concrete_keyset || exit 1 | ||
|
||
end_time=$(date +%s) | ||
echo "Key generation (TFHE-rs) completed in $(($end_time - $start_time)) seconds." | ||
} | ||
|
||
# Ask user which method to use | ||
echo "Choose key generation method:" | ||
echo "1) Generate keys starting from Concrete" | ||
echo "2) Generate keys starting from TFHE-rs" | ||
read -p "Enter choice (1 or 2): " choice | ||
|
||
case $choice in | ||
1) | ||
generate_concrete_keys | ||
;; | ||
2) | ||
generate_tfhers_keys | ||
;; | ||
*) | ||
echo "Invalid choice. Exiting." | ||
exit 1 | ||
;; | ||
esac |
10 changes: 10 additions & 0 deletions
10
frontends/concrete-python/examples/tfhers-ml/demo/04_quantize_values.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Quantizing input values..." | ||
|
||
../../../tests/tfhers-utils/target/release/tfhers_utils quantize \ | ||
--value=5.1,3.5,1.4,0.2,4.9,3,1.4,0.2,4.7,3.2,1.3,0.2,4.6,3.1,1.5,0.2,5,3.6,1.4,0.2 \ | ||
--config ../input_quantizer.json \ | ||
-o $TDIR/quantized_values || exit 1 | ||
|
||
echo "Quantized values saved to: $TDIR/quantized_values" |
11 changes: 11 additions & 0 deletions
11
frontends/concrete-python/examples/tfhers-ml/demo/05_encrypt_tfhe_rs.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Encrypting quantized values with TFHE-rs..." | ||
|
||
../../../tests/tfhers-utils/target/release/tfhers_utils encrypt-with-key \ | ||
--signed \ | ||
--value=$(cat $TDIR/quantized_values) \ | ||
--ciphertext $TDIR/tfhers_ct \ | ||
--client-key $TDIR/tfhers_client_key || exit 1 | ||
|
||
echo "Ciphertext saved to: $TDIR/tfhers_ct" |
10 changes: 10 additions & 0 deletions
10
frontends/concrete-python/examples/tfhers-ml/demo/06_run_concrete.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Running computation in Concrete..." | ||
start_time=$(date +%s) | ||
|
||
python ../example.py run -k $TDIR/concrete_keyset -c $TDIR/tfhers_ct -o $TDIR/tfhers_ct_out || exit 1 | ||
|
||
echo "Computation output saved to: $TDIR/tfhers_ct_out" | ||
end_time=$(date +%s) | ||
echo "Computation completed in $(($end_time - $start_time)) seconds." |
12 changes: 12 additions & 0 deletions
12
frontends/concrete-python/examples/tfhers-ml/demo/07_decrypt_tfhe_rs.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Decrypting output with TFHE-rs..." | ||
|
||
../../../tests/tfhers-utils/target/release/tfhers_utils decrypt-with-key \ | ||
--tensor \ | ||
--signed \ | ||
--ciphertext $TDIR/tfhers_ct_out \ | ||
--client-key $TDIR/tfhers_client_key \ | ||
--plaintext $TDIR/result_plaintext || exit 1 | ||
|
||
echo "Decrypted plaintext saved to: $TDIR/result_plaintext" |
8 changes: 8 additions & 0 deletions
8
frontends/concrete-python/examples/tfhers-ml/demo/08_rescale_output.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Rescaling output values..." | ||
|
||
rescaled_values=$(python -c "print(','.join(map(lambda x: str(int(x) << 10), [$(cat $TDIR/result_plaintext)])))" || exit 1) | ||
echo "$rescaled_values" > $TDIR/rescaled_plaintext || exit 1 | ||
|
||
echo "Rescaled plaintext saved to: $TDIR/rescaled_plaintext" |
12 changes: 12 additions & 0 deletions
12
frontends/concrete-python/examples/tfhers-ml/demo/09_dequantize_values.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
echo "Dequantizing rescaled values and print expected values..." | ||
|
||
../../../tests/tfhers-utils/target/release/tfhers_utils dequantize \ | ||
--value=$(cat $TDIR/rescaled_plaintext) \ | ||
--shape=5,3 \ | ||
--config ../output_quantizer.json || exit 1 | ||
|
||
echo | ||
echo "Expected values:" | ||
jq -r '.dequantized_predictions[] | @csv' ../test_values.json |
Empty file.
37 changes: 37 additions & 0 deletions
37
frontends/concrete-python/examples/tfhers-ml/demo/run_demo.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/bin/bash | ||
set -e # Exit on any error | ||
|
||
export TDIR="./my_temp_dir" | ||
|
||
# Function to execute a step and check its result | ||
run_step() { | ||
local step=$1 | ||
local description=$2 | ||
|
||
echo "================================================================" | ||
echo "$description" | ||
read -r | ||
|
||
# Source the script instead of running it in a subshell | ||
source "./${step}_"*.sh | ||
if [ $? -ne 0 ]; then | ||
echo "Step $step failed! Aborting..." | ||
exit 1 | ||
fi | ||
echo "================================================================" | ||
echo | ||
} | ||
|
||
# Main demo sequence | ||
echo "Starting demo sequence..." | ||
echo | ||
|
||
# run_step "01" "Setup temporary directory" | ||
# run_step "02" "Build TFHE-rs utilities" | ||
run_step "03" "Generate keys" | ||
run_step "04" "Quantize values" | ||
run_step "05" "Encrypt with TFHE-rs" | ||
run_step "06" "Run computation in Concrete" | ||
run_step "07" "Decrypt with TFHE-rs" | ||
run_step "08" "Rescale output" | ||
run_step "09" "Dequantize values" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters