Skip to content

Commit 9588821

Browse files
committed
doc: start adding erc20 benchmarks
1 parent e12638d commit 9588821

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

tfhe/docs/getting-started/benchmarks/cpu/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ All CPU benchmarks were launched on an `AWS hpc7a.96xlarge` instance equipped wi
99
{% endhint %}
1010

1111
* [Integer operations](cpu-integer-operations.md)
12+
* [ERC20](cpu-erc20.md)
1213
* [Programmable Bootstrapping](cpu-programmable-bootstrapping.md)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
As TFHE-rs is the underlying library of the Zama Confidential Blockchain Protocol, to illustrate real-world performance,
2+
consider an ERC20 transfer that requires executing the following sequence of operations:
3+
```rust
4+
fn erc20_transfer_whitepaper(
5+
from_amount: &FheUint64,
6+
to_amount: &FheUint64,
7+
amount: &FheUint64,
8+
) -> (FheUint64, FheUint64) {
9+
let has_enough_funds = (from_amount).ge(amount);
10+
11+
let mut new_to_amount = to_amount + amount;
12+
new_to_amount = has_enough_funds.select(&new_to_amount, to_amount);
13+
14+
let mut new_from_amount = from_amount - amount;
15+
new_from_amount = has_enough_funds.select(&new_from_amount, from_amount);
16+
17+
(new_from_amount, new_to_amount)
18+
}
19+
```
20+
This is one way to compute an encrypted ERC20 transfer, but it is not the most efficient.
21+
Instead, it is possible to compute the same transfer in a more efficient way by not using the `select` operation:
22+
```rust
23+
fn erc20_transfer_no_select(
24+
from_amount: &FheUint64,
25+
to_amount: &FheUint64,
26+
amount: &FheUint64,
27+
) -> (FheUint64, FheUint64) {
28+
let has_enough_funds = (from_amount).ge(amount);
29+
30+
let amount = amount * FheType::cast_from(has_enough_funds);
31+
32+
let new_to_amount = to_amount + &amount;
33+
let new_from_amount = from_amount - &amount;
34+
35+
(new_from_amount, new_to_amount)
36+
}
37+
```
38+
An even more efficient way to compute an encrypted ERC20 transfer is to use the `overflowing_sub` operation as follows:
39+
```rust
40+
use tfhe::FheUint64;
41+
fn erc20_transfer_overflow(
42+
from_amount: &FheUint64,
43+
to_amount: &FheUint64,
44+
amount: &FheUint64,
45+
) -> (FheUint64, FheUint64) {
46+
let (new_from, did_not_have_enough) = (from_amount).overflowing_sub(amount);
47+
let did_not_have_enough = &did_not_have_enough;
48+
let had_enough_funds = !did_not_have_enough;
49+
50+
let (new_from_amount, new_to_amount) = rayon::join(
51+
|| did_not_have_enough.if_then_else(from_amount, &new_from),
52+
|| to_amount + (amount * FheType::cast_from(had_enough_funds)),
53+
);
54+
(new_from_amount, new_to_amount)
55+
}
56+
```
57+
In a blockchain protocol, the FHE operations would not be the only ones used to compute the transfer:
58+
ciphertext compression and decompression, as well as rerandomization, would also be used.
59+
Network communications would also introduce significant overhead.
60+
For the sake of simplicity, here the focus is only placed on the performance of the FHE operations.
61+
The latency and throughput of these three ERC20 FHE transfer implementations are compared in the following table:
62+
63+
TODO add SVG
64+
65+
The throughput shown here is the maximum that can be achieved with TFHE-rs on CPU, in an ideal scenario.
66+
In a blockchain protocol, the throughput would be limited by the latency of the network.

tfhe/docs/getting-started/benchmarks/gpu/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ All GPU benchmarks were launched on H100 GPUs, and rely on the multithreaded PBS
99
{% endhint %}
1010

1111
* [Integer operations](gpu-integer-operations.md)
12+
* [ERC20](gpu-erc20.md)
1213
* [Programmable Bootstrapping](gpu-programmable-bootstrapping.md)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Similarly to the [CPU benchmarks](../cpu/cpu-erc20.md), the latency and throughput of a confidential ERC20 token transfer can be measured.
2+
3+
TODO add SVG
4+
5+
The throughput shown here is the maximum that can be achieved with TFHE-rs on an 8xH100 GPU node, in an ideal scenario.
6+
In a blockchain protocol, the throughput would be limited by the latency of the network and the necessity to apply
7+
other operations (compression, decompression, rerandomization).

tfhe/docs/getting-started/benchmarks/hpu/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ All HPU benchmarks were launched on AMD Alveo v80 FPGAs.
99
{% endhint %}
1010

1111
* [Integer operations](hpu-integer-operations.md)
12+
* [ERC20](hpu-erc20.md)

tfhe/docs/getting-started/benchmarks/hpu/hpu-erc20.md

Whitespace-only changes.

tfhe/src/test_user_docs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ mod test_cpu_doc {
1515
configuration_rust_configuration
1616
);
1717

18+
// BENCHMARKS
19+
doctest!(
20+
"../docs/getting-started/benchmarks/cpu/cpu-erc20.md",
21+
benchmarks_cpu_erc20
22+
);
23+
1824
// FHE COMPUTATION
1925

2026
// ADVANCED FEATURES

0 commit comments

Comments
 (0)