# tak-pesar/tgcrypto architecture

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

- Interactive diagram: https://gitdiagram.com/tak-pesar/tgcrypto
- Repository: https://github.com/tak-pesar/tgcrypto

## Overview

##### Main Components
- PHP Extension Layer: Represents the part that integrates with PHP. This includes the PHP code (like run-tests.php and any PHP-specific tests in the tests/ directory), which interacts with the extension.
- Rust Cryptography Library: The core cryptographic functions are implemented in Rust (files like src/lib.rs and managed via Cargo.toml). This serves as the underlying logic behind the PHP extension.
- Build & Integration Utilities: The install.sh script (and possibly the build.yml GitHub Actions file) shows the automated installation and continuous integration aspects.
- Testing Framework: The PHP scripts (run-tests.php and tests/test-00*.php) are used to run performance checks and validate functionality.

##### Relationships and Interactions
- PHP acts as the client that makes calls to the PHP extension.
- The PHP extension functions as a wrapper that forwards cryptographic function calls to the Rust library through an FFI layer (commonly using cbindgen or similar tooling to create the bridging code).
- The Rust library processes the cryptographic functions and returns the results back through the extension to PHP.
- The installation script (install.sh) integrates the building of the Rust library (using Cargo) with the PHP environment setup.
- Testing scripts validate that PHP correctly interacts with the underlying Rust logic and that performance meets expectations.

##### Important Architectural Patterns
- Language Interoperability / FFI: The design centers on bridging PHP and Rust, likely using a Foreign Function Interface (FFI) pattern.
- Layered Architecture: There is clear separation between the presentation/client layer (PHP), the extension layer (acting as a mediator) and the core logic layer (Rust library).

##### Relevant Technologies/Frameworks
- PHP for end-user code and tests.
- Rust for the actual cryptographic computations.
- Cargo for building and managing the Rust project.
- GitHub Actions (via .github/workflows/build.yml) for CI/CD integration.
- Unix shell scripting (install.sh) for installation and deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Build & Integration Process"
        INSTALL["Installation Script"]:::build
        CI["CI Pipeline"]:::build
    end

    PHP_EXT["PHP Extension Layer"]:::php
    RUST_LIB["Rust Cryptography Library"]:::rust
    PHP_CLIENT["PHP Client"]:::phpclient

    %% Build & Integration Process builds the PHP Extension Layer and Rust Library
    INSTALL -->|"builds"| PHP_EXT
    INSTALL -->|"builds"| RUST_LIB
    CI -->|"testsBuild"| PHP_EXT
    CI -->|"testsBuild"| RUST_LIB

    %% Client calls the PHP Extension Layer which in turn calls the Rust Library
    PHP_CLIENT -->|"calls"| PHP_EXT
    PHP_EXT -->|"FFI_call"| RUST_LIB
    RUST_LIB -->|"returns"| PHP_EXT
    PHP_EXT -->|"returns"| PHP_CLIENT

    %% Click Events for Build & Integration Utilities
    click INSTALL "https://github.com/tak-pesar/tgcrypto/blob/main/install.sh"
    click CI "https://github.com/tak-pesar/tgcrypto/blob/main/.github/workflows/build.yml"

    %% Click Events for PHP Extension Layer (mapped to run-tests.php and tests/ directory)
    click PHP_EXT "https://github.com/tak-pesar/tgcrypto/blob/main/run-tests.php"
    click PHP_EXT "https://github.com/tak-pesar/tgcrypto/tree/main/tests/"

    %% Click Events for Rust Cryptography Library (mapped to src/lib.rs and Cargo.toml)
    click RUST_LIB "https://github.com/tak-pesar/tgcrypto/blob/main/src/lib.rs"
    click RUST_LIB "https://github.com/tak-pesar/tgcrypto/blob/main/Cargo.toml"

    %% Styles
    classDef php fill:#f7d679,stroke:#333,stroke-width:2px;
    classDef rust fill:#d0f0c0,stroke:#333,stroke-width:2px;
    classDef build fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef phpclient fill:#ffb6c1,stroke:#333,stroke-width:2px;
```
