Skip to content

quiaingmuarua/mini_tools

Repository files navigation

Mini Tools Collection

A comprehensive collection of educational programming tools and language implementations, covering compilers, virtual machines, cryptography, and systems programming.

CI codecov Python 3.9+ License: MIT

🎯 Purpose

This project is a comprehensive collection of educational and practical mini-tools for understanding fundamental computer science concepts:

  • Compiler Construction - C compiler with LLVM backend
  • Virtual Machines - JVM implementation and JavaScript VM with VMP protection
  • Cryptography & Security - TLS protocols, ECDHE, encryption algorithms
  • Systems Programming - ELF parsing, linking, and binary analysis

πŸš€ Tools Overview

πŸ”§ MiniC - C Compiler with LLVM Backend

  • Complete C subset compiler with lexer, parser, and LLVM IR generation
  • JIT execution using MCJIT engine
  • AOT compilation to native executables
  • Optimization support with LLVM passes (O0, O2)
  • Features: variables, functions, control flow (if/while), expressions

β˜• Mini JVM - Java Virtual Machine

  • Bytecode virtual machine with stack-based execution
  • Object-oriented features with classes, methods, inheritance
  • Static method support and virtual method dispatch
  • Frame-based execution with local variables and operand stack

🟨 Mini JSVMP - JavaScript VM with VMP Protection

  • Complete JavaScript interpreter with bytecode compilation
  • Advanced VMP protection:
    • Opcode permutation and stream decoding
    • Immediate encryption with position-based PRNG
    • Integrity checks and anti-tampering
  • Language features: variables, functions, closures, control flow

πŸ” MiniTLS - Cryptographic Tools

  • ECDHE key exchange implementation (NIST P-256)
  • SEC1 point encoding/decoding (compressed/uncompressed)
  • HKDF key derivation function
  • Cryptographic primitives: AES, MD5, SHA256
  • Montgomery ladder scalar multiplication (side-channel resistant)

πŸ”— Mini Linker - Binary Analysis Tools

  • ELF parser supporting ELF32/ELF64, little/big endian
  • Binary analysis with header, section, and symbol parsing
  • Cross-platform support for various architectures

πŸ“¦ Installation

Prerequisites

# For MiniC (LLVM backend)
pip install llvmlite

# For all tools (development)
pip install -e ".[dev]"

Development Installation

# Clone the repository
git clone https://github.com/your-username/mini-tools.git
cd mini-tools

# Install in development mode with all dependencies
pip install -e ".[dev]"

πŸ§ͺ Usage Examples

πŸ”§ MiniC - C Compiler

from minic.driver import build_ir_from_source
from minic.runtime import emit_executable, run_executable

# C source code
c_code = '''
int add(int a, int b) { return a + b; }

int main() {
    int x = 2;
    int y = 3;
    int z = add(x, y);
    return z;
}
'''

# Compile to LLVM IR
llvm_ir = build_ir_from_source(c_code, optimization_level=0)

# Generate executable
exe_path = emit_executable(llvm_ir, "my_program")
exit_code = run_executable(exe_path)
print(f"Program returned: {exit_code}")  # Should print 5

β˜• Mini JVM

from mini_jvm.runtime import VM, ClassDef, Method, ObjRef

# Create a simple class with methods
class_def = ClassDef(
    name="Calculator",
    super=None,
    methods={"add": add_method},
    static_methods={"main": main_method}
)

# Run the virtual machine
vm = VM()
vm.load_class(class_def)
result = vm.call_static("Calculator", "main", [])

🟨 Mini JSVMP - JavaScript VM

// Regular execution
const code = `
function factorial(n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}
print(factorial(5));
`;

// Compile and run
compileAndRun(code);

// VMP protected execution
const protectedHex = packToHexHardened(bytecode, constants, functions);
runHexHardened(protectedHex, builtins);

πŸ” MiniTLS - Cryptographic Tools

from minit_tls.network.mini_ecdhe import gen_keypair, ecdhe_shared, hkdf_sha256

# Generate key pairs for Alice and Bob
alice_sk, alice_pk = gen_keypair()
bob_sk, bob_pk = gen_keypair()

# Perform ECDHE key exchange
alice_shared = ecdhe_shared(alice_sk, bob_pk)
bob_shared = ecdhe_shared(bob_sk, alice_pk)

# Both parties now have the same shared secret
assert alice_shared == bob_shared

# Derive session keys using HKDF
session_key = hkdf_sha256(alice_shared, salt=b"", info=b"demo", length=32)
print(f"Session key: {session_key.hex()}")

πŸ”— Mini Linker - ELF Analysis

# Analyze ELF files
python mini_linker/mini_read_elf.py -h binary_file    # ELF header
python mini_linker/mini_read_elf.py -l binary_file    # Program headers  
python mini_linker/mini_read_elf.py -S binary_file    # Section headers
python mini_linker/mini_read_elf.py -s binary_file    # Symbol table

πŸ”§ Development

Running Tests

# Run all tests
pytest

# Run tests for specific components
pytest minic/test/           # C compiler tests
pytest minit_tls/test/       # Cryptography tests

# Run tests with coverage
pytest --cov=minic --cov=minit_tls --cov=mini_jvm --cov=mini_linker --cov-report=html

# Run specific test files
pytest minit_tls/test/test_mini_ecdhe.py
pytest minic/test/minic_test.py

Code Quality

# Format code
black .

# Sort imports
isort .

# Lint code
flake8 .

# Type checking
mypy minic minit_tls mini_jvm mini_linker

Tool-Specific Development

MiniC Development

# Run C compiler tests
python minic/test/minic_test.py

# Test with different optimization levels
python minic/test/example.py

Mini JSVMP Development

# Run JavaScript VM tests
node mini_jsvmp/test_examples.js

Cryptography Development

# Run comprehensive ECDHE tests
python minit_tls/test/test_mini_ecdhe.py

πŸ—οΈ Project Structure

mini_tools/
β”œβ”€β”€ minic/                   # C Compiler with LLVM
β”‚   β”œβ”€β”€ lexer.py            # Tokenization
β”‚   β”œβ”€β”€ parser.py           # AST parsing  
β”‚   β”œβ”€β”€ codegen.py          # LLVM IR generation
β”‚   β”œβ”€β”€ runtime.py          # JIT & AOT execution
β”‚   β”œβ”€β”€ driver.py           # Compilation driver
β”‚   β”œβ”€β”€ examples/           # C source examples
β”‚   └── test/               # Compiler tests
β”œβ”€β”€ mini_jvm/               # Java Virtual Machine
β”‚   └── runtime.py          # JVM implementation
β”œβ”€β”€ mini_jsvmp/             # JavaScript VM with VMP
β”‚   β”œβ”€β”€ mini_jsvmp.js       # Main VM implementation
β”‚   β”œβ”€β”€ example.js          # Usage examples
β”‚   └── test_examples.js    # Test cases
β”œβ”€β”€ minit_tls/              # Cryptographic Tools
β”‚   β”œβ”€β”€ network/            # TLS protocols
β”‚   β”‚   β”œβ”€β”€ mini_ecdhe.py   # ECDHE implementation  
β”‚   β”‚   └── mini_tls.py     # TLS protocol
β”‚   β”œβ”€β”€ cryptor/            # Crypto primitives
β”‚   β”‚   β”œβ”€β”€ aes_encrypt_craft.py
β”‚   β”‚   β”œβ”€β”€ md5_craft.py
β”‚   β”‚   └── sha256_craft.py
β”‚   β”œβ”€β”€ example/            # Usage examples
β”‚   └── test/               # Cryptography tests
β”œβ”€β”€ mini_linker/            # Binary Analysis Tools
β”‚   └── mini_read_elf.py    # ELF parser
β”œβ”€β”€ .github/workflows/      # CI/CD configuration
β”œβ”€β”€ pyproject.toml          # Project configuration
└── README.md               # This file

πŸ§ͺ Testing Philosophy

This project follows comprehensive testing practices across all components:

Testing Approaches

  • Unit Tests - Individual function and component testing
  • Integration Tests - Cross-component interaction testing
  • Compiler Tests - End-to-end compilation and execution testing
  • VM Tests - Bytecode execution and runtime behavior testing
  • Cryptographic Tests - Mathematical property and vector validation
  • Binary Analysis Tests - ELF parsing and format validation

Tool-Specific Testing

  • MiniC: IR generation, optimization, and executable output validation
  • Mini JVM: Bytecode execution, method dispatch, and object lifecycle
  • Mini JSVMP: VMP protection, bytecode integrity, and language features
  • MiniTLS: Cryptographic correctness, known test vectors, interoperability
  • Mini Linker: ELF format compliance, cross-architecture support

🚦 Continuous Integration

Multi-component CI/CD pipeline with GitHub Actions:

Testing Matrix

  • Python versions: 3.9, 3.10, 3.11, 3.12
  • Operating systems: Ubuntu, macOS
  • LLVM dependencies: llvmlite compatibility testing
  • Node.js testing: JavaScript VM validation

Quality Assurance

  • Code formatting: black, isort
  • Linting: flake8, mypy type checking
  • Security scanning: bandit, safety
  • Coverage reporting: pytest-cov, codecov integration
  • Dependency management: pip-audit for vulnerability scanning

⚠️ Security Notice

Educational Purpose: These implementations are primarily for educational and research purposes. While they follow established best practices, they have not undergone formal security audits.

Production Use Guidance:

  • Cryptography: Use established libraries (cryptography, pycryptodome, OpenSSL)
  • Compilers: For production, use mature toolchains (GCC, Clang, OpenJDK)
  • VMs: Consider proven implementations (V8, SpiderMonkey, OpenJDK HotSpot)

πŸ“š Educational Resources

Compiler Construction

Virtual Machines

Cryptography

Binary Analysis

🀝 Contributing

We welcome contributions to all components! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Add tests for new functionality
  4. Ensure all tests pass (pytest)
  5. Format code (black ., isort .)
  6. Submit a pull request

Component-Specific Guidelines

  • MiniC: Add test cases for new language features
  • Mini JVM: Include bytecode test cases
  • Mini JSVMP: Test both regular and VMP-protected execution
  • MiniTLS: Provide cryptographic test vectors
  • Mini Linker: Test with various ELF files

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • LLVM Project - Infrastructure for modern compiler design
  • NIST - Cryptographic standards and specifications
  • ELF Specification - Binary format documentation
  • Open Source Community - Inspiration and foundational knowledge
  • Contributors - All users and contributors to this project

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published