Math (Fixed Point)
make_floor()¶
Create floor(value / scale) for encrypted scaled integers.
This cell verifies the make_floor 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.fixed_point import make_floor
fn = make_floor(min_input=-2, max_input=3)
def test_make_floor_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_floor_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_floor: all inputs were skipped — test is broken"
print(f"make_floor tests passed! ({_successes}/{len(inputset)})")
compile_floor()¶
Compile floor(value / scale) for encrypted scaled integers.
This cell verifies the compile_floor 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.fixed_point import compile_floor
from concrete_fhe_toolkit.math.fixed_point import make_floor
circuit = compile_floor(min_input=-2, max_input=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_floor(min_input=-2, max_input=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_floor: all inputs were skipped — test is broken"
print(f"compile_floor tests passed! ({_successes}/{len(inputset)})")
make_ceil()¶
Create ceil(value / scale) for encrypted scaled integers.
This cell verifies the make_ceil 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.fixed_point import make_ceil
fn = make_ceil(min_input=-2, max_input=3)
def test_make_ceil_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_ceil_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_ceil: all inputs were skipped — test is broken"
print(f"make_ceil tests passed! ({_successes}/{len(inputset)})")
compile_ceil()¶
Compile ceil(value / scale) for encrypted scaled integers.
This cell verifies the compile_ceil 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.fixed_point import compile_ceil
from concrete_fhe_toolkit.math.fixed_point import make_ceil
circuit = compile_ceil(min_input=-2, max_input=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_ceil(min_input=-2, max_input=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_ceil: all inputs were skipped — test is broken"
print(f"compile_ceil tests passed! ({_successes}/{len(inputset)})")
make_trunc()¶
Create trunc(value / scale) toward zero for encrypted scaled integers.
This cell verifies the make_trunc 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.fixed_point import make_trunc
fn = make_trunc(min_input=-2, max_input=3)
def test_make_trunc_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_trunc_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_trunc: all inputs were skipped — test is broken"
print(f"make_trunc tests passed! ({_successes}/{len(inputset)})")
compile_trunc()¶
Compile trunc(value / scale) toward zero for encrypted scaled integers.
This cell verifies the compile_trunc 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.fixed_point import compile_trunc
from concrete_fhe_toolkit.math.fixed_point import make_trunc
circuit = compile_trunc(min_input=-2, max_input=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_trunc(min_input=-2, max_input=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_trunc: all inputs were skipped — test is broken"
print(f"compile_trunc tests passed! ({_successes}/{len(inputset)})")
make_round()¶
Create round(value / scale) using Python's ties-to-even rule.
This cell verifies the make_round 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.fixed_point import make_round
fn = make_round(min_input=-2, max_input=3)
def test_make_round_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_round_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_round: all inputs were skipped — test is broken"
print(f"make_round tests passed! ({_successes}/{len(inputset)})")
compile_round()¶
Compile round(value / scale) using Python's ties-to-even rule.
This cell verifies the compile_round 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.fixed_point import compile_round
from concrete_fhe_toolkit.math.fixed_point import make_round
circuit = compile_round(min_input=-2, max_input=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_round(min_input=-2, max_input=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_round: all inputs were skipped — test is broken"
print(f"compile_round tests passed! ({_successes}/{len(inputset)})")
make_floor_ceil()¶
Create a function returning both floor and ceil for a scaled integer.
This cell verifies the make_floor_ceil 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.fixed_point import make_floor_ceil
fn = make_floor_ceil(min_input=-2, max_input=3)
def test_make_floor_ceil_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_floor_ceil_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_floor_ceil: all inputs were skipped — test is broken"
print(f"make_floor_ceil tests passed! ({_successes}/{len(inputset)})")
compile_floor_ceil()¶
Compile a circuit returning both floor and ceil for a scaled integer.
This cell verifies the compile_floor_ceil 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.fixed_point import compile_floor_ceil
from concrete_fhe_toolkit.math.fixed_point import make_floor_ceil
circuit = compile_floor_ceil(min_input=-2, max_input=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_floor_ceil(min_input=-2, max_input=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_floor_ceil: all inputs were skipped — test is broken"
print(f"compile_floor_ceil tests passed! ({_successes}/{len(inputset)})")
make_rescale()¶
Create rescaling from one fixed-point scale to another.
This cell verifies the make_rescale 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_rescale
# Error: make_rescale() missing 2 required keyword-only arguments: 'input_scale' and 'output_scale'
compile_rescale()¶
Compile rescaling from one fixed-point scale to another.
This cell verifies the compile_rescale 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_rescale
# Error: make_rescale() missing 2 required keyword-only arguments: 'input_scale' and 'output_scale'
make_round_to_multiple()¶
Create quantization of an encrypted integer to the nearest multiple of step.
This cell verifies the make_round_to_multiple 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.fixed_point import make_round_to_multiple
fn = make_round_to_multiple(min_input=-2, max_input=3, step=3)
def test_make_round_to_multiple_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_round_to_multiple_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_round_to_multiple: all inputs were skipped — test is broken"
print(f"make_round_to_multiple tests passed! ({_successes}/{len(inputset)})")
compile_round_to_multiple()¶
Compile quantization of an encrypted integer to the nearest multiple of step.
This cell verifies the compile_round_to_multiple 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.fixed_point import compile_round_to_multiple
from concrete_fhe_toolkit.math.fixed_point import make_round_to_multiple
circuit = compile_round_to_multiple(min_input=-2, max_input=3, step=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_round_to_multiple(min_input=-2, max_input=3, step=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_round_to_multiple: all inputs were skipped — test is broken"
print(f"compile_round_to_multiple tests passed! ({_successes}/{len(inputset)})")
make_modf()¶
Create a function returning (fractional_part, integer_part) like math.modf.
This cell verifies the make_modf 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.fixed_point import make_modf
fn = make_modf(min_input=-2, max_input=3)
def test_make_modf_enc(value):
import numpy as np
res = fn(value)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_make_modf_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_modf: all inputs were skipped — test is broken"
print(f"make_modf tests passed! ({_successes}/{len(inputset)})")
compile_modf()¶
Compile a circuit returning (fractional_part, integer_part) like math.modf.
This cell verifies the compile_modf 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.fixed_point import compile_modf
from concrete_fhe_toolkit.math.fixed_point import make_modf
circuit = compile_modf(min_input=-2, max_input=3)
inputset = [(3,), (-2,), (0,), (2,)]
_successes = 0
for inp in inputset:
try:
expected = make_modf(min_input=-2, max_input=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_modf: all inputs were skipped — test is broken"
print(f"compile_modf tests passed! ({_successes}/{len(inputset)})")
make_fixed_point_multiply()¶
Create multiplication of two scaled values with the product rescaled back.
This cell verifies the make_fixed_point_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.fixed_point import make_fixed_point_multiply
fn = make_fixed_point_multiply()
def test_make_fixed_point_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_fixed_point_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_fixed_point_multiply: all inputs were skipped — test is broken"
print(f"make_fixed_point_multiply tests passed! ({_successes}/{len(inputset)})")
compile_fixed_point_multiply()¶
Compile rescaled multiplication of two scaled encrypted values.
This cell verifies the compile_fixed_point_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.fixed_point import compile_fixed_point_multiply
from concrete_fhe_toolkit.math.fixed_point import make_fixed_point_multiply
circuit = compile_fixed_point_multiply(min_input=-2, max_input=3)
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
_successes = 0
for inp in inputset:
try:
expected = make_fixed_point_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_fixed_point_multiply: all inputs were skipped — test is broken"
print(f"compile_fixed_point_multiply tests passed! ({_successes}/{len(inputset)})")
make_encode_fixed_point() (Helper)¶
Demonstrates the make_encode_fixed_point function. This is a client-side (cleartext) helper function. It is not an FHE circuit and cannot be compiled with fhe.Compiler.
from concrete_fhe_toolkit.math.fixed_point import make_encode_fixed_point
encode_fn = make_encode_fixed_point(scale=10)
clear_float = 2.5
encoded_int = encode_fn(clear_float)
print(f'Encoded {clear_float} with scale 10 -> {encoded_int} (type: {type(encoded_int).__name__})')
assert encoded_int == 25
print('Encoding successful! Ready for encryption.')
make_decode_fixed_point() (Helper)¶
Demonstrates the make_decode_fixed_point function. This is a client-side (cleartext) helper function. It is not an FHE circuit and cannot be compiled with fhe.Compiler.
from concrete_fhe_toolkit.math.fixed_point import make_decode_fixed_point
decode_fn = make_decode_fixed_point(scale=10)
decrypted_int = 25
decoded_float = decode_fn(decrypted_int)
print(f'Decoded {decrypted_int} with scale 10 -> {decoded_float} (type: {type(decoded_float).__name__})')
assert decoded_float == 2.5
print('Decoding successful! Ready for client usage.')