108 points · by zacchj · 3y ago · 22 comments
# Now we train in the clear and quantize the weights
model = LogisticRegression(n_bits=8)
model.fit(X_train, y_train)
# We can simulate the predictions in the clear
y_pred_clear = model.predict(X_test)
# We then compile on a representative set
model.compile(X_train)
# Finally we run the inference on encrypted inputs !
y_pred_fhe = model.predict(X_test, fhe="execute")
print("In clear :", y_pred_clear)
print("In FHE :", y_pred_fhe)
print(f"Similarity: {int((y_pred_fhe == y_pred_clear).mean()*100)}%")
There’s some ways to go on performance, but the ergonomics of using FHE are already pretty good!``` from concrete import fhe
def add(x, y): return x + y
compiler = fhe.Compiler(add, {"x": "encrypted", "y": "encrypted"}) inputset = [(2, 3), (0, 0), (1, 6), (7, 7), (7, 1), (3, 2), (6, 1), (1, 7), (4, 5), (5, 4)]
print(f"Compiling...") circuit = compiler.compile(inputset)
print(f"Generating keys...") circuit.keygen()
examples = [(3, 4), (1, 2), (7, 7), (0, 0)] for example in examples: encrypted_example = circuit.encrypt(*example) encrypted_result = circuit.run(encrypted_example) result = circuit.decrypt(encrypted_result) print(f"Evaluation of {' + '.join(map(str, example))} homomorphically = {result}") ```
Here, that's more for non-ML computations.
That being said you could probably do the reverse and encrypt the parameters of the model and not the inputs/outputs if you are deploying the model directly to the client.
[1] https://github.com/mmastrac/oblivious-cpu [2] https://hcrypt.com/shape-cpu/
FHE becomes a lot more interesting when you can hide the structure of your computation behind a VM.
https://jeremykun.com/2023/02/13/googles-fully-homomorphic-e...
It's a really fun write up. I prefer the syntax of Google's. But Zama is doing great work.
I really enjoyed the podcast with them here. It clarified a lot for me about the intersection of FHE and ZK.
Basically you'd write your algorithms in C++ but instead of using the built-in types like int or float you'd use custom types that had all of their operators overloaded. Your code would look pretty similar to what you'd have before (modulo the type definitions) but when compiled your algorithm would turn into an incredibly inscrutable state machine where some parts of the state machine would come from some kind of protection dongle. Pretty effective.
https://github.com/zama-ai/concrete-ml#a-simple-concrete-ml-...
In the meantime if you want a good introduction to the FHE scheme we use behind the scene, you can take a look here: https://www.zama.ai/post/tfhe-deep-dive-part-1