# mupq/pqm4 architecture

> Architecture diagram of the GitHub repository mupq/pqm4, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mupq/pqm4
- Repository: https://github.com/mupq/pqm4

## Overview

##### Main Components

The system architecture should be divided into these key components:

a) Hardware Layer:
- ARM Cortex-M4 Development Boards
- NUCLEO-L4R5ZI (primary)
- STM32F4 Discovery
- NUCLEO-L476RG
- CW308T-STM32F3
- MPS2-AN386 (FPGA/simulation)

b) Core Cryptographic Components:
- KEM (Key Encapsulation Mechanisms) Module
- Digital Signature Module
- Common Cryptographic Primitives
- FIPS202 (SHA-3/SHAKE)
- SHA-2
- AES Module

c) Testing & Benchmarking Framework:
- Test Runner
- Benchmark System
- Testvector Generator
- Stack Usage Analyzer

d) Build System:
- Make-based Compilation
- Platform-specific Configurations
- Optimization Controls

##### Relationships and Data Flow

- Communication between host PC and target board
- Data flow between cryptographic modules
- Test/benchmark data collection paths
- Build system dependencies

##### Detailed Component Breakdown

For each cryptographic implementation, show:
- API interfaces
- Internal components
- Memory management
- Platform-specific optimizations

##### Implementation Variants

Different implementation types:
- clean (from PQClean)
- ref (NIST reference)
- opt (C optimized)
- m4 (Cortex-M4 optimized)
- m4f (Cortex-M4F optimized)

**┌─────────────────────────────────────────────────────────┐**
- Host System                         │
- ┌─────────────┐  ┌──────────────┐  ┌──────────────┐  │
- Test Scripts│  │Benchmark Tool│  │Build System  │  │
- ┘  └──────────────┘  └──────────────┘  │
- ┘

**┌───────────────────────────────────────────────────────┐**
- Communication Layer                    │
- ┌────────────┐    ┌───────────┐    ┌────────────┐   │
- Serial   │    │  ST-Link  │    │   QEMU     │   │
- ┘    └───────────┘    └────────────┘   │
- ┘

**┌───────────────────────────────────────────────────────┐**
- Target Hardware Layer                    │
- ┌────────────────────────────────────────┐          │
- ARM Cortex-M4 Board            │          │
- ┌──────────┐  ┌──────────┐           │          │
- KEM    │  │  Sign    │           │          │
- ┘  └──────────┘           │          │
- ┌──────────┐  ┌──────────┐           │          │
- SHA-3   │  │   AES    │           │          │
- ┘  └──────────┘           │          │
- ┘          │
- ┘

##### Additional Elements

Include:
- Version information
- Supported platforms
- Memory requirements
- Performance metrics collection points
- Error handling paths
- Configuration options

## Diagram (Mermaid)

```mermaid
graph TB
    %% Style definitions
    classDef hardware fill:#E0E0E0,stroke:#333
    classDef crypto fill:#D4E6F1,stroke:#333
    classDef testing fill:#D5F5E3,stroke:#333
    classDef build fill:#FAE5D3,stroke:#333
    classDef comms fill:#FCF3CF,stroke:#333

    subgraph Host["Host System Layer"]
        TestRunner["Test Runner"]:::testing
        BenchSys["Benchmark System"]:::testing
        TestVec["Testvector Generator"]:::testing
        BuildSys["Build System"]:::build
    end

    subgraph Comms["Communication Layer"]
        Serial["Serial Interface"]:::comms
        STLink["ST-Link"]:::comms
        QEMU["QEMU"]:::comms
    end

    subgraph Hardware["Hardware Layer"]
        direction TB
        subgraph Boards["Development Boards"]
            NUCLEO["NUCLEO-L4R5ZI"]:::hardware
            STM32F4["STM32F4 Discovery"]:::hardware
            L476RG["NUCLEO-L476RG"]:::hardware
            CW308T["CW308T-STM32F3"]:::hardware
            MPS2["MPS2-AN386"]:::hardware
        end

        subgraph HAL["Hardware Abstraction"]
            PlatformHAL["Platform HAL"]:::hardware
            MemLayout["Memory Layout"]:::hardware
        end
    end

    subgraph Crypto["Core Cryptographic Components"]
        direction TB
        KEM["KEM Module"]:::crypto
        DigSig["Digital Signatures"]:::crypto
        
        subgraph Primitives["Cryptographic Primitives"]
            direction TB
            SHA3["SHA-3/SHAKE"]:::crypto
            SHA2["SHA-2"]:::crypto
            AES["AES Module"]:::crypto
            RNG["Random Number Generator"]:::crypto
        end
    end

    %% Relationships
    TestRunner --> Serial
    BenchSys --> Serial
    Serial --> Boards
    STLink --> Boards
    QEMU --> Boards
    
    Boards --> HAL
    HAL --> Crypto
    BuildSys --> Crypto
    TestVec --> TestRunner

    %% Click events
    click TestRunner "https://github.com/mupq/pqm4/blob/master/test.py"
    click BenchSys "https://github.com/mupq/pqm4/blob/master/benchmarks.py"
    click TestVec "https://github.com/mupq/pqm4/blob/master/testvectors.py"
    click BuildSys "https://github.com/mupq/pqm4/tree/master/Makefile"
    click PlatformHAL "https://github.com/mupq/pqm4/blob/master/common/hal-opencm3.c"
    click MemLayout "https://github.com/mupq/pqm4/blob/master/ldscripts/nucleo-l4r5zi.ld"
    click KEM "https://github.com/mupq/pqm4/tree/master/crypto_kem/"
    click DigSig "https://github.com/mupq/pqm4/tree/master/crypto_sign/"
    click AES "https://github.com/mupq/pqm4/blob/master/common/aes.c"
    click SHA2 "https://github.com/mupq/pqm4/blob/master/common/crypto_hashblocks_sha512.c"
    click SHA3 "https://github.com/mupq/pqm4/blob/master/common/keccakf1600.S"
    click RNG "https://github.com/mupq/pqm4/blob/master/common/randombytes.c"
    click Serial "https://github.com/mupq/pqm4/blob/master/hostside/host_unidirectional.py"

    %% Legend
    subgraph Legend
        H["Hardware"]:::hardware
        C["Cryptographic"]:::crypto
        T["Testing"]:::testing
        B["Build"]:::build
        Co["Communication"]:::comms
    end
```
