Skip to content

Commit

Permalink
Merge pull request ethereum#76 from zama-ai/petar/remove-delegate-cip…
Browse files Browse the repository at this point in the history
…hertext

Remove redundant delegateCiphertext precompile
  • Loading branch information
dartdart26 authored Apr 20, 2023
2 parents 5bad456 + 0bc6c21 commit 7e71798
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 81 deletions.
29 changes: 5 additions & 24 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{65}): &fheAdd{},
common.BytesToAddress([]byte{66}): &verifyCiphertext{},
common.BytesToAddress([]byte{67}): &reencrypt{},
common.BytesToAddress([]byte{68}): &delegateCiphertext{},
// slot 68 is available for use
common.BytesToAddress([]byte{69}): &require{},
common.BytesToAddress([]byte{70}): &fheLte{},
common.BytesToAddress([]byte{71}): &fheSub{},
Expand All @@ -95,7 +95,7 @@ var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{65}): &fheAdd{},
common.BytesToAddress([]byte{66}): &verifyCiphertext{},
common.BytesToAddress([]byte{67}): &reencrypt{},
common.BytesToAddress([]byte{68}): &delegateCiphertext{},
// slot 68 is available for use
common.BytesToAddress([]byte{69}): &require{},
common.BytesToAddress([]byte{70}): &fheLte{},
common.BytesToAddress([]byte{71}): &fheSub{},
Expand Down Expand Up @@ -123,7 +123,7 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{65}): &fheAdd{},
common.BytesToAddress([]byte{66}): &verifyCiphertext{},
common.BytesToAddress([]byte{67}): &reencrypt{},
common.BytesToAddress([]byte{68}): &delegateCiphertext{},
// slot 68 is available for use
common.BytesToAddress([]byte{69}): &require{},
common.BytesToAddress([]byte{70}): &fheLte{},
common.BytesToAddress([]byte{71}): &fheSub{},
Expand Down Expand Up @@ -151,7 +151,7 @@ var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{65}): &fheAdd{},
common.BytesToAddress([]byte{66}): &verifyCiphertext{},
common.BytesToAddress([]byte{67}): &reencrypt{},
common.BytesToAddress([]byte{68}): &delegateCiphertext{},
// slot 68 is available for use
common.BytesToAddress([]byte{69}): &require{},
common.BytesToAddress([]byte{70}): &fheLte{},
common.BytesToAddress([]byte{71}): &fheSub{},
Expand Down Expand Up @@ -179,7 +179,7 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{65}): &fheAdd{},
common.BytesToAddress([]byte{66}): &verifyCiphertext{},
common.BytesToAddress([]byte{67}): &reencrypt{},
common.BytesToAddress([]byte{68}): &delegateCiphertext{},
// slot 68 is available for use
common.BytesToAddress([]byte{69}): &require{},
common.BytesToAddress([]byte{70}): &fheLte{},
common.BytesToAddress([]byte{71}): &fheSub{},
Expand Down Expand Up @@ -1432,25 +1432,6 @@ func (e *reencrypt) Run(accessibleState PrecompileAccessibleState, caller common
return nil, errors.New("unverified ciphertext handle")
}

type delegateCiphertext struct{}

func (e *delegateCiphertext) RequiredGas(input []byte) uint64 {
// TODO
return 8
}

func (e *delegateCiphertext) Run(accessibleState PrecompileAccessibleState, caller common.Address, addr common.Address, input []byte, readOnly bool) ([]byte, error) {
if len(input) != 32 {
return nil, errors.New("invalid ciphertext handle")
}
ct := getVerifiedCiphertext(accessibleState, common.BytesToHash(input))
if ct != nil {
ct.verifiedDepths.add(accessibleState.Interpreter().evm.depth + 1)
return nil, nil
}
return nil, errors.New("unverified ciphertext handle")
}

type require struct{}

func (e *require) RequiredGas(input []byte) uint64 {
Expand Down
57 changes: 0 additions & 57 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,63 +449,6 @@ func toPrecompileInput(hashes ...common.Hash) []byte {
return ret
}

func TestDelegateCiphertext(t *testing.T) {
c := &delegateCiphertext{}
depth := 1
state := newTestState()
state.interpreter.evm.depth = depth
state.interpreter.evm.Commit = true
addr := common.Address{}
readOnly := false
hash := verifyCiphertextInTestMemory(state.interpreter, 1, depth).getHash()
input := toPrecompileInput(hash)
_, err := c.Run(state, addr, addr, input, readOnly)
if err != nil {
t.Fatalf(err.Error())
}
state.interpreter.evm.depth++
res := getVerifiedCiphertextFromEVM(state.interpreter, hash)
if res == nil {
t.Fatalf("expected ciphertext is verified at depth + 1")
}
if res.verifiedDepths.count() != 2 || !res.verifiedDepths.has(depth) || !res.verifiedDepths.has(depth+1) {
t.Fatalf("expected ciphertext to be verified at depth and depth + 1")
}
}

func TestDelegateUnverifiedCiphertextAtDepth(t *testing.T) {
c := &delegateCiphertext{}
depth := 1
state := newTestState()
state.interpreter.evm.depth = depth + 1
state.interpreter.evm.Commit = true
addr := common.Address{}
readOnly := false
hash := verifyCiphertextInTestMemory(state.interpreter, 1, depth).getHash()
input := toPrecompileInput(hash)
_, err := c.Run(state, addr, addr, input, readOnly)
if err == nil {
t.Fatalf("expected that delegate failed")
}
res := getVerifiedCiphertextFromEVM(state.interpreter, hash)
if res != nil {
t.Fatalf("expected that the ciphertext is not verified at depth + 1")
}
state.interpreter.evm.depth = depth + 2
res = getVerifiedCiphertextFromEVM(state.interpreter, hash)
if res != nil {
t.Fatalf("expected that the ciphertext is not verified at depth + 2")
}
state.interpreter.evm.depth = depth
res = getVerifiedCiphertextFromEVM(state.interpreter, hash)
if res == nil {
t.Fatalf("expected that the ciphertext is still verified at depth")
}
if res.verifiedDepths.count() != 1 || !res.verifiedDepths.has(state.interpreter.evm.depth) {
t.Fatalf("expected ciphertext to be verified at depth")
}
}

func TestFheAdd(t *testing.T) {
c := &fheAdd{}
depth := 1
Expand Down

0 comments on commit 7e71798

Please sign in to comment.