feat(frontends): provide trivial encryption

This commit is contained in:
Alexandre Péré
2024-08-27 16:29:20 +02:00
committed by Quentin Bourgerie
parent a8bba20a7e
commit 20a0c9e0c4
5 changed files with 84 additions and 0 deletions

View File

@@ -258,6 +258,28 @@ for x in range(10):
assert np.array_equal(circuit.encrypt_run_decrypt(x), np.array([[x, x, x], [x, x, x]]) + 1)
```
## fhe.constant(value)
Allows you to create an encrypted constant of a given value.
```python
from concrete import fhe
import numpy as np
@fhe.compiler({"x": "encrypted", "a":"clear"})
def f(x, a):
z = fhe.constant(a)
return x + z
inputset = range(10)
circuit = f.compile(inputset)
for x in range(10):
assert circuit.encrypt_run_decrypt(x, 5) == x + 5
```
This extension is also compatible with constant arrays.
## fhe.hint(value, \*\*kwargs)
Hint properties of a value. Imagine you have this circuit:

View File

@@ -52,6 +52,7 @@ from .extensions import (
LookupTable,
array,
bits,
constant,
conv,
hint,
identity,

View File

@@ -6,6 +6,7 @@ from numpy import where as if_then_else
from .array import array
from .bits import bits
from .constant import constant
from .convolution import conv
from .hint import hint
from .identity import identity, refresh

View File

@@ -0,0 +1,16 @@
"""
Declaration of `constant` functions, to allow server side trivial encryption.
"""
from typing import Any, Union
from ..tracing import Tracer
from .zeros import zeros
def constant(x: Union[Tracer, Any]) -> Union[Tracer, Any]:
"""
Trivial encryption of a cleartext value.
"""
return zeros(() if isinstance(x, int) else x.shape) + x

View File

@@ -0,0 +1,44 @@
"""
Tests of 'constant' extension.
"""
import numpy as np
from concrete import fhe
def test_constant_scalar(helpers):
"""
Test that fhe.constant works with scalars.
"""
configuration = helpers.configuration()
@fhe.compiler({"x": "clear", "y": "encrypted"})
def f(x, y):
return fhe.constant(x) + y
inputset = [(np.random.randint(0, 2**5), np.random.randint(0, 2**5)) for _ in range(100)]
circuit = f.compile(inputset, configuration.fork())
x = np.random.randint(0, 2**5)
y = np.random.randint(0, 2**5)
assert circuit.encrypt_run_decrypt(x, y) == x + y
def test_constant_tensor(helpers):
"""
Test that fhe.constant works with arrays.
"""
configuration = helpers.configuration()
@fhe.compiler({"x": "clear", "y": "encrypted"})
def f(x, y):
return fhe.constant(x) + y
inputset = [
(np.random.randint(0, 2**5, size=10), np.random.randint(0, 2**5, size=10))
for _ in range(100)
]
circuit = f.compile(inputset, configuration.fork())
x = np.random.randint(0, 2**5, size=10)
y = np.random.randint(0, 2**5, size=10)
assert np.all(circuit.encrypt_run_decrypt(x, y) == x + y)