Skip to content

Wrong usage of LDA raises runtime panic #208

@Bbalon-a

Description

@Bbalon-a

To reproduce
When using LDA with wrong input data x

from scalib.modeling import Lda, LdaAcc
import numpy as np
# 1000 traces of length 10, with value between 0 and 4091
traces = np.random.randint(0, 4092, (1000, 10), dtype=np.int16)
# classes between 0 and 15 (2 variables)
x = np.random.randint(16, 32, (1000, 2), dtype=np.uint16) #Instead of randint(0,16)
lda_acc = LdaAcc(nc=16, pois=[list(range(5)), list(range(4, 10))])
lda_acc.fit_u(traces, x)
lda = Lda(lda_acc, p=3) # Projection to 3 dimensions.
# predict classes for new traces
new_traces = np.random.randint(0, 256, (20,10), dtype=np.int16)
predicted_proba = lda.predict_proba(new_traces)

Outputs:

thread '<unnamed>' panicked at /host/home/runner/work/_temp/cargo_home/registry/src/index.crates.io-1949cf8c6b5b557f/ndarray-0.16.1/src/arraytraits.rs:36:5:
ndarray: index out of bounds
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/DATA4/common/bbalon/test/test_lda.py", line 8, in <module>
    lda_acc.fit_u(traces, x)
  File "/DATA4/common/bbalon/test/.venv/lib/python3.12/site-packages/scalib/modeling/lda.py", line 436, in fit_u
    self._inner.fit(traces, x, get_config())
pyo3_runtime.PanicException: ndarray: index out of bounds

Expected behaviour:
I would see an error similar to the one in the SNR function :

_scalib_ext.ScalibError: A SNR class value of a variable is larger than the given number of classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions