docs: add dot prod & scalar_select

This commit is contained in:
tmontaigu
2025-04-07 10:30:13 +02:00
parent 11a291906e
commit e567b7afd9
4 changed files with 59 additions and 2 deletions

View File

@@ -30,8 +30,9 @@
* [Min/Max operations](fhe-computation/operations/min-max-operations.md)
* [Ternary conditional operations](fhe-computation/operations/ternary-conditional-operations.md)
* [Casting operations](fhe-computation/operations/casting-operations.md)
* [Boolean Operations](fhe-computation/operations/boolean-operations.md)
* [String Operations](fhe-computation/operations/string-operations.md)
* [Boolean operations](fhe-computation/operations/boolean-operations.md)
* [String operations](fhe-computation/operations/string-operations.md)
* [Dot product](fhe-computation/operations/dot-product.md)
* [Core workflow](fhe-computation/compute/README.md)
* [Configuration and key generation](fhe-computation/compute/configure-and-generate-keys.md)
* [Server key](fhe-computation/compute/set-the-server-key.md)

View File

@@ -0,0 +1,37 @@
# Dot Product
This document details the dot product operations supported by **TFHE-rs**.
| name | symbol | type |
|---------------|----------------| ------ |
| Dot Product | `dot_product` | Binary |
Currently, the dot product supports the following case:
- One operand is a slice of `FheBool`
- The other operand is a slice of clear values (e.g., `u64`)
- Both slices must be of the same length
The following example shows how to perform dot product:
```rust
use tfhe::prelude::*;
use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheBool, FheUint8};
fn main() {
let (client_key, server_key) = generate_keys(ConfigBuilder::default());
set_server_key(server_key);
let a = [true, false, true]
.into_iter()
.map(|b| FheBool::encrypt(b, &client_key))
.collect::<Vec<_>>();
let b = [2u8, 3u8, 4u8];
let result = FheUint8::dot_product(&a, &b);
let decrypted: u8 = result.decrypt(&client_key);
assert_eq!(decrypted, 6u8);
}
```

View File

@@ -49,6 +49,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let clear_res: i32 = encrypted_res.decrypt(&client_key);
assert_eq!(clear_res, clear_a);
// Ternary conditional also supports operands that are in clear (except for the condition)
// with the `scalar` prefix
let encrypted_res = &encrypted_comp.scalar_select(&encrypted_a, clear_b);
let clear_res: i32 = encrypted_res.decrypt(&client_key);
assert_eq!(clear_res, clear_a);
let encrypted_res = &encrypted_comp.scalar_select(clear_a, &encrypted_b);
let clear_res: i32 = encrypted_res.decrypt(&client_key);
assert_eq!(clear_res, clear_a);
// When both possible results are in clear the form to be used is
let encrypted_res = FheInt32::select(encrypted_comp, clear_a, clear_b);
let clear_res: i32 = encrypted_res.decrypt(&client_key);
assert_eq!(clear_res, clear_a);
Ok(())
}
```

View File

@@ -109,6 +109,10 @@ mod test_cpu_doc {
"../docs/fhe-computation/operations/string-operations.md",
operations_string_operations
);
doctest!(
"../docs/fhe-computation/operations/dot-product.md",
operations_dot_product
);
// TOOLING
doctest!("../docs/fhe-computation/tooling/debug.md", tooling_debug);