Skip to content

Commit

Permalink
Use hash(counter) instead of just counter as handle
Browse files Browse the repository at this point in the history
Rationale is that SSTORE charges less for zeroes. Using hash(counter),
we make gas estimation close to random ciphertext bytes.
  • Loading branch information
dartdart26 committed Jun 22, 2023
1 parent 904dae4 commit 45482b3
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ func importCiphertext(accessibleState PrecompileAccessibleState, ct *tfheCiphert
// Used when we want to skip FHE computation, e.g. gas estimation.
func importRandomCiphertext(accessibleState PrecompileAccessibleState, t fheUintType) []byte {
nextCtHash := &accessibleState.Interpreter().evm.nextCiphertextHashOnGasEst
ctHashBytes := nextCtHash.Bytes()
ctHashBytes := crypto.Keccak256(nextCtHash.Bytes())
handle := common.BytesToHash(ctHashBytes)
ct := new(tfheCiphertext)
ct.fheUintType = t
Expand Down Expand Up @@ -1451,6 +1451,11 @@ func (e *verifyCiphertext) Run(accessibleState PrecompileAccessibleState, caller
ctBytes := input[:len(input)-1]
ctType := fheUintType(input[len(input)-1])

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !accessibleState.Interpreter().evm.Commit && !accessibleState.Interpreter().evm.EthCall {
return importRandomCiphertext(accessibleState, ctType), nil
}

ct := new(tfheCiphertext)
err := ct.deserializeCompact(ctBytes, ctType)
if err != nil {
Expand Down

0 comments on commit 45482b3

Please sign in to comment.