-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from PyryL/week5
Week5
- Loading branch information
Showing
13 changed files
with
331 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Implementation | ||
|
||
## Project structure | ||
|
||
This project is divided into packages and modules that handle a small portion of the functionality. Packages have strict hierarchy that allows importing modules only from the same or lower-ranked package. Below is a diagram showing the import structure between packages. | ||
|
||
```mermaid | ||
classDiagram | ||
encryption <|-- utilities | ||
encryption <|-- constants | ||
encryption <|-- entities | ||
entities <|-- constants | ||
utilities <|-- entities | ||
utilities <|-- constants | ||
ccakem <|-- utilities | ||
ccakem <|-- entities | ||
ccakem <|-- constants | ||
ccakem <|-- encryption | ||
class encryption { | ||
encrypt | ||
decrypt | ||
keygen | ||
} | ||
class utilities { | ||
byte_conversion | ||
compression | ||
encoding | ||
cbd | ||
parse | ||
pseudo_random | ||
round | ||
} | ||
class entities { | ||
polring | ||
} | ||
class constants | ||
class ccakem | ||
``` | ||
|
||
Here are short descriptions of what is the purpose of each package: | ||
|
||
* **utilities** package provides some basic functionalities, such as conversions, rounding and encoding, for higher-ranked modules to use. | ||
* **entities** contains data structures. | ||
* **constants** module has some fixed numerical values defined in the Kyber specification. | ||
* **encryption** has capabilities for Kyber asymmetric encryption. | ||
* **ccakem** has functions that utilize encryption and make Kyber a key-encapsulation mechanism. | ||
|
||
## Utilities | ||
|
||
Here are more in-depth descriptions of modules in utilities package. | ||
|
||
### Byte conversion | ||
|
||
Byte conversion module has some basic functions that integers and bit arrays to bytes, and vice versa. | ||
|
||
### Compression | ||
|
||
During the en/decryption the coefficients of polynomial ring are in modulo `q`, that is, between 0 and `q-1` (inclusive). When we transfer these polynomial rings, we can, however, reduce the size by downscaling these coefficients. That is done with compress and decompress functions. | ||
|
||
### Encoding | ||
|
||
Usually the polynomial rings are handled as `PolynomialRing` instances. We can not, however, send these instances over the Internet, so we have to encode them into byte arrays. At the other end, we need to recover polynomial ring from the byte array. This is done with encode and decode functions. | ||
|
||
### Parse | ||
|
||
Parse is a pseudo-random function that generates a specific type of polynomial ring instance from a random byte stream. This is different from decoding in that the input is byte stream instead of byte array, i.e., the number of bytes required to form the result is not known beforehand. | ||
|
||
### CBD | ||
|
||
This module provides a single function that deterministically produces a polynomial ring from byte array. Behavior of this function is quite similar to `parse` but in this case the length of the input byte array is fixed. | ||
|
||
### Pseudo-random | ||
|
||
This module includes multiple functions that use SHA-3 hash algorithm family to deterministically produce pseudo-random byte arrays from given seeds. | ||
|
||
### Round | ||
|
||
This small module provides a function that rounds floats in a "traditional" way, that is, ties rounded up instead of away from zero. For example, Python's built-in round function outputs `round(-3.5)=-4` whereas `normal_round(-3.5)=-3`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Week 5 | ||
|
||
_27.11. – 3.12.2023_ | ||
|
||
This week I improved and added unit tests to lift branch coverage back to 100% (from 99% last week). I also added new performance tests. Documentation went through a process where I improved explanation and added the last missing one, implementation document. Finally, I had some time to response some of the issues that the first peer review pointed out. | ||
|
||
Total working time: 5 hours |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from perf_tests.test_encryption import runner as encryption_test_runner | ||
from perf_tests.test_ccakem import runner as ccakem_test_runner | ||
from perf_tests.test_aes_integration import runner as aes_integration_test_runner | ||
|
||
encryption_test_runner() | ||
ccakem_test_runner() | ||
aes_integration_test_runner() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from random import seed, randbytes | ||
from time import time | ||
from Crypto.Cipher import AES | ||
from kyber.ccakem import ccakem_generate_keys, ccakem_encrypt, ccakem_decrypt | ||
|
||
def run(payload: bytes) -> tuple[float, float, float]: | ||
""":returns Durations of handshake and actual payload transfer in seconds as a tuple.""" | ||
|
||
t0 = time() | ||
|
||
# Alice | ||
private_key, public_key = ccakem_generate_keys() | ||
|
||
# send public_key Alice->Bob | ||
|
||
# Bob | ||
ss_ciphertext, shared_secret1 = ccakem_encrypt(public_key) | ||
|
||
# send ss_ciphertext Bob->Alice | ||
|
||
# Alice | ||
shared_secret2 = ccakem_decrypt(ss_ciphertext, private_key) | ||
|
||
t1 = time() | ||
|
||
# Alice | ||
aes_cipher = AES.new(shared_secret2, AES.MODE_GCM) | ||
payload_nonce = aes_cipher.nonce | ||
payload_ciphertext, payload_tag = aes_cipher.encrypt_and_digest(payload) | ||
|
||
# send payload_ciphertext, payload_tag and payload_nonce Alice->Bob | ||
|
||
# Bob | ||
aes_cipher = AES.new(shared_secret1, AES.MODE_GCM, nonce=payload_nonce) | ||
decrypted_payload = aes_cipher.decrypt_and_verify(payload_ciphertext, payload_tag) | ||
|
||
assert payload == decrypted_payload | ||
|
||
return (t1-t0, time()-t1) | ||
|
||
def runner(): | ||
seed(42) | ||
payload = randbytes(100_000_000) # 100 megabytes | ||
print("Starting AES integration performance test (about 3 seconds)") | ||
durations = run(payload) | ||
print("Results:") | ||
print(f"Handshake: {durations[0]:.2f} sec") | ||
print(f"Payload transfer: {durations[1]:.2f} sec") | ||
print(f"Total: {sum(durations):.2f} sec") | ||
|
||
if __name__ == "__main__": | ||
runner() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from time import time | ||
from kyber.ccakem import ccakem_generate_keys, ccakem_encrypt, ccakem_decrypt | ||
|
||
def run_test() -> tuple[float, float, float]: | ||
t0 = time() | ||
|
||
private_key, public_key = ccakem_generate_keys() | ||
|
||
t1 = time() | ||
|
||
ciphertext, shared_secret1 = ccakem_encrypt(public_key) | ||
|
||
t2 = time() | ||
|
||
shared_secret2 = ccakem_decrypt(ciphertext, private_key) | ||
|
||
t3 = time() | ||
|
||
assert shared_secret1 == shared_secret2 | ||
|
||
return (t1-t0, t2-t1, t3-t2) | ||
|
||
def runner(): | ||
print("Starting ccakem performance test (about 2 mins)") | ||
|
||
test_iters = 250 | ||
averages = [0, 0, 0] | ||
|
||
for _ in range(test_iters): | ||
durations = run_test() | ||
averages = [averages[i]+durations[i] for i in range(3)] | ||
|
||
print("Results (averages):") | ||
print(f"Keypair generation: {averages[0]/test_iters:.5f} sec") | ||
print(f"Encryption: {averages[1]/test_iters:.5f} sec") | ||
print(f"Decryption: {averages[2]/test_iters:.5f} sec") | ||
|
||
if __name__ == "__main__": | ||
runner() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.