Skip to content

Commit

Permalink
Merge pull request #57 from zama-ai/feat/fix-gas-prices
Browse files Browse the repository at this point in the history
Update gas prices based on benchmarks on hpc7a
  • Loading branch information
immortal-tofu authored Jan 12, 2024
2 parents 952262c + 9f360f2 commit f90dd22
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
86 changes: 52 additions & 34 deletions fhevm/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ type GasCosts struct {
FheDecrypt map[FheUintType]uint64
FheBitwiseOp map[FheUintType]uint64
FheMul map[FheUintType]uint64
FheDiv map[FheUintType]uint64
FheRem map[FheUintType]uint64
FheScalarMul map[FheUintType]uint64
FheScalarDiv map[FheUintType]uint64
FheScalarRem map[FheUintType]uint64
FheShift map[FheUintType]uint64
FheScalarShift map[FheUintType]uint64
FheLe map[FheUintType]uint64
FheMinMax map[FheUintType]uint64
FheScalarMinMax map[FheUintType]uint64
FheNegNot map[FheUintType]uint64
FheReencrypt map[FheUintType]uint64
FheTrivialEncrypt map[FheUintType]uint64
Expand All @@ -79,54 +82,69 @@ type GasCosts struct {
func DefaultGasCosts() GasCosts {
return GasCosts{
FheAddSub: map[FheUintType]uint64{
FheUint8: 83000,
FheUint16: 108000,
FheUint32: 130000,
FheUint8: 108000,
FheUint16: 138000,
FheUint32: 169000,
},
FheDecrypt: map[FheUintType]uint64{
FheUint8: 500000,
FheUint16: 500000,
FheUint32: 500000,
},
FheBitwiseOp: map[FheUintType]uint64{
FheUint8: 20000,
FheUint16: 21000,
FheUint32: 22000,
FheUint8: 30000,
FheUint16: 31000,
FheUint32: 32000,
},
FheMul: map[FheUintType]uint64{
FheUint8: 150000,
FheUint16: 200000,
FheUint32: 270000,
FheUint8: 185000,
FheUint16: 252000,
FheUint32: 361000,
},
FheScalarMul: map[FheUintType]uint64{
FheUint8: 128000,
FheUint16: 132000,
FheUint32: 168000,
},
FheDiv: map[FheUintType]uint64{
FheUint8: 1370000,
FheUint16: 3500000,
FheUint32: 9120000,
FheScalarDiv: map[FheUintType]uint64{
FheUint8: 200000,
FheUint16: 400000,
FheUint32: 800000,
},
FheRem: map[FheUintType]uint64{
FheUint8: 1370000, // TODO: check again rem gas
FheUint16: 3500000,
FheUint32: 9120000,
FheScalarRem: map[FheUintType]uint64{
FheUint8: 200000,
FheUint16: 400000,
FheUint32: 800000,
},
FheShift: map[FheUintType]uint64{
FheUint8: 105000,
FheUint16: 128000,
FheUint32: 160000,
FheUint8: 218000,
FheUint16: 273000,
FheUint32: 335000,
},
FheScalarShift: map[FheUintType]uint64{
FheUint8: 135000,
FheUint16: 161000,
FheUint32: 188000,
},
FheLe: map[FheUintType]uint64{
FheUint8: 61000,
FheUint16: 83000,
FheUint32: 109000,
FheUint8: 50000,
FheUint16: 53000,
FheUint32: 56000,
},
FheMinMax: map[FheUintType]uint64{
FheUint8: 108000,
FheUint16: 134000,
FheUint32: 150000,
FheUint8: 215000,
FheUint16: 272000,
FheUint32: 332000,
},
FheScalarMinMax: map[FheUintType]uint64{
FheUint8: 131000,
FheUint16: 159000,
FheUint32: 189000,
},
FheNegNot: map[FheUintType]uint64{
FheUint8: 83000,
FheUint16: 108000,
FheUint32: 130000,
FheUint8: 30000,
FheUint16: 31000,
FheUint32: 32000,
},
// TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle.
FheReencrypt: map[FheUintType]uint64{
Expand All @@ -152,9 +170,9 @@ func DefaultGasCosts() GasCosts {
FheUint32: EvmNetSstoreInitGas + 400000,
},
FheIfThenElse: map[FheUintType]uint64{
FheUint8: 61000,
FheUint16: 83000,
FheUint32: 109000,
FheUint8: 55000,
FheUint16: 60000,
FheUint32: 65000,
},
// TODO: As of now, only support FheUint8. All optimistic require predicates are
// downcast to FheUint8 at the solidity level. Eventually move to ebool.
Expand Down
13 changes: 8 additions & 5 deletions fhevm/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,15 @@ func fheMulRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger.Error("fheMul RequiredGas() operand type mismatch", "lhs", lhs.ciphertext.fheUintType, "rhs", rhs.ciphertext.fheUintType)
return 0
}
return environment.FhevmParams().GasCosts.FheMul[lhs.ciphertext.fheUintType]
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheMul RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarMul[lhs.ciphertext.fheUintType]
}
return environment.FhevmParams().GasCosts.FheMul[lhs.ciphertext.fheUintType]
}

func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand Down Expand Up @@ -435,14 +436,15 @@ func fheShlRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger.Error("fheShift RequiredGas() operand type mismatch", "lhs", lhs.ciphertext.fheUintType, "rhs", rhs.ciphertext.fheUintType)
return 0
}
return environment.FhevmParams().GasCosts.FheShift[lhs.ciphertext.fheUintType]
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheShift RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarShift[lhs.ciphertext.fheUintType]
}
return environment.FhevmParams().GasCosts.FheShift[lhs.ciphertext.fheUintType]
}

func fheShrRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand All @@ -468,14 +470,15 @@ func fheMinRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger.Error("fheMin/Max RequiredGas() operand type mismatch", "lhs", lhs.ciphertext.fheUintType, "rhs", rhs.ciphertext.fheUintType)
return 0
}
return environment.FhevmParams().GasCosts.FheMinMax[lhs.ciphertext.fheUintType]
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheMin/Max RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarMinMax[lhs.ciphertext.fheUintType]
}
return environment.FhevmParams().GasCosts.FheMinMax[lhs.ciphertext.fheUintType]
}

func fheMaxRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand Down Expand Up @@ -519,8 +522,8 @@ func fheDivRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger.Error("fheDiv RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarDiv[lhs.ciphertext.fheUintType]
}
return environment.FhevmParams().GasCosts.FheDiv[lhs.ciphertext.fheUintType]
}

func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand All @@ -540,8 +543,8 @@ func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger.Error("fheRem RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarRem[lhs.ciphertext.fheUintType]
}
return environment.FhevmParams().GasCosts.FheRem[lhs.ciphertext.fheUintType]
}

func fheBitAndRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand Down

0 comments on commit f90dd22

Please sign in to comment.