Skip to content

Commit

Permalink
fix: call otel describe functions after err checks
Browse files Browse the repository at this point in the history
Make sure we call otel functions that describe operand types after doing
a check for an error. That avoids a possible nil pointer dereference.

Rename `ciphertextStorage` to `CiphertextStorageAddress` such that it is
public and can be used by clients.
  • Loading branch information
dartdart26 committed Aug 12, 2024
1 parent 98d95ba commit 06e7045
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
16 changes: 8 additions & 8 deletions fhevm/ciphertext_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/zama-ai/fhevm-go/fhevm/tfhe"
)

var ciphertextStorage = common.BytesToAddress([]byte{94})
var CiphertextStorageAddress = common.BytesToAddress([]byte{94})

func newInt(buf []byte) *uint256.Int {
i := uint256.NewInt(0)
Expand Down Expand Up @@ -50,13 +50,13 @@ func newCiphertextMetadata(buf [32]byte) *ciphertextMetadata {
}

func isCiphertextPersisted(env EVMEnvironment, handle common.Hash) bool {
metadataInt := newInt(env.GetState(ciphertextStorage, handle).Bytes())
metadataInt := newInt(env.GetState(CiphertextStorageAddress, handle).Bytes())
return !metadataInt.IsZero()
}

// Returns the ciphertext metadata for the given handle or nil if it doesn't point to a ciphertext.
func loadCiphertextMetadata(env EVMEnvironment, handle common.Hash) *ciphertextMetadata {
metadataInt := newInt(env.GetState(ciphertextStorage, handle).Bytes())
metadataInt := newInt(env.GetState(CiphertextStorageAddress, handle).Bytes())
if metadataInt.IsZero() {
return nil
}
Expand All @@ -73,7 +73,7 @@ func loadCiphertext(env EVMEnvironment, handle common.Hash) (ct *tfhe.TfheCipher
return ct, 0
}

metadataInt := newInt(env.GetState(ciphertextStorage, handle).Bytes())
metadataInt := newInt(env.GetState(CiphertextStorageAddress, handle).Bytes())
if metadataInt.IsZero() {
return nil, ColdSloadCostEIP2929
}
Expand All @@ -83,7 +83,7 @@ func loadCiphertext(env EVMEnvironment, handle common.Hash) (ct *tfhe.TfheCipher
idx := newInt(handle.Bytes())
idx.AddUint64(idx, 1)
for left > 0 {
bytes := env.GetState(ciphertextStorage, idx.Bytes32())
bytes := env.GetState(CiphertextStorageAddress, idx.Bytes32())
toAppend := minUint64(uint64(len(bytes)), left)
left -= toAppend
ctBytes = append(ctBytes, bytes[0:toAppend]...)
Expand Down Expand Up @@ -117,7 +117,7 @@ func persistCiphertext(env EVMEnvironment, handle common.Hash, ct *tfhe.TfheCiph
metadata.fheUintType = ct.Type()

// Persist the metadata in storage.
env.SetState(ciphertextStorage, handle, metadata.serialize())
env.SetState(CiphertextStorageAddress, handle, metadata.serialize())

ciphertextSlot := newInt(handle.Bytes())
ciphertextSlot.AddUint64(ciphertextSlot, 1)
Expand All @@ -133,7 +133,7 @@ func persistCiphertext(env EVMEnvironment, handle common.Hash, ct *tfhe.TfheCiph
ctBytes := ct.Serialize()
for i, b := range ctBytes {
if i%32 == 0 && i != 0 {
env.SetState(ciphertextStorage, ciphertextSlot.Bytes32(), common.BytesToHash(ctPart32))
env.SetState(CiphertextStorageAddress, ciphertextSlot.Bytes32(), common.BytesToHash(ctPart32))
ciphertextSlot.AddUint64(ciphertextSlot, 1)
ctPart32 = make([]byte, 32)
partIdx = 0
Expand All @@ -142,7 +142,7 @@ func persistCiphertext(env EVMEnvironment, handle common.Hash, ct *tfhe.TfheCiph
partIdx++
}
if len(ctPart32) != 0 {
env.SetState(ciphertextStorage, ciphertextSlot.Bytes32(), common.BytesToHash(ctPart32))
env.SetState(CiphertextStorageAddress, ciphertextSlot.Bytes32(), common.BytesToHash(ctPart32))
}
}

Expand Down
16 changes: 8 additions & 8 deletions fhevm/operators_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Ad

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheAdd failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheAdd operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -50,11 +50,11 @@ func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Ad

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheAdd scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -87,11 +87,11 @@ func fheSubRun(environment EVMEnvironment, caller common.Address, addr common.Ad

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheSub failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheSub operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -116,11 +116,11 @@ func fheSubRun(environment EVMEnvironment, caller common.Address, addr common.Ad

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheSub scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -153,11 +153,11 @@ func fheMulRun(environment EVMEnvironment, caller common.Address, addr common.Ad

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheMul failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheMul operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -182,11 +182,11 @@ func fheMulRun(environment EVMEnvironment, caller common.Address, addr common.Ad

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheMul scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -223,11 +223,11 @@ func fheDivRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return nil, err
} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheDiv scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -264,11 +264,11 @@ func fheRemRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return nil, err
} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheRem scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down
22 changes: 11 additions & 11 deletions fhevm/operators_bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func fheShlRun(environment EVMEnvironment, caller common.Address, addr common.Ad

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheShl failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheShl operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -50,11 +50,11 @@ func fheShlRun(environment EVMEnvironment, caller common.Address, addr common.Ad

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheShl scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -87,11 +87,11 @@ func fheShrRun(environment EVMEnvironment, caller common.Address, addr common.Ad

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheShr failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheShr operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -116,11 +116,11 @@ func fheShrRun(environment EVMEnvironment, caller common.Address, addr common.Ad

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheShr scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -153,11 +153,11 @@ func fheRotlRun(environment EVMEnvironment, caller common.Address, addr common.A

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheShl failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheShl operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -182,11 +182,11 @@ func fheRotlRun(environment EVMEnvironment, caller common.Address, addr common.A

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheRotl scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -219,11 +219,11 @@ func fheRotrRun(environment EVMEnvironment, caller common.Address, addr common.A

if !isScalar {
lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheRotr failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if lhs.Type() != rhs.Type() {
msg := "fheRotr operand type mismatch"
logger.Error(msg, "lhs", lhs.Type(), "rhs", rhs.Type())
Expand All @@ -248,11 +248,11 @@ func fheRotrRun(environment EVMEnvironment, caller common.Address, addr common.A

} else {
lhs, rhs, _, err := getScalarOperands(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))
if err != nil {
logger.Error("fheRotr scalar failed to load inputs", "err", err, "input", hex.EncodeToString(input))
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs))

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
Expand Down Expand Up @@ -364,11 +364,11 @@ func fheBitAndRun(environment EVMEnvironment, caller common.Address, addr common
}

lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheBitAnd failed to load inputs", "err", err)
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))

if lhs.Type() != rhs.Type() {
msg := "fheBitAnd operand type mismatch"
Expand Down Expand Up @@ -411,11 +411,11 @@ func fheBitOrRun(environment EVMEnvironment, caller common.Address, addr common.
}

lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheBitOr failed to load inputs", "err", err)
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))

if lhs.Type() != rhs.Type() {
msg := "fheBitOr operand type mismatch"
Expand Down Expand Up @@ -458,11 +458,11 @@ func fheBitXorRun(environment EVMEnvironment, caller common.Address, addr common
}

lhs, rhs, _, err := load2Ciphertexts(environment, input)
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))
if err != nil {
logger.Error("fheBitXor failed to load inputs", "err", err)
return nil, err
}
otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs))

if lhs.Type() != rhs.Type() {
msg := "fheBitXor operand type mismatch"
Expand Down
Loading

0 comments on commit 06e7045

Please sign in to comment.