From dc1c6c74afd7db66040167e50bcb51bcefe1022e Mon Sep 17 00:00:00 2001 From: youben11 Date: Thu, 4 Apr 2024 12:11:37 +0100 Subject: [PATCH] feat(compiler): warn when there is overflow in simulation --- .../compiler/lib/Runtime/simulation.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp b/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp index f89318d535..021f301ac6 100644 --- a/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp +++ b/compilers/concrete-compiler/compiler/lib/Runtime/simulation.cpp @@ -183,9 +183,19 @@ void sim_wop_pbs_crt( uint64_t sim_neg_lwe_u64(uint64_t plaintext) { return ~plaintext + 1; } -uint64_t sim_add_lwe_u64(uint64_t lhs, uint64_t rhs) { return lhs + rhs; } +uint64_t sim_add_lwe_u64(uint64_t lhs, uint64_t rhs) { + if (lhs > UINT64_MAX - rhs) { + printf("WARNING: overflow happened during addition in simulation\n"); + } + return lhs + rhs; +} -uint64_t sim_mul_lwe_u64(uint64_t lhs, uint64_t rhs) { return lhs * rhs; } +uint64_t sim_mul_lwe_u64(uint64_t lhs, uint64_t rhs) { + if (rhs != 0 && lhs > UINT64_MAX / rhs) { + printf("WARNING: overflow happened during multiplication in simulation\n"); + } + return lhs * rhs; +} void sim_encode_expand_lut_for_boostrap( uint64_t *out_allocated, uint64_t *out_aligned, uint64_t out_offset,