Computes a tolerance value suitable for comparing the cleartext
result of a CKKS circuit against a decrypted ciphertext. The
value is a function of the scheme's scaling factor, the
circuit's multiplicative depth at the point of decryption, and
the ScalingTechnique-specific precision loss per level.
Arguments
- x
For the numeric method: integer multiplicative depth at the point of decryption. For the Ciphertext method: a
Ciphertextobject whose associated context supplies all parameters.- ...
Method-specific arguments. The numeric method accepts
scaling_factor_bits(integer bit size of the scaling modulus, typical values 50/59/78),scaling_technique(a character string like"FLEXIBLEAUTO"or an integer from theScalingTechniqueenum), andk(conservative multiplicative factor, default 8). The Ciphertext method accepts onlyk.
Value
Numeric scalar — the tolerance value to use as
tolerance in tinytest::expect_equal() or as atol in a
manual diff.
Details
Two dispatch forms:
Stage 1 (numeric): pass parameters as direct arguments. Useful when the ciphertext isn't yet constructed — e.g. when you need a tolerance before calling
encrypt().Stage 2 (Ciphertext): pass a
Ciphertextdirectly. The helper reads the associatedCryptoContextviaget_crypto_context(), pulls the multiplicative depth (from context) minus the ciphertext's current level, computes the scaling factor bits viackks_scaling_factor_bits(), and looks up the scaling technique. This form is preferred at test sites where a ciphertext exists.
Examples
## Stage 1 -- pass the parameters directly:
fhe_ckks_tolerance(4L, 50L, "FLEXIBLEAUTO")
#> [1] 1.136868e-13
## Stage 2 -- pass a ciphertext and let it read the parameters
## off the associated context:
cc <- fhe_context("CKKS", multiplicative_depth = 4L,
scaling_mod_size = 50L, batch_size = 8L)
kp <- key_gen(cc, eval_mult = TRUE)
pt <- make_ckks_packed_plaintext(cc, c(0.1, 0.2, 0.3, 0.4))
ct <- encrypt(kp@public, pt, cc = cc)
fhe_ckks_tolerance(ct)
#> [1] 0.0002441406
