Monocypher Documentation

repository·master·Indexed 19 days ago

https://github.com/loupvaillant/monocypher

A portable, easy-to-use, and auditable cryptographic library written in C, designed to provide the speed of libsodium with a footprint similar to TweetNaCl. Supports C (C99 and later) and C++ (C++98 and later).

Tokens
777
Snippets
5
Records
5
Agent score
23%

What's inside Monocypher

  1. Install Monocypher as a precompiled library

    master

    You can build Monocypher as a library and link against it.

    1. Run make to build the library.
    2. Use the header src/monocypher.h and either lib/libmonocypher.a (static) or lib/libmonocypher.so (shared).

    Default compiler settings are gcc -std=c99 with flags -pedantic -Wall -Wextra -O3 -march=native. To use a different compiler or flags, pass them to make:

    $ make CC="clang -std=c11" CFLAGS="-O2"
    $ make CC="clang -std=c11" CFLAGS="-O2"
  2. Install Monocypher to your system

    master

    To install Monocypher system-wide (defaults to /usr/local), run make followed by make install as root. You can customize the installation path using PREFIX or DESTDIR.

    $ make install PREFIX="/opt"

    After installation, use pkg-config to compile and link your programs:

    $ gcc program.c $(pkg-config monocypher --cflags) -c
    $ gcc program.o $(pkg-config monocypher --libs) -o program

    If you want to perform a partial installation, use these sub-targets:

    • install-lib: Installs only the library.
    • install-doc: Installs only the documentation.
    • install-pc: Installs only the pkg-config file.
    $ make install PREFIX="/opt"
    $ gcc program.c $(pkg-config monocypher --cflags) -c
    $ gcc program.o $(pkg-config monocypher --libs) -o program
  3. Install Monocypher by including source files

    master

    The simplest way to integrate Monocypher into your project is to include the source files directly. This method supports C (C99 and later) and C++ (C++98 and later).

    Include the following files:

    • src/monocypher.h
    • src/monocypher.c

    If you require optional support for SHA-512 or Ed25519, you must also include:

    • src/optional/monocypher-ed25519.h
    • src/optional/monocypher-ed25519.c
    /* Include these in your project build */
    #include "src/monocypher.h"
    #include "src/monocypher.c"
  4. Verify installation with the test suite

    master

    It is highly recommended to run the test suite at least once after installation to ensure correctness. A successful run will end with All tests OK!.

    $ make test

    For deeper verification, you can run the tests under Clang sanitizers or Valgrind using the provided scripts:

    $ tests/test.sh
    $ tests/coverage.sh
    $ make test
  5. Customize Monocypher build options

    master

    Monocypher provides several preprocessor flags for customization:

    Performance Tuning

    • -DBLAKE2_NO_UNROLLING: By default, the BLAKE2b inner loop is unrolled for speed (approx. 25% faster). On some embedded platforms, unrolling may be slower or consume too much code space. Enabling this flag reduces the binary size by ~5KB and may improve performance on constrained devices.

    C++ Integration

    • MONOCYPHER_CPP_NAMESPACE: When defined, Monocypher will be wrapped in a C++ namespace. If undefined (default), the library uses extern "C" automatically when the header is detected in C++ code.

    Function Prefixing

    To avoid name collisions with other libraries, you can rename all crypto_ functions to a custom prefix using the provided script:

    ./change-prefix.sh foobar

    (This replaces crypto_ with foobar_)

    ./change-prefix.sh foobar