Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Feature/add new types #92

Merged
merged 9 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
325 changes: 320 additions & 5 deletions fhevm/contracts_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion fhevm/fhelib_required_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func fheIfThenElseRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger.Error("IfThenElse op RequiredGas() inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
if first.fheUintType() != FheUint8 {
if first.fheUintType() != FheBool {
logger.Error("IfThenElse op RequiredGas() invalid type for condition", "first", first.fheUintType())
return 0
}
Expand Down
37 changes: 25 additions & 12 deletions fhevm/fhelib_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.Le(rhs.ciphertext)
Expand All @@ -270,7 +270,7 @@ func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.ScalarLe(rhs.Uint64())
Expand Down Expand Up @@ -312,7 +312,7 @@ func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.Lt(rhs.ciphertext)
Expand All @@ -336,7 +336,7 @@ func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.ScalarLt(rhs.Uint64())
Expand Down Expand Up @@ -378,7 +378,7 @@ func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.Eq(rhs.ciphertext)
Expand All @@ -402,7 +402,7 @@ func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.ScalarEq(rhs.Uint64())
Expand Down Expand Up @@ -444,7 +444,7 @@ func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.Ge(rhs.ciphertext)
Expand All @@ -468,7 +468,7 @@ func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.ScalarGe(rhs.Uint64())
Expand Down Expand Up @@ -510,7 +510,7 @@ func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.Gt(rhs.ciphertext)
Expand All @@ -534,7 +534,7 @@ func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.ScalarGt(rhs.Uint64())
Expand Down Expand Up @@ -708,7 +708,7 @@ func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.Ne(rhs.ciphertext)
Expand All @@ -732,7 +732,7 @@ func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Add

// If we are doing gas estimation, skip execution and insert a random ciphertext as a result.
if !environment.IsCommitting() && !environment.IsEthCall() {
return importRandomCiphertext(environment, lhs.fheUintType()), nil
return importRandomCiphertext(environment, FheBool), nil
}

result, err := lhs.ciphertext.ScalarNe(rhs.Uint64())
Expand Down 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,
immortal-tofu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading