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

Commit

Permalink
chore: fix decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-mori3322 committed May 23, 2024
1 parent c41d00a commit 3891225
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
20 changes: 15 additions & 5 deletions fhevm/tee_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,30 @@ func teeOptimisticRequireRun(environment EVMEnvironment, caller common.Address,
// one 0, the result will be 0 (false).
func teeEvaluateRemainingOptimisticRequires(environment EVMEnvironment) (bool, error) {
requires := environment.FhevmData().optimisticRequires
len := len(requires)
length := len(requires)
defer func() { environment.FhevmData().resetTeeOptimisticRequires() }()
if len != 0 {
if length != 0 {
var cumulative *tfhe.TfheCiphertext = requires[0]
var err error
for i := 1; i < len; i++ {
for i := 1; i < length; i++ {
cumulative, err = cumulative.Bitand(requires[i])
if err != nil {
environment.GetLogger().Error("evaluateRemainingOptimisticRequires bitand failed", "err", err)
return false, err
}
}
result, err := decryptValue(environment, cumulative)
return result != 0, err
result, err := tee.Decrypt(cumulative)
if err != nil {
return false, err
}

plaintext := result.Value
// Always return a 32-byte big-endian integer.
ret := make([]byte, 32)
copy(ret[32-len(plaintext):], plaintext)

retVal := *new(big.Int).SetBytes(ret)
return retVal.Uint64() != 0, err
}
return true, nil
}
38 changes: 38 additions & 0 deletions fhevm/tee_crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,41 @@ func TestTeeDecryptRun(t *testing.T) {
}
})
}

func TestTeeOptimisticRequire(t *testing.T) {
signature := "teeOptimisticRequire(uint256)"
// func teeOptimisticRequireRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) {

rapid.Check(t, func(t *rapid.T) {
testcases := []struct {
typ tfhe.FheUintType
require uint64
}{
{tfhe.FheUint8, uint64(1)},
{tfhe.FheUint8, uint64(1)},
}

environment := newTestEVMEnvironment()
depth := 1
environment.depth = depth
for _, tc := range testcases {
addr := common.Address{}
readOnly := false
ct, err := importTeePlaintextToEVM(environment, depth, tc.require, tc.typ)
if err != nil {
t.Fatalf(err.Error())
}

input := toLibPrecompileInput(signature, false, ct.GetHash())
_, err = FheLibRun(environment, addr, addr, input, readOnly)
if err != nil {
t.Fatalf(err.Error())
}
}

optReqResult, _ := teeEvaluateRemainingOptimisticRequires(environment)
if !optReqResult {
t.Fatalf("incorrect result, expected=%d, got=0", 1)
}
})
}

0 comments on commit 3891225

Please sign in to comment.