simple-crypto / SCALib

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

Generic Factor does not use Public Variables #139

Closed F-Lehmann closed 2 months ago

F-Lehmann commented 8 months ago

Describe the bug Generic Factor treat Public Variables as Uniformly distributed

To Reproduce

import numpy as np
import scalib.attacks 

nc = 2
fg = scalib.attacks.FactorGraph(f'''
NC {nc}
PUB SINGLE A
VAR SINGLE NOT_A
GENERIC SINGLE NOT
PROPERTY NOT(A,NOT_A)
''')

NOT = []

for a in range(nc):
    NOT.append([a,not a])

gen_factors = {
    'NOT':scalib.attacks.GenFactor.sparse_functional(np.array(NOT,dtype=np.uint32))
}

A = 0
bp = scalib.attacks.BPState(fg,1,public_values={'A':A},gen_factors=gen_factors)
bp.bp_acyclic('NOT_A')
result = bp.get_distribution('NOT_A')
print(result)

A = 1
bp = scalib.attacks.BPState(fg,1,public_values={'A':A},gen_factors=gen_factors)
bp.bp_acyclic('NOT_A')
result = bp.get_distribution('NOT_A')
print(result)

Observed behavior

Results are

[0.5 0.5]
[0.5 0.5]

Expected behavior

If the Public whould have been respected the expected result is:

[0.0 1.0]
[1.0 0.0]

Environment (please complete the following information):