Skip to content

Commit edf7961

Browse files
authored
docs(common): updates inputs (#302)
* docs(common): updates input * docs(common): updates introduction README
1 parent b4e31de commit edf7961

File tree

2 files changed

+25
-41
lines changed

2 files changed

+25
-41
lines changed

docs/introductions/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,18 @@ like this:
4848
// SPDX-License-Identifier: BSD-3-Clause-Clear
4949
pragma solidity ^0.8.24;
5050
51-
import { FHE } from "@fhevm/solidity/lib/FHE.sol";
51+
import { FHE, euint64 } from "@fhevm/solidity/lib/FHE.sol";
5252
import { SepoliaConfig } from "@fhevm/solidity/config/ZamaConfig.sol";
5353
54-
contract MyCounter is SepoliaZamaFHEVMConfig {
54+
contract MyCounter is ZamaConfig {
5555
euint64 counter;
5656
constructor() {
5757
counter = FHE.asEuint64(0);
5858
}
5959
6060
function add() public {
6161
counter = FHE.add(counter, 1);
62+
FHE.allowThis(counter);
6263
}
6364
}
6465
```

docs/solidity-guides/inputs.md

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ When a function in a smart contract is called, it may accept two types of parame
2828
Here’s an example of a Solidity function accepting multiple encrypted parameters:
2929

3030
```solidity
31-
function myExample(
32-
address account,
33-
uint id,
34-
bool isAllowed,
31+
function exampleFunction(
3532
externalEbool param1,
3633
externalEuint64 param2,
3734
externalEuint8 param3,
@@ -43,56 +40,43 @@ function myExample(
4340

4441
In this example, `param1`, `param2`, and `param3` are encrypted inputs for `ebool`, `euint64`, and `euint8` while `inputProof` contains the corresponding ZKPoK to validate their authenticity.
4542

46-
## Client-Side implementation
43+
### Input Generation using Hardhat
4744

48-
To interact with such a function, developers can use the [@fhevm/sdk](https://github.com/zama-ai/fhevmjs) library to create and manage encrypted inputs. Below is an example implementation:
45+
In the below example, we use Alice's address to create the encrypted inputs and submits the transaction.
4946

50-
```javascript
51-
import { createInstances } from "../instance";
52-
import { getSigners, initSigners } from "../signers";
47+
```typescript
48+
import * as hre from "hardhat";
49+
import { fhevm } from "hardhat";
5350

54-
await initSigners(); // Initialize signers
55-
const signers = await getSigners();
51+
const input = hre.fhevm.createEncryptedInput(contract.address, signers.alice.address);
52+
input.add64(transferAmount);
53+
const encryptedInput = await input.encrypt();
5654

57-
const instance = await createInstances(this.signers);
58-
// Create encrypted inputs
59-
const input = instance.createEncryptedInput(contractAddress, userAddress);
60-
const inputs = input.add64(64).addBool(true).add8(4).encrypt(); // Encrypt the parameters
61-
62-
// Call the smart contract function with encrypted inputs
63-
contract.myExample(
64-
"0xa5e1defb98EFe38EBb2D958CEe052410247F4c80", // Account address
65-
32, // Plaintext parameter
66-
true, // Plaintext boolean parameter
67-
inputs.handles[0], // Handle for the first parameter
68-
inputs.handles[1], // Handle for the second parameter
69-
inputs.handles[2], // Handle for the third parameter
70-
inputs.inputProof, // Proof to validate all encrypted inputs
71-
);
55+
tx = await myContract
56+
.connect(signers.alice)
57+
[
58+
"exampleFunction(bytes32,bytes32,bytes32,bytes)"
59+
](signers.bob.address, encryptedInput.handles[0], encryptedInput.handles[1], encryptedInput.handles[2], encryptedTransferAmount.inputProof);
60+
61+
await tx.wait();
7262
```
73-
74-
In this example:
75-
76-
- **`add64`, `addBool`, and `add8`**: Specify the types and values of inputs to encrypt.
77-
- **`encrypt`**: Generates the encrypted inputs and the zero-knowledge proof.
78-
7963
## Validating encrypted inputs
8064

8165
Smart contracts process encrypted inputs by verifying them against the associated zero-knowledge proof. This is done using the `FHE.asEuintXX`, `FHE.asEbool`, or `FHE.asEaddress` functions, which validate the input and convert it into the appropriate encrypted type.
8266

83-
### Example validation that goes along the client-Side implementation
67+
### Example validation
8468

8569
This example demonstrates a function that performs multiple encrypted operations, such as updating a user's encrypted balance and toggling an encrypted boolean flag:
8670

8771
```solidity
8872
function myExample(
89-
externalEuint64,
73+
externalEuint64 encryptedAmount,
9074
externalEbool encryptedToggle,
9175
bytes calldata inputProof
9276
) public {
9377
// Validate and convert the encrypted inputs
94-
euint64 amount = FHE.asEuint64(encryptedAmount, inputProof);
95-
ebool toggleFlag = FHE.asEbool(encryptedToggle, inputProof);
78+
euint64 amount = FHE.fromExternal(encryptedAmount, inputProof);
79+
ebool toggleFlag = FHE.fromExternal(encryptedToggle, inputProof);
9680
9781
// Update the user's encrypted balance
9882
balances[msg.sender] = FHE.add(balances[msg.sender], amount);
@@ -110,10 +94,9 @@ This example demonstrates a function that performs multiple encrypted operations
11094
function getEncryptedFlag() public view returns (ebool) {
11195
return userFlags[msg.sender];
11296
}
113-
}
11497
```
11598

116-
### Example validation in the `encryptedERC20.sol` smart contract
99+
### Example validation in the `ConfidentialERC20.sol` smart contract
117100

118101
Here’s an example of a smart contract function that verifies an encrypted input before proceeding:
119102

@@ -134,7 +117,7 @@ function transfer(
134117
### How validation works
135118

136119
1. **Input verification**:\
137-
The `FHE.asEuintXX` function ensures that the input is a valid ciphertext with a corresponding ZKPoK.
120+
The `FHE.fromExternal` function ensures that the input is a valid ciphertext with a corresponding ZKPoK.
138121
2. **Type conversion**:\
139122
The function transforms `externalEbool`, `externalEaddress`, `externalEuintXX` into the appropriate encrypted type (`ebool`, `eaddress`, `euintXX`) for further operations within the contract.
140123

0 commit comments

Comments
 (0)