Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include more tests for decaps in mlkem #88

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions tests/test_ml_kem.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def generic_keygen_kat(self, ML_KEM, index):
self.assertEqual(ek, ek_kat)
self.assertEqual(dk, dk_kat)

def generic_encap_decap_kat(self, ML_KEM, index):
def generic_encap_kat(self, ML_KEM, index):
with open(
"assets/ML-KEM-encapDecap-FIPS203/internalProjection.json"
) as f:
Expand All @@ -126,6 +126,24 @@ def generic_encap_decap_kat(self, ML_KEM, index):
K_prime = ML_KEM.decaps(dk_kat, c_kat)
self.assertEqual(K_prime, k_kat)

def generic_decap_kat(self, ML_KEM, index):
with open(
"assets/ML-KEM-encapDecap-FIPS203/internalProjection.json"
) as f:
data = json.load(f)
kat_data = data["testGroups"][3 + index]["tests"]

# Parse out the decaps key
dk_hex = data["testGroups"][3 + index]["dk"]
dk_kat = bytes.fromhex(dk_hex)

# Ensure that decaps works
for test in kat_data:
c_kat = bytes.fromhex(test["c"])
k_kat = bytes.fromhex(test["k"])
K = ML_KEM.decaps(dk_kat, c_kat)
self.assertEqual(K, k_kat)

def test_ML_KEM_512_keygen(self):
self.generic_keygen_kat(ML_KEM_512, 0)

Expand All @@ -135,11 +153,20 @@ def test_ML_KEM_768_keygen(self):
def test_ML_KEM_1024_keygen(self):
self.generic_keygen_kat(ML_KEM_1024, 2)

def test_ML_KEM_512_encap_decap(self):
self.generic_encap_decap_kat(ML_KEM_512, 0)
def test_ML_KEM_512_encap(self):
self.generic_encap_kat(ML_KEM_512, 0)

def test_ML_KEM_768_encap(self):
self.generic_encap_kat(ML_KEM_768, 1)

def test_ML_KEM_1024_encap(self):
self.generic_encap_kat(ML_KEM_1024, 2)

def test_ML_KEM_512_decap(self):
self.generic_decap_kat(ML_KEM_512, 0)

def test_ML_KEM_768_encap_decap(self):
self.generic_encap_decap_kat(ML_KEM_768, 1)
def test_ML_KEM_768_decap(self):
self.generic_decap_kat(ML_KEM_768, 1)

def test_ML_KEM_1024_encap_decap(self):
self.generic_encap_decap_kat(ML_KEM_1024, 2)
def test_ML_KEM_1024_decap(self):
self.generic_decap_kat(ML_KEM_1024, 2)