Skip to content

Commit

Permalink
Merge pull request ethereum#18 from zama-ai/feature/add_fhe_public_en…
Browse files Browse the repository at this point in the history
…cryption

feature(reencypt): add public fhe encryption
  • Loading branch information
leventdem authored Dec 20, 2022
2 parents fe3593d + af92b28 commit df9eeaf
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ uint64_t decrypt_integer(BufferView cks_buf_view, BufferView ct_buf_view)
return res;
}
void public_encrypt_integer(BufferView pks_buff_view, uint64_t val, Buffer* ct_buf)
{
ShortintCiphertext *ct = NULL;
ShortintPublicKey *pks = NULL;
int deser_ok = shortint_deserialize_public_key(pks_buff_view, &pks);
assert(deser_ok == 0);
int encrypt_ok = shortint_public_key_encrypt(pks, val, &ct);
assert(encrypt_ok == 0);
int ser_ok = shortint_serialize_ciphertext(ct, ct_buf);
assert(ser_ok == 0);
destroy_shortint_public_key(pks);
destroy_shortint_ciphertext(ct);
}
*/
import "C"

Expand Down Expand Up @@ -1357,13 +1375,13 @@ func fheDecrypt(input []byte) (uint64, error) {
return uint64(decryted_value), nil
}

func fheEncryptToUserKey(value uint64, userAddress common.Address) ([]byte, error) {
func fheEncryptToNetworkKey(value uint64) ([]byte, error) {
if value > 15 {
return nil, errors.New("input must be less than 15")
}

userPublicKey := strings.ToLower(usersKeysDir + userAddress.Hex())
cks, err := os.ReadFile(userPublicKey)
networkKey := strings.ToLower(networkKeysDir + "cks")
cks, err := os.ReadFile(networkKey)
if err != nil {
return nil, err
}
Expand All @@ -1390,35 +1408,35 @@ func fheEncryptToUserKey(value uint64, userAddress common.Address) ([]byte, erro
return ctBytes, nil
}

func fheEncryptToNetworkKey(value uint64) ([]byte, error) {
func fheEncryptToUserKey(value uint64, userAddress common.Address) (ret []byte, err error) {
if value > 15 {
return nil, errors.New("input must be less than 15")
}

networkKey := strings.ToLower(networkKeysDir + "cks")
cks, err := os.ReadFile(networkKey)
userPublicKey := strings.ToLower(usersKeysDir + userAddress.Hex())
pks, err := os.ReadFile(userPublicKey)
if err != nil {
return nil, err
}

cServerKey := C.CBytes(cks)
viewServerKey := C.BufferView{
pointer: (*C.uchar)(cServerKey),
length: (C.ulong)(len(cks)),
cPublicKey := C.CBytes(pks)
viewPublicKey := C.BufferView{
pointer: (*C.uchar)(cPublicKey),
length: (C.ulong)(len(pks)),
}

result := &C.Buffer{}
C.encrypt_integer(viewServerKey, C.ulong(value), result)
C.public_encrypt_integer(viewPublicKey, C.ulong(value), result)

ctBytes := C.GoBytes(unsafe.Pointer(result.pointer), C.int(result.length))

// TODO: for testing
err = os.WriteFile("/tmp/encrypt_result", ctBytes, 0644)
err = os.WriteFile("/tmp/public_encrypt_result", ctBytes, 0644)
if err != nil {
return nil, err
}

C.free(cServerKey)
C.free(cPublicKey)

return ctBytes, nil
}
Expand Down

0 comments on commit df9eeaf

Please sign in to comment.