Skip to content

Commit

Permalink
Merge pull request #34 from FhenixProtocol/YonatanE22-patch-2
Browse files Browse the repository at this point in the history
Update Converting-between-Types.md
  • Loading branch information
toml01 authored Oct 28, 2024
2 parents cdaa5c5 + ef43c8f commit 95c1925
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions docs/devdocs/Writing Smart Contracts/Converting-between-Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ description: Converting between different FHE types

# Converting Between Types

As a user of `FHE.sol`, you'll often need to convert between various encrypted types or from plaintext to encrypted form within your contracts. This documentation illustrates how you can leverage the type conversion functions provided in `FHE.sol` to manipulate encrypted data effectively.
Conversion between various encrypted types or from plaintext to encrypted within contracts is frequently required. This section covers procedures for leveraging type conversion functions in `FHE.sol` to manipulate encrypted data effectively.

## Using Conversion Functions

1. **Converting Encrypted to Other Encrypted Types:**

Suppose you have a voting contract and you want to convert an encrypted boolean vote to an encrypted integer before tallying.
The following code shows conversion of an encrypted Boolean vote to an encrypted integer before tallying in a voting smart contract.

- **Contract Example:**
```Javascript
Expand All @@ -27,7 +27,7 @@ Suppose you have a voting contract and you want to convert an encrypted boolean

2. **Converting from Plaintext to Encrypted Type:**

If you're initializing an encrypted counter in a contract, you might start with a plaintext value that needs to be encrypted.
The following code shows initializing an encrypted counter in a smart contract, starting from a plaintext value that needs to be encrypted.

- **Contract Example:**
```Javascript
Expand All @@ -41,21 +41,10 @@ If you're initializing an encrypted counter in a contract, you might start with
}
```

> Note that when converting from plaintext to encrypted, the value is still exposed in plaintext to the contract and on the public blockchain.
> This pattern should only be used when the plaintext data is not sensitive and can be exposed publicly.
>
> After encrypting from plaintext, the resulting encrypted number should be considered public until it is involved in an FHE operation that
> included an input which was sent encrypted to the contract.
### :warning: Important Security Note
When converting from plaintext to encrypted (also called [**Trivial Encryption**](./Useful-Tips.md#trivial-encryption), the value is still in plaintext and exposed to the contract, as well as on the public blockchain. This type of conversion should only be used when plaintext data is not sensitive, and it is safe to expose on a public blockchain.

### Conversion Functions
#### Tips for Users
- **Understand the Types:** Know the types you are working with and the implications of converting between them. Ensure the conversion is logical and secure.
For example, you can only convert addresses to and from the `euint256` type.
- **Monitor Gas Usage:** Be aware of the gas costs associated with type conversions, especially if they occur within functions that are called frequently.
- **Test Thoroughly:** Always test your contracts with various scenarios to ensure that type conversions are behaving as expected.
As a user of `FHE.sol`, understanding and utilizing type conversions is essential for manipulating encrypted data within your smart contracts. By following the examples and best practices provided, you can ensure your contracts are efficient, secure, and functional. Remember to test thoroughly and consider the implications of each conversion to maintain the integrity and reliability of your contract's operations.
## Final Tips​
- **Understand the Data Types**: Know the data types that you are working with and the implications of converting between them. Ensure that the conversion is logical and secure.
- **Monitor Gas Usage**: Be aware of the gas costs associated with specific types of conversions, especially when functions are frequently called.
- **Test Thoroughly**: Always test contracts with various scenarios to ensure that type conversions are behaving as expected.

0 comments on commit 95c1925

Please sign in to comment.