simple-crypto / SCALib

Side-Channel Analysis Library
GNU Affero General Public License v3.0
74 stars 19 forks source link

Error when using non finite value in key rank #165

Closed cmomin closed 2 months ago

cmomin commented 5 months ago

Describe the bug

A bug occurs when trying to compute a key rank approximation when non-finite scores are used.

To Reproduce

from scalib.postprocessing import rank_accuracy
import numpy as np
(rmin,r,rmax) = rank_accuracy(-np.log(np.zeros((1,2))),[0])

Observed behavior

<ipython-input-1-b61ab406b2ff>:3: RuntimeWarning: divide by zero encountered in log
  (rmin,r,rmax) = rank_accuracy(-np.log(np.zeros((1,2))),[0])
thread '<unnamed>' panicked at 'Ranking error: Non-finite cost', scalib-py/src/ranking.rs:21:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---------------------------------------------------------------------------
PanicException                            Traceback (most recent call last)
Cell In[1], line 3
      1 from scalib.postprocessing import rank_accuracy
      2 import numpy as np
----> 3 (rmin,r,rmax) = rank_accuracy(-np.log(np.zeros((1,2))),[0])

File ~/.pyenv/versions/3.10.13/lib/python3.10/site-packages/scalib/postprocessing/rankestimation.py:136, in rank_accuracy(costs, key, acc_bit, method, max_nb_bin)
    100 r"""Estimate the rank of the full keys based on scores based on histograms.
    101 
    102 Parameters
   (...)
    133         - **rmax** is an upper bound for the key rank.
    134 """
    135 with scalib.utils.interruptible():
--> 136     return _scalib_ext.rank_accuracy(
    137         costs,
    138         key,
    139         2.0**acc_bit,
    140         _choose_merge_value(costs),
    141         method,
    142         max_nb_bin,
    143         get_config(),
    144     )

PanicException: Ranking error: Non-finite cost

Expected behavior Not panicking, handling particular case of non-finite value

Environment (please complete the following information):

Additional context Python install from pyenv (https://github.com/pyenv/pyenv)