Skip to content

Commit

Permalink
Merge branch 'main' into randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
eshelB authored Oct 27, 2024
2 parents 8e1fe17 + 4efa5c9 commit b1e859c
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 33 deletions.
14 changes: 10 additions & 4 deletions docs/devdocs/Examples and References/Examples-fheDapps.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Here you can find a list of some cool apps that you can use as a reference
<td>FHERC-20</td>
<td><a href="https://github.com/FhenixProtocol/example-contracts/blob/master/wrapping-ERC20/WrappingERC20.sol">View on Github</a><br /></td>
<td><a href="http://fhenix-demo.pages.dev/">FHERC-20 Demo</a></td>
<td></td>
<td>A token in which balances are encrypted and viewable only to the holder.</td>
</tr>
<tr>
<td>Blind Auction</td>
<td><a href="https://github.com/FhenixProtocol/blind-auction-example">View on Github</a></td>
<td><a href="https://github.com/FhenixProtocol/blind-auction-example/tree/main/frontend">View on Github</a></td>
<td></td>
<td>An auction in which is bid is kept encrypted until the Auction ends and the winner is revealed.</td>
</tr>

[//]: # (<tr>)
Expand All @@ -43,13 +43,13 @@ Here you can find a list of some cool apps that you can use as a reference
<td>Confidential Voting</td>
<td><a href="https://github.com/FhenixProtocol/confidential-voting">View on Github</a></td>
<td></td>
<td></td>
<td>A voting in which the individual votes are kept secret.</td>
</tr>
<tr>
<td>Simple Lottery</td>
<td><a href="https://github.com/FhenixProtocol/example-contracts/blob/master/lottery/Lottery.sol">View on Github</a></td>
<td></td>
<td></td>
<td>A very simple game leveraging the fact that you can pick a winning number and keep it private.</td>
</tr>
<tr>
<td>Contract Playground</td>
Expand All @@ -63,6 +63,12 @@ Here you can find a list of some cool apps that you can use as a reference
<td><a href="https://dapps.zama.ai/">https://dapps.zama.ai/</a><br /></td>
<td>NOTE: These examples are not directly compatible with Fhenix and must be adapted</td>
</tr>
<tr>
<td>Unique Bid Auction</td>
<td><a href="https://github.com/Syndika-Corp/fhenix-contracts">View on Github</a></td>
<td><a href="https://fhenix.netlify.app/">Bids_Party</a><br /></td>
<td>A showcase of a blind bid auction using a unique bidding mechanism</td>
</tr>


[//]: # (<tr>)
Expand Down
6 changes: 3 additions & 3 deletions docs/devdocs/FhenixJS/Decryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The data is returned to the user using [sealed box encryption ](https://bitbeans

:::tip[Don't Want to Seal?]
Fhenix supports standard decryption as well. Mostly suited for public data, an unsealed plaintext value can be returned from a contract.
You can read more about how to do this [here](#../).
You can read more about how to do this [here](../Writing%20Smart%20Contracts/Returning-Data.md).
:::

### Encrypted Values & Permits
Expand All @@ -25,7 +25,7 @@ The main difference with inEuint* is that you can be explicit with what is the e

A `Permit` is a data structure that helps contracts know who is trying to call a specific function.

The fhenix.js Javascript library includes methods to support creating parameters for values that require [Permits & Access Control](../Encryption and Privacy/Permits-Access-Control.md). These methods can help creating ephemeral transaction keys, which are used by the smart contract to create a secure encryption channel to the caller.
The fhenix.js Javascript library includes methods to support creating parameters for values that require [Permits & Access Control](../Encryption%20and%20Privacy/Permits-Access-Control.md). These methods can help creating ephemeral transaction keys, which are used by the smart contract to create a secure encryption channel to the caller.
Similarly to decryption, this usage can be implemented by any compliant library, but we include direct support in fhenix.js.&#x20;

This is done in 3 steps: generating a permit, querying the contract and unsealing the data.
Expand All @@ -49,7 +49,7 @@ When you create a permit it gets stored in `localstorage`. This makes permits ea

#### 2. Querying the Contract

We recommend that contracts implement the Permit/Permission interfaces (though this is not strictly required!).
We recommend that contracts implement the Permit/Permission interfaces (though this is not strictly required).
In this case, we can easily inject our permit into the function call.

```javascript
Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/FhenixJS/Encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Encryption in Fhenix is done using the global chain key. This key is loaded when

When we perform encryption, we specify the type of `euint` (Encrypted Integer) we want to create. This should match the expected type in the Solidity contract we are working with.

First, initialize the library -
First, initialize the library:

```Typescript
import { FhenixClient } from 'fhenixjs';
Expand Down
13 changes: 11 additions & 2 deletions docs/devdocs/FhenixJS/Network Keys.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,32 @@ If you're using fhenix.js you don't need to worry about this, as the public key

##### Fetching the Public Key Manually

However, if you're using interfacing with Fhenix directly, you'll need to fetch the public key from the network you're connecting to. This can be done by calling the `getPublicKey` function on the network you're connecting to.
However, if you're using interfacing with Fhenix directly, you'll need to fetch the public key from the network you're connecting to. This can be done by calling the `getNetworkPublicKey` function on the network you're connecting to.

The Public Key is constant for the lifetime of the network, but still has to be fetched once by the user to be able to encrypt data. To do this, we use a special precompiled function that can be accessed programmatically in the following way:

<Tabs>
<TabItem value="ethers.js" label="ethers.js">
```javascript
const networkPkAbi = new Interface(["function getNetworkPublicKey()"])
let result = await provider.call({
to: "0x0000000000000000000000000000000000000080",
data: networkPkAbi.encodeFunctionData("getNetworkPublicKey");
});
```
</TabItem>
<TabItem value="web3.js" label="Web3.js">
```javascript
const networkPkSig = web3.eth.abi.encodeFunctionSignature("getNetworkPublicKey()");
let result = await web3.eth.call({
to: "0x0000000000000000000000000000000000000080"
to: "0x0000000000000000000000000000000000000080",
data: networkPkSig
});
```
</TabItem>
<TabItem value="cast (Foundry)" label="cast (Foundry)">
```shell
cast call 0x0000000000000000000000000000000000000080 --rpc-url "https://api.helium.fhenix.zone" "getNetworkPublicKey()"
```
</TabItem>
</Tabs>
10 changes: 5 additions & 5 deletions docs/devdocs/Setting Up Your Environment/Foundry.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ This tool will allow you to simulate development on the Fhenix network including
The tool uses Foundry Cheatcodes to represent the execution of our Fhenix native precompiles.
Please be aware that the operations performed by using this template only simulate the behavior of real FHE operations, and will probably not be a good representation of performance, gas, etc.

The code is pretty new, and may contain bugs or edge-cases that we have not tested, so your feedback is very important for us! If you have any issues, comments or requests please open an issue in the [Fhenix Hardhat Template Repository](https://github.com/FhenixProtocol/fhenix-foundry-template)
The code is pretty new, and may contain bugs or edge-cases that we have not tested, so your feedback is very important for us! If you have any issues, comments or requests please open an issue
in the Fhenix Foundry Template [Repository](https://github.com/FhenixProtocol/fhenix-foundry-template).

## Getting Started

To create a new repository using our [Fhenix Hardhat Template](https://github.com/FhenixProtocol/fhenix-foundry-template), click the
To create a new repository using our [Fhenix Foundry Template](https://github.com/FhenixProtocol/fhenix-foundry-template), click the
[`Use this template`](https://github.com/fhenixprotocol/fhenix-foundry-template/generate) button.
Alternatively, install the template manually as follows:

Expand All @@ -27,10 +28,9 @@ If this is your first time using Foundry, refer to the
- Simulated FHE Operations: All FHE operations, including encryption, decryption, and encrypted data handling, are
simulated to replicate their behavior in a network environment. This approach facilitates seamless development and
testing without requiring a fully operational FHE network.
- Permissions: The template includes utilities (PermissionHelper.sol) for creating permissions related to FHE
- Permissions: The template includes utilities (`PermissionHelper.sol`) for creating permissions related to FHE
operations. These utilities enable users to test and verify that contracts correctly implement access-controlled
actions, such as viewing balances of encrypted tokens. For more about permissions, see the [Fhenix Documentation] https://docs.fhenix.zone/docs/devdocs/Writing%20Smart%20Contracts/Permissions)
section.
actions, such as viewing balances of encrypted tokens. For more about permissions, see [here](../Writing%20Smart%20Contracts/Permissions.md).

## Writing Tests

Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Setting Up Your Environment/Gitpod.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ got unsealed result: 10

## Next Steps

To learn more about the Fhenix Hardhat Plugin, please visit the [Fhenix Hardhat Plugin](/docs/fhenix-hardhat-plugin) or learn more about [Developing dApps with Fhenix](/docs/developing-dapps-with-fhenix).
To learn more about the Fhenix Hardhat Plugin, please visit the [Fhenix Hardhat Plugin](../Tools%20and%20Utilities/Fhenix-Hardhat-Plugin.md).
2 changes: 1 addition & 1 deletion docs/devdocs/Tools and Utilities/Fhenix-Hardhat-Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To use FhenixJS in your Hardhat project, first install the plugin via npm (or yo
pnpm install fhenix-hardhat-plugin
```

If you wish to run your own local dev environment, please install the [fhenix-hardhat-docker](https://github.com/fhenixprotocol/fhenix-hardhat-docker) plugin as well.
If you wish to run your own local dev environment, please install the [fhenix-hardhat-docker](https://www.npmjs.com/package/fhenix-hardhat-docker) plugin as well.

```sh
pnpm install fhenix-hardhat-docker
Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Tutorials/Basic/Deploying.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TabItem from '@theme/TabItem';

Now that we have our completed WrappingERC20 token, the next step is to see if our code actually works!

To do this, we'll be writing tests in typescript using hardhat, and deploying them on our [LocalFhenix](../../Setting%20Up%20Your%20Environment/Hardhat.md) environment which we set up earlier.
To do this, we'll be writing tests in typescript using hardhat, and deploying them on our [LocalFhenix](../../Setting%20Up%20Your%20Environment/Hardhat.md#start-localfhenix) environment which we set up earlier.

:::tip[Note]
At this stage, using hardhat network is not supported, as Fhenix uses custom extensions to the EVM that enable FHE operations
Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Tutorials/Basic/Setting Up.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Change the directory into the repository and install the project dependencies

### I'm more of a Free Spirit

Alternative set up paths can be just cloning or forking the example repository, or starting from scratch using our [fhenix hardhat plugin](../../devdocs/docs/hardhat), [Gitpod environment](../../devdocs/docs/gitpod) or Github Workspaces
Alternative set up paths can be just cloning or forking the example repository, or starting from scratch using our [Fhenix hardhat plugin](../../Tools%20and%20Utilities/Fhenix-Hardhat-Plugin.md), [Gitpod environment](../../Setting%20Up%20Your%20Environment/Gitpod.mdx) or Github Workspaces

## Configuring and Starting Localfhenix
In order for us to locally deploy and test contracts we will need to run our own node, for this specific use-case we have a docker image named `localfhenix`.
Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Tutorials/Basic/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FhenixJS is injected by the Fhenix Hardhat plugin and can be used automatically
We will break down each step, providing code snippets and explanations to ensure you understand how to test the contract effectively.

:::note
At the moment, only Hardhat is supported as a full testing environment. Stay tuned for Foundry support in the future.
This section describes testing in a Hardhat project. For instructions on testing with Foundry, check [this](../../Setting%20Up%20Your%20Environment/Foundry.md#writing-tests).
:::

## Step-by-Step Guide
Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Tutorials/Your-First-FHE-Contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ If you want to learn more about working with Fhenix, please check out [docs for

### Have Questions?

Hop into our [Discord](#) and ask questions in the #dev-general or #tech-questions channels!
Hop into our [Discord](https://discord.gg/FuVgxrvJMY) and ask questions in the `#dev-general` or `#tech-questions` channels!
2 changes: 1 addition & 1 deletion docs/devdocs/Writing Smart Contracts/FHE-sol.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The currently supported types are `inEuint8`, `inEuint16`, `inEuint32`, `inEuint
- **Purpose**: Converts a plaintext number, encrypted variable or an `inEaddress` encrypted input into an `eaddress` type.

### `decrypt` - Decrypt Encrypted Data
- **Purpose**: Decrypts `euint`, `ebool` or `eaddress` encrypted value back to its plaintext form. If the value should only be revealed to a specific address, the `sealoutput` function should be used instead. Learn more abut sealing [here](../Returning-Data).
- **Purpose**: Decrypts `euint`, `ebool` or `eaddress` encrypted value back to its plaintext form. If the value should only be revealed to a specific address, the `sealoutput` function should be used instead. Learn more abut sealing [here](./Returning-Data.md).

### Arithmetic Operations
FHE.sol supports encrypted arithmetic operations like addition and subtraction. These operations can be performed directly on `euint` types, enabling encrypted computations.
Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Writing Smart Contracts/Returning-Data.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This means that we can consume sealed data directly from Metamask, which provide

Fetch an address's public key using the `eth_getEncryptionPublicKey` method, seal the data for that specific public key (either as a permit or by using the public key directly), and then use Metamask's `eth_decrypt` call to provide a guided decryption experience.

:::danger[Warning]
:::warning[Warning]
Metamask's `eth_getEncryptionPublicKey` and `eth_decrypt` methods are deprecated. We provide these examples to demonstrate compatibility with native wallet encryption/decryption procedures. We aim to maintain compatibility as new standards emerge for encryption on Ethereum.
:::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Note that all functions are supported in both direct function calls and library
| Seal Output | `sealOutput` | n/a | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> |
| Randomness | `randomEuintX` | n/a | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <g>✔</g> | <r>✘</r> | <r>✘</r> |

:::danger
:::danger[Caveat]
At the moment it is not possible to do `ebool result = (lhs == rhs)` and others that return a boolean result. This is because FHE.sol expects a `ebool`, while Solidity only allows overloading to return a regular boolean.
Instead, we recommend `ebool result = lhs.eq(rhs)`.
:::
Expand Down
47 changes: 41 additions & 6 deletions docs/devdocs/Writing Smart Contracts/Useful-Tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ description: Tidbits of wisdom for working with FHE

## Trivial Encryption

When we are using `FHE.asEuint*(PLAINTEXT_NUMBER)` we are actually using a trivial encryption of our FHE scheme. Unlike normal FHE encryption trivial encryption is a deterministic encryption. The meaning is that if you will do it twice you will still get the same result
When we are using `FHE.asEuintX(plaintext_number)` we are actually using a trivial encryption of our FHE scheme. Unlike normal FHE encryption trivial encryption is a deterministic encryption. The meaning is that if you will do it twice you will still get the same result

## Default Value of a Euint

When having a `euint*` variable uninitialized it will be considered as 0. Every FHE function that will receive an uninitialized euint* will assume it is `FHE.asEuint*(0)`.
You can assume now that `FHE.asEuint*(0)`is used quite often - Luckily we realized this and decided to have the values of`FHE.asEuint*(0)`pre-calculated on node initialization so when you use`FHE.asEuint\*(0)` we will just return those values.
When having a `euintx` variable uninitialized it will be considered as 0. Every FHE function that will receive an uninitialized `euintx` will assume it is `FHE.asEuintX(0)`.
You can assume now that `FHE.asEuintX(0)`is used quite often - Luckily we realized this and decided to have the values of `FHE.asEuintX(0)` pre-calculated on node initialization so when you use`FHE.asEuintX(0)` we will just return those values.

## Re-encrypting a Value

Expand All @@ -28,14 +28,14 @@ Now that we know that, we can add 0 (cryptographically with FHE.add) to all of t
All the operations are supported both in TXs and in Queries. That being said we strongly advise to think twice before you use those operations inside a TX. `FHE.req` is actually exposing the value of your encrypted data. Assuming we will send the transaction and monitor the gas usage we can probably identify whether the `FHE.req` condition met or not and understand a lot about what the encrypted values represent.
Example:

```javascript
```solidity
function f(euint8 a, euint8 b) public {
FHE.req(a.eq(b));
// Do some heavy logic
}
```

In this case, if `a` and `b` won't be equal it will fail immediately and take less gas than the case when `a` and `b` are equal which means that one who checks the gas can easily know the equality of `a` and `b` it won't leak their values but it will leak confidential data.
In this case, if `a` and `b` won't be equal it will fail immediately and take less gas than the case when `a` and `b` are equal which means that one who checks the gas can easily know the equality of `a` and `b` it won't leak their values, but it will leak confidential data.
The rule of thumb that we are suggesting is to use `FHE.req` only in `view` functions while the logic of `FHE.req` in txs can be implemented using `FHE.select`

## FHE.decrypt()
Expand All @@ -55,4 +55,39 @@ Currently, we support many FHE operations. Some of them might take a lot of time
When writing FHE code we encourage you to use the operations wisely and choose what operation should be used.
Example: Instead of `ENCRYPTED_UINT_32 * FHE.asEuint32(2)` you can use `FHE.shl(ENCRYPTED_UINT_32, FHE.asEuint32(1))` in some cases `FHE.div(ENCRYPTED_UINT_32, FHE.asEuint32(2))` can be replaced by `FHE.shr(ENCRYPTED_UINT_32, FHE.asEuint32(1))`

For more detailed benchmarks please refer to: [Gas-and-Benchmarks](./Gas-and-Benchmarks)
For more detailed benchmarks please refer to: [Gas and Benchmarks](./Gas-and-Benchmarks)

## Randomness

Confidentiality is a crucial step in order to achieve on-chain randomness. Fhenix, as a chain that implements confidentiality, is a great space to implement and use on-chain random numbers and this is part of our roadmap.
We know that there are some #BUIDLers that are planning to implement dapps that leverage both confidentiality and random numbers so until we will have on-chain true random, we are suggesting to use the following implementation as a MOCKUP.

:::danger
PLEASE NOTE THAT THIS RANDOM NUMBER IS VERY PREDICTABLE AND SHOULD NOT BE USED IN PRODUCTION.
:::

```solidity
library RandomMock {
function getFakeRandom() internal returns (uint256) {
uint blockNumber = block.number;
uint256 blockHash = uint256(blockhash(blockNumber));
return blockHash;
}
function getFakeRandomU8() public view returns (euint8) {
uint8 blockHash = uint8(getFakeRandom());
return FHE.asEuint8(blockHash);
}
function getFakeRandomU16() public view returns (euint16) {
uint16 blockHash = uint16(getFakeRandom());
return FHE.asEuint16(blockHash);
}
function getFakeRandomU32() public view returns (euint32) {
uint32 blockHash = uint32(getFakeRandom());
return FHE.asEuint32(blockHash);
}
}
```
Loading

0 comments on commit b1e859c

Please sign in to comment.