This document outlines the functionality and structure of the toy.c
file, which implements a basic toy cryptosystem for educational purposes. The system utilizes matrix operations and polynomial multiplication in a finite field for encryption and decryption.
Please note: This is a simplified cryptosystem designed for learning and should not be used for real-world security applications.
toy.c
: Main implementation file containing the cryptosystem code.toy.h
: Header file declaring constants and function prototypes.
The toy cryptosystem showcases basic encryption and decryption concepts using matrices and polynomials. It operates through three main functions:
toy_gen
(Key Generation): Generates public and private keys for the system.toy_enc
(Encryption): Encrypts plaintext messages using the public key.toy_dec
(Decryption): Decrypts ciphertexts using the private key.
- Purpose: Generates keys and parameters for the cryptosystem.
- Parameters:
A
: Matrix used in the system.t
: Vector used in the system.s
: Secret key vector.
- Details:
- Generates a random matrix
A
with values moduloTK_Q
. - Generates small random vectors
s
ande
. - Performs matrix-vector multiplication and vector addition to obtain
t
.
- Generates a random matrix
- Purpose: Encrypts plaintext messages using the public key.
- Parameters:
A
: Matrix used in the system.t
: Vector used in the system.plain
: Plaintext message to be encrypted.u
: Vector result of encryption.v
: Vector result of encryption with added noise.
- Details:
- Generates random vectors
e1
,e2
, andr
. - Performs matrix-vector multiplication and vector addition to get
u
. - Performs matrix-vector multiplication, vector addition, and adds noise to get
v
.
- Generates random vectors
- Purpose: Decrypts ciphertexts using the private key.
- Parameters:
s
: Secret key vector.u
: Vector from encryption.v
: Vector from encryption with added noise.
- Details:
- Performs matrix-vector multiplication to get
suResult
. - Calculates the difference between
v
andsuResult
. - Reconstructs the plaintext from the decrypted values.
- Performs matrix-vector multiplication to get
toy_polmul_naive
: Performs polynomial multiplication in Z97[X]/(X^4+1).multiplyAndAccumulate
: Performs matrix multiplication and accumulation for each row in matrixA
.toy_fillSmall
: Generates small Gaussian random numbers.transpose3D
: Transposes a 3D array.
TK_Q
: Modulus for finite field operations.POLY_SCALAR_SIZE
,POLY_VECTOR_SIZE
,POLY_MATRIX_SIZE
: Sizes of polynomial scalars, vectors, and matrices.TK_K
,TK_N
: Parameters defining the dimensions of the matrix.
- Key Generation:
- Generate random matrix
A
. - Generate small random vectors
s
ande
. - Perform matrix-vector multiplication and vector addition to obtain
t
.
- Generate random matrix
- Encryption:
- Generate random vectors
e1
,e2
, andr
. - Perform matrix-vector multiplication and vector addition to get
u
. - Perform matrix-vector multiplication, vector addition, and add noise to get
v
.
- Generate random vectors
- Decryption:
- Perform matrix-vector multiplication to get
suResult
. - Calculate the difference between
v
andsuResult
. - Reconstruct the plaintext from the decrypted values.
- Perform matrix-vector multiplication to get
The main function tests the encryption and decryption functions for all possible 4-bit plaintext values. It iterates through each value, encrypts it, decrypts the ciphertext, and compares the results. The output verifies the functionality of the cryptosystem for all tested values.