Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
chore: refactor deserialization of decrypted values
Browse files Browse the repository at this point in the history
  • Loading branch information
leventdem committed Mar 18, 2024
1 parent 4beb114 commit c3abd74
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 78 deletions.
45 changes: 1 addition & 44 deletions fhevm/operators_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -292,49 +291,7 @@ func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (*big.Int
plaintextBytes := res.Plaintext

// Variable to hold the resulting big.Int
var plaintextBigInt *big.Int

switch fheType {
case kms.FheType_Bool, kms.FheType_Euint4, kms.FheType_Euint8:

if len(plaintextBytes) > 0 {
plaintextBigInt = big.NewInt(int64(plaintextBytes[0]))
} else {
return nil, errors.New("decryption resulted in empty plaintext for a single-byte FheType")
}
case kms.FheType_Euint16:
// For Euint16, ensure plaintextBytes has at least 2 bytes.
if len(plaintextBytes) >= 2 {
// Use binary.BigEndian.Uint16 to convert bytes to uint16, then to big.Int.
uintVal := binary.BigEndian.Uint16(plaintextBytes)
plaintextBigInt = big.NewInt(int64(uintVal))
} else {
return nil, errors.New("decryption resulted in insufficient bytes for FheType_Euint16")
}
case kms.FheType_Euint32:
// Similar to Euint16, but with 4 bytes to uint32.
if len(plaintextBytes) >= 4 {
uintVal := binary.BigEndian.Uint32(plaintextBytes)
plaintextBigInt = big.NewInt(int64(uintVal))
} else {
return nil, errors.New("decryption resulted in insufficient bytes for FheType_Euint32")
}
case kms.FheType_Euint64:
// For Euint64, ensure there are 8 bytes to work with.
if len(plaintextBytes) >= 8 {
uintVal := binary.BigEndian.Uint64(plaintextBytes)
plaintextBigInt = new(big.Int).SetUint64(uintVal)
} else {
return nil, errors.New("decryption resulted in insufficient bytes for FheType_Euint64")
}
case kms.FheType_Euint160:
logger.Info("decrypt success", "plaintextBytes", plaintextBytes)
logger.Info("decrypt success", "plaintextBytes", fmt.Sprintf("%v", plaintextBytes))
// Special handling for FheUint160, already covered.
plaintextBigInt, err = tfhe.U256BytesToBigInt(plaintextBytes)
default:
return nil, fmt.Errorf("unsupported FheType: %v", fheType)
}
plaintextBigInt := new(big.Int).SetBytes(plaintextBytes)

return plaintextBigInt, nil

Expand Down
34 changes: 0 additions & 34 deletions fhevm/tfhe/tfhe_wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import "C"

import (
_ "embed"
"encoding/binary"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -135,36 +134,3 @@ func u256ToBigInt(u256 C.U256) *big.Int {

return new(big.Int).SetBytes(buf)
}

// U256BytesToBigInt takes a 32-byte big-endian slice and returns a big.Int.
func U256BytesToBigInt(plaintextBytes []byte) (*big.Int, error) {
if len(plaintextBytes) != 32 {
return nil, fmt.Errorf("byte slice is not the correct length for U256: got %d bytes, want 32", len(plaintextBytes))
}

// Split the byte slice into four u64 parts considering big-endian encoding
w0 := binary.BigEndian.Uint64(plaintextBytes[0:8])
w1 := binary.BigEndian.Uint64(plaintextBytes[8:16])
w2 := binary.BigEndian.Uint64(plaintextBytes[16:24])
w3 := binary.BigEndian.Uint64(plaintextBytes[24:32])

// Print the u64 parts for verification
// fmt.Printf("U256\n")
// fmt.Printf("w0: %d\n", w0)
// fmt.Printf("w1: %d\n", w1)
// fmt.Printf("w2: %d\n", w2)
// fmt.Printf("w3: %d\n", w3)

// Combine the u64 parts into low and high u128 parts to construct the big.Int
low := new(big.Int).SetUint64(w0)
low.Or(low, new(big.Int).Lsh(new(big.Int).SetUint64(w1), 64))

high := new(big.Int).SetUint64(w2)
high.Or(high, new(big.Int).Lsh(new(big.Int).SetUint64(w3), 64))

// Shift the high part by 128 bits to the left and add it to the low part
bigIntValue := new(big.Int).Lsh(high, 128)
bigIntValue.Add(bigIntValue, low)

return bigIntValue, nil
}

0 comments on commit c3abd74

Please sign in to comment.