Arithmetic
compare()¶
This function compares two values and returns 1 if x > y, 0 if x == y, and -1 if x < y. Includes edge cases like negatives and zero.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import compare
def test_compare(x, y):
return compare(x, y)
compiler = fhe.Compiler(test_compare, {"x": "encrypted", "y": "encrypted"})
inputset = [(15, 10), (10, 10), (-15, 10), (0, 0), (-5, -10), (-10, -5)]
circuit = compiler.compile(inputset)
assert circuit.encrypt_run_decrypt(15, 10) == 1
assert circuit.encrypt_run_decrypt(10, 10) == 0
assert circuit.encrypt_run_decrypt(-15, 10) == -1
assert circuit.encrypt_run_decrypt(0, 0) == 0
assert circuit.encrypt_run_decrypt(-5, -10) == 1
assert circuit.encrypt_run_decrypt(-10, -5) == -1
print("compare tests passed!")
compile_compare()¶
Returns an already compiled FHE circuit to compare two encrypted values. Tests positives, negatives, and zeros.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import compile_compare
circuit = compile_compare(min_value=-15, max_value=15)
assert circuit.encrypt_run_decrypt(10, 5) == 1
assert circuit.encrypt_run_decrypt(12, 12) == 0
assert circuit.encrypt_run_decrypt(-10, 15) == -1
assert circuit.encrypt_run_decrypt(0, 0) == 0
assert circuit.encrypt_run_decrypt(-15, -15) == 0
print("compile_compare tests passed!")
sign()¶
Returns the sign of a number (1, -1, or 0). Tests positives, negatives, and zero.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import sign
def test_sign(x):
return sign(x)
compiler = fhe.Compiler(test_sign, {"x": "encrypted"})
inputset = [(15,), (-15,), (0,), (1,), (-1,)]
circuit = compiler.compile(inputset)
assert circuit.encrypt_run_decrypt(15) == 1
assert circuit.encrypt_run_decrypt(-15) == -1
assert circuit.encrypt_run_decrypt(0) == 0
assert circuit.encrypt_run_decrypt(1) == 1
assert circuit.encrypt_run_decrypt(-1) == -1
print("sign tests passed!")
compile_sign()¶
Returns a compiled FHE circuit to return the sign of a number.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import compile_sign
circuit = compile_sign(min_value=-15, max_value=15)
assert circuit.encrypt_run_decrypt(10) == 1
assert circuit.encrypt_run_decrypt(-10) == -1
assert circuit.encrypt_run_decrypt(0) == 0
print("compile_sign tests passed!")
make_floor_divide()¶
Creates an exact encrypted floor division. Tests exact division, floor division, negatives, and division by zero. Bounds are kept very small to prevent FHE bivariate table lookup from exploding RAM.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import make_floor_divide
floor_divide = make_floor_divide(zero_result=7)
def test_floor_divide(num, den):
return floor_divide(num, den)
compiler = fhe.Compiler(test_floor_divide, {"num": "encrypted", "den": "encrypted"})
inputset = [(4, 2), (4, -2), (-4, 2), (-4, -2), (3, 0), (0, 3)]
circuit = compiler.compile(inputset)
assert circuit.encrypt_run_decrypt(4, 2) == 2
assert circuit.encrypt_run_decrypt(4, -2) == -2
assert circuit.encrypt_run_decrypt(-4, 2) == -2
assert circuit.encrypt_run_decrypt(-4, -2) == 2
assert circuit.encrypt_run_decrypt(3, 0) == 7
assert circuit.encrypt_run_decrypt(0, 3) == 0
print("make_floor_divide tests passed!")
make_floor_divide_by_product()¶
Creates a division by a product (numerator // (left * right)). Tests positives, negatives, zeroes.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import make_floor_divide_by_product
div_prod = make_floor_divide_by_product(zero_result=7)
def test_div_prod(num, left, right):
return div_prod(num, left, right)
compiler = fhe.Compiler(test_div_prod, {"num": "encrypted", "left": "encrypted", "right": "encrypted"})
inputset = [(4, 2, 1), (-4, 2, -1), (4, -2, 1), (4, 0, 1), (0, 2, 1)]
circuit = compiler.compile(inputset)
assert circuit.encrypt_run_decrypt(4, 2, 1) == 2
assert circuit.encrypt_run_decrypt(-4, 2, -1) == 2
assert circuit.encrypt_run_decrypt(4, -2, 1) == -2
assert circuit.encrypt_run_decrypt(4, 0, 1) == 7
assert circuit.encrypt_run_decrypt(0, 2, 1) == 0
print("make_floor_divide_by_product tests passed!")
compile_floor_divide()¶
Returns a compiled floor division circuit. Note: This function only supports nonnegative inputs by design.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import compile_floor_divide
circuit = compile_floor_divide(max_numerator=5, max_denominator=3, zero_result=7)
assert circuit.encrypt_run_decrypt(5, 2) == 2
assert circuit.encrypt_run_decrypt(4, 3) == 1
assert circuit.encrypt_run_decrypt(5, 0) == 7
assert circuit.encrypt_run_decrypt(0, 2) == 0
print("compile_floor_divide tests passed!")
compile_floor_divide_by_product()¶
Returns a compiled circuit for division by a product. Note: This function only supports nonnegative inputs by design.
from concrete import fhe
from concrete_fhe_toolkit.arithmetic import compile_floor_divide_by_product
circuit = compile_floor_divide_by_product(max_numerator=5, max_left=2, max_right=2, zero_result=7)
assert circuit.encrypt_run_decrypt(4, 2, 1) == 2
assert circuit.encrypt_run_decrypt(5, 0, 2) == 7
assert circuit.encrypt_run_decrypt(0, 2, 2) == 0
print("compile_floor_divide_by_product tests passed!")