Skip to content

Commit

Permalink
Merge pull request ethereum#62 from zama-ai/louis-patch-cks-bug
Browse files Browse the repository at this point in the history
fix(tfhe): warn when key file not found

Patch the bug where a full node would silently fail when not finding the client key `cks`.
Nodes now only look for the client key `cks` if they are configured in `oracle` mode and emit a warning when either the server key `sks` or the client key `cks` is not found.
  • Loading branch information
tremblaythibaultl authored Mar 24, 2023
2 parents a3fc1a7 + f1cd803 commit 64bc5ba
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions core/vm/tfhe.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ void* trivial_encrypt(void* sks, uint64_t value) {
return ct;
}
size_t get_message_modulus(void* cks) {
size_t get_message_modulus(void* sks) {
size_t modulus = 0;
const int r = shortint_client_key_get_message_modulus(cks, &modulus);
const int r = shortint_server_key_get_message_modulus(sks, &modulus);
assert(r == 0);
return modulus;
Expand All @@ -167,8 +167,10 @@ import "C"
import (
"crypto/rand"
"errors"
"fmt"
"os"
"runtime"
"strings"
"sync/atomic"
"time"
"unsafe"
Expand Down Expand Up @@ -223,24 +225,27 @@ func init() {

sks_bytes, err := os.ReadFile(networkKeysDir + "sks")
if err != nil {
fmt.Print("WARNING: file sks not found.\n")
return
}
sks = C.deserialize_server_key(toBufferView(sks_bytes))

cks_bytes, err := os.ReadFile(networkKeysDir + "cks")
if err != nil {
return
if strings.ToLower(tomlConfig.Oracle.Mode) == "oracle" {
cks_bytes, err := os.ReadFile(networkKeysDir + "cks")
if err != nil {
fmt.Print("WARNING: file cks not found.\n")
return
}
cks = C.deserialize_client_key(toBufferView(cks_bytes))
}

sks = C.deserialize_server_key(toBufferView(sks_bytes))
cks = C.deserialize_client_key(toBufferView(cks_bytes))

// Use trivial encryption to determine the ciphertext size for the used parameters.
// Note: parameters are embedded in the client `cks` key.
ct := new(tfheCiphertext)
ct.trivialEncrypt(1)
fheCiphertextSize = len(ct.serialize())

fheMessageModulus = uint64(C.get_message_modulus(cks))
fheMessageModulus = uint64(C.get_message_modulus(sks))

go runGc()
}
Expand Down

0 comments on commit 64bc5ba

Please sign in to comment.