| 1 | # Supercop |
| 2 | |
| 3 | > This project could use a rename :/ |
| 4 | |
| 5 | The objective of this project is to provide fast cryptographic operations for |
| 6 | Monero wallets. Unfortunately, there isn't necessarily a single fastest library |
| 7 | for all platforms - donna64 for instance likely performs poorly when |
| 8 | cross-compiled to asm.js or 32-bit arm. This project mitigates this issue by |
| 9 | copying the cryptographic libraries with as little changes as possible (ideally |
| 10 | zero), and then provides smaller and easier to audit "glue" functions that use |
| 11 | the cryptographic library to perform typical Monero wallet tasks. These "glue" |
| 12 | functions are often not obvious to the typical developer (otherwise they would |
| 13 | be using the cryptographic libraries already), however auditing these functions |
| 14 | should be straightforward to anyone familar with how the cryptography works. |
| 15 | |
| 16 | The project is also designed to be used in-tree in other projects or installable |
| 17 | on a system. The default Monero wallets use the in-tree implementation which |
| 18 | serve as a more complex example of its usage. The project directory structure: |
| 19 | |
| 20 | - **crypto_sign** - whose name is taken from supercop. The code in here is |
| 21 | from upstream cryptographic libraries with minimal changes (position |
| 22 | independent ASM). |
| 23 | - **include** - The only files that should be included by external projects, |
| 24 | with the exception of the auto-generated header. |
| 25 | - **src** - Contains all of the glue functions and build code. |
| 26 | - **functions.cmake** - The raw components for building the library. Used by |
| 27 | the default Monero wallet. |
| 28 | - **intree.cmake** - Creates a cmake library target `monero-crypto-intree` |
| 29 | and generates a header at `<BUILD_LOCATION>/include/monero/crypto.h`. |
| 30 | - **CMakeLists.txt** - Declares a new project for creating/installing a |
| 31 | shared or static `monero-crypto` library. |
| 32 | |
| 33 | > Downstream projects cannot currently use this project _without_ also using |
| 34 | > the `monero-project/monero/src/crypto/crypto.h` functions - specifically |
| 35 | > `crypto::derivation_to_scalar`. So currently this project provides |
| 36 | > performance enhancements but not standalone usage. |
| 37 | |
| 38 | ## Usage |
| 39 | |
| 40 | Every cryptographic library implements the same function signatures but with |
| 41 | unique namespaces to avoid collisions in the C symbol table. Downstream |
| 42 | projects can either use a specific library and header file OR dynamically |
| 43 | select a library and use a cmake generated header that uses macro replacement |
| 44 | to reference the selected library. Since each library has unique symbol names, |
| 45 | multiple can be used simulatenously (benchmarking, etc). |
| 46 | |
| 47 | ## C Wallet Functions |
| 48 | |
| 49 | Every cryptographic library implements: |
| 50 | |
| 51 | - **`monero_crypto_<namespace>_scalarmult(char* out, const char* pub, const char* sec)`** - |
| 52 | where `out` is the shared secret, `pub` is an ed25519 public key, and `sec` |
| 53 | is a user secret key. This operation is a standard ECDH operation on the |
| 54 | ed25519 curve. |
| 55 | - **`monero_crypto_<namespace>_generate_key_derivation(char* out, const char* tx_pub, const char* view_sec)`** - |
| 56 | where `out` is the shared secret, `tx_pub` is the public key from the |
| 57 | transaction and `view_sec` is the users view-key secret. |
| 58 | - **`monero_crypto_<namespace>_generate_subaddress_public_key(char* out, const char* output_pub, const char* special_sec)`** - |
| 59 | where `out` is the computed spend-public (primary or subaddress), |
| 60 | `output_pub` is the public key from the transaction output, and |
| 61 | `special_sec` is the output from `crypto::derivation_to_scalar` (which is |
| 62 | given the shared secret from `generate_key_derivation`). |
| 63 | |
| 64 | All `char` pointers must be exactly 32-bytes. The auto-generated header has an |
| 65 | empty namespace for these functions and uses macro replacement to forward to the |
| 66 | selected library. If using dynamically selected libraries, include the |
| 67 | auto-generated header and use `monero_crypto_generate_key_derivation`, etc. |
| 68 | |
| 69 | ## CMake Usage |
| 70 | ### Explicit Library Usage |
| 71 | A library can be selected explicitly in-tree: |
| 72 | |
| 73 | ``` |
| 74 | include(supercop/functions.cmake) |
| 75 | monero_crypto_get_target("amd64-64-24k" CRYPTO_TARGET) |
| 76 | |
| 77 | add_library(all $<OBJECT_TARGETS:${CRYPTO_TARGET}> all.cpp) |
| 78 | add_library(referenced referenced.cpp) |
| 79 | target_link_libraries(referenced ${CRYPTO_TARGET}) |
| 80 | ``` |
| 81 | |
| 82 | The `all` library will have all C functions in the `amd64-64-24k` library |
| 83 | whereas the `referenced` library will only have C functions referenced in |
| 84 | `referenced.cpp`. |
| 85 | |
| 86 | ## Basic In-Tree Dynamic Usage |
| 87 | A library can be selected at build time in-tree: |
| 88 | |
| 89 | ``` |
| 90 | include(supercop/intree.cmake) |
| 91 | |
| 92 | add_library(all $<OBJECT_TARGETS:monero-crypto-intree> all.cpp) |
| 93 | add_library(referenced referenced.cpp) |
| 94 | target_link_libraries(referenced monero-crypto-intree) |
| 95 | ``` |
| 96 | |
| 97 | The best available library for the target platform is selected which can be |
| 98 | overriden with `-DMONERO_CRYPTO_LIBRARY=amd64-51-30k` or by setting the |
| 99 | variable with the same name. |
| 100 | |
| 101 | A header file is written to `<BUILD_LOCATION>/include/monero/crypto.h` which |
| 102 | contains empty namespace macros that expand to the |
| 103 | [wallet functions](#c-wallet-functions) for the selected library. Including |
| 104 | this header file and using the empty namespace macros will allow for the |
| 105 | crypto library to be selected dynamically. |
| 106 | |
| 107 | See the [explicit usage section](#explicit-library-usage) for the difference |
| 108 | between the `all` and `referenced` libraries. |
| 109 | |
| 110 | ## Advanced In-Tree Dynamic Usage |
| 111 | See `intree.cmake` on how to use the individual cmake functions in your project |
| 112 | for more control. Each of the cmake functions is documented. |
| 113 | |
| 114 | ## Standalone Usage |
| 115 | Is not-yet fully functional since the `crypto::derivation_to_scalar` from the |
| 116 | Monero source tree is still needed. However this is a proof-of-concept that |
| 117 | will be useful for testing if your project does not use cmake. |
| 118 | |
| 119 | Running `cmake <source_path> . && make && sudo make install` will autodetect |
| 120 | the best crypto library, build it in the current directory, and then install to |
| 121 | the default location for your system. An error is generated if your target |
| 122 | platform does not have a supported crypto library. The auto-selected library |
| 123 | can be overriden with `-DMONERO_CRYPTO_LIBRARY=amd64-51-30k` at the first |
| 124 | cmake step. Using `-DMONERO_CRYPTO_LIBRARY=invalid` will generate an error |
| 125 | that displays all available crypto libraries. |