Math (Combinatorics)
make_factorial()¶
Create n! for an encrypted n in [0, max_n].
This cell verifies the make_factorial 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.combinatorics import make_factorial
import math
fn = make_factorial(max_n=3)
def test_make_factorial_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_factorial_enc, {'value': 'encrypted'})
inputset = [(0,), (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_factorial: all inputs were skipped — test is broken"
print(f"make_factorial tests passed! ({_successes}/{len(inputset)})")
compile_factorial()¶
Compile n! for an encrypted n in [0, max_n].
This cell verifies the compile_factorial 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.combinatorics import compile_factorial
import math
from concrete_fhe_toolkit.math.combinatorics import make_factorial
circuit = compile_factorial(max_n=3)
inputset = [(0,), (1,), (2,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = make_factorial(max_n=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_factorial: all inputs were skipped — test is broken"
print(f"compile_factorial tests passed! ({_successes}/{len(inputset)})")
make_fibonacci()¶
Create the nth Fibonacci number for encrypted n in [0, max_n].
This cell verifies the make_fibonacci 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.combinatorics import make_fibonacci
import math
fn = make_fibonacci(max_n=3)
def test_make_fibonacci_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_fibonacci_enc, {'value': 'encrypted'})
inputset = [(0,), (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_fibonacci: all inputs were skipped — test is broken"
print(f"make_fibonacci tests passed! ({_successes}/{len(inputset)})")
compile_fibonacci()¶
Compile the nth Fibonacci number for encrypted n in [0, max_n].
This cell verifies the compile_fibonacci 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.combinatorics import compile_fibonacci
import math
from concrete_fhe_toolkit.math.combinatorics import make_fibonacci
circuit = compile_fibonacci(max_n=3)
inputset = [(0,), (1,), (2,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = make_fibonacci(max_n=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_fibonacci: all inputs were skipped — test is broken"
print(f"compile_fibonacci tests passed! ({_successes}/{len(inputset)})")
make_power()¶
Create public-base exponentiation for an encrypted exponent.
This cell verifies the make_power 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_exponential can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.combinatorics import make_power
import math
fn = make_power(base=2, max_exponent=3)
def test_make_power_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_power_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_power: all inputs were skipped — test is broken"
print(f"make_power tests passed! ({_successes}/{len(inputset)})")
compile_power()¶
Compile public-base exponentiation for an encrypted exponent.
This cell verifies the compile_power 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_exponential can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.combinatorics import compile_power
import math
from concrete_fhe_toolkit.math.combinatorics import make_power
circuit = compile_power(base=2, max_exponent=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_power(base=2, max_exponent=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_power: all inputs were skipped — test is broken"
print(f"compile_power tests passed! ({_successes}/{len(inputset)})")
make_comb()¶
Create math.comb(n, r), returning invalid_result when r > n.
This cell verifies the make_comb 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_combinations can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.combinatorics import make_comb
import math
fn = make_comb(max_n=3, invalid_result=7)
def test_make_comb_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_comb_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (3, 3), (2, 2), (0, 0), (3, 0)]
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_comb: all inputs were skipped — test is broken"
print(f"make_comb tests passed! ({_successes}/{len(inputset)})")
compile_comb()¶
Compile math.comb(n, r) for encrypted n and r in [0, max_n].
This cell verifies the compile_comb 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_combinations can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.combinatorics import compile_comb
import math
from concrete_fhe_toolkit.math.combinatorics import make_comb
circuit = compile_comb(max_n=3, invalid_result=7)
inputset = [(3, 2), (3, 3), (2, 2), (0, 0), (3, 0)]
_successes = 0
for inp in inputset:
try:
expected = make_comb(max_n=3, invalid_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_comb: all inputs were skipped — test is broken"
print(f"compile_comb tests passed! ({_successes}/{len(inputset)})")
make_perm()¶
Create math.perm(n, r), returning invalid_result when r > n.
This cell verifies the make_perm 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_permutations can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.combinatorics import make_perm
import math
fn = make_perm(max_n=3, invalid_result=7)
def test_make_perm_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_perm_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (3, 3), (2, 2), (0, 0), (3, 0)]
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_perm: all inputs were skipped — test is broken"
print(f"make_perm tests passed! ({_successes}/{len(inputset)})")
compile_perm()¶
Compile math.perm(n, r) for encrypted n and r in [0, max_n].
This cell verifies the compile_perm 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_permutations can also be used equivalently.
from concrete import fhe
from concrete_fhe_toolkit.math.combinatorics import compile_perm
import math
from concrete_fhe_toolkit.math.combinatorics import make_perm
circuit = compile_perm(max_n=3, invalid_result=7)
inputset = [(3, 2), (3, 3), (2, 2), (0, 0), (3, 0)]
_successes = 0
for inp in inputset:
try:
expected = make_perm(max_n=3, invalid_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_perm: all inputs were skipped — test is broken"
print(f"compile_perm tests passed! ({_successes}/{len(inputset)})")
make_powmod()¶
Create pow(base, exponent, modulus) with a public base and modulus.
This cell verifies the make_powmod 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.combinatorics import make_powmod
import math
fn = make_powmod(base=2, modulus=5, max_exponent=3)
def test_make_powmod_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_powmod_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_powmod: all inputs were skipped — test is broken"
print(f"make_powmod tests passed! ({_successes}/{len(inputset)})")
compile_powmod()¶
Compile pow(base, exponent, modulus) with a public base and modulus.
This cell verifies the compile_powmod 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.combinatorics import compile_powmod
import math
from concrete_fhe_toolkit.math.combinatorics import make_powmod
circuit = compile_powmod(base=2, modulus=5, max_exponent=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_powmod(base=2, modulus=5, max_exponent=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_powmod: all inputs were skipped — test is broken"
print(f"compile_powmod tests passed! ({_successes}/{len(inputset)})")