Math (Number Theory)
make_gcd()¶
Create math.gcd for two encrypted bounded integers.
This cell verifies the make_gcd 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.number_theory import make_gcd
import math
fn = make_gcd(min_value=-2, max_value=3)
def test_make_gcd_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_gcd_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (2, 3), (3, 1), (3, 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_gcd: all inputs were skipped — test is broken"
print(f"make_gcd tests passed! ({_successes}/{len(inputset)})")
compile_gcd()¶
Compile math.gcd for two encrypted bounded integers.
This cell verifies the compile_gcd 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.number_theory import compile_gcd
import math
from concrete_fhe_toolkit.math.number_theory import make_gcd
circuit = compile_gcd(min_value=-2, max_value=3)
inputset = [(3, 2), (2, 3), (3, 1), (3, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_gcd(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_gcd: all inputs were skipped — test is broken"
print(f"compile_gcd tests passed! ({_successes}/{len(inputset)})")
make_lcm()¶
Create math.lcm for two encrypted bounded integers.
This cell verifies the make_lcm 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.number_theory import make_lcm
import math
fn = make_lcm(min_value=-2, max_value=3)
def test_make_lcm_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_lcm_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (2, 3), (3, 1), (3, 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_lcm: all inputs were skipped — test is broken"
print(f"make_lcm tests passed! ({_successes}/{len(inputset)})")
compile_lcm()¶
Compile math.lcm for two encrypted bounded integers.
This cell verifies the compile_lcm 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.number_theory import compile_lcm
import math
from concrete_fhe_toolkit.math.number_theory import make_lcm
circuit = compile_lcm(min_value=-2, max_value=3)
inputset = [(3, 2), (2, 3), (3, 1), (3, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_lcm(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_lcm: all inputs were skipped — test is broken"
print(f"compile_lcm tests passed! ({_successes}/{len(inputset)})")
make_is_coprime()¶
Create a predicate returning 1 when gcd(left, right) == 1.
This cell verifies the make_is_coprime 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.number_theory import make_is_coprime
import math
fn = make_is_coprime(min_value=-2, max_value=3)
def test_make_is_coprime_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_coprime_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 2), (2, 3), (3, 1), (3, 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_coprime: all inputs were skipped — test is broken"
print(f"make_is_coprime tests passed! ({_successes}/{len(inputset)})")
compile_is_coprime()¶
Compile a predicate returning 1 for coprime encrypted integers.
This cell verifies the compile_is_coprime 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.number_theory import compile_is_coprime
import math
from concrete_fhe_toolkit.math.number_theory import make_is_coprime
circuit = compile_is_coprime(min_value=-2, max_value=3)
inputset = [(3, 2), (2, 3), (3, 1), (3, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_is_coprime(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_is_coprime: all inputs were skipped — test is broken"
print(f"compile_is_coprime tests passed! ({_successes}/{len(inputset)})")
make_is_divisible()¶
Create divisibility testing with explicit denominator-zero behavior.
This cell verifies the make_is_divisible 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.number_theory import make_is_divisible
import math
fn = make_is_divisible(min_numerator=-2, max_numerator=3, min_denominator=-2, max_denominator=3, zero_result=7)
def test_make_is_divisible_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_divisible_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_is_divisible: all inputs were skipped — test is broken"
print(f"make_is_divisible tests passed! ({_successes}/{len(inputset)})")
compile_is_divisible()¶
Compile encrypted divisibility testing.
This cell verifies the compile_is_divisible 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.number_theory import compile_is_divisible
import math
from concrete_fhe_toolkit.math.number_theory import make_is_divisible
circuit = compile_is_divisible(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_is_divisible(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_is_divisible: all inputs were skipped — test is broken"
print(f"compile_is_divisible tests passed! ({_successes}/{len(inputset)})")
make_isqrt()¶
Create math.isqrt for encrypted input in [0, max_value].
This cell verifies the make_isqrt 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.number_theory import make_isqrt
import math
fn = make_isqrt(max_value=3)
def test_make_isqrt_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_isqrt_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_isqrt: all inputs were skipped — test is broken"
print(f"make_isqrt tests passed! ({_successes}/{len(inputset)})")
compile_isqrt()¶
Compile math.isqrt for encrypted input in [0, max_value].
This cell verifies the compile_isqrt 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.number_theory import compile_isqrt
import math
from concrete_fhe_toolkit.math.number_theory import make_isqrt
circuit = compile_isqrt(max_value=3)
inputset = [(0,), (1,), (2,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = make_isqrt(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_isqrt: all inputs were skipped — test is broken"
print(f"compile_isqrt tests passed! ({_successes}/{len(inputset)})")
make_is_even()¶
Create a predicate returning 1 for even encrypted integers.
This cell verifies the make_is_even 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.number_theory import make_is_even
import math
fn = make_is_even(min_value=-2, max_value=3)
def test_make_is_even_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_is_even_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_is_even: all inputs were skipped — test is broken"
print(f"make_is_even tests passed! ({_successes}/{len(inputset)})")
compile_is_even()¶
Compile a predicate returning 1 for even encrypted integers.
This cell verifies the compile_is_even 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.number_theory import compile_is_even
import math
from concrete_fhe_toolkit.math.number_theory import make_is_even
circuit = compile_is_even(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_is_even(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_is_even: all inputs were skipped — test is broken"
print(f"compile_is_even tests passed! ({_successes}/{len(inputset)})")
make_is_odd()¶
Create a predicate returning 1 for odd encrypted integers.
This cell verifies the make_is_odd 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.number_theory import make_is_odd
import math
fn = make_is_odd(min_value=-2, max_value=3)
def test_make_is_odd_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_is_odd_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_is_odd: all inputs were skipped — test is broken"
print(f"make_is_odd tests passed! ({_successes}/{len(inputset)})")
compile_is_odd()¶
Compile a predicate returning 1 for odd encrypted integers.
This cell verifies the compile_is_odd 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.number_theory import compile_is_odd
import math
from concrete_fhe_toolkit.math.number_theory import make_is_odd
circuit = compile_is_odd(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_is_odd(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_is_odd: all inputs were skipped — test is broken"
print(f"compile_is_odd tests passed! ({_successes}/{len(inputset)})")
make_is_prime()¶
Create a predicate returning 1 for prime encrypted integers.
This cell verifies the make_is_prime 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.number_theory import make_is_prime
import math
fn = make_is_prime(min_value=-2, max_value=3)
def test_make_is_prime_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_is_prime_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_is_prime: all inputs were skipped — test is broken"
print(f"make_is_prime tests passed! ({_successes}/{len(inputset)})")
compile_is_prime()¶
Compile a predicate returning 1 for prime encrypted integers.
This cell verifies the compile_is_prime 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.number_theory import compile_is_prime
import math
from concrete_fhe_toolkit.math.number_theory import make_is_prime
circuit = compile_is_prime(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_is_prime(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_is_prime: all inputs were skipped — test is broken"
print(f"compile_is_prime tests passed! ({_successes}/{len(inputset)})")
make_totient()¶
Create Euler's totient for encrypted bounded integers (0 for n <= 0).
This cell verifies the make_totient 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.number_theory import make_totient
import math
fn = make_totient(min_value=-2, max_value=3)
def test_make_totient_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_totient_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_totient: all inputs were skipped — test is broken"
print(f"make_totient tests passed! ({_successes}/{len(inputset)})")
compile_totient()¶
Compile Euler's totient for encrypted bounded integers.
This cell verifies the compile_totient 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.number_theory import compile_totient
import math
from concrete_fhe_toolkit.math.number_theory import make_totient
circuit = compile_totient(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_totient(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_totient: all inputs were skipped — test is broken"
print(f"compile_totient tests passed! ({_successes}/{len(inputset)})")
make_next_prime()¶
Create the smallest prime strictly greater than an encrypted integer.
This cell verifies the make_next_prime 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.number_theory import make_next_prime
import math
fn = make_next_prime(min_value=-2, max_value=3)
def test_make_next_prime_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_next_prime_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_next_prime: all inputs were skipped — test is broken"
print(f"make_next_prime tests passed! ({_successes}/{len(inputset)})")
compile_next_prime()¶
Compile the smallest prime strictly greater than an encrypted integer.
This cell verifies the compile_next_prime 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.number_theory import compile_next_prime
import math
from concrete_fhe_toolkit.math.number_theory import make_next_prime
circuit = compile_next_prime(min_value=-2, max_value=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_next_prime(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_next_prime: all inputs were skipped — test is broken"
print(f"compile_next_prime tests passed! ({_successes}/{len(inputset)})")
make_mod_inverse()¶
Create the modular inverse of value mod modulus for encrypted inputs.
This cell verifies the make_mod_inverse 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.number_theory import make_mod_inverse
import math
fn = make_mod_inverse(min_value=-2, max_value=3, invalid_result=7)
def test_make_mod_inverse_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_mod_inverse_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_mod_inverse: all inputs were skipped — test is broken"
print(f"make_mod_inverse tests passed! ({_successes}/{len(inputset)})")
compile_mod_inverse()¶
Compile the modular inverse of value mod modulus for encrypted inputs.
This cell verifies the compile_mod_inverse 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.number_theory import compile_mod_inverse
import math
from concrete_fhe_toolkit.math.number_theory import make_mod_inverse
circuit = compile_mod_inverse(min_value=-2, max_value=3, invalid_result=7)
inputset = [(3, 2), (-2, 3), (3, -2), (3, 3), (2, 0), (0, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_mod_inverse(min_value=-2, max_value=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_mod_inverse: all inputs were skipped — test is broken"
print(f"compile_mod_inverse tests passed! ({_successes}/{len(inputset)})")
make_hypot()¶
Create round(hypot(x, y)) for two encrypted bounded integers.
This cell verifies the make_hypot 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.number_theory import make_hypot
import math
fn = make_hypot(min_value=-2, max_value=3)
def test_make_hypot_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_hypot_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_hypot: all inputs were skipped — test is broken"
print(f"make_hypot tests passed! ({_successes}/{len(inputset)})")
compile_hypot()¶
Compile round(hypot(x, y)) for two encrypted bounded integers.
This cell verifies the compile_hypot 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.number_theory import compile_hypot
import math
from concrete_fhe_toolkit.math.number_theory import make_hypot
circuit = compile_hypot(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_hypot(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_hypot: all inputs were skipped — test is broken"
print(f"compile_hypot tests passed! ({_successes}/{len(inputset)})")
make_ilogb()¶
Create floor(log2(|x|)) for encrypted integers; invalid_result handles x == 0.
This cell verifies the make_ilogb 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.number_theory import make_ilogb
import math
fn = make_ilogb(min_value=0, max_value=3, invalid_result=7)
def test_make_ilogb_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_ilogb_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_ilogb: all inputs were skipped — test is broken"
print(f"make_ilogb tests passed! ({_successes}/{len(inputset)})")
compile_ilogb()¶
Compile floor(log2(|x|)) for encrypted integers; invalid_result handles x == 0.
This cell verifies the compile_ilogb 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.number_theory import compile_ilogb
import math
from concrete_fhe_toolkit.math.number_theory import make_ilogb
circuit = compile_ilogb(min_value=0, max_value=3, invalid_result=7)
inputset = [(1,), (2,), (3,)]
_successes = 0
for inp in inputset:
try:
expected = make_ilogb(min_value=0, max_value=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_ilogb: all inputs were skipped — test is broken"
print(f"compile_ilogb tests passed! ({_successes}/{len(inputset)})")
make_dist()¶
Create round(Euclidean distance) between two encrypted coordinate lists.
This cell verifies the make_dist 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.number_theory import make_dist
import math
fn = make_dist(size=3, min_value=-2, max_value=3)
def test_make_dist_enc(p, q):
import numpy as np
res = fn(p, q)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_dist_enc, {'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 = 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_dist: all inputs were skipped — test is broken"
print(f"make_dist tests passed! ({_successes}/{len(inputset)})")
compile_dist()¶
Compile round(Euclidean distance) between two encrypted coordinate arrays.
This cell verifies the compile_dist 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.number_theory import compile_dist
import math
from concrete_fhe_toolkit.math.number_theory import make_dist
circuit = compile_dist(size=3, min_value=-2, max_value=3)
inputset = [([0, 1, 1], [0, 0, 1]), ([1, 0, 1], [1, 0, 1]), ([0, 0, 0], [1, 1, 1])]
_successes = 0
for inp in inputset:
try:
expected = make_dist(size=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_dist: all inputs were skipped — test is broken"
print(f"compile_dist tests passed! ({_successes}/{len(inputset)})")
make_pow()¶
Create base**exponent for an encrypted base and encrypted exponent.
This cell verifies the make_pow 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.number_theory import make_pow
import math
fn = make_pow(min_base=-2, max_base=3, max_exponent=3)
def test_make_pow_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_pow_enc, {'left': 'encrypted', 'right': 'encrypted'})
inputset = [(3, 1), (2, 2), (0, 0), (-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_pow: all inputs were skipped — test is broken"
print(f"make_pow tests passed! ({_successes}/{len(inputset)})")
compile_pow()¶
Compile base**exponent for an encrypted base and encrypted exponent.
This cell verifies the compile_pow 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.number_theory import compile_pow
import math
from concrete_fhe_toolkit.math.number_theory import make_pow
circuit = compile_pow(min_base=-2, max_base=3, max_exponent=3)
inputset = [(3, 1), (2, 2), (0, 0), (-2, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_pow(min_base=-2, max_base=3, 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_pow: all inputs were skipped — test is broken"
print(f"compile_pow tests passed! ({_successes}/{len(inputset)})")