Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Converting-between-Types.md #34

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.