Finance (Core)
apply_rate()¶
Multiply an encrypted amount by a public rate.
This cell verifies the apply_rate 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.finance.core import apply_rate
def test_apply_rate(amount):
import numpy as np
res = apply_rate(amount, 0.05)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_apply_rate, {'amount': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = apply_rate(inp[0], 0.05)
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, "apply_rate: all inputs were skipped — test is broken"
print(f"apply_rate tests passed! ({_successes}/{len(inputset)})")
return_actual_value() (Helper)¶
Demonstrates the return_actual_value 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.finance.core import return_actual_value
decrypted_val = 1500 # 15.00 scaled by 100
actual_float = return_actual_value(decrypted_val)
print(f'Decrypted integer {decrypted_val} to real value -> {actual_float}')
assert actual_float == 15.0
print('Decoding successful!')
calculate_tax()¶
Calculate the tax amount at RATE_SCALE scaling.
This cell verifies the calculate_tax 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.finance.core import calculate_tax
def test_calculate_tax(amount):
import numpy as np
res = calculate_tax(amount, 0.05)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_calculate_tax, {'amount': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = calculate_tax(inp[0], 0.05)
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, "calculate_tax: all inputs were skipped — test is broken"
print(f"calculate_tax tests passed! ({_successes}/{len(inputset)})")
discount()¶
Calculate the discounted amount at RATE_SCALE scaling.
This cell verifies the discount 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.finance.core import discount
def test_discount(amount):
import numpy as np
res = discount(amount, 0.05)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_discount, {'amount': 'encrypted'})
inputset = [(3,), (-2,), (0,), (2,)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = discount(inp[0], 0.05)
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, "discount: all inputs were skipped — test is broken"
print(f"discount tests passed! ({_successes}/{len(inputset)})")
simple_interest()¶
Calculate simple interest (amount * rate * time) at RATE_SCALE scaling.
This cell verifies the simple_interest 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.finance.core import simple_interest
def test_simple_interest(amount, time_period):
import numpy as np
res = simple_interest(amount, 0.05, time_period)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_simple_interest, {'amount': 'encrypted', 'time_period': 'encrypted'})
inputset = [(3, 1), (-2, -2), (0, 0), (2, 2), (1, 3)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = simple_interest(inp[0], 0.05, inp[1])
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, "simple_interest: all inputs were skipped — test is broken"
print(f"simple_interest tests passed! ({_successes}/{len(inputset)})")