Math (Binary Division)
unsigned_divide_bits()¶
Return little-endian quotient bits for unsigned floor division.
This cell verifies the unsigned_divide_bits 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.binary_division import unsigned_divide_bits
def test_unsigned_divide_bits(numerator_bits, denominator_bits):
import numpy as np
res = unsigned_divide_bits(numerator_bits, denominator_bits, quotient_width=8, zero_result=0)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_unsigned_divide_bits, {'numerator_bits': 'encrypted', 'denominator_bits': 'encrypted'})
inputset = [([0, 1, 0], [1, 0, 1]), ([1, 1, 1], [1, 1, 1]), ([0, 0, 0], [0, 0, 0])]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = unsigned_divide_bits(inp[0], inp[1], quotient_width=8, zero_result=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, "unsigned_divide_bits: all inputs were skipped — test is broken"
print(f"unsigned_divide_bits tests passed! ({_successes}/{len(inputset)})")
fixed_point_divide_bits()¶
Return quotient bits for floor((numerator << fractional_bits) / denominator).
This cell verifies the fixed_point_divide_bits 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.binary_division import fixed_point_divide_bits
def test_fixed_point_divide_bits(numerator_bits, denominator_bits):
import numpy as np
res = fixed_point_divide_bits(numerator_bits, denominator_bits, fractional_bits=3, quotient_width=8, zero_result=0)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_fixed_point_divide_bits, {'numerator_bits': 'encrypted', 'denominator_bits': 'encrypted'})
inputset = [([0, 1, 0], [1, 0, 1]), ([1, 1, 1], [1, 1, 1]), ([0, 0, 0], [0, 0, 0])]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = fixed_point_divide_bits(inp[0], inp[1], fractional_bits=3, quotient_width=8, zero_result=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, "fixed_point_divide_bits: all inputs were skipped — test is broken"
print(f"fixed_point_divide_bits tests passed! ({_successes}/{len(inputset)})")
make_unsigned_floor_divide()¶
Create unsigned encrypted floor division from scalar integer inputs.
This cell verifies the make_unsigned_floor_divide 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.
# Could not auto-generate test for make_unsigned_floor_divide
# Error: make_unsigned_floor_divide() missing 2 required positional arguments: 'numerator_width' and 'denominator_width'
make_fixed_point_divide()¶
Create fixed-point unsigned division from scalar integer inputs.
This cell verifies the make_fixed_point_divide 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.
# Could not auto-generate test for make_fixed_point_divide
# Error: make_fixed_point_divide() missing 2 required positional arguments: 'numerator_width' and 'denominator_width'
compile_unsigned_floor_divide()¶
Compile unsigned floor division using a bit-level restoring circuit.
This cell verifies the compile_unsigned_floor_divide 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.
# Could not auto-generate test for compile_unsigned_floor_divide
# Error: make_unsigned_floor_divide() missing 2 required positional arguments: 'numerator_width' and 'denominator_width'
compile_fixed_point_divide()¶
Compile fixed-point unsigned division using a bit-level circuit.
This cell verifies the compile_fixed_point_divide 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.
# Could not auto-generate test for compile_fixed_point_divide
# Error: make_fixed_point_divide() missing 2 required positional arguments: 'numerator_width' and 'denominator_width'
unsigned_mod_bits()¶
Return little-endian remainder bits for unsigned modulo.
This cell verifies the unsigned_mod_bits 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.binary_division import unsigned_mod_bits
def test_unsigned_mod_bits(numerator_bits, denominator_bits):
import numpy as np
res = unsigned_mod_bits(numerator_bits, denominator_bits, zero_result=0)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_unsigned_mod_bits, {'numerator_bits': 'encrypted', 'denominator_bits': 'encrypted'})
inputset = [([0, 1, 0], [1, 0, 1]), ([1, 1, 1], [1, 1, 1]), ([0, 0, 0], [0, 0, 0])]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = unsigned_mod_bits(inp[0], inp[1], zero_result=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, "unsigned_mod_bits: all inputs were skipped — test is broken"
print(f"unsigned_mod_bits tests passed! ({_successes}/{len(inputset)})")
make_unsigned_mod()¶
Create unsigned encrypted modulo from scalar integer inputs.
This cell verifies the make_unsigned_mod 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.
# Could not auto-generate test for make_unsigned_mod
# Error: make_unsigned_mod() missing 2 required positional arguments: 'numerator_width' and 'denominator_width'
compile_unsigned_mod()¶
Compile unsigned modulo using the bit-level restoring circuit.
This cell verifies the compile_unsigned_mod 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.
# Could not auto-generate test for compile_unsigned_mod
# Error: make_unsigned_mod() missing 2 required positional arguments: 'numerator_width' and 'denominator_width'