Skip to content

Commit

Permalink
feat: add FheBool and FheUint4
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Feb 27, 2024
1 parent 26427fd commit 9f8901b
Show file tree
Hide file tree
Showing 11 changed files with 2,524 additions and 316 deletions.
286 changes: 285 additions & 1 deletion fhevm/contracts_test.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions fhevm/fhelib_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,11 @@ func generateRandom(environment EVMEnvironment, caller common.Address, resultTyp
// Apply upperBound, if set.
var randUint uint64
switch resultType {
case FheUint4:
randBytes := make([]byte, 1)
cipher.XORKeyStream(randBytes, randBytes)
randUint = uint64(randBytes[0])
randUint = uint64(applyUpperBound(randUint, 4, upperBound))
case FheUint8:
randBytes := make([]byte, 1)
cipher.XORKeyStream(randBytes, randBytes)
Expand Down Expand Up @@ -1462,6 +1467,10 @@ func reencryptRun(environment EVMEnvironment, caller common.Address, addr common

var fheType kms.FheType
switch ct.fheUintType() {
case FheBool:
fheType = kms.FheType_Bool
case FheUint4:
fheType = kms.FheType_Euint4
case FheUint8:
fheType = kms.FheType_Euint8
case FheUint16:
Expand Down Expand Up @@ -1632,6 +1641,10 @@ func decryptValue(environment EVMEnvironment, ct *TfheCiphertext) (uint64, error
logger := environment.GetLogger()
var fheType kms.FheType
switch ct.Type() {
case FheBool:
fheType = kms.FheType_Bool
case FheUint4:
fheType = kms.FheType_Euint4
case FheUint8:
fheType = kms.FheType_Euint8
case FheUint16:
Expand Down
25 changes: 25 additions & 0 deletions fhevm/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,116 +67,139 @@ type GasCosts struct {
func DefaultGasCosts() GasCosts {
return GasCosts{
FheAddSub: map[FheUintType]uint64{
FheUint4: 84000 + AdjustFHEGas,
FheUint8: 84000 + AdjustFHEGas,
FheUint16: 123000 + AdjustFHEGas,
FheUint32: 152000 + AdjustFHEGas,
FheUint64: 178000 + AdjustFHEGas,
},
FheDecrypt: map[FheUintType]uint64{
FheUint4: 500000,
FheUint8: 500000,
FheUint16: 500000,
FheUint32: 500000,
FheUint64: 500000,
},
FheBitwiseOp: map[FheUintType]uint64{
FheBool: 23000 + AdjustFHEGas,
FheUint4: 24000 + AdjustFHEGas,
FheUint8: 24000 + AdjustFHEGas,
FheUint16: 24000 + AdjustFHEGas,
FheUint32: 25000 + AdjustFHEGas,
FheUint64: 28000 + AdjustFHEGas,
},
FheMul: map[FheUintType]uint64{
FheUint4: 187000 + AdjustFHEGas,
FheUint8: 187000 + AdjustFHEGas,
FheUint16: 252000 + AdjustFHEGas,
FheUint32: 349000 + AdjustFHEGas,
FheUint64: 631000 + AdjustFHEGas,
},
FheScalarMul: map[FheUintType]uint64{
FheUint4: 149000 + AdjustFHEGas,
FheUint8: 149000 + AdjustFHEGas,
FheUint16: 198000 + AdjustFHEGas,
FheUint32: 254000 + AdjustFHEGas,
FheUint64: 346000 + AdjustFHEGas,
},
FheScalarDiv: map[FheUintType]uint64{
FheUint4: 228000 + AdjustFHEGas,
FheUint8: 228000 + AdjustFHEGas,
FheUint16: 304000 + AdjustFHEGas,
FheUint32: 388000 + AdjustFHEGas,
FheUint64: 574000 + AdjustFHEGas,
},
FheScalarRem: map[FheUintType]uint64{
FheUint4: 450000 + AdjustFHEGas,
FheUint8: 450000 + AdjustFHEGas,
FheUint16: 612000 + AdjustFHEGas,
FheUint32: 795000 + AdjustFHEGas,
FheUint64: 1095000 + AdjustFHEGas,
},
FheShift: map[FheUintType]uint64{
FheUint4: 123000 + AdjustFHEGas,
FheUint8: 123000 + AdjustFHEGas,
FheUint16: 143000 + AdjustFHEGas,
FheUint32: 173000 + AdjustFHEGas,
FheUint64: 217000 + AdjustFHEGas,
},
FheScalarShift: map[FheUintType]uint64{
FheUint4: 25000 + AdjustFHEGas,
FheUint8: 25000 + AdjustFHEGas,
FheUint16: 25000 + AdjustFHEGas,
FheUint32: 25000 + AdjustFHEGas,
FheUint64: 28000 + AdjustFHEGas,
},
FheLe: map[FheUintType]uint64{
FheUint4: 46000 + AdjustFHEGas,
FheUint8: 46000 + AdjustFHEGas,
FheUint16: 46000 + AdjustFHEGas,
FheUint32: 72000 + AdjustFHEGas,
FheUint64: 76000 + AdjustFHEGas,
},
FheMinMax: map[FheUintType]uint64{
FheUint4: 94000 + AdjustFHEGas,
FheUint8: 94000 + AdjustFHEGas,
FheUint16: 120000 + AdjustFHEGas,
FheUint32: 148000 + AdjustFHEGas,
FheUint64: 189000 + AdjustFHEGas,
},
FheScalarMinMax: map[FheUintType]uint64{
FheUint4: 114000 + AdjustFHEGas,
FheUint8: 114000 + AdjustFHEGas,
FheUint16: 140000 + AdjustFHEGas,
FheUint32: 154000 + AdjustFHEGas,
FheUint64: 182000 + AdjustFHEGas,
},
FheNot: map[FheUintType]uint64{
FheUint4: 25000 + AdjustFHEGas,
FheUint8: 25000 + AdjustFHEGas,
FheUint16: 25000 + AdjustFHEGas,
FheUint32: 26000 + AdjustFHEGas,
FheUint64: 27000 + AdjustFHEGas,
},
FheNeg: map[FheUintType]uint64{
FheUint4: 79000 + AdjustFHEGas,
FheUint8: 79000 + AdjustFHEGas,
FheUint16: 114000 + AdjustFHEGas,
FheUint32: 150000 + AdjustFHEGas,
FheUint64: 189000 + AdjustFHEGas,
},
// TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle.
FheReencrypt: map[FheUintType]uint64{
FheBool: 1000,
FheUint4: 1000,
FheUint8: 1000,
FheUint16: 1100,
FheUint32: 1200,
},
// As of now, verification costs only cover ciphertext deserialization and assume there is no ZKPoK to verify.
FheVerify: map[FheUintType]uint64{
FheBool: 200,
FheUint4: 200,
FheUint8: 200,
FheUint16: 300,
FheUint32: 400,
FheUint64: 800,
},
FheTrivialEncrypt: map[FheUintType]uint64{
FheBool: 100,
FheUint4: 100,
FheUint8: 100,
FheUint16: 200,
FheUint32: 300,
FheUint64: 600,
},
// TODO: These will change once we have an FHE-based random generaration.
FheRand: map[FheUintType]uint64{
FheUint4: EvmNetSstoreInitGas + 100000,
FheUint8: EvmNetSstoreInitGas + 100000,
FheUint16: EvmNetSstoreInitGas + 100000,
FheUint32: EvmNetSstoreInitGas + 100000,
FheUint64: EvmNetSstoreInitGas + 100000,
},
FheIfThenElse: map[FheUintType]uint64{
FheUint4: 37000 + AdjustFHEGas,
FheUint8: 37000 + AdjustFHEGas,
FheUint16: 37000 + AdjustFHEGas,
FheUint32: 40000 + AdjustFHEGas,
Expand All @@ -188,11 +211,13 @@ func DefaultGasCosts() GasCosts {
// For every subsequent optimistic require, we need to bitand it with the current require value - that
// works, because we assume requires have a value of 0 or 1.
FheOptRequire: map[FheUintType]uint64{
FheUint4: 170000,
FheUint8: 170000,
FheUint16: 180000,
FheUint32: 190000,
},
FheOptRequireBitAnd: map[FheUintType]uint64{
FheUint4: 20000,
FheUint8: 20000,
FheUint16: 20000,
FheUint32: 20000,
Expand Down
Loading

0 comments on commit 9f8901b

Please sign in to comment.