Skip to content

Commit

Permalink
Merge pull request #93 from zama-ai/feat/gas-prices
Browse files Browse the repository at this point in the history
fix: adjust gas prices
  • Loading branch information
immortal-tofu authored Mar 5, 2024
2 parents 051d226 + ad68e5d commit 586591f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
31 changes: 28 additions & 3 deletions fhevm/fhelib_required_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,33 @@ func fheLtRequiredGas(environment EVMEnvironment, input []byte) uint64 {
}

func fheEqRequiredGas(environment EVMEnvironment, input []byte) uint64 {
// Implement in terms of le, because comparison costs are currently the same.
return fheLeRequiredGas(environment, input)
input = input[:minInt(65, len(input))]

logger := environment.GetLogger()
isScalar, err := isScalarOp(input)
if err != nil {
logger.Error("comparison RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input))
return 0
}
var lhs, rhs *verifiedCiphertext
if !isScalar {
lhs, rhs, err = get2VerifiedOperands(environment, input)
if err != nil {
logger.Error("comparison RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
if lhs.fheUintType() != rhs.fheUintType() {
logger.Error("comparison RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType())
return 0
}
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("comparison RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
}
return environment.FhevmParams().GasCosts.FheEq[lhs.fheUintType()]
}

func fheGeRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand All @@ -123,7 +148,7 @@ func fheGtRequiredGas(environment EVMEnvironment, input []byte) uint64 {

func fheNeRequiredGas(environment EVMEnvironment, input []byte) uint64 {
// Implement in terms of le, because comparison costs are currently the same.
return fheLeRequiredGas(environment, input)
return fheEqRequiredGas(environment, input)
}

func fheShlRequiredGas(environment EVMEnvironment, input []byte) uint64 {
Expand Down
50 changes: 29 additions & 21 deletions fhevm/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type GasCosts struct {
FheScalarRem map[FheUintType]uint64
FheShift map[FheUintType]uint64
FheScalarShift map[FheUintType]uint64
FheEq map[FheUintType]uint64
FheLe map[FheUintType]uint64
FheMinMax map[FheUintType]uint64
FheScalarMinMax map[FheUintType]uint64
Expand All @@ -67,7 +68,7 @@ type GasCosts struct {
func DefaultGasCosts() GasCosts {
return GasCosts{
FheAddSub: map[FheUintType]uint64{
FheUint4: 60000 + AdjustFHEGas,
FheUint4: 55000 + AdjustFHEGas,
FheUint8: 84000 + AdjustFHEGas,
FheUint16: 123000 + AdjustFHEGas,
FheUint32: 152000 + AdjustFHEGas,
Expand All @@ -82,7 +83,7 @@ func DefaultGasCosts() GasCosts {
},
FheBitwiseOp: map[FheUintType]uint64{
FheBool: 16000 + AdjustFHEGas,
FheUint4: 23000 + AdjustFHEGas,
FheUint4: 22000 + AdjustFHEGas,
FheUint8: 24000 + AdjustFHEGas,
FheUint16: 24000 + AdjustFHEGas,
FheUint32: 25000 + AdjustFHEGas,
Expand All @@ -96,28 +97,28 @@ func DefaultGasCosts() GasCosts {
FheUint64: 631000 + AdjustFHEGas,
},
FheScalarMul: map[FheUintType]uint64{
FheUint4: 110000 + AdjustFHEGas,
FheUint4: 78000 + AdjustFHEGas,
FheUint8: 149000 + AdjustFHEGas,
FheUint16: 198000 + AdjustFHEGas,
FheUint32: 254000 + AdjustFHEGas,
FheUint64: 346000 + AdjustFHEGas,
},
FheScalarDiv: map[FheUintType]uint64{
FheUint4: 120000 + AdjustFHEGas,
FheUint4: 129000 + AdjustFHEGas,
FheUint8: 228000 + AdjustFHEGas,
FheUint16: 304000 + AdjustFHEGas,
FheUint32: 388000 + AdjustFHEGas,
FheUint64: 574000 + AdjustFHEGas,
},
FheScalarRem: map[FheUintType]uint64{
FheUint4: 250000 + AdjustFHEGas,
FheUint4: 276000 + AdjustFHEGas,
FheUint8: 450000 + AdjustFHEGas,
FheUint16: 612000 + AdjustFHEGas,
FheUint32: 795000 + AdjustFHEGas,
FheUint64: 1095000 + AdjustFHEGas,
},
FheShift: map[FheUintType]uint64{
FheUint4: 110000 + AdjustFHEGas,
FheUint4: 106000 + AdjustFHEGas,
FheUint8: 123000 + AdjustFHEGas,
FheUint16: 143000 + AdjustFHEGas,
FheUint32: 173000 + AdjustFHEGas,
Expand All @@ -130,38 +131,45 @@ func DefaultGasCosts() GasCosts {
FheUint32: 25000 + AdjustFHEGas,
FheUint64: 28000 + AdjustFHEGas,
},
FheLe: map[FheUintType]uint64{
FheUint4: 46000 + AdjustFHEGas,
FheUint8: 46000 + AdjustFHEGas,
FheUint16: 46000 + AdjustFHEGas,
FheEq: map[FheUintType]uint64{
FheUint4: 41000 + AdjustFHEGas,
FheUint8: 43000 + AdjustFHEGas,
FheUint16: 44000 + AdjustFHEGas,
FheUint32: 72000 + AdjustFHEGas,
FheUint64: 76000 + AdjustFHEGas,
},
FheLe: map[FheUintType]uint64{
FheUint4: 60000 + AdjustFHEGas,
FheUint8: 72000 + AdjustFHEGas,
FheUint16: 95000 + AdjustFHEGas,
FheUint32: 118000 + AdjustFHEGas,
FheUint64: 146000 + AdjustFHEGas,
},
FheMinMax: map[FheUintType]uint64{
FheUint4: 50000 + AdjustFHEGas,
FheUint8: 94000 + AdjustFHEGas,
FheUint16: 120000 + AdjustFHEGas,
FheUint32: 148000 + AdjustFHEGas,
FheUint64: 189000 + AdjustFHEGas,
FheUint4: 106000 + AdjustFHEGas,
FheUint8: 118000 + AdjustFHEGas,
FheUint16: 143000 + AdjustFHEGas,
FheUint32: 173000 + AdjustFHEGas,
FheUint64: 200000 + AdjustFHEGas,
},
FheScalarMinMax: map[FheUintType]uint64{
FheUint4: 80000 + AdjustFHEGas,
FheUint4: 111000 + AdjustFHEGas,
FheUint8: 114000 + AdjustFHEGas,
FheUint16: 140000 + AdjustFHEGas,
FheUint32: 154000 + AdjustFHEGas,
FheUint64: 182000 + AdjustFHEGas,
},
FheNot: map[FheUintType]uint64{
FheUint4: 25000 + AdjustFHEGas,
FheUint8: 25000 + AdjustFHEGas,
FheUint4: 23000 + AdjustFHEGas,
FheUint8: 24000 + AdjustFHEGas,
FheUint16: 25000 + AdjustFHEGas,
FheUint32: 26000 + AdjustFHEGas,
FheUint64: 27000 + AdjustFHEGas,
},
FheNeg: map[FheUintType]uint64{
FheUint4: 50000 + AdjustFHEGas,
FheUint8: 79000 + AdjustFHEGas,
FheUint16: 114000 + AdjustFHEGas,
FheUint8: 85000 + AdjustFHEGas,
FheUint16: 121000 + AdjustFHEGas,
FheUint32: 150000 + AdjustFHEGas,
FheUint64: 189000 + AdjustFHEGas,
},
Expand Down Expand Up @@ -199,7 +207,7 @@ func DefaultGasCosts() GasCosts {
FheUint64: EvmNetSstoreInitGas + 100000,
},
FheIfThenElse: map[FheUintType]uint64{
FheUint4: 37000 + AdjustFHEGas,
FheUint4: 35000 + AdjustFHEGas,
FheUint8: 37000 + AdjustFHEGas,
FheUint16: 37000 + AdjustFHEGas,
FheUint32: 40000 + AdjustFHEGas,
Expand Down

0 comments on commit 586591f

Please sign in to comment.