Zama 백서

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 31쪽

y the gateway listener. | `true` | | `kmsConnectorGwListener.image.name` | The docker imagename for the gateway listener. | `ghcr.io/zama-ai/fhevm/kms-connector/gw-listener` | | `kmsConnectorGwListener.image.tag` | The docker image tag for the gateway listener. | `v0.9.0` | | `kmsConnectorGwListener.replicas` | The number of replicas for the gateway listener. | `1` | | `kmsConnectorKmsWorker.enabled` | If `true`, deploy the KMS worker. | `true` | | `kmsConnectorKmsWorker.image.name` | The docker image name for the KMS worker. | `ghcr.io/zama-ai/fhevm/kms-connector/kms-worker` | | `kmsConnectorKmsWorker.image.tag` | The docker image tag for the KMS worker. | `v0.9.0` | | `kmsConnectorKmsWorker.replicas` | The number of replicas for the KMS worker. | `1` | | `kmsConnectorTxSender.enabled` | If `true`, deploy the transaction sender. | `true` | | `kmsConnectorTxSender.image.name` | The docker image name for the transaction sender. | `ghcr.io/zama-ai/fhevm/kms-connector/tx-sender` | | `kmsConnectorTxSender.image.tag` | The docker image tag for the transaction sender. | `v0.9.0` | | `kmsConnectorTxSender.replicas` | The number of replicas for the transaction sender. | `1` | | `kmsConnectorTxSender.awsKms.enabled` | Whether to enable the AWS KMS signer for the transaction sender. | `false` | | `kmsConnectorTxSender.awsKms.configmap.name` | The name of the configmap containing the AWS KMS Key ID. | `mpc-party` | | `kmsConnectorTxSender.awsKms.configmap.key` | The key in the configmap containing the AWS KMS Key ID. | `KMS_CONNECTOR_AWS_KMS_CONFIG__KEY_ID` | | `kmsConnectorTxSender.wallet.secret.name` | The name of the secret containing the wallet. | `kms-connector-tx-sender` | | `kmsConnectorTxSender.wallet.secret.key` | The key in the secret containing the wallet. | `kms-wallet` | | `podAnnotations` | Annotations to be added to all pods. | `{}` | | `podLabels` | Labels to be added to all pods. | `{}` | # docs # examples # SUMMARY ### Basic * [FHE counter](/protocol/solidity-guides/v0.10/docs/examples/fhe-counter) * FHE Operations * [Add](/protocol/solidity-guides/v0.10/docs/examples/fheadd) * [If then else](/protocol/solidity-guides/v0.10/docs/examples/fheifthenelse) * Encryption * [Encrypt single value](/protocol/solidity-guides/v0.10/docs/examples/fhe-encrypt-single-value) * [Encrypt multiple values](/protocol/solidity-guides/v0.10/docs/examples/fhe-encrypt-multiple-values) * Decryption * [User decrypt single value](/protocol/solidity-guides/v0.10/docs/examples/fhe-user-decrypt-single-value) * [User decrypt multiple values](/protocol/solidity-guides/v0.10/docs/examples/fhe-user-decrypt-multiple-values) * [Public Decrypt single value](/protocol/solidity-guides/v0.10/docs/examples/heads-or-tails) * [Public Decrypt multiple values](/protocol/solidity-guides/v0.10/docs/examples/highest-die-roll) ### OpenZeppelin confidential contracts * [Library installation and overview](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin) * [ERC7984 Standard](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin/erc7984) * [ERC7984 Tutorial](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin/erc7984-tutorial) * [ERC7984 to ERC20 Wrapper](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin/erc7984erc20wrappermock) * [Swap ERC7984 to ERC20](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin/swaperc7984toerc20) * [Swap ERC7984 to ERC7984](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin/swaperc7984toerc7984) * [Vesting Wallet](/protocol/solidity-guides/v0.10/docs/examples/openzeppelin/vesting-wallet) # fhe-counter This example demonstrates how to build an confidential counter using FHEVM, in comparison to a simple counter. {% hint style="info" %} To run this example correctly, make sure the files are placed in the following directories: * `.sol` file → `<your-project-root-dir>/contracts/` * `.ts` file → `<your-project-root-dir>/test/` This ensures Hardhat can compile and test your contracts as expected. {% endhint %} ### A simple counter {% tabs %} {% tab title="counter.sol" %} ```solidity // SPDX-License-Identifier: BSD-3-Clause-Clear pragma solidity ^0.8.24; /// @title A simple counter contract contract Counter { uint32 private _count; /// @notice Returns the current count function getCount() external view returns (uint32) { return _count; } /// @notice Increments the counter by a specific value function increment(uint32 value) external { _count += value; } /// @notice Decrements the counter by a specific value function decrement(uint32 value) external { require(_count >= value, "Counter: cannot decrement below zero"); _count -= value; } } ``` {% endtab %} {% tab title="counter.ts" %} ```ts import { Counter, Counter__factory } from "../types"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { expect } from "chai"; import { ethers } from "hardhat"; type Signers = { deployer: HardhatEthersSigner; alice: HardhatEthersSigner; bob: HardhatEthersSigner; }; async function deployFixture() { const factory = (await ethers.getContractFactory("Counter")) as Counter__factory; const counterContract = (await factory.deploy()) as Counter; const counterContractAddress = await counterContract.getAddress(); return { counterContract, counterContractAddress }; } describe("Counter", function () { let signers: Signers; let counterContract: Counter; before(async function () { const ethSigners: HardhatEthersSigner[] = await ethers.getSigners(); signers = { deployer: ethSigners[0], alice: ethSigners[1], bob: ethSigners[2] }; }); beforeEach(async () => { ({ counterContract } = await deployFixture()); }); it("count should be zero after deployment", async function () { const count = await counterContract.getCount(); console.log(`Counter.getCount() === ${count}`); // Expect initial count to be 0 after deployment expect(count).to.eq(0); }); it("increment the counter by 1", async function () { const countBeforeInc = await counterContract.getCount(); const tx = await counterContract.connect(signers.alice).increment(1); await tx.wait(); const countAfterInc = await counterContract.getCount(); expect(countAfterInc).to.eq(countBeforeInc + 1n); }); it("decrement the counter by 1", async function () { // First increment, count becomes 1 let tx = await counterContract.connect(signers.alice).increment(1); await tx.wait(); // Then decrement, count goes back to 0 tx = await counterContract.connect(signers.alice).decrement(1); await tx.wait(); const count = await counterContract.getCount(); expect(count).to.eq(0); }); }); ``` {% endtab %} {% endtabs %} ### An FHE counter {% tabs %} {% tab title="FHECounter.sol" %} ```solidity // SPDX-License-Identifier: BSD-3-Clause-Clear pragma solidity ^0.8.24; import { FHE, euint32, externalEuint32 } from "@fhevm/solidity/lib/FHE.sol"; import { ZamaEthereumConfig } from "@fhevm/solidity/config/ZamaConfig.sol"; /// @title A simple FHE counter contract contract FHECounter is ZamaEthereumConfig { euint32 private _count; /// @notice Returns the current count function getCount() external view returns (euint32) { return _count; } /// @notice Increments the counter by a specified encrypted value. /// @dev This example omits overflow/underflow checks for simplicity and readability. /// In a production contract, proper range checks should be implemented. function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external { euint32 encryptedEuint32 = FHE.fromExternal(inputEuint32, inputProof); _count = FHE.add(_count, encryptedEuint32); FHE.allowThis(_count); FHE.allow(_count, msg.sender); } /// @notice Decrements the counter by a specified encrypted value. /// @dev This example omits overflow/underflow checks for simplicity and readability. /// In a production contract, proper range checks should be implemented. function decrement(externalEuint32 inputEuint32, bytes calldata inputProof) external { euint32 encryptedEuint32 = FHE.fromExternal(inputEuint32, inputProof); _count = FHE.sub(_count, encryptedEuint32); FHE.allowThis(_count); FHE.allow(_count, msg.sender); } } ``` {% endtab %} {% tab title="FHECounter.ts" %} ```ts import { FHECounter, FHECounter__factory } from "../types"; import { FhevmType } from "@fhevm/hardhat-plugin"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { expect } from "chai"; import { ethers, fhevm } from "hardhat"; type Signers = { deployer: HardhatEthersSigner; alice: HardhatEthersSigner; bob: HardhatEthersSigner; }; async function deployFixture() { const factory = (await ethers.getContractFactory("FHECounter")) as FHECounter__factory; const fheCounterContract = (await factory.deploy()) as FHECounter; const fheCounterContractAddress = await fheCounterContract.getAddress(); return { fheCounterContract, fheCounterContractAddress }; } describe("FHECounter", function () { let signers: Signers; let fheCounterContract: FHECounter; let fheCounterContractAddress: string; before(async function () { const ethSigners: HardhatEthersSigner[] = await ethers.getSigners(); signers = { deployer: ethSigners[0], alice: ethSigners[1], bob: ethSigners[2] }; }); beforeEach(async () => { ({ fheCounterContract, fheCounterContractAddress } = await deployFixture()); }); it("encrypted count should be uninitialized after deployment", async function () { const encryptedCount = await fheCounterContract.getCount(); // Expect initial count to be bytes32(0) after deployment, // (meaning the encrypted count value is uninitialized) expect(encryptedCount).to.eq(ethers.ZeroHash); }); it("increment the counter by 1", async function () { const encryptedCountBeforeInc = await fheCounterContract.getCount(); expect(encryptedCountBeforeInc).to.eq(ethers.ZeroHash); const clearCountBeforeInc = 0; // Encrypt constant 1 as a euint32 const clearOne = 1; const encryptedOne = await fhevm .createEncryptedInput(fheCounterContractAddress, signers.alice.address) .add32(clearOne) .encrypt(); const tx = await fheCounterContract .connect(signers.alice) .increment(encryptedOne.handles[0], encryptedOne.inputProof); await tx.wait(); const encryptedCountAfterInc = await fheCounterContract.getCount(); const clearCountAfterInc = await fhevm.userDecryptEuint( FhevmType.euint32, encryptedCountAfterInc, fheCounterContractAddress, signers.alice, ); expect(clearCountAfterInc).to.eq(clearCountBeforeInc + clearOne); }); it("decrement the counter by 1", async function () { // Encrypt constant 1 as a euint32 const clearOne = 1; const encryptedOne = await fhevm .createEncryptedInput(fheCounterContractAddress, signers.alice.address) .add32(clearOne) .encrypt(); // First increment by 1, count becomes 1 let tx = await fheCounterContract .connect(signers.alice) .increment(encryptedOne.handles[0], encryptedOne.inputProof); await tx.wait(); // Then decrement by 1, count goes back to 0 tx = await fheCounterContract.connect(signers.alice).decrement(encryptedOne.handles[0], encryptedOne.inputProof); await tx.wait(); const encryptedCountAfterDec = await fheCounterContract.getCount(); const clearCountAfterDec = await fhevm.userDecryptEuint( FhevmType.euint32, encryptedCountAfterDec, fheCounterContractAddress, signers.alice, ); expect(clearCountAfterDec).to.eq(0); }); }); ``` {% endtab %} {% endtabs %} # fhe-encrypt-multiple-value This example demonstrates how to build an confidential counter using FHEVM, in comparison to a simple counter. {% hint style="info" %} To run this example correctly, make sure the files are placed in the following directories: * `.sol` file → `<your-project-root-dir>/contracts/` * `.ts` file → `<your-project-root-dir>/test/` This ensures Hardhat can compile and test your contracts as expected. {% endhint %} ### A simple counter {% tabs %} {% tab title="counter.sol" %} ```solidity // SPDX-License-Identifier: BSD-3-Clause-Clear pragma solidity ^0.8.24; /// @title A simple counter contract contract Counter { uint32 private _count; /// @notice Returns the current count function getCount() external view returns (uint32) { return _count; } /// @notice Increments the counter by 1 function increment(uint32 value) external { _count += value; } /// @notice Decrements the counter by 1 function decrement(uint32 value) external { require(_count > value, "Counter: cannot decrement below zero"); _count -= value; } } ``` {% endtab %} {% tab title="counter.ts" %} ```ts import { Counter, Counter__factory } from "../types"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { expect } from "chai"; import { ethers } from "hardhat"; type Signers = { deployer: HardhatEthersSigner; alice: HardhatEthersSigner; bob: HardhatEthersSigner; }; async function deployFixture() { const factory = (await ethers.getContractFactory("Counter")) as Counter__factory; const counterContract = (await factory.deploy()) as Counter; const counterContractAddress = await counterContract.getAddress(); return { counterContract, counterContractAddress }; } describe("Counter", function () { let signers: Signers; let counterContract: Counter; before(async function () { const ethSigners: HardhatEthersSigner[] = await ethers.getSigners(); signers = { deployer: ethSigners[0], alice: ethSigners[1], bob: ethSigners[2] }; }); beforeEach(async () => { ({ counterContract } = await deployFixture()); }); it("count should be zero after deployment", async function () { const count = await counterContract.getCount(); console.log(`Counter.getCount() === ${count}`); // Expect initial count to be 0 after deployment expect(count).to.eq(0); }); it("increment the counter by 1", async function () { const countBeforeInc = await counterContract.getCount(); const tx = await counterContract.connect(signers.alice).increment(1); await tx.wait(); const countAfterInc = await counterContract.getCount(); expect(countAfterInc).to.eq(countBeforeInc + 1n); }); it("decrement the counter by 1", async function () { // First increment, count becomes 1 let tx = await counterContract.connect(signers.alice).increment(); await tx.wait(); // Then decrement, count goes back to 0 tx = await counterContract.connect(signers.alice).decrement(1); await tx.wait(); const count = await counterContract.getCount(); expect(count).to.eq(0); }); }); ``` {% endtab %} {% endtabs %} ### An FHE counter {% tabs %} {% tab title="FHECounter.sol" %} ```solidity // SPDX-License-Identifier: BSD-3-Clause-Clear pragma solidity ^0.8.24; import { FHE, euint32, externalEuint32 } from "@fhevm/solidity/lib/FHE.sol"; import { ZamaEthereumConfig } from "@fhevm/solidity/config/ZamaConfig.sol"; /// @title A simple FHE counter contract contract FHECounter is ZamaEthereumConfig { euint32 private _count; /// @notice Returns the current count function getCount() external view returns (euint32) { return _count; } /// @notice Increments the counter by a specified encrypted value. /// @dev This example omits overflow/underflow checks for simplicity and readability. /// In a production contract, proper range checks should be implemented. function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external { euint32 encryptedEuint32 = FHE.fromExternal(inputEuint32, inputProof); _count = FHE.add(_count, encryptedEuint32); FHE.allowThis(_count); FHE.allow(_count, msg.sender); } /// @notice Decrements the counter by a specified encrypted value. /// @dev This example omits overflow/underflow checks for simplicity and readability. /// In a production contract, proper range checks should be implemented. function decrement(externalEuint32 inputEuint32, bytes calldata inputProof) external { euint32 encryptedEuint32 = FHE.fromExternal(inputEuint32, inputProof); _count = FHE.sub(_count, encryptedEuint32); FHE.allowThis(_count); FHE.allow(_count, msg.sender); } } ``` {% endtab %} {% tab title="FHECounter.ts" %} ```ts import { FHECounter, FHECounter__factory } from "../types"; import { FhevmType } from "@fhevm/hardhat-plugin"; import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers"; import { expect } from "chai"; import { ethers, fhevm } from "hardhat"; type Signers = { deployer: HardhatEthersSigner; alice: HardhatEthersSigner; bob: HardhatEthersSigner; }; async function deployFixture() { const factory = (await ethers.getContractFactory("FHECounter")) as FHECounter__factory; const fheCounterContract = (await factory.deploy()) as FHECounter; const fheCounterContractAddress = await fheCounterContract.getAddress(); return { fheCounterContract, fheCounterContractAddress }; } describe("FHECounter", function () { let signers: Signers; let fheCounterContract: FHECounter; let fheCounterContractAddress: string; before(async function () { const ethSigners: HardhatEthersSigner[] = await ethers.getSigners(); signers = { deployer: ethSigners[0], alice: ethSigners[1], bob: ethSigners[2] }; }); beforeEach(async () => { ({ fheCounterContract, fheCounterContractAddress } = await deployFixture()); }); it("encrypted count should be uninitialized after deployment", async function () { const encryptedCount = await fheCounterContract.getCount(); // Expect initial count to be bytes32(0) after deployment, // (meaning the encrypted count value is uninitialized) expect(encryptedCount).to.eq(ethers.ZeroHash); }); it("increment the counter by 1", async function () { const encryptedCountBeforeInc = await fheCounterContract.getCount(); expect(encryptedCountBeforeInc).to.eq(ethers.ZeroHash); const clearCountBeforeInc = 0; // Encrypt constant 1 as a euint32 const clearOne = 1; const encryptedOne = await fhevm .createEncryptedInput(fheCounterContractAddress, signers.alice.address) .add32(clearOne) .encrypt(); const tx = await fheCounterContract .connect(signers.alice) .increment(encryptedOne.handles[0], encryptedOne.inputProof); await tx.wait(); const encryptedCountAfterInc = await fheCounterContract.getCount(); const clearCountAfterInc = await fhevm.userDecryptEuint( FhevmType.euint32, encryptedCountAfterInc, fheCounterContractAddress, signers.alice, ); expect(clearCountAfterInc).to.eq(clearCountBeforeInc + clearOne); }); it("decrement the counter by 1", async function () { // Encrypt constant 1 as a euint32 const clearOne = 1; const encryptedOne = await fhevm .createEncryptedInput(fheCounterContractAddress, signers.alice.address) .add32(clearOne) .encrypt(); // First increment by 1, count becomes 1 let tx = await fheCounterContract .connect(signers.alice) .increment(encryptedOne.handles[0], encryptedOne.inputProof); await tx.wait(); // Then decrement by 1, count goes back to 0 tx = await fheCounterContract.connect(signers.alice).decrement(encryptedOne.handles[0], encryptedOne.inputProof); await tx.wait(); const encryptedCountAfterDec = await fheCounterContract.getCount(); const clearCountAfterInc = await fhevm.userDecryptEuint( FhevmType.euint32, encryptedCountAfterDec, fheCounterContractAddress, signers.alice, ); expect(clearCountAfterInc).to.eq(0); }); }); ``` {% endtab %} {% endtabs %} # fhe-encrypt-multiple-values This example demonstrates the FHE encryption mechanism with multiple values. {% hint style="info" %} To run this example correctly, make sure the files are placed in the following directories: * `.sol` file → `<your-project-root-dir>/contracts/` * `.ts` file → `<your-project-root-dir>/test/` This ensures Hardhat can compile and test your contracts as expected. {% endhint %} {% tabs %} {% tab title="EncryptMultipleValues.sol" %} ```solidity // SPDX-License-Identifier: BSD-3-Clause-Clear pragma solidity ^0.8.24; import { FHE, externalEbool, externalEuint32, externalEaddress, ebool, euint32, eaddress } from "@fh
프로젝트로 돌아가기