Skip to content

Commit

Permalink
Merge pull request #1 from zama-ai/feat/add-kms
Browse files Browse the repository at this point in the history
Feat/add kms
Fix for time improvement not yet applied.
  • Loading branch information
leventdem authored Jun 28, 2024
2 parents fd14489 + b12364c commit 4994e73
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
KEY_GEN=true
KEY_GEN=false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
res
running_node
work_dir
33 changes: 24 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ KMS_DEV_VERSION ?= aa90d98
FHEVM_SOLIDITY_REPO ?= fhevm
FHEVM_SOLIDITY_PATH ?= $(WORKDIR)/$(FHEVM_SOLIDITY_REPO)
FHEVM_SOLIDITY_PATH_EXISTS := $(shell test -d $(FHEVM_SOLIDITY_PATH)/.git && echo "true" || echo "false")
FHEVM_SOLIDITY_VERSION ?= v0.5.0-1
FHEVM_SOLIDITY_VERSION ?= 1e91f1f2b3055a6abda0f7a5fa372ec4ffae9116

export GO111MODULE = on

Expand Down Expand Up @@ -77,7 +77,7 @@ init-ethermint-node:
@$(MAKE) init-ethermint-node-from-registry

init-ethermint-node-from-registry:
@docker compose -f docker-compose/docker-compose.validator.yml run validator bash /config/setup.sh
@docker compose -f docker-compose/docker-compose-full.yml run validator bash /config/setup.sh
$(MAKE) change-running-node-owner
$(MAKE) generate-fhe-keys-registry

Expand All @@ -93,13 +93,13 @@ else
endif


run-ethermint:
@docker compose -f docker-compose/docker-compose.validator.yml -f docker-compose/docker-compose.validator.override.yml up --detach
run-full:
@docker compose -f docker-compose/docker-compose-full.yml -f docker-compose/docker-compose-full.override.yml up --detach
@echo 'sleep a little to let the docker start up'
sleep 10

stop-ethermint:
@docker compose -f docker-compose/docker-compose.validator.yml down
stop-full:
@docker compose -f docker-compose/docker-compose-full.yml down

TEST_FILE := run_tests.sh
TEST_IF_FROM_REGISTRY :=
Expand All @@ -113,20 +113,35 @@ run-e2e-test: check-all-test-repo
@cd $(FHEVM_SOLIDITY_PATH) && ./setup-local-fhevm.sh
@cd $(FHEVM_SOLIDITY_PATH) && npx hardhat test


prepare-e2e-test: check-all-test-repo
@cd $(FHEVM_SOLIDITY_PATH) && npm ci
@sleep 5
@./scripts/fund_test_addresses_docker.sh
@cd $(FHEVM_SOLIDITY_PATH) && cp .env.example .env
@cd $(FHEVM_SOLIDITY_PATH) && npm i
@cd $(FHEVM_SOLIDITY_PATH) && ./setup-local-fhevm.sh

run-async-test:
@cd $(FHEVM_SOLIDITY_PATH) && npx hardhat test --grep 'test async decrypt uint8'

run-true-input-async-test:
@cd $(FHEVM_SOLIDITY_PATH) && npx hardhat test --grep 'test async decrypt uint64 non-trivial'

e2e-test:
@$(MAKE) check-all-test-repo
@$(MAKE) init-ethermint-node-from-registry
$(MAKE) run-ethermint
$(MAKE) run-full
$(MAKE) run-e2e-test
$(MAKE) stop-ethermint
$(MAKE) stop-full


clean-node-storage:
@echo 'clean node storage'
sudo rm -rf running_node

clean: clean-node-storage
$(MAKE) stop-ethermint
$(MAKE) stop-full
rm -rf $(BUILDDIR)/
rm -rf $(WORKDIR)/

Expand Down
157 changes: 107 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# How to run the new fhEVM
# Demo of fhEVM + KMS (centralized)

THis documentation is only temporary, it allows to run the last fhEVM with the fhe keys generated using kms-core dedicated binary.
This documentation purpose is to show how to run fhEVM for FHE execution and KMS for async decryption and reencryption.

# Key generation

Please update `KEY_GEN` value in `.env`
## Key generation

Please update `KEY_GEN` value in `.env`. Default is `false`

| KEY_GEN | Purpose |
|---------|-------------------------------------------------------------------------------|
Expand All @@ -14,59 +15,56 @@ THis documentation is only temporary, it allows to run the last fhEVM with the f



# Fast run and test
## Fast run and test

Execute the following commands:

```bash
make e2e-test
# Init node and copy or gen fhe keys
make init-ethermint-node
# Run fhEVM + full KMS components
make run-full
# Deploy ACL, Gateway ...
make prepare-e2e-test
# This test will fail (first event catch is buggy - we are on it)
make run-async-test
# This one is working
make run-async-test
# A non trivial test
make run-true-input-async-test
# Manual test
cd work_dir/fhevm & npx hardhat test --grep 'test async decrypt uint32'
```

Note: if you get `override the existing name orchestrator [y/N]: `, just ^C and run `make clean-node-storage` to remove the state.


# Init fhEVM

```bash
make init-ethermint-node
```

This will initialize and generate the fhe keys.
IMPORTANT: ensure to have 15 GB of empty ram to generate the keys.

# Run fhEVM
<details><summary>Docker logs</summary>
<p>

```bash
make run-ethermint
# Check the logs
docker logs ethermintnode0 -f
```
# Check logs for Gateway
docker logs zama-kms-gateway-1 -f

# Stop fhEVM

```bash
make stop-ethermint
```
# On the second try you should see

# Fresh start
# 2024-06-27T16:59:35.432399Z INFO gateway::events::manager: ⭐ event_decryption: 1
# 2024-06-27T16:59:35.432410Z INFO gateway::events::manager: Handled event decryption: 1
# 2024-06-27T16:59:35.432460Z INFO gateway::blockchain::ciphertext_provider: Getting ciphertext for ct_handle: "aa9f8f90ebf0fa8e30caee92f0b97e158f1ec659b363101d07beac9b0cc90200"
# 2024-06-27T16:59:35.436144Z INFO gateway::blockchain::handlers: 🚀 request_id: 1, fhe_type: euint8
# 2024-06-27T16:59:35.439802Z INFO gateway::blockchain::kms_blockchain: 📦 Stored ciphertext, handle: 00008138b65173b5c57fc98d0fce54e5ff10635127e526144ffbe21d7099e3a1e1516574
# 2024-06-27T16:59:35.439813Z INFO gateway::blockchain::kms_blockchain: 🍊 Decrypting ciphertext of size: 33080

```bash
make clean
# Check the logs for the node
docker logs zama-kms-validator-1 -f
```

Note: Fhe keys are in res/keys folder, delete them to regenerate new keys at ```make init-ethermint-node```

</p>
</details>

# Test using fhevm

```bash
# if not executed before
make init-ethermint-node
# if not executed before
make run-ethermint
# In new terminal
make run-e2e-test
```

<details><summary>Pre deployment</summary>
<p>
You should see the pre-processing steps, i.e. deployment of ACL, Gateway, KMSVerifier ...

```bash
Expand Down Expand Up @@ -97,22 +95,81 @@ ownerAddress 0x305F1F471e9baCFF2b3549F9601f9A4BEafc94e1
GatewayContract was deployed at address: 0xc8c9303Cd7F337fab769686B593B87DC3403E0ce
Account 0x97F272ccfef4026A1F3f0e0E879d514627B84E69 was succesfully added as an gateway relayer

```

</p>
</details>

<br />


> [!NOTE]
> If you get `override the existing name orchestrator [y/N]: `, just ^C and run `make clean-node-storage` to remove the state.
# Init fhEVM

```bash
make init-ethermint-node
```

Then some tests, only ERC20 and rand for now:
Initialize and generate/copy FHE keys based on `KEY_GEN` value in `.env`.

> [!NOTE]
> If KEY_GEN is set to `false`, ensure to have 15 GB of empty RAM to generate the keys. On Mac, do not forget to increase the allocated RAM to docker process.
## Run fhEVM + KMS components

```bash
make run-full
# Check the logs for the node
docker logs zama-kms-validator-1 -f
# Check logs for Gateway
docker logs zama-kms-gateway-1 -f
```

You should see the following docker images:

```
zama-kms-gateway-1 ghcr.io/zama-ai/kms-blockchain-gateway-dev:aa90d98
zama-kms-connector-1 ghcr.io/zama-ai/kms-blockchain-connector-dev:50872c4
zama-kms-validator-1 ghcr.io/zama-ai/ethermint-node:v0.5.0
zama-kms-core-1 ghcr.io/zama-ai/kms-service-dev:aa90d98
zama-kms-kv-store-1 ghcr.io/zama-ai/kms-blockchain-gateway-dev:aa90d98
zama-kms-blockchain-validator-1 ghcr.io/zama-ai/kms-blockchain-asc-dev:50872c4
```

## Stop fhEVM + KMS

```bash
EncryptedERC20
✔ should mint the contract (5083ms)
✔ should transfer tokens between two users (12342ms)
✔ should not transfer tokens between two users (12324ms)
✔ should be able to transferFrom only if allowance is sufficient (23991ms)
make stop-full
```

## Fresh start

Rand
✔ 8 bits generate and decrypt (25643ms)
✔ 8 bits generate with upper bound and decrypt (25160ms)
✔ 16 bits generate and decrypt (25706
```bash
make clean
```

> [!NOTE]
> FHE keys are in res/keys folder, delete them to regenerate new keys at ```make init-ethermint-node``` step.

## Test using fhevm

```bash
# if not executed before
make init-ethermint-node
# if not executed before
make run-full
# In new terminal
make run-e2e-test
```

or in one command

```bash
make e2e-test
```



56 changes: 56 additions & 0 deletions default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
tick_interval_secs = 1
storage_path = "./temp/events.toml"

[oracle]
addresses = ["http://validator:26657"]

[blockchain]
addresses = ["http://blockchain-validator:9090"]
contract = "wasm14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s0phg4d"

[blockchain.signkey]
# It should be either mnemonic or bip32
mnemonic = "bachelor similar spirit copper rely carbon web hobby conduct wrap conduct wire shine parrot erosion divert crucial balance lock reason price ignore educate open"
bip32 = "xprv9s21ZrQH143K3FQVQn1Z6"

[blockchain.fee]
denom = "ucosm"
amount = 3000000

[core]
addresses = ["http://localhost:50051"]

[core.timeout_config]
channel_timeout = 60

[core.timeout_config.crs]
initial_wait_time = 60
retry_interval = 60
max_poll_count = 120

[core.timeout_config.keygen]
initial_wait_time = 18000
retry_interval = 15000
max_poll_count = 1150

[core.timeout_config.preproc]
initial_wait_time = 18000
retry_interval = 15000
max_poll_count = 1150

[core.timeout_config.decryption]
initial_wait_time = 1
retry_interval = 1
max_poll_count = 24

[core.timeout_config.reencryption]
initial_wait_time = 1
retry_interval = 1
max_poll_count = 24

[tracing]
service_name = "kms-asc-connector"
endpoint = "http://localhost:4317"

[store]
url = "http://store:8088"
Loading

0 comments on commit 4994e73

Please sign in to comment.