Math (Basic)
add()¶
Return left + right.
This cell verifies the add function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import add
def test_add(left, right):
import numpy as np
res = add(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_add, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = add(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "add: all inputs were skipped — test is broken"
print(f"add tests passed! ({_successes}/{len(inputset)})")
subtract()¶
Return left - right.
This cell verifies the subtract function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import subtract
def test_subtract(left, right):
import numpy as np
res = subtract(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_subtract, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = subtract(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "subtract: all inputs were skipped — test is broken"
print(f"subtract tests passed! ({_successes}/{len(inputset)})")
multiply()¶
Return left * right.
This cell verifies the multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import multiply
def test_multiply(left, right):
import numpy as np
res = multiply(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_multiply, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = multiply(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "multiply: all inputs were skipped — test is broken"
print(f"multiply tests passed! ({_successes}/{len(inputset)})")
negate()¶
Return -value.
This cell verifies the negate function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import negate
def test_negate(value):
import numpy as np
res = negate(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_negate, {'value': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = negate(inp[0])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "negate: all inputs were skipped — test is broken"
print(f"negate tests passed! ({_successes}/{len(inputset)})")
square()¶
Return value squared.
This cell verifies the square function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import square
def test_square(value):
import numpy as np
res = square(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_square, {'value': 'encrypted'})
inputset = [(2,), (-2,), (0,), (3,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = square(inp[0])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "square: all inputs were skipped — test is broken"
print(f"square tests passed! ({_successes}/{len(inputset)})")
cube()¶
Return cube of a number.
This cell verifies the cube function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import cube
def test_cube(value):
import numpy as np
res = cube(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_cube, {'value': 'encrypted'})
inputset = [(2,), (-2,), (0,), (3,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = cube(inp[0])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "cube: all inputs were skipped — test is broken"
print(f"cube tests passed! ({_successes}/{len(inputset)})")
equal()¶
Return 1 if left equals right, otherwise 0.
This cell verifies the equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import equal
def test_equal(left, right):
import numpy as np
res = equal(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_equal, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = equal(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "equal: all inputs were skipped — test is broken"
print(f"equal tests passed! ({_successes}/{len(inputset)})")
not_equal()¶
Return 1 if left differs from right, otherwise 0.
This cell verifies the not_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import not_equal
def test_not_equal(left, right):
import numpy as np
res = not_equal(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_not_equal, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = not_equal(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "not_equal: all inputs were skipped — test is broken"
print(f"not_equal tests passed! ({_successes}/{len(inputset)})")
less()¶
Return 1 if left is strictly less than right, otherwise 0.
This cell verifies the less function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import less
def test_less(left, right):
import numpy as np
res = less(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_less, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = less(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "less: all inputs were skipped — test is broken"
print(f"less tests passed! ({_successes}/{len(inputset)})")
less_equal()¶
Return 1 if left is less than or equal to right, otherwise 0.
This cell verifies the less_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import less_equal
def test_less_equal(left, right):
import numpy as np
res = less_equal(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_less_equal, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = less_equal(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "less_equal: all inputs were skipped — test is broken"
print(f"less_equal tests passed! ({_successes}/{len(inputset)})")
greater()¶
Return 1 if left is strictly greater than right, otherwise 0.
This cell verifies the greater function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import greater
def test_greater(left, right):
import numpy as np
res = greater(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_greater, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = greater(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "greater: all inputs were skipped — test is broken"
print(f"greater tests passed! ({_successes}/{len(inputset)})")
greater_equal()¶
Return 1 if left is greater than or equal to right, otherwise 0.
This cell verifies the greater_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import greater_equal
def test_greater_equal(left, right):
import numpy as np
res = greater_equal(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_greater_equal, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = greater_equal(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "greater_equal: all inputs were skipped — test is broken"
print(f"greater_equal tests passed! ({_successes}/{len(inputset)})")
is_zero()¶
Return 1 if value is exactly 0, otherwise 0.
This cell verifies the is_zero function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import is_zero
def test_is_zero(value):
import numpy as np
res = is_zero(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_is_zero, {'value': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = is_zero(inp[0])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "is_zero: all inputs were skipped — test is broken"
print(f"is_zero tests passed! ({_successes}/{len(inputset)})")
maximum()¶
Return the max number of a pair.
This cell verifies the maximum function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import maximum
def test_maximum(left, right):
import numpy as np
res = maximum(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_maximum, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = maximum(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "maximum: all inputs were skipped — test is broken"
print(f"maximum tests passed! ({_successes}/{len(inputset)})")
minimum()¶
Return the min number of a pair.
This cell verifies the minimum function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import minimum
def test_minimum(left, right):
import numpy as np
res = minimum(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_minimum, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = minimum(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "minimum: all inputs were skipped — test is broken"
print(f"minimum tests passed! ({_successes}/{len(inputset)})")
make_scalar_multiply()¶
Create multiplication by a public integer constant.
This cell verifies the make_scalar_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_scalar_multiply
fn = make_scalar_multiply(multiplier=2)
def test_make_scalar_multiply_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_scalar_multiply_enc, {'value': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_scalar_multiply: all inputs were skipped — test is broken"
print(f"make_scalar_multiply tests passed! ({_successes}/{len(inputset)})")
make_is_close()¶
Create an integer closeness predicate using a public tolerance.
This cell verifies the make_is_close function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_is_close
fn = make_is_close(absolute_tolerance=1)
def test_make_is_close_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_is_close_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_is_close: all inputs were skipped — test is broken"
print(f"make_is_close tests passed! ({_successes}/{len(inputset)})")
compile_add()¶
Compile encrypted integer addition over inclusive bounds.
This cell verifies the compile_add function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_add
from concrete_fhe_toolkit.math.basic import add
circuit = compile_add(min_left=-2, max_left=3, min_right=-2, max_right=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = add(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_add: all inputs were skipped — test is broken"
print(f"compile_add tests passed! ({_successes}/{len(inputset)})")
compile_subtract()¶
Compile encrypted integer subtraction over inclusive bounds.
This cell verifies the compile_subtract function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
Aliases: compile_sub can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_subtract
from concrete_fhe_toolkit.math.basic import subtract
circuit = compile_subtract(min_left=-2, max_left=3, min_right=-2, max_right=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = subtract(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_subtract: all inputs were skipped — test is broken"
print(f"compile_subtract tests passed! ({_successes}/{len(inputset)})")
compile_multiply()¶
Compile encrypted integer multiplication over inclusive bounds.
This cell verifies the compile_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_multiply
from concrete_fhe_toolkit.math.basic import multiply
circuit = compile_multiply(min_left=-2, max_left=3, min_right=-2, max_right=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = multiply(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_multiply: all inputs were skipped — test is broken"
print(f"compile_multiply tests passed! ({_successes}/{len(inputset)})")
compile_negate()¶
Compile encrypted integer negation.
This cell verifies the compile_negate function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_negate
from concrete_fhe_toolkit.math.basic import negate
circuit = compile_negate(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = negate(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_negate: all inputs were skipped — test is broken"
print(f"compile_negate tests passed! ({_successes}/{len(inputset)})")
compile_square()¶
Compile encrypted integer squaring.
This cell verifies the compile_square function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_square
from concrete_fhe_toolkit.math.basic import square
circuit = compile_square(min_value=-2, max_value=3)
inputset = [(2,), (-2,), (0,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = square(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_square: all inputs were skipped — test is broken"
print(f"compile_square tests passed! ({_successes}/{len(inputset)})")
compile_cube()¶
Compile encrypted integer cubing.
This cell verifies the compile_cube function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_cube
from concrete_fhe_toolkit.math.basic import cube
circuit = compile_cube(min_value=-2, max_value=3)
inputset = [(2,), (-2,), (0,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = cube(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_cube: all inputs were skipped — test is broken"
print(f"compile_cube tests passed! ({_successes}/{len(inputset)})")
compile_scalar_multiply()¶
Compile multiplication by a public integer constant.
This cell verifies the compile_scalar_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
Aliases: compile_scalar_mul can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_scalar_multiply
from concrete_fhe_toolkit.math.basic import make_scalar_multiply
circuit = compile_scalar_multiply(min_value=-2, max_value=3, multiplier=2)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_scalar_multiply(multiplier=2)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_scalar_multiply: all inputs were skipped — test is broken"
print(f"compile_scalar_multiply tests passed! ({_successes}/{len(inputset)})")
compile_equal()¶
Compile encrypted equality.
This cell verifies the compile_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_equal
from concrete_fhe_toolkit.math.basic import equal
circuit = compile_equal(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = equal(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_equal: all inputs were skipped — test is broken"
print(f"compile_equal tests passed! ({_successes}/{len(inputset)})")
compile_not_equal()¶
Compile encrypted inequality.
This cell verifies the compile_not_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_not_equal
from concrete_fhe_toolkit.math.basic import not_equal
circuit = compile_not_equal(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = not_equal(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_not_equal: all inputs were skipped — test is broken"
print(f"compile_not_equal tests passed! ({_successes}/{len(inputset)})")
compile_less()¶
Compile encrypted less-than comparison.
This cell verifies the compile_less function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_less
from concrete_fhe_toolkit.math.basic import less
circuit = compile_less(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = less(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_less: all inputs were skipped — test is broken"
print(f"compile_less tests passed! ({_successes}/{len(inputset)})")
compile_less_equal()¶
Compile encrypted less-than-or-equal comparison.
This cell verifies the compile_less_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_less_equal
from concrete_fhe_toolkit.math.basic import less_equal
circuit = compile_less_equal(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = less_equal(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_less_equal: all inputs were skipped — test is broken"
print(f"compile_less_equal tests passed! ({_successes}/{len(inputset)})")
compile_greater()¶
Compile encrypted greater-than comparison.
This cell verifies the compile_greater function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_greater
from concrete_fhe_toolkit.math.basic import greater
circuit = compile_greater(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = greater(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_greater: all inputs were skipped — test is broken"
print(f"compile_greater tests passed! ({_successes}/{len(inputset)})")
compile_greater_equal()¶
Compile encrypted greater-than-or-equal comparison.
This cell verifies the compile_greater_equal function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_greater_equal
from concrete_fhe_toolkit.math.basic import greater_equal
circuit = compile_greater_equal(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = greater_equal(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_greater_equal: all inputs were skipped — test is broken"
print(f"compile_greater_equal tests passed! ({_successes}/{len(inputset)})")
compile_is_zero()¶
Compile an encrypted is-zero predicate.
This cell verifies the compile_is_zero function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_is_zero
from concrete_fhe_toolkit.math.basic import is_zero
circuit = compile_is_zero(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = is_zero(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_is_zero: all inputs were skipped — test is broken"
print(f"compile_is_zero tests passed! ({_successes}/{len(inputset)})")
compile_maximum()¶
Compile the encrypted maximum of two integers over inclusive bounds.
This cell verifies the compile_maximum function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_maximum
from concrete_fhe_toolkit.math.basic import maximum
circuit = compile_maximum(min_left=-2, max_left=3, min_right=-2, max_right=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = maximum(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_maximum: all inputs were skipped — test is broken"
print(f"compile_maximum tests passed! ({_successes}/{len(inputset)})")
compile_minimum()¶
Compile the encrypted minimum of two integers over inclusive bounds.
This cell verifies the compile_minimum function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_minimum
from concrete_fhe_toolkit.math.basic import minimum
circuit = compile_minimum(min_left=-2, max_left=3, min_right=-2, max_right=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = minimum(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_minimum: all inputs were skipped — test is broken"
print(f"compile_minimum tests passed! ({_successes}/{len(inputset)})")
compile_is_close()¶
Compile encrypted integer closeness with an absolute tolerance.
This cell verifies the compile_is_close function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_is_close
from concrete_fhe_toolkit.math.basic import make_is_close
circuit = compile_is_close(min_value=-2, max_value=3, absolute_tolerance=1)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_is_close(absolute_tolerance=1)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_is_close: all inputs were skipped — test is broken"
print(f"compile_is_close tests passed! ({_successes}/{len(inputset)})")
make_absolute()¶
Create an absolute-value lookup over inclusive signed bounds.
This cell verifies the make_absolute function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
Aliases: make_abs can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_absolute
fn = make_absolute(min_value=-2, max_value=3)
def test_make_absolute_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_absolute_enc, {'value': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_absolute: all inputs were skipped — test is broken"
print(f"make_absolute tests passed! ({_successes}/{len(inputset)})")
compile_absolute()¶
Compile absolute value over inclusive signed bounds.
This cell verifies the compile_absolute function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
Aliases: compile_abs can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_absolute
from concrete_fhe_toolkit.math.basic import make_absolute
circuit = compile_absolute(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_absolute(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_absolute: all inputs were skipped — test is broken"
print(f"compile_absolute tests passed! ({_successes}/{len(inputset)})")
make_clamp()¶
Create a lookup that clamps input into [min_value, max_value].
This cell verifies the make_clamp function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_clamp
fn = make_clamp(min_input=-2, max_input=3, min_value=-2, max_value=3)
def test_make_clamp_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_clamp_enc, {'value': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_clamp: all inputs were skipped — test is broken"
print(f"make_clamp tests passed! ({_successes}/{len(inputset)})")
compile_clamp()¶
Compile clamping of an encrypted integer into public bounds.
This cell verifies the compile_clamp function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_clamp
from concrete_fhe_toolkit.math.basic import make_clamp
circuit = compile_clamp(min_input=-2, max_input=3, min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_clamp(min_input=-2, max_input=3, min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_clamp: all inputs were skipped — test is broken"
print(f"compile_clamp tests passed! ({_successes}/{len(inputset)})")
make_modulo()¶
Create Python-style modulo with an explicit zero-denominator result.
This cell verifies the make_modulo function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_modulo
fn = make_modulo(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)
def test_make_modulo_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_modulo_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_modulo: all inputs were skipped — test is broken"
print(f"make_modulo tests passed! ({_successes}/{len(inputset)})")
compile_modulo()¶
Compile Python-style modulo with an explicit zero-denominator result.
This cell verifies the compile_modulo function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_modulo
from concrete_fhe_toolkit.math.basic import make_modulo
circuit = compile_modulo(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_modulo(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_modulo: all inputs were skipped — test is broken"
print(f"compile_modulo tests passed! ({_successes}/{len(inputset)})")
make_divmod()¶
Create quotient-and-remainder lookup with explicit zero behavior.
This cell verifies the make_divmod function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_divmod
fn = make_divmod(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_quotient=3, zero_remainder=3)
def test_make_divmod_enc(numerator, denominator):
import numpy as np
res = fn(numerator, denominator)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_divmod_enc, {'numerator': 'encrypted', 'denominator': 'encrypted'})
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_divmod: all inputs were skipped — test is broken"
print(f"make_divmod tests passed! ({_successes}/{len(inputset)})")
compile_divmod()¶
Compile quotient and remainder with explicit zero-denominator behavior.
This cell verifies the compile_divmod function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_divmod
from concrete_fhe_toolkit.math.basic import make_divmod
circuit = compile_divmod(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_quotient=3, zero_remainder=3)
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_divmod(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_quotient=3, zero_remainder=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_divmod: all inputs were skipped — test is broken"
print(f"compile_divmod tests passed! ({_successes}/{len(inputset)})")
select()¶
Return when_true when control is 1, otherwise when_false.
This cell verifies the select function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import select
def test_select(control, when_true, when_false):
import numpy as np
res = select(control, when_true, when_false)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_select, {'control': 'encrypted', 'when_true': 'encrypted', 'when_false': 'encrypted'})
inputset = [(3, 2, 1), (-2, -2, 3), (0, 0, 0), (2, -2, 2), (10, 5, -2)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = select(inp[0], inp[1], inp[2])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "select: all inputs were skipped — test is broken"
print(f"select tests passed! ({_successes}/{len(inputset)})")
compile_select()¶
Compile an oblivious select over two encrypted bounded branches.
This cell verifies the compile_select function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_select
from concrete_fhe_toolkit.math.basic import select
circuit = compile_select(min_value=-2, max_value=3)
inputset = [(3, 2, 1), (-2, -2, 3), (0, 0, 0), (2, -2, 2), (10, 5, -2)]
_successes = 0
for inp in inputset:
try:
expected = select(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_select: all inputs were skipped — test is broken"
print(f"compile_select tests passed! ({_successes}/{len(inputset)})")
make_abs_diff()¶
Create |left - right| for two encrypted bounded integers.
This cell verifies the make_abs_diff function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_abs_diff
fn = make_abs_diff(min_value=-2, max_value=3)
def test_make_abs_diff_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_abs_diff_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_abs_diff: all inputs were skipped — test is broken"
print(f"make_abs_diff tests passed! ({_successes}/{len(inputset)})")
compile_abs_diff()¶
Compile |left - right| for two encrypted bounded integers.
This cell verifies the compile_abs_diff function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_abs_diff
from concrete_fhe_toolkit.math.basic import make_abs_diff
circuit = compile_abs_diff(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_abs_diff(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_abs_diff: all inputs were skipped — test is broken"
print(f"compile_abs_diff tests passed! ({_successes}/{len(inputset)})")
make_copysign()¶
Create copysign(x, y): |x| when y >= 0, otherwise -|x|.
This cell verifies the make_copysign function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_copysign
fn = make_copysign(min_value=-2, max_value=3)
def test_make_copysign_enc(x, y):
import numpy as np
res = fn(x, y)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_copysign_enc, {'x': 'encrypted', 'y': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_copysign: all inputs were skipped — test is broken"
print(f"make_copysign tests passed! ({_successes}/{len(inputset)})")
compile_copysign()¶
Compile copysign(x, y) over inclusive signed bounds.
This cell verifies the compile_copysign function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_copysign
from concrete_fhe_toolkit.math.basic import make_copysign
circuit = compile_copysign(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_copysign(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_copysign: all inputs were skipped — test is broken"
print(f"compile_copysign tests passed! ({_successes}/{len(inputset)})")
make_saturating_add()¶
Create addition whose result is clamped back into [min_value, max_value].
This cell verifies the make_saturating_add function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_saturating_add
fn = make_saturating_add(min_value=-2, max_value=3)
def test_make_saturating_add_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_saturating_add_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_saturating_add: all inputs were skipped — test is broken"
print(f"make_saturating_add tests passed! ({_successes}/{len(inputset)})")
make_saturating_subtract()¶
Create subtraction whose result is clamped back into [min_value, max_value].
This cell verifies the make_saturating_subtract function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_saturating_subtract
fn = make_saturating_subtract(min_value=-2, max_value=3)
def test_make_saturating_subtract_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_saturating_subtract_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_saturating_subtract: all inputs were skipped — test is broken"
print(f"make_saturating_subtract tests passed! ({_successes}/{len(inputset)})")
make_saturating_multiply()¶
Create multiplication whose result is clamped back into [min_value, max_value].
This cell verifies the make_saturating_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_saturating_multiply
fn = make_saturating_multiply(min_value=-2, max_value=3)
def test_make_saturating_multiply_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_saturating_multiply_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_saturating_multiply: all inputs were skipped — test is broken"
print(f"make_saturating_multiply tests passed! ({_successes}/{len(inputset)})")
compile_saturating_add()¶
Compile clamped encrypted addition.
This cell verifies the compile_saturating_add function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_saturating_add
from concrete_fhe_toolkit.math.basic import make_saturating_add
circuit = compile_saturating_add(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_saturating_add(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_saturating_add: all inputs were skipped — test is broken"
print(f"compile_saturating_add tests passed! ({_successes}/{len(inputset)})")
compile_saturating_subtract()¶
Compile clamped encrypted subtraction.
This cell verifies the compile_saturating_subtract function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_saturating_subtract
from concrete_fhe_toolkit.math.basic import make_saturating_subtract
circuit = compile_saturating_subtract(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_saturating_subtract(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_saturating_subtract: all inputs were skipped — test is broken"
print(f"compile_saturating_subtract tests passed! ({_successes}/{len(inputset)})")
compile_saturating_multiply()¶
Compile clamped encrypted multiplication.
This cell verifies the compile_saturating_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_saturating_multiply
from concrete_fhe_toolkit.math.basic import make_saturating_multiply
circuit = compile_saturating_multiply(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_saturating_multiply(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_saturating_multiply: all inputs were skipped — test is broken"
print(f"compile_saturating_multiply tests passed! ({_successes}/{len(inputset)})")
make_fdim()¶
Create fdim(x, y) = max(x - y, 0) for two encrypted bounded integers.
This cell verifies the make_fdim function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_fdim
fn = make_fdim(min_value=-2, max_value=3)
def test_make_fdim_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_fdim_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_fdim: all inputs were skipped — test is broken"
print(f"make_fdim tests passed! ({_successes}/{len(inputset)})")
compile_fdim()¶
Compile fdim(x, y) = max(x - y, 0) over inclusive bounds.
This cell verifies the compile_fdim function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_fdim
from concrete_fhe_toolkit.math.basic import make_fdim
circuit = compile_fdim(min_value=-2, max_value=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_fdim(min_value=-2, max_value=3)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_fdim: all inputs were skipped — test is broken"
print(f"compile_fdim tests passed! ({_successes}/{len(inputset)})")
fma()¶
Return left * right + addend (fused multiply-add, exact on integers).
This cell verifies the fma function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import fma
def test_fma(left, right, addend):
import numpy as np
res = fma(left, right, addend)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_fma, {'left': 'encrypted', 'right': 'encrypted', 'addend': 'encrypted'})
inputset = [(3, 2, 1), (-2, -2, 3), (0, 0, 0), (2, -2, 2), (10, 5, -2)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fma(inp[0], inp[1], inp[2])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "fma: all inputs were skipped — test is broken"
print(f"fma tests passed! ({_successes}/{len(inputset)})")
compile_fma()¶
Compile fused multiply-add over three encrypted bounded inputs.
This cell verifies the compile_fma function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_fma
from concrete_fhe_toolkit.math.basic import fma
circuit = compile_fma(min_value=-2, max_value=3)
inputset = [(3, 2, 1), (-2, -2, 3), (0, 0, 0), (2, -2, 2), (10, 5, -2)]
_successes = 0
for inp in inputset:
try:
expected = fma(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_fma: all inputs were skipped — test is broken"
print(f"compile_fma tests passed! ({_successes}/{len(inputset)})")
make_remainder()¶
Create IEEE-style remainder(x, y) = x - round(x / y) * y (ties to even).
This cell verifies the make_remainder function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_remainder
fn = make_remainder(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)
def test_make_remainder_enc(left, right):
import numpy as np
res = fn(left, right)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_remainder_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_remainder: all inputs were skipped — test is broken"
print(f"make_remainder tests passed! ({_successes}/{len(inputset)})")
compile_remainder()¶
Compile IEEE-style remainder with explicit zero-denominator behavior.
This cell verifies the compile_remainder function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_remainder
from concrete_fhe_toolkit.math.basic import make_remainder
circuit = compile_remainder(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_remainder(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_remainder: all inputs were skipped — test is broken"
print(f"compile_remainder tests passed! ({_successes}/{len(inputset)})")
make_ldexp()¶
Create x * 2**exponent with a public exponent.
This cell verifies the make_ldexp function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
Aliases: make_scalbn can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import make_ldexp
fn = make_ldexp(exponent=2)
def test_make_ldexp_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_ldexp_enc, {'value': 'encrypted'})
inputset = [(1,), (2,), (3,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fn(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "make_ldexp: all inputs were skipped — test is broken"
print(f"make_ldexp tests passed! ({_successes}/{len(inputset)})")
compile_ldexp()¶
Compile x * 2**exponent with a public exponent.
This cell verifies the compile_ldexp function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
Aliases: compile_scalbn can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import compile_ldexp
from concrete_fhe_toolkit.math.basic import make_ldexp
circuit = compile_ldexp(min_value=0, max_value=3, exponent=2)
inputset = [(1,), (2,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = make_ldexp(exponent=2)(*inp)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "compile_ldexp: all inputs were skipped — test is broken"
print(f"compile_ldexp tests passed! ({_successes}/{len(inputset)})")
fsum()¶
Sum of an iterable of encrypted values (tournament reduction, exact on integers).
This cell verifies the fsum function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import fsum
def test_fsum(values):
import numpy as np
res = fsum(values)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_fsum, {'values': 'encrypted'})
inputset = [([0, 1, 2],), ([-1, 0, 1],), ([0, 0, 0],)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fsum(inp[0])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "fsum: all inputs were skipped — test is broken"
print(f"fsum tests passed! ({_successes}/{len(inputset)})")
prod()¶
Product of an iterable of encrypted values times a public start value.
This cell verifies the prod function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import prod
def test_prod(values):
import numpy as np
res = prod(values, 1)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_prod, {'values': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = prod(inp[0], 1)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "prod: all inputs were skipped — test is broken"
print(f"prod tests passed! ({_successes}/{len(inputset)})")
sumprod()¶
Sum of products of paired values from two iterables (dot product).
This cell verifies the sumprod function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.math.basic import sumprod
def test_sumprod(p, q):
import numpy as np
res = sumprod(p, q)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_sumprod, {'p': 'encrypted', 'q': 'encrypted'})
inputset = [([0, 1, 1], [0, 0, 1]), ([1, 0, 1], [1, 0, 1]), ([0, 0, 0], [1, 1, 1])]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = sumprod(inp[0], inp[1])
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "sumprod: all inputs were skipped — test is broken"
print(f"sumprod tests passed! ({_successes}/{len(inputset)})")