Skip to content

Commit

Permalink
fix: missing gas costs and memory issues
Browse files Browse the repository at this point in the history
Add missing gas costs for multiple operations, mainly for FheBool,
FheUint160 and FheUint2048.

Note that verify for bool is more expensive due to lack of cast from
2048 bits to bool - doing not equal instead.

Make `castTo()` fail and not panic on bad type input.

Fixed a memory leak in `executeTernaryCiphertextOperation()` - the
`first_ptr` pointer was never freed.

Refactor some of the code such that it uses `defer` with destroy as
close to the point where memory is allocated as possible - that fixes
memory leaks on early returns in multiple places.

C code needs to be refactored and reduced. Maybe we can use codegen or
a tool for that.
  • Loading branch information
dartdart26 committed Jun 19, 2024
1 parent d22f070 commit f5fa26a
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 184 deletions.
98 changes: 57 additions & 41 deletions fhevm/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ func DefaultGasCosts() GasCosts {
tfhe.FheUint64: 178000 + AdjustFHEGas,
},
FheDecrypt: map[tfhe.FheUintType]uint64{
tfhe.FheUint4: 500000,
tfhe.FheUint8: 500000,
tfhe.FheUint16: 500000,
tfhe.FheUint32: 500000,
tfhe.FheUint64: 500000,
tfhe.FheBool: 500000,
tfhe.FheUint4: 500000,
tfhe.FheUint8: 500000,
tfhe.FheUint16: 500000,
tfhe.FheUint32: 500000,
tfhe.FheUint64: 500000,
tfhe.FheUint2048: 500000,
},
FheBitwiseOp: map[tfhe.FheUintType]uint64{
tfhe.FheBool: 16000 + AdjustFHEGas,
Expand Down Expand Up @@ -130,12 +132,13 @@ func DefaultGasCosts() GasCosts {
tfhe.FheUint64: 28000 + AdjustFHEGas,
},
FheEq: map[tfhe.FheUintType]uint64{
tfhe.FheUint4: 41000 + AdjustFHEGas,
tfhe.FheUint8: 43000 + AdjustFHEGas,
tfhe.FheUint16: 44000 + AdjustFHEGas,
tfhe.FheUint32: 72000 + AdjustFHEGas,
tfhe.FheUint64: 76000 + AdjustFHEGas,
tfhe.FheUint160: 80000 + AdjustFHEGas,
tfhe.FheUint4: 41000 + AdjustFHEGas,
tfhe.FheUint8: 43000 + AdjustFHEGas,
tfhe.FheUint16: 44000 + AdjustFHEGas,
tfhe.FheUint32: 72000 + AdjustFHEGas,
tfhe.FheUint64: 76000 + AdjustFHEGas,
tfhe.FheUint160: 80000 + AdjustFHEGas,
tfhe.FheUint2048: 160000 + AdjustFHEGas,
},
FheArrayEqBigArrayFactor: 1000,
FheLe: map[tfhe.FheUintType]uint64{
Expand All @@ -160,6 +163,7 @@ func DefaultGasCosts() GasCosts {
tfhe.FheUint64: 182000 + AdjustFHEGas,
},
FheNot: map[tfhe.FheUintType]uint64{
tfhe.FheBool: 22000 + AdjustFHEGas,
tfhe.FheUint4: 23000 + AdjustFHEGas,
tfhe.FheUint8: 24000 + AdjustFHEGas,
tfhe.FheUint16: 25000 + AdjustFHEGas,
Expand All @@ -174,6 +178,7 @@ func DefaultGasCosts() GasCosts {
tfhe.FheUint64: 189000 + AdjustFHEGas,
},
// TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle.
// DEPRECATED.
FheReencrypt: map[tfhe.FheUintType]uint64{
tfhe.FheBool: 1000,
tfhe.FheUint4: 1000,
Expand All @@ -183,20 +188,24 @@ func DefaultGasCosts() GasCosts {
},
// As of now, verification costs only cover ciphertext deserialization and assume there is no ZKPoK to verify.
FheVerify: map[tfhe.FheUintType]uint64{
tfhe.FheBool: 200,
tfhe.FheUint4: 200,
tfhe.FheUint8: 200,
tfhe.FheUint16: 300,
tfhe.FheUint32: 400,
tfhe.FheUint64: 800,
tfhe.FheBool: 200 + 5000, // TODO: Requires an FheUint160 comparisson via FheEq. Make it cheaper than that, though. Need to fix that.
tfhe.FheUint4: 200 + 500,
tfhe.FheUint8: 200 + 500,
tfhe.FheUint16: 300 + 500,
tfhe.FheUint32: 400 + 500,
tfhe.FheUint64: 800 + 500,
tfhe.FheUint160: 1200 + 500,
tfhe.FheUint2048: 2000 + 500,
},
FheTrivialEncrypt: map[tfhe.FheUintType]uint64{
tfhe.FheBool: 100,
tfhe.FheUint4: 100,
tfhe.FheUint8: 100,
tfhe.FheUint16: 200,
tfhe.FheUint32: 300,
tfhe.FheUint64: 600,
tfhe.FheBool: 100,
tfhe.FheUint4: 100,
tfhe.FheUint8: 100,
tfhe.FheUint16: 200,
tfhe.FheUint32: 300,
tfhe.FheUint64: 600,
tfhe.FheUint160: 700,
tfhe.FheUint2048: 900,
},
// TODO: These will change once we have an FHE-based random generaration.
FheRand: map[tfhe.FheUintType]uint64{
Expand All @@ -214,31 +223,38 @@ func DefaultGasCosts() GasCosts {
tfhe.FheUint64: 43000 + AdjustFHEGas,
},
FheGetCiphertext: map[tfhe.FheUintType]uint64{
tfhe.FheUint8: 12000,
tfhe.FheUint16: 14000,
tfhe.FheUint32: 18000,
tfhe.FheUint64: 28000,
tfhe.FheBool: 10000,
tfhe.FheUint8: 12000,
tfhe.FheUint16: 14000,
tfhe.FheUint32: 18000,
tfhe.FheUint64: 28000,
tfhe.FheUint160: 50000,
tfhe.FheUint2048: 100000,
},
// TODO: The values here are chosen somewhat arbitrarily.
// Also, we don't take into account whether a ciphertext existed (either "current" or "original") for the given handle.
// Finally, costs are likely to change in the future.
FheStorageSstoreGas: map[tfhe.FheUintType]uint64{
tfhe.FheUint4: SstoreFheUint4Gas,
tfhe.FheUint8: SstoreFheUint4Gas * 2,
tfhe.FheUint16: SstoreFheUint4Gas * 4,
tfhe.FheUint32: SstoreFheUint4Gas * 8,
tfhe.FheUint64: SstoreFheUint4Gas * 16,
tfhe.FheUint128: SstoreFheUint4Gas * 32,
tfhe.FheUint160: SstoreFheUint4Gas * 40,
tfhe.FheBool: SstoreFheUint4Gas / 2,
tfhe.FheUint4: SstoreFheUint4Gas,
tfhe.FheUint8: SstoreFheUint4Gas * 2,
tfhe.FheUint16: SstoreFheUint4Gas * 4,
tfhe.FheUint32: SstoreFheUint4Gas * 8,
tfhe.FheUint64: SstoreFheUint4Gas * 16,
tfhe.FheUint128: SstoreFheUint4Gas * 32,
tfhe.FheUint160: SstoreFheUint4Gas * 40,
tfhe.FheUint2048: SstoreFheUint4Gas * 120,
},
FheStorageSloadGas: map[tfhe.FheUintType]uint64{
tfhe.FheUint4: SloadFheUint4Gas,
tfhe.FheUint8: SloadFheUint4Gas * 2,
tfhe.FheUint16: SloadFheUint4Gas * 4,
tfhe.FheUint32: SloadFheUint4Gas * 8,
tfhe.FheUint64: SloadFheUint4Gas * 16,
tfhe.FheUint128: SloadFheUint4Gas * 32,
tfhe.FheUint160: SloadFheUint4Gas * 40,
tfhe.FheBool: SloadFheUint4Gas / 2,
tfhe.FheUint4: SloadFheUint4Gas,
tfhe.FheUint8: SloadFheUint4Gas * 2,
tfhe.FheUint16: SloadFheUint4Gas * 4,
tfhe.FheUint32: SloadFheUint4Gas * 8,
tfhe.FheUint64: SloadFheUint4Gas * 16,
tfhe.FheUint128: SloadFheUint4Gas * 32,
tfhe.FheUint160: SloadFheUint4Gas * 40,
tfhe.FheUint2048: SloadFheUint4Gas * 120, // TODO: technically, it is more than 10 times bigger than 160 bits
},
}
}
Expand Down
Loading

0 comments on commit f5fa26a

Please sign in to comment.