This file contains hidden or 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
| import os | |
| from Crypto.Cipher import AES | |
| # Can be 16, 24, or 32 bytes | |
| KEY = b"A"*16 | |
| ############ | |
| # ECB mode # | |
| ############ | |
| aes_ecb = AES.new(KEY, mode=AES.MODE_ECB) |
This file contains hidden or 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
| evaluate("Encryption is awesome") | |
| + the sentence was positive (original: 99.99%, simulated: 99.99%, actual: 99.99%, difference: 0.00%, took: 33.813 seconds) |
This file contains hidden or 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
| def evaluate(sentence): | |
| try: | |
| embedded = encode(sentence) | |
| except KeyError as error: | |
| print("! the word", error, "is unknown") | |
| return | |
| if embedded.shape[0] > SENTENCE_LENGTH_LIMIT: | |
| print(f"! the sentence should not contain more than {SENTENCE_LENGTH_LIMIT} tokens") | |
| return | |
| padded = np.zeros((SENTENCE_LENGTH_LIMIT, 300)) |
This file contains hidden or 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
| context = homomorphic_inferer.create_context() | |
| keys = context.keygen() |
This file contains hidden or 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
| SENTENCE_LENGTH_LIMIT = 5 | |
| inferer = Inferer(model) | |
| homomorphic_inferer = hnp.compile_fhe( | |
| inferer.infer, | |
| { | |
| "x": hnp.encrypted_ndarray(bounds=(-1, 1), shape=(SENTENCE_LENGTH_LIMIT, 300)) | |
| }, | |
| config=hnp.config.CompilationConfig( |
This file contains hidden or 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
| class Inferer: | |
| def __init__(self, model): | |
| parameters = list(model.lstm.parameters()) | |
| W_ii, W_if, W_ig, W_io = parameters[0].split(HIDDEN_SIZE) | |
| W_hi, W_hf, W_hg, W_ho = parameters[1].split(HIDDEN_SIZE) | |
| b_ii, b_if, b_ig, b_io = parameters[2].split(HIDDEN_SIZE) | |
| b_hi, b_hf, b_hg, b_ho = parameters[3].split(HIDDEN_SIZE) | |
| self.W_ii = W_ii.detach().numpy() |
This file contains hidden or 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
| HIDDEN_SIZE = 100 | |
| class Model(torch.nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.lstm = torch.nn.LSTM(input_size=300, hidden_size=HIDDEN_SIZE) | |
| self.fc = torch.nn.Linear(HIDDEN_SIZE, 1) | |
| self.sigmoid = torch.nn.Sigmoid() |
This file contains hidden or 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
| x_enc= keys.encrypt(x) | |
| res = h.run(keys.public_keys, x_enc) | |
| print(f"Encrypted computation result: {keys.decrypt(res)}") |
This file contains hidden or 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
| print(f"Encrypted computation result: {h.encrypt_and_run(keys, x)}") |
This file contains hidden or 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
| ctx = h.create_context() | |
| keys = ctx.keygen() |
NewerOlder