├── .dockerignore ├── .github └── workflows │ ├── docker-build.yml │ └── format.yml ├── Dockerfile ├── LICENSE ├── README.md ├── cargo-config.toml ├── docker-build.sh ├── docker-run-all-rustc.sh ├── docker-run-all.sh ├── docker-run-basic.sh ├── docker-run-emulator.sh ├── docker-run-rustc-1cpu.sh ├── docker-run-rustc-2cpus.sh ├── docker-run-rustc-4cpus.sh ├── docker-run-rustc-8cpus.sh ├── docker-run-usb.sh ├── emulator-package.xml ├── horcrux.dynamic_detect.patch ├── rustbuild-config.toml ├── scripts ├── android-runner.sh ├── bench-haraka.sh ├── bench-horcrux.sh ├── bench.sh ├── build-application-relinked.sh ├── build-application-stage1.sh ├── build-application.sh ├── build-relinked.sh ├── clone-rustlang-1.67.0-1286ee23e.sh ├── clone-rustlang-head.sh ├── emulator.sh ├── flamedisk.sh ├── launch-app-debug.sh ├── launch-app-release.sh ├── script-gradle.sh ├── script-java-incorrect-arm64.sh ├── script-java-incorrect-x86_64.sh ├── script-java.sh ├── script-relinked.sh ├── script-rust-default-nostrip.sh ├── script-rust-default.sh ├── script-rust-nightly-nostrip.sh ├── script-rust-nightly.sh ├── script-rust-stage1-nostrip.sh ├── script-rust-stage1.sh ├── setup.sh ├── stage0.sh ├── stage1.sh └── strip-rust.sh ├── src ├── MyRustSimdApplication │ ├── .gitignore │ ├── app │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── myrustapplication │ │ │ │ ├── MainActivity.java │ │ │ │ └── NativeLibrary.java │ │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ └── settings.gradle ├── android-simd │ ├── .gitignore │ ├── Cargo.toml │ └── src │ │ ├── aes.rs │ │ ├── cpu.rs │ │ ├── gf2n.rs │ │ ├── lib.rs │ │ ├── logger.rs │ │ └── pmul.rs └── relinked │ ├── .gitignore │ ├── Cargo.toml │ └── src │ ├── gf2n.rs │ └── lib.rs ├── stdarch.patch └── tools └── flamedisk ├── .gitignore ├── Cargo.toml └── src └── main.rs /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | .github 3 | LICENSE 4 | *.md 5 | docker-*.sh 6 | rustc-build 7 | -------------------------------------------------------------------------------- /.github/workflows/docker-build.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | name: Docker build 3 | jobs: 4 | build: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - uses: actions/checkout@v2 8 | - name: Get some system information 9 | run: | 10 | id; 11 | df; 12 | free; 13 | nproc 14 | - name: Docker build 15 | run: ./docker-build.sh 16 | - name: Build Android application (nightly Rust) 17 | run: | 18 | docker run \ 19 | --rm \ 20 | --cap-drop=all \ 21 | --security-opt no-new-privileges \ 22 | --read-only \ 23 | -u dev \ 24 | --memory=3g \ 25 | --memory-swap=3g \ 26 | --memory-swappiness=0 \ 27 | --env USER=dev \ 28 | --tmpfs /tmp:size=16m \ 29 | --tmpfs /home/dev/build:exec,size=512m \ 30 | --tmpfs /home/dev/.gradle:exec,size=512m \ 31 | --tmpfs /home/dev/.android:size=1m \ 32 | --tmpfs /home/dev/.cargo/registry:size=32m \ 33 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 34 | --entrypoint /home/dev/build-application.sh \ 35 | android-rust-simd 36 | - name: Build Android application (re-linked library) 37 | run: | 38 | docker run \ 39 | --rm \ 40 | --cap-drop=all \ 41 | --security-opt no-new-privileges \ 42 | --read-only \ 43 | -u dev \ 44 | --memory=3g \ 45 | --memory-swap=3g \ 46 | --memory-swappiness=0 \ 47 | --env USER=dev \ 48 | --tmpfs /tmp:size=16m \ 49 | --tmpfs /home/dev/build:exec,size=512m \ 50 | --tmpfs /home/dev/.gradle:exec,size=512m \ 51 | --tmpfs /home/dev/.android:size=1m \ 52 | --tmpfs /home/dev/.cargo/registry:size=32m \ 53 | --tmpfs /home/dev/.cargo/git:size=1m \ 54 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 55 | --entrypoint /home/dev/build-application-relinked.sh \ 56 | android-rust-simd 57 | - name: Build Android application (stage 1 Rust) 58 | run: | 59 | mkdir -p rustc-build; 60 | chmod 777 rustc-build; 61 | PWD=`pwd`; 62 | docker run \ 63 | --rm \ 64 | --cap-drop=all \ 65 | --security-opt no-new-privileges \ 66 | --read-only \ 67 | -u dev \ 68 | --memory=5g \ 69 | --memory-swap=5g \ 70 | --memory-swappiness=0 \ 71 | --env USER=dev \ 72 | --tmpfs /tmp:size=256m \ 73 | --tmpfs /home/dev/build:exec,size=512m \ 74 | --tmpfs /home/dev/.gradle:exec,size=512m \ 75 | --tmpfs /home/dev/.android:size=1m \ 76 | --tmpfs /home/dev/.cargo/registry:size=512m \ 77 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 78 | --volume ${PWD}/rustc-build:/home/dev/rustc-build \ 79 | --entrypoint /home/dev/build-application-stage1.sh \ 80 | android-rust-simd 81 | -------------------------------------------------------------------------------- /.github/workflows/format.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | name: Formatting 3 | jobs: 4 | check: 5 | runs-on: ubuntu-latest 6 | env: 7 | CARGO_UNSTABLE_SPARSE_REGISTRY: true 8 | steps: 9 | - uses: actions/checkout@v2 10 | - uses: dtolnay/rust-toolchain@nightly 11 | with: 12 | components: rustfmt 13 | - name: Check Rust formatting (android-simd) 14 | run: cargo fmt --verbose --manifest-path src/android-simd/Cargo.toml -- --check 15 | - name: Check Rust formatting (relinked) 16 | run: cargo fmt --verbose --manifest-path src/relinked/Cargo.toml -- --check 17 | - name: Check Rust formatting (flamedisk) 18 | run: cargo fmt --verbose --manifest-path tools/flamedisk/Cargo.toml -- --check 19 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-slim 2 | 3 | RUN apt-get update \ 4 | && apt-get install -y \ 5 | wget \ 6 | unzip \ 7 | build-essential \ 8 | python3 \ 9 | curl \ 10 | cmake \ 11 | git \ 12 | openjdk-11-jdk-headless \ 13 | libgl1-mesa-dri \ 14 | libgl1-mesa-glx \ 15 | libpulse0 \ 16 | libasound2 \ 17 | libxcomposite1 \ 18 | libxcursor1 \ 19 | libxdamage1 \ 20 | libxi6 \ 21 | libxtst6 \ 22 | --no-install-recommends \ 23 | && rm -rf /var/lib/apt/lists/* 24 | 25 | RUN useradd --uid 1000 --create-home --shell /bin/bash dev 26 | # For some reason the host adds the USB device to the 46(plugdev) group. Being 27 | # member of this group is enough to have rw access to the USB device and run 28 | # adb. 29 | RUN groupadd --gid 106 --system kvm \ 30 | && usermod --groups 46,106 dev 31 | 32 | USER dev 33 | WORKDIR "/home/dev" 34 | 35 | # Install Gradle manually. The Debian package is too outdated. 36 | ENV GRADLE_ROOT=/home/dev/opt/gradle 37 | 38 | RUN mkdir -p ${GRADLE_ROOT} 39 | RUN wget -nv https://services.gradle.org/distributions/gradle-7.5.1-bin.zip -O gradle-7.5.1-bin.zip \ 40 | && sha256sum gradle-7.5.1-bin.zip \ 41 | && echo "f6b8596b10cce501591e92f229816aa4046424f3b24d771751b06779d58c8ec4 gradle-7.5.1-bin.zip" | sha256sum -c - \ 42 | && unzip gradle-7.5.1-bin.zip -d ${GRADLE_ROOT} \ 43 | && rm gradle-7.5.1-bin.zip 44 | 45 | ENV PATH=${PATH}:${GRADLE_ROOT}/gradle-7.5.1/bin 46 | 47 | # Set the ${ANDROID_HOME} variable, so that the tools can find our installation. 48 | # See https://developer.android.com/studio/command-line/variables#envar. 49 | ENV ANDROID_HOME=/home/dev/opt/android-sdk 50 | 51 | RUN mkdir -p ${ANDROID_HOME} 52 | RUN wget -nv https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip \ 53 | -O ${HOME}/commandlinetools-linux-8512546_latest.zip \ 54 | && sha256sum commandlinetools-linux-8512546_latest.zip \ 55 | && echo "2ccbda4302db862a28ada25aa7425d99dce9462046003c1714b059b5c47970d8 commandlinetools-linux-8512546_latest.zip" | sha256sum -c - \ 56 | && unzip commandlinetools-linux-8512546_latest.zip -d ${ANDROID_HOME}/cmdline-tools \ 57 | && rm commandlinetools-linux-8512546_latest.zip 58 | 59 | ENV PATH=${PATH}:${ANDROID_HOME}/cmdline-tools/cmdline-tools/bin:${ANDROID_HOME}/platform-tools 60 | 61 | # Given the following configuration in `build.gradle`: 62 | # classpath 'com.android.tools.build:gradle:7.3.0' 63 | # the build tools version to use is 30.0.3. 64 | # See https://mvnrepository.com/artifact/com.android.tools.build/gradle/7.3.0. 65 | RUN yes | sdkmanager --licenses \ 66 | && sdkmanager --list \ 67 | && sdkmanager --verbose \ 68 | "build-tools;30.0.3" \ 69 | "ndk;25.1.8937393" \ 70 | "platforms;android-33" \ 71 | "system-images;android-29;default;x86_64" \ 72 | && rm -R ${HOME}/.android/ 73 | RUN sdkmanager --list_installed 74 | 75 | RUN cd ${HOME}/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/ \ 76 | && ln -s aarch64-linux-android30-clang aarch64-linux-android-clang \ 77 | && ln -s armv7a-linux-androideabi30-clang arm-linux-androideabi-clang \ 78 | && ln -s i686-linux-android30-clang i686-linux-android-clang \ 79 | && ln -s x86_64-linux-android30-clang x86_64-linux-android-clang 80 | 81 | # Replace the Android emulator by a custom one (version 31.3.11 from the 82 | # archive at https://developer.android.com/studio/emulator_archive). Indeed, 83 | # from version 31.3.12 on, the disk size is too big and doesn't fit in a 84 | # reasonably-sized tmpfs. 85 | # 86 | # See https://developer.android.com/studio/releases/emulator#31-3-12. 87 | RUN rm -R ${ANDROID_HOME}/emulator \ 88 | && wget -nv https://dl.google.com/android/repository/emulator-linux_x64-9058569.zip \ 89 | -O ${HOME}/emulator-linux_x64-9058569.zip \ 90 | && sha256sum emulator-linux_x64-9058569.zip \ 91 | && echo "5b06dae2b8c79b0a39456c3b4d31cf1895571bbf9763cc8ba84c8fdae15673e8 emulator-linux_x64-9058569.zip" | sha256sum -c - \ 92 | && yes | unzip emulator-linux_x64-9058569.zip -d ${ANDROID_HOME} \ 93 | && rm emulator-linux_x64-9058569.zip 94 | 95 | USER root 96 | COPY emulator-package.xml /home/dev/opt/android-sdk/emulator/package.xml 97 | USER dev 98 | 99 | # Create an Android virtual device. 100 | RUN avdmanager create avd \ 101 | -n test_avd \ 102 | -d pixel \ 103 | -k "system-images;android-29;default;x86_64" \ 104 | && mv ${HOME}/.android ${HOME}/android 105 | 106 | # Install Rust toolchain. 107 | ENV NDK_HOME=${ANDROID_HOME}/ndk/25.1.8937393 108 | 109 | RUN wget -nv https://sh.rustup.rs -O rustup.sh \ 110 | && sha256sum rustup.sh \ 111 | && echo "be3535b3033ff5e0ecc4d589a35d3656f681332f860c5fd6684859970165ddcc rustup.sh" | sha256sum -c - \ 112 | && sh rustup.sh -y \ 113 | && rm rustup.sh \ 114 | && rm .profile \ 115 | && ${HOME}/.cargo/bin/rustup target add \ 116 | aarch64-linux-android \ 117 | armv7-linux-androideabi \ 118 | i686-linux-android \ 119 | x86_64-linux-android \ 120 | && ${HOME}/.cargo/bin/rustup toolchain install nightly \ 121 | && ${HOME}/.cargo/bin/rustup target add --toolchain nightly \ 122 | aarch64-linux-android \ 123 | armv7-linux-androideabi \ 124 | i686-linux-android \ 125 | x86_64-linux-android \ 126 | && mkdir -p ${HOME}/rustup \ 127 | && mv ${HOME}/.rustup/toolchains ${HOME}/rustup/toolchains 128 | 129 | ENV PATH=${PATH}:/home/dev/.cargo/bin 130 | 131 | USER root 132 | 133 | COPY \ 134 | scripts/build-application.sh \ 135 | scripts/build-application-relinked.sh \ 136 | scripts/build-application-stage1.sh \ 137 | scripts/setup.sh \ 138 | scripts/clone-rustlang-head.sh \ 139 | scripts/clone-rustlang-1.67.0-1286ee23e.sh \ 140 | scripts/stage0.sh \ 141 | scripts/stage1.sh \ 142 | scripts/strip-rust.sh \ 143 | scripts/script-rust-default.sh \ 144 | scripts/script-rust-default-nostrip.sh \ 145 | scripts/script-rust-nightly.sh \ 146 | scripts/script-rust-nightly-nostrip.sh \ 147 | scripts/script-rust-stage1.sh \ 148 | scripts/script-rust-stage1-nostrip.sh \ 149 | scripts/script-gradle.sh \ 150 | scripts/script-java.sh \ 151 | scripts/script-java-incorrect-arm64.sh \ 152 | scripts/script-java-incorrect-x86_64.sh \ 153 | scripts/emulator.sh \ 154 | scripts/launch-app-debug.sh \ 155 | scripts/launch-app-release.sh \ 156 | scripts/flamedisk.sh \ 157 | scripts/android-runner.sh \ 158 | scripts/build-relinked.sh \ 159 | scripts/script-relinked.sh \ 160 | scripts/bench.sh \ 161 | scripts/bench-haraka.sh \ 162 | scripts/bench-horcrux.sh \ 163 | /home/dev/ 164 | RUN chmod 555 \ 165 | /home/dev/build-application.sh \ 166 | /home/dev/build-application-relinked.sh \ 167 | /home/dev/build-application-stage1.sh \ 168 | /home/dev/setup.sh \ 169 | /home/dev/clone-rustlang-head.sh \ 170 | /home/dev/clone-rustlang-1.67.0-1286ee23e.sh \ 171 | /home/dev/stage0.sh \ 172 | /home/dev/stage1.sh \ 173 | /home/dev/strip-rust.sh \ 174 | /home/dev/script-rust-default.sh \ 175 | /home/dev/script-rust-default-nostrip.sh \ 176 | /home/dev/script-rust-nightly.sh \ 177 | /home/dev/script-rust-nightly-nostrip.sh \ 178 | /home/dev/script-rust-stage1.sh \ 179 | /home/dev/script-rust-stage1-nostrip.sh \ 180 | /home/dev/script-gradle.sh \ 181 | /home/dev/script-java.sh \ 182 | /home/dev/script-java-incorrect-arm64.sh \ 183 | /home/dev/script-java-incorrect-x86_64.sh \ 184 | /home/dev/emulator.sh \ 185 | /home/dev/launch-app-debug.sh \ 186 | /home/dev/launch-app-release.sh \ 187 | /home/dev/flamedisk.sh \ 188 | /home/dev/android-runner.sh \ 189 | /home/dev/build-relinked.sh \ 190 | /home/dev/script-relinked.sh \ 191 | /home/dev/bench.sh \ 192 | /home/dev/bench-haraka.sh \ 193 | /home/dev/bench-horcrux.sh 194 | 195 | COPY --chown=1000:1000 cargo-config.toml /home/dev/.cargo/config 196 | COPY --chown=1000:1000 \ 197 | rustbuild-config.toml \ 198 | stdarch.patch \ 199 | horcrux.dynamic_detect.patch \ 200 | /home/dev/ 201 | 202 | COPY --chown=1000:1000 src /home/dev/src 203 | COPY --chown=1000:1000 tools /home/dev/tools 204 | 205 | USER dev 206 | 207 | ENTRYPOINT [ "/bin/bash" ] 208 | 209 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Guillaume Endignoux 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Rust example application with SIMD instructions 2 | 3 | This repository is a demonstration of an Android app integrating a native Rust library. 4 | Additionally, this Rust library demonstrates detecting support for SIMD instructions. 5 | 6 | You can find more information in the following blog posts: 7 | 8 | - [Compiling Rust libraries for Android apps: a deep dive](https://gendignoux.com/blog/2022/10/24/rust-library-android.html), 9 | - [Detecting SIMD support on ARM with Android (and patching the Rust compiler for it)](https://gendignoux.com/blog/2022/11/09/rust-simd-detect-arm-android.html), 10 | - [Testing SIMD instructions on ARM with Rust on Android](https://gendignoux.com/blog/2023/01/05/rust-arm-simd-android.html). 11 | 12 | ## Usage 13 | 14 | To build a Docker container containing all the build tools and the demo application, run: 15 | 16 | ```bash 17 | $ sudo ./docker-build.sh 18 | ``` 19 | 20 | ### Building and running the demo Android application 21 | 22 | You can then launch this Docker container to build the application. 23 | This comes in various configurations: 24 | 25 | - `docker-run-basic.sh`: minimal setup to compile the application within the container. 26 | - `docker-run-emulator.sh`: besides compiling the application, this setup allows to spawn an Android emulator within the container to test this application. 27 | - `docker-run-usb.sh`: besides compiling the application, this setup gives the container access to the USB devices, so that you can install the application on a real device via ADB. 28 | - `docker-run-all.sh`: setup with everything available in the container. 29 | 30 | ```bash 31 | $ sudo ./docker-run-basic.sh 32 | $ ./script-rust.sh && ./script-java.sh 33 | ``` 34 | 35 | You can build various flavors of the Rust library. 36 | 37 | - `script-rust-nightly.sh`: basic script using the nightly Rust toolchain, with stripping of debug symbols enabled. 38 | - `script-rust-nightly-nostrip.sh`: same, but without stripping debug symbols. 39 | - `script-rust-stage1.sh` and `script-rust-stage1-nostrip.sh`: using a locally built `stage1` Rust compiler (see below). 40 | - `script-rust-default.sh` and `script-rust-default-nostrip.sh`: using the default Rust toolchain (stable). 41 | - `script-relinked.sh`: more advanced library, which bundles another library linked twice (see the corresponding [blog post](https://gendignoux.com/blog/2023/01/05/rust-arm-simd-android.html#mixing-dynamic-and-static-detection-re-linking-a-dependency)). 42 | 43 | Then, multiple ways are provided to build the Java part of the Android app. 44 | 45 | - `script-java.sh`: normal build of the Android app. 46 | - `script-java-incorrect-arm64.sh`: Android app built with the ARM-64 library in the wrong folder. 47 | - `script-java-incorrect-x86_64.sh`: Android app built with the x86_64 library in the wrong folder. 48 | 49 | You can then spawn an Android emulator with `emulator.sh`, and use the `launch-app-debug.sh` or `launch-app-release.sh` scripts to install+launch the application via ADB to either the emulator or a real device connected via USB. 50 | 51 | ### Building and using a patched Rust compiler 52 | 53 | This repository also shows how to patch and build the Rust compiler. 54 | You can launch the Docker container in various scenarios: 55 | 56 | - `docker-run-rustc-1cpu.sh`: with 1 CPU, 2 GB of RAM, 57 | - `docker-run-rustc-2cpu.sh`: with 2 CPUs, 2 GB of RAM, 58 | - `docker-run-rustc-4cpu.sh`: with 4 CPUs, 3 GB of RAM, 59 | - `docker-run-rustc-8cpu.sh`: with 8 CPUs, 5 GB of RAM, 60 | - `docker-run-all-rustc.sh`: to compile `rustc` + build the app. 61 | 62 | Once launched, you can clone and compile `rustc` for Android with: 63 | 64 | - `scripts/clone-rustlang-head.sh`: clone the latest commit on the Rust repository, 65 | - `scripts/stage0.sh`: build a stage 0 compiler, 66 | - `scripts/stage1.sh`: build a stage 1 compiler. 67 | 68 | You'll also find the following tool to generate a flame graph of the disk space used by a build of `rustc`: 69 | 70 | - `tools/flamedisk`: small Rust tool to generate an input suitable for `flamegraph.pl`, 71 | - `scripts/flamedisk.sh`: driver script to generate the flame graph. 72 | 73 | ### Running unit tests and benchmarks on an Android device 74 | 75 | This repository shows how to run Rust unit tests and benchmarks directly on an attached Android device (physical device via USB or emulator), without using a full Android application. 76 | The `android-runner.sh` script tells Cargo how to do that. 77 | 78 | Within the Docker container, you can run the following benchmarks: 79 | 80 | - `bench.sh`: demo application, located in `src/android-simd`, 81 | - `bench-haraka.sh`: my implementation of the Haraka hash function (https://github.com/gendx/haraka-rs), 82 | - `bench-horcrux.sh`: my implementation of Shamir's Secret Sharing (https://github.com/gendx/horcrux). 83 | -------------------------------------------------------------------------------- /cargo-config.toml: -------------------------------------------------------------------------------- 1 | [target.aarch64-linux-android] 2 | linker = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang" 3 | runner = "/home/dev/android-runner.sh" 4 | 5 | [target.armv7-linux-androideabi] 6 | linker = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi30-clang" 7 | runner = "/home/dev/android-runner.sh" 8 | 9 | [target.i686-linux-android] 10 | linker = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android30-clang" 11 | runner = "/home/dev/android-runner.sh" 12 | 13 | [target.x86_64-linux-android] 14 | linker = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android30-clang" 15 | runner = "/home/dev/android-runner.sh" 16 | -------------------------------------------------------------------------------- /docker-build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | docker build -t android-rust-simd . 3 | -------------------------------------------------------------------------------- /docker-run-all-rustc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | mkdir -p rustc-build 4 | chown 1000:1000 rustc-build 5 | PWD=`pwd` 6 | 7 | docker run \ 8 | --rm \ 9 | -it \ 10 | --cap-drop=all \ 11 | --security-opt no-new-privileges \ 12 | --read-only \ 13 | -u dev \ 14 | --cpus 8 \ 15 | --memory=5g \ 16 | --memory-swap=5g \ 17 | --memory-swappiness=0 \ 18 | --env USER=dev \ 19 | --env "DISPLAY=unix${DISPLAY}" \ 20 | --tmpfs /tmp:size=256m \ 21 | --tmpfs /home/dev/build:exec,size=512m \ 22 | --tmpfs /home/dev/.gradle:exec,size=512m \ 23 | --tmpfs /home/dev/.android:size=3g \ 24 | --tmpfs /home/dev/.cargo/registry:size=512m \ 25 | --tmpfs /home/dev/.cargo/git:size=1m \ 26 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 27 | --volume /tmp/.X11-unix:/tmp/.X11-unix \ 28 | --volume ${PWD}/rustc-build:/home/dev/rustc-build \ 29 | --device /dev/bus/usb \ 30 | --device /dev/dri \ 31 | --device /dev/kvm \ 32 | android-rust-simd 33 | -------------------------------------------------------------------------------- /docker-run-all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | docker run \ 3 | --rm \ 4 | -it \ 5 | --cap-drop=all \ 6 | --security-opt no-new-privileges \ 7 | --read-only \ 8 | -u dev \ 9 | --memory=3g \ 10 | --memory-swap=3g \ 11 | --memory-swappiness=0 \ 12 | --env USER=dev \ 13 | --env "DISPLAY=unix${DISPLAY}" \ 14 | --tmpfs /tmp:size=16m \ 15 | --tmpfs /home/dev/build:exec,size=512m \ 16 | --tmpfs /home/dev/.gradle:exec,size=512m \ 17 | --tmpfs /home/dev/.android:size=3g \ 18 | --tmpfs /home/dev/.cargo/registry:size=32m \ 19 | --tmpfs /home/dev/.cargo/git:size=1m \ 20 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 21 | --volume /tmp/.X11-unix:/tmp/.X11-unix \ 22 | --device /dev/bus/usb \ 23 | --device /dev/dri \ 24 | --device /dev/kvm \ 25 | android-rust-simd 26 | -------------------------------------------------------------------------------- /docker-run-basic.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | docker run \ 3 | --rm \ 4 | -it \ 5 | --cap-drop=all \ 6 | --security-opt no-new-privileges \ 7 | --read-only \ 8 | -u dev \ 9 | --memory=3g \ 10 | --memory-swap=3g \ 11 | --memory-swappiness=0 \ 12 | --env USER=dev \ 13 | --tmpfs /tmp:size=16m \ 14 | --tmpfs /home/dev/build:exec,size=512m \ 15 | --tmpfs /home/dev/.gradle:exec,size=512m \ 16 | --tmpfs /home/dev/.android:size=1m \ 17 | --tmpfs /home/dev/.cargo/registry:size=32m \ 18 | --tmpfs /home/dev/.cargo/git:size=1m \ 19 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 20 | android-rust-simd 21 | -------------------------------------------------------------------------------- /docker-run-emulator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | docker run \ 3 | --rm \ 4 | -it \ 5 | --cap-drop=all \ 6 | --security-opt no-new-privileges \ 7 | --read-only \ 8 | -u dev \ 9 | --memory=3g \ 10 | --memory-swap=3g \ 11 | --memory-swappiness=0 \ 12 | --env USER=dev \ 13 | --env "DISPLAY=unix${DISPLAY}" \ 14 | --tmpfs /tmp:size=16m \ 15 | --tmpfs /home/dev/build:exec,size=512m \ 16 | --tmpfs /home/dev/.gradle:exec,size=512m \ 17 | --tmpfs /home/dev/.android:size=3g \ 18 | --tmpfs /home/dev/.cargo/registry:size=32m \ 19 | --tmpfs /home/dev/.cargo/git:size=1m \ 20 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 21 | --volume /tmp/.X11-unix:/tmp/.X11-unix \ 22 | --device /dev/dri \ 23 | --device /dev/kvm \ 24 | android-rust-simd 25 | -------------------------------------------------------------------------------- /docker-run-rustc-1cpu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | mkdir -p rustc-build 4 | chown 1000:1000 rustc-build 5 | PWD=`pwd` 6 | 7 | docker run \ 8 | --rm \ 9 | -it \ 10 | --cap-drop=all \ 11 | --security-opt no-new-privileges \ 12 | --read-only \ 13 | -u dev \ 14 | --cpus 1 \ 15 | --memory=2g \ 16 | --memory-swap=2g \ 17 | --memory-swappiness=0 \ 18 | --env USER=dev \ 19 | --tmpfs /tmp:size=256m \ 20 | --tmpfs /home/dev/.cargo/registry:size=512m \ 21 | --tmpfs /home/dev/.cargo/git:size=1m \ 22 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 23 | --volume $PWD/rustc-build:/home/dev/rustc-build \ 24 | android-rust-simd 25 | -------------------------------------------------------------------------------- /docker-run-rustc-2cpus.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | mkdir -p rustc-build 4 | chown 1000:1000 rustc-build 5 | PWD=`pwd` 6 | 7 | docker run \ 8 | --rm \ 9 | -it \ 10 | --cap-drop=all \ 11 | --security-opt no-new-privileges \ 12 | --read-only \ 13 | -u dev \ 14 | --cpus 2 \ 15 | --memory=2g \ 16 | --memory-swap=2g \ 17 | --memory-swappiness=0 \ 18 | --env USER=dev \ 19 | --tmpfs /tmp:size=256m \ 20 | --tmpfs /home/dev/.cargo/registry:size=512m \ 21 | --tmpfs /home/dev/.cargo/git:size=1m \ 22 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 23 | --volume ${PWD}/rustc-build:/home/dev/rustc-build \ 24 | android-rust-simd 25 | -------------------------------------------------------------------------------- /docker-run-rustc-4cpus.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | mkdir -p rustc-build 4 | chown 1000:1000 rustc-build 5 | PWD=`pwd` 6 | 7 | docker run \ 8 | --rm \ 9 | -it \ 10 | --cap-drop=all \ 11 | --security-opt no-new-privileges \ 12 | --read-only \ 13 | -u dev \ 14 | --cpus 4 \ 15 | --memory=3g \ 16 | --memory-swap=3g \ 17 | --memory-swappiness=0 \ 18 | --env USER=dev \ 19 | --tmpfs /tmp:size=256m \ 20 | --tmpfs /home/dev/.cargo/registry:size=512m \ 21 | --tmpfs /home/dev/.cargo/git:size=1m \ 22 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 23 | --volume ${PWD}/rustc-build:/home/dev/rustc-build \ 24 | android-rust-simd 25 | -------------------------------------------------------------------------------- /docker-run-rustc-8cpus.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | mkdir -p rustc-build 4 | chown 1000:1000 rustc-build 5 | PWD=`pwd` 6 | 7 | docker run \ 8 | --rm \ 9 | -it \ 10 | --cap-drop=all \ 11 | --security-opt no-new-privileges \ 12 | --read-only \ 13 | -u dev \ 14 | --cpus 8 \ 15 | --memory=5g \ 16 | --memory-swap=5g \ 17 | --memory-swappiness=0 \ 18 | --env USER=dev \ 19 | --tmpfs /tmp:size=256m \ 20 | --tmpfs /home/dev/.cargo/registry:size=512m \ 21 | --tmpfs /home/dev/.cargo/git:size=1m \ 22 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 23 | --volume ${PWD}/rustc-build:/home/dev/rustc-build \ 24 | android-rust-simd 25 | -------------------------------------------------------------------------------- /docker-run-usb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | docker run \ 3 | --rm \ 4 | -it \ 5 | --cap-drop=all \ 6 | --security-opt no-new-privileges \ 7 | --read-only \ 8 | -u dev \ 9 | --memory=3g \ 10 | --memory-swap=3g \ 11 | --memory-swappiness=0 \ 12 | --env USER=dev \ 13 | --tmpfs /tmp:size=16m \ 14 | --tmpfs /home/dev/build:exec,size=512m \ 15 | --tmpfs /home/dev/.gradle:exec,size=512m \ 16 | --tmpfs /home/dev/.android:size=1m \ 17 | --tmpfs /home/dev/.cargo/registry:size=32m \ 18 | --tmpfs /home/dev/.cargo/git:size=1m \ 19 | --tmpfs /home/dev/.rustup/toolchains:exec,size=1m \ 20 | --device /dev/bus/usb \ 21 | android-rust-simd 22 | -------------------------------------------------------------------------------- /emulator-package.xml: -------------------------------------------------------------------------------- 1 | Terms and Conditions 2 | 3 | This is the Android Software Development Kit License Agreement 4 | 5 | 1. Introduction 6 | 7 | 1.1 The Android Software Development Kit (referred to in the License Agreement as the "SDK" and specifically including the Android system files, packaged APIs, and Google APIs add-ons) is licensed to you subject to the terms of the License Agreement. The License Agreement forms a legally binding contract between you and Google in relation to your use of the SDK. 8 | 9 | 1.2 "Android" means the Android software stack for devices, as made available under the Android Open Source Project, which is located at the following URL: http://source.android.com/, as updated from time to time. 10 | 11 | 1.3 A "compatible implementation" means any Android device that (i) complies with the Android Compatibility Definition document, which can be found at the Android compatibility website (http://source.android.com/compatibility) and which may be updated from time to time; and (ii) successfully passes the Android Compatibility Test Suite (CTS). 12 | 13 | 1.4 "Google" means Google Inc., a Delaware corporation with principal place of business at 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States. 14 | 15 | 16 | 2. Accepting the License Agreement 17 | 18 | 2.1 In order to use the SDK, you must first agree to the License Agreement. You may not use the SDK if you do not accept the License Agreement. 19 | 20 | 2.2 By clicking to accept, you hereby agree to the terms of the License Agreement. 21 | 22 | 2.3 You may not use the SDK and may not accept the License Agreement if you are a person barred from receiving the SDK under the laws of the United States or other countries, including the country in which you are resident or from which you use the SDK. 23 | 24 | 2.4 If you are agreeing to be bound by the License Agreement on behalf of your employer or other entity, you represent and warrant that you have full legal authority to bind your employer or such entity to the License Agreement. If you do not have the requisite authority, you may not accept the License Agreement or use the SDK on behalf of your employer or other entity. 25 | 26 | 27 | 3. SDK License from Google 28 | 29 | 3.1 Subject to the terms of the License Agreement, Google grants you a limited, worldwide, royalty-free, non-assignable, non-exclusive, and non-sublicensable license to use the SDK solely to develop applications for compatible implementations of Android. 30 | 31 | 3.2 You may not use this SDK to develop applications for other platforms (including non-compatible implementations of Android) or to develop another SDK. You are of course free to develop applications for other platforms, including non-compatible implementations of Android, provided that this SDK is not used for that purpose. 32 | 33 | 3.3 You agree that Google or third parties own all legal right, title and interest in and to the SDK, including any Intellectual Property Rights that subsist in the SDK. "Intellectual Property Rights" means any and all rights under patent law, copyright law, trade secret law, trademark law, and any and all other proprietary rights. Google reserves all rights not expressly granted to you. 34 | 35 | 3.4 You may not use the SDK for any purpose not expressly permitted by the License Agreement. Except to the extent required by applicable third party licenses, you may not copy (except for backup purposes), modify, adapt, redistribute, decompile, reverse engineer, disassemble, or create derivative works of the SDK or any part of the SDK. 36 | 37 | 3.5 Use, reproduction and distribution of components of the SDK licensed under an open source software license are governed solely by the terms of that open source software license and not the License Agreement. 38 | 39 | 3.6 You agree that the form and nature of the SDK that Google provides may change without prior notice to you and that future versions of the SDK may be incompatible with applications developed on previous versions of the SDK. You agree that Google may stop (permanently or temporarily) providing the SDK (or any features within the SDK) to you or to users generally at Google's sole discretion, without prior notice to you. 40 | 41 | 3.7 Nothing in the License Agreement gives you a right to use any of Google's trade names, trademarks, service marks, logos, domain names, or other distinctive brand features. 42 | 43 | 3.8 You agree that you will not remove, obscure, or alter any proprietary rights notices (including copyright and trademark notices) that may be affixed to or contained within the SDK. 44 | 45 | 46 | 4. Use of the SDK by You 47 | 48 | 4.1 Google agrees that it obtains no right, title or interest from you (or your licensors) under the License Agreement in or to any software applications that you develop using the SDK, including any intellectual property rights that subsist in those applications. 49 | 50 | 4.2 You agree to use the SDK and write applications only for purposes that are permitted by (a) the License Agreement and (b) any applicable law, regulation or generally accepted practices or guidelines in the relevant jurisdictions (including any laws regarding the export of data or software to and from the United States or other relevant countries). 51 | 52 | 4.3 You agree that if you use the SDK to develop applications for general public users, you will protect the privacy and legal rights of those users. If the users provide you with user names, passwords, or other login information or personal information, you must make the users aware that the information will be available to your application, and you must provide legally adequate privacy notice and protection for those users. If your application stores personal or sensitive information provided by users, it must do so securely. If the user provides your application with Google Account information, your application may only use that information to access the user's Google Account when, and for the limited purposes for which, the user has given you permission to do so. 53 | 54 | 4.4 You agree that you will not engage in any activity with the SDK, including the development or distribution of an application, that interferes with, disrupts, damages, or accesses in an unauthorized manner the servers, networks, or other properties or services of any third party including, but not limited to, Google or any mobile communications carrier. 55 | 56 | 4.5 You agree that you are solely responsible for (and that Google has no responsibility to you or to any third party for) any data, content, or resources that you create, transmit or display through Android and/or applications for Android, and for the consequences of your actions (including any loss or damage which Google may suffer) by doing so. 57 | 58 | 4.6 You agree that you are solely responsible for (and that Google has no responsibility to you or to any third party for) any breach of your obligations under the License Agreement, any applicable third party contract or Terms of Service, or any applicable law or regulation, and for the consequences (including any loss or damage which Google or any third party may suffer) of any such breach. 59 | 60 | 5. Your Developer Credentials 61 | 62 | 5.1 You agree that you are responsible for maintaining the confidentiality of any developer credentials that may be issued to you by Google or which you may choose yourself and that you will be solely responsible for all applications that are developed under your developer credentials. 63 | 64 | 6. Privacy and Information 65 | 66 | 6.1 In order to continually innovate and improve the SDK, Google may collect certain usage statistics from the software including but not limited to a unique identifier, associated IP address, version number of the software, and information on which tools and/or services in the SDK are being used and how they are being used. Before any of this information is collected, the SDK will notify you and seek your consent. If you withhold consent, the information will not be collected. 67 | 68 | 6.2 The data collected is examined in the aggregate to improve the SDK and is maintained in accordance with Google's Privacy Policy. 69 | 70 | 71 | 7. Third Party Applications 72 | 73 | 7.1 If you use the SDK to run applications developed by a third party or that access data, content or resources provided by a third party, you agree that Google is not responsible for those applications, data, content, or resources. You understand that all data, content or resources which you may access through such third party applications are the sole responsibility of the person from which they originated and that Google is not liable for any loss or damage that you may experience as a result of the use or access of any of those third party applications, data, content, or resources. 74 | 75 | 7.2 You should be aware the data, content, and resources presented to you through such a third party application may be protected by intellectual property rights which are owned by the providers (or by other persons or companies on their behalf). You may not modify, rent, lease, loan, sell, distribute or create derivative works based on these data, content, or resources (either in whole or in part) unless you have been specifically given permission to do so by the relevant owners. 76 | 77 | 7.3 You acknowledge that your use of such third party applications, data, content, or resources may be subject to separate terms between you and the relevant third party. In that case, the License Agreement does not affect your legal relationship with these third parties. 78 | 79 | 80 | 8. Using Android APIs 81 | 82 | 8.1 Google Data APIs 83 | 84 | 8.1.1 If you use any API to retrieve data from Google, you acknowledge that the data may be protected by intellectual property rights which are owned by Google or those parties that provide the data (or by other persons or companies on their behalf). Your use of any such API may be subject to additional Terms of Service. You may not modify, rent, lease, loan, sell, distribute or create derivative works based on this data (either in whole or in part) unless allowed by the relevant Terms of Service. 85 | 86 | 8.1.2 If you use any API to retrieve a user's data from Google, you acknowledge and agree that you shall retrieve data only with the user's explicit consent and only when, and for the limited purposes for which, the user has given you permission to do so. If you use the Android Recognition Service API, documented at the following URL: https://developer.android.com/reference/android/speech/RecognitionService, as updated from time to time, you acknowledge that the use of the API is subject to the Data Processing Addendum for Products where Google is a Data Processor, which is located at the following URL: https://privacy.google.com/businesses/gdprprocessorterms/, as updated from time to time. By clicking to accept, you hereby agree to the terms of the Data Processing Addendum for Products where Google is a Data Processor. 87 | 88 | 89 | 9. Terminating the License Agreement 90 | 91 | 9.1 The License Agreement will continue to apply until terminated by either you or Google as set out below. 92 | 93 | 9.2 If you want to terminate the License Agreement, you may do so by ceasing your use of the SDK and any relevant developer credentials. 94 | 95 | 9.3 Google may at any time, terminate the License Agreement with you if: (A) you have breached any provision of the License Agreement; or (B) Google is required to do so by law; or (C) the partner with whom Google offered certain parts of SDK (such as APIs) to you has terminated its relationship with Google or ceased to offer certain parts of the SDK to you; or (D) Google decides to no longer provide the SDK or certain parts of the SDK to users in the country in which you are resident or from which you use the service, or the provision of the SDK or certain SDK services to you by Google is, in Google's sole discretion, no longer commercially viable. 96 | 97 | 9.4 When the License Agreement comes to an end, all of the legal rights, obligations and liabilities that you and Google have benefited from, been subject to (or which have accrued over time whilst the License Agreement has been in force) or which are expressed to continue indefinitely, shall be unaffected by this cessation, and the provisions of paragraph 14.7 shall continue to apply to such rights, obligations and liabilities indefinitely. 98 | 99 | 100 | 10. DISCLAIMER OF WARRANTIES 101 | 102 | 10.1 YOU EXPRESSLY UNDERSTAND AND AGREE THAT YOUR USE OF THE SDK IS AT YOUR SOLE RISK AND THAT THE SDK IS PROVIDED "AS IS" AND "AS AVAILABLE" WITHOUT WARRANTY OF ANY KIND FROM GOOGLE. 103 | 104 | 10.2 YOUR USE OF THE SDK AND ANY MATERIAL DOWNLOADED OR OTHERWISE OBTAINED THROUGH THE USE OF THE SDK IS AT YOUR OWN DISCRETION AND RISK AND YOU ARE SOLELY RESPONSIBLE FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR OTHER DEVICE OR LOSS OF DATA THAT RESULTS FROM SUCH USE. 105 | 106 | 10.3 GOOGLE FURTHER EXPRESSLY DISCLAIMS ALL WARRANTIES AND CONDITIONS OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 107 | 108 | 109 | 11. LIMITATION OF LIABILITY 110 | 111 | 11.1 YOU EXPRESSLY UNDERSTAND AND AGREE THAT GOOGLE, ITS SUBSIDIARIES AND AFFILIATES, AND ITS LICENSORS SHALL NOT BE LIABLE TO YOU UNDER ANY THEORY OF LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR EXEMPLARY DAMAGES THAT MAY BE INCURRED BY YOU, INCLUDING ANY LOSS OF DATA, WHETHER OR NOT GOOGLE OR ITS REPRESENTATIVES HAVE BEEN ADVISED OF OR SHOULD HAVE BEEN AWARE OF THE POSSIBILITY OF ANY SUCH LOSSES ARISING. 112 | 113 | 114 | 12. Indemnification 115 | 116 | 12.1 To the maximum extent permitted by law, you agree to defend, indemnify and hold harmless Google, its affiliates and their respective directors, officers, employees and agents from and against any and all claims, actions, suits or proceedings, as well as any and all losses, liabilities, damages, costs and expenses (including reasonable attorneys fees) arising out of or accruing from (a) your use of the SDK, (b) any application you develop on the SDK that infringes any copyright, trademark, trade secret, trade dress, patent or other intellectual property right of any person or defames any person or violates their rights of publicity or privacy, and (c) any non-compliance by you with the License Agreement. 117 | 118 | 119 | 13. Changes to the License Agreement 120 | 121 | 13.1 Google may make changes to the License Agreement as it distributes new versions of the SDK. When these changes are made, Google will make a new version of the License Agreement available on the website where the SDK is made available. 122 | 123 | 124 | 14. General Legal Terms 125 | 126 | 14.1 The License Agreement constitutes the whole legal agreement between you and Google and governs your use of the SDK (excluding any services which Google may provide to you under a separate written agreement), and completely replaces any prior agreements between you and Google in relation to the SDK. 127 | 128 | 14.2 You agree that if Google does not exercise or enforce any legal right or remedy which is contained in the License Agreement (or which Google has the benefit of under any applicable law), this will not be taken to be a formal waiver of Google's rights and that those rights or remedies will still be available to Google. 129 | 130 | 14.3 If any court of law, having the jurisdiction to decide on this matter, rules that any provision of the License Agreement is invalid, then that provision will be removed from the License Agreement without affecting the rest of the License Agreement. The remaining provisions of the License Agreement will continue to be valid and enforceable. 131 | 132 | 14.4 You acknowledge and agree that each member of the group of companies of which Google is the parent shall be third party beneficiaries to the License Agreement and that such other companies shall be entitled to directly enforce, and rely upon, any provision of the License Agreement that confers a benefit on (or rights in favor of) them. Other than this, no other person or company shall be third party beneficiaries to the License Agreement. 133 | 134 | 14.5 EXPORT RESTRICTIONS. THE SDK IS SUBJECT TO UNITED STATES EXPORT LAWS AND REGULATIONS. YOU MUST COMPLY WITH ALL DOMESTIC AND INTERNATIONAL EXPORT LAWS AND REGULATIONS THAT APPLY TO THE SDK. THESE LAWS INCLUDE RESTRICTIONS ON DESTINATIONS, END USERS AND END USE. 135 | 136 | 14.6 The rights granted in the License Agreement may not be assigned or transferred by either you or Google without the prior written approval of the other party. Neither you nor Google shall be permitted to delegate their responsibilities or obligations under the License Agreement without the prior written approval of the other party. 137 | 138 | 14.7 The License Agreement, and your relationship with Google under the License Agreement, shall be governed by the laws of the State of California without regard to its conflict of laws provisions. You and Google agree to submit to the exclusive jurisdiction of the courts located within the county of Santa Clara, California to resolve any legal matter arising from the License Agreement. Notwithstanding this, you agree that Google shall still be allowed to apply for injunctive remedies (or an equivalent type of urgent legal relief) in any jurisdiction. 139 | 140 | 141 | January 16, 201931311Android Emulator 142 | -------------------------------------------------------------------------------- /horcrux.dynamic_detect.patch: -------------------------------------------------------------------------------- 1 | diff --git a/horcrux/src/gf2n.rs b/horcrux/src/gf2n.rs 2 | index 5bc0221..501d455 100644 3 | --- a/horcrux/src/gf2n.rs 4 | +++ b/horcrux/src/gf2n.rs 5 | @@ -294,13 +294,9 @@ fn mul_clmul_u64::propagate_carries(words, carry) 7 | } 8 | 9 | -#[cfg(all( 10 | - feature = "clmul", 11 | - target_arch = "aarch64", 12 | - target_feature = "neon", 13 | - target_feature = "aes" 14 | -))] 15 | -fn mul_clmul_u64( 16 | +#[cfg(all(feature = "clmul", target_arch = "aarch64"))] 17 | +#[target_feature(enable = "neon", enable = "aes")] 18 | +unsafe fn mul_clmul_u64( 19 | x: &GF2n, 20 | y: &GF2n, 21 | ) -> GF2n { 22 | @@ -316,7 +312,7 @@ fn mul_clmul_u64> 64) as u64; 30 | 31 | @@ -540,12 +536,7 @@ impl Self { 44 | if NWORDS == 1 { 45 | @@ -672,19 +663,11 @@ impl Self { 49 | - #[cfg(any( 50 | - all( 51 | - feature = "clmul", 52 | - target_arch = "x86_64", 53 | - target_feature = "sse2", 54 | - target_feature = "pclmulqdq" 55 | - ), 56 | - all( 57 | - feature = "clmul", 58 | - target_arch = "aarch64", 59 | - target_feature = "neon", 60 | - target_feature = "aes" 61 | - ) 62 | + #[cfg(all( 63 | + feature = "clmul", 64 | + target_arch = "x86_64", 65 | + target_feature = "sse2", 66 | + target_feature = "pclmulqdq" 67 | ))] 68 | if W::NBITS == 64 { 69 | // Safety: W == u64 when NBITS == 64. 70 | @@ -696,6 +679,21 @@ impl = unsafe { std::mem::transmute(&self) }; 81 | + // Safety: W == u64 when NBITS == 64. 82 | + let y: &GF2n = unsafe { std::mem::transmute(other) }; 83 | + // Safety: target_feature's "neon" and "aes" are available in this block. 84 | + let tmp: GF2n = unsafe { mul_clmul_u64(x, y) }; 85 | + // Safety: W == u64 when NBITS == 64. 86 | + let result: &Self = unsafe { std::mem::transmute(&tmp) }; 87 | + return *result; 88 | + } 89 | self.mul_as_add(other) 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /rustbuild-config.toml: -------------------------------------------------------------------------------- 1 | # Includes one of the default files in src/bootstrap/defaults 2 | profile = "library" 3 | changelog-seen = 2 4 | 5 | [target.aarch64-linux-android] 6 | android-ndk = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64" 7 | 8 | [target.armv7-linux-androideabi] 9 | android-ndk = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64" 10 | 11 | [target.i686-linux-android] 12 | android-ndk = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64" 13 | 14 | [target.x86_64-linux-android] 15 | android-ndk = "/home/dev/opt/android-sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64" 16 | -------------------------------------------------------------------------------- /scripts/android-runner.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | echo "####################" 6 | echo "# android-runner.sh invoked with: $@" 7 | echo "####################" 8 | 9 | # The binary to upload and run is the first argument. 10 | BINARY_PATH="$1" 11 | BINARY=`basename ${BINARY_PATH}` 12 | # Remove the first parameter. 13 | shift 14 | 15 | # Push the test binary on the device via ADB. 16 | adb push "${BINARY_PATH}" "/data/local/tmp/$BINARY" 17 | adb shell "chmod 755 /data/local/tmp/$BINARY" 18 | 19 | # Run the test binary, forwarding the remaining parameters, so that benchmarks, 20 | # test filtering, etc. work. 21 | adb shell "/data/local/tmp/$BINARY $@" 22 | 23 | # Cleanup. 24 | adb shell "rm /data/local/tmp/$BINARY" 25 | -------------------------------------------------------------------------------- /scripts/bench-haraka.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | 7 | cd build/ 8 | git clone https://github.com/gendx/haraka-rs 9 | cd haraka-rs/ 10 | 11 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target aarch64-linux-android 12 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target armv7-linux-androideabi 13 | cargo +nightly bench --target aarch64-linux-android 14 | cargo +nightly bench --target armv7-linux-androideabi 15 | -------------------------------------------------------------------------------- /scripts/bench-horcrux.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | 7 | cd rustc-build/rust 8 | rustup toolchain link stage1 build/x86_64-unknown-linux-gnu/stage1 9 | cd ../.. 10 | 11 | cd build/ 12 | git clone https://github.com/gendx/horcrux 13 | cd horcrux 14 | 15 | cd horcrux 16 | echo "Testing fallback implementation" 17 | cargo +nightly test --target aarch64-linux-android --release -- test::gf064 18 | cargo +nightly bench --target aarch64-linux-android -- bench_mul 19 | cargo +nightly bench --target aarch64-linux-android -- bench_invert 20 | cargo +nightly bench --target aarch64-linux-android -- compact::bench_split_10 21 | cargo +nightly bench --target aarch64-linux-android -- compact::bench_reconstruct_10 22 | 23 | echo "Testing static CPU feature detection" 24 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly test --target aarch64-linux-android --release -- test::gf064 25 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target aarch64-linux-android -- bench_mul 26 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target aarch64-linux-android -- bench_invert 27 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target aarch64-linux-android -- compact::bench_split_10 28 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target aarch64-linux-android -- compact::bench_reconstruct_10 29 | cd .. 30 | 31 | git reset --hard HEAD 32 | git apply ~/horcrux.dynamic_detect.patch 33 | 34 | cd horcrux 35 | echo "Testing dynamic CPU feature detection" 36 | cargo +stage1 test --target aarch64-linux-android --release -- test::gf064 37 | cargo +stage1 bench --target aarch64-linux-android -- bench_mul 38 | cargo +stage1 bench --target aarch64-linux-android -- bench_invert 39 | cargo +stage1 bench --target aarch64-linux-android -- compact::bench_split_10 40 | cargo +stage1 bench --target aarch64-linux-android -- compact::bench_reconstruct_10 41 | cd .. 42 | -------------------------------------------------------------------------------- /scripts/bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | 7 | cd rustc-build/rust 8 | rustup toolchain link stage1 build/x86_64-unknown-linux-gnu/stage1 9 | cd ../.. 10 | 11 | cp -rf src/android-simd build/ 12 | cd build/android-simd 13 | cargo fmt --check 14 | cd ../.. 15 | 16 | set +x 17 | echo "Benchmarking with nightly toolchain" 18 | set -x 19 | RUST_TOOLCHAIN=nightly ./build-relinked.sh 20 | 21 | cd build/android-simd 22 | 23 | # We need to disable LTO, otherwise we get a linker error ("duplicate symbol: rust_eh_personality") 24 | RUSTFLAGS='-L /home/dev/build/relinked' cargo +nightly test --features relink --target aarch64-linux-android --profile release-nolto || true 25 | RUSTFLAGS='-L /home/dev/build/relinked' cargo +nightly bench --features relink --target aarch64-linux-android 26 | cargo +nightly bench --target aarch64-linux-android 27 | RUSTFLAGS='-C target-feature=+aes' cargo +nightly bench --target aarch64-linux-android 28 | 29 | cd ../.. 30 | 31 | set +x 32 | echo "Benchmarking with stage1 toolchain" 33 | set -x 34 | RUST_TOOLCHAIN=stage1 ./build-relinked.sh 35 | 36 | cd build/android-simd 37 | 38 | # Somehow --release works on stage1. 39 | RUSTFLAGS='-L /home/dev/build/relinked' cargo +stage1 test --features relink --target aarch64-linux-android --release || true 40 | RUSTFLAGS='-L /home/dev/build/relinked' cargo +stage1 bench --features relink --target aarch64-linux-android 41 | cargo +stage1 bench --target aarch64-linux-android 42 | RUSTFLAGS='-C target-feature=+aes' cargo +stage1 bench --target aarch64-linux-android 43 | 44 | cd ../.. 45 | -------------------------------------------------------------------------------- /scripts/build-application-relinked.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ./setup.sh 6 | ./script-relinked.sh 7 | ./script-java.sh 8 | -------------------------------------------------------------------------------- /scripts/build-application-stage1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ./setup.sh 6 | ./clone-rustlang-1.67.0-1286ee23e.sh 7 | ./stage0.sh 8 | ./stage1.sh 9 | ./script-rust-stage1.sh 10 | ./script-java.sh 11 | -------------------------------------------------------------------------------- /scripts/build-application.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ./setup.sh 6 | ./script-rust-nightly.sh 7 | ./script-java.sh 8 | -------------------------------------------------------------------------------- /scripts/build-relinked.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | export PATH=${PATH}:${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin 7 | OBJDUMP=${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-objdump 8 | 9 | cp -rf src/relinked build/ 10 | cd build/relinked 11 | 12 | cargo fmt --check 13 | 14 | cargo +${RUST_TOOLCHAIN} build --target aarch64-linux-android --release 15 | ${OBJDUMP} target/aarch64-linux-android/release/librelinked.a --syms | grep aes 16 | ${OBJDUMP} target/aarch64-linux-android/release/librelinked.a --syms | grep gf 17 | #${OBJDUMP} target/aarch64-linux-android/release/librelinked.a --disassemble-symbols=aesenc_fallback 2> /dev/null 18 | cp target/aarch64-linux-android/release/librelinked.a libfallback.a 19 | 20 | RUSTFLAGS='-C target-feature=+aes' cargo +${RUST_TOOLCHAIN} build --target aarch64-linux-android --release 21 | ${OBJDUMP} target/aarch64-linux-android/release/librelinked.a --syms | grep aes 22 | ${OBJDUMP} target/aarch64-linux-android/release/librelinked.a --syms | grep gf 23 | #${OBJDUMP} target/aarch64-linux-android/release/librelinked.a --disassemble-symbols=aesenc_simd 2> /dev/null 24 | cp target/aarch64-linux-android/release/librelinked.a libsimd.a 25 | 26 | rm -R target 27 | -------------------------------------------------------------------------------- /scripts/clone-rustlang-1.67.0-1286ee23e.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | rm -Rf rustc-build/* 6 | cd rustc-build 7 | mkdir rust 8 | cd rust 9 | git init 10 | git remote add origin https://github.com/rust-lang/rust 11 | time git fetch --depth 1 origin 1286ee23e4e2dec8c1696d3d76c6b26d97bbcf82 12 | time git checkout FETCH_HEAD 13 | -------------------------------------------------------------------------------- /scripts/clone-rustlang-head.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | rm -Rf rustc-build/* 6 | cd rustc-build 7 | time git clone --depth=1 https://github.com/rust-lang/rust 8 | -------------------------------------------------------------------------------- /scripts/emulator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | rm -rf ${HOME}/.android/* 6 | cp -r ${HOME}/android/* ${HOME}/.android/ 7 | ${ANDROID_HOME}/emulator/emulator -verbose -avd test_avd & 8 | -------------------------------------------------------------------------------- /scripts/flamedisk.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | cp -r tools/flamedisk rustc-build/ 6 | 7 | cd rustc-build 8 | git clone https://github.com/brendangregg/FlameGraph 9 | 10 | cd flamedisk 11 | cargo fmt --check 12 | cargo run -- > ../du.samples 13 | cd .. 14 | 15 | ./FlameGraph/flamegraph.pl --title "Disk usage" --countname "bytes" --nametype "File:" --colors mem du.samples > du.svg 16 | -------------------------------------------------------------------------------- /scripts/launch-app-debug.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -ux 4 | 5 | adb logcat -c 6 | 7 | adb uninstall com.example.myrustapplication 8 | adb install ${HOME}/build/android-simd-app-debug.apk 9 | adb shell monkey -p com.example.myrustapplication 1 10 | 11 | sleep 2 12 | 13 | adb logcat -d | grep MyRustSimdApplication 14 | -------------------------------------------------------------------------------- /scripts/launch-app-release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -ux 4 | 5 | adb logcat -c 6 | 7 | adb uninstall com.example.myrustapplication 8 | adb install ${HOME}/build/android-simd-app-release.apk 9 | adb shell monkey -p com.example.myrustapplication 1 10 | 11 | sleep 2 12 | 13 | adb logcat -d | grep MyRustSimdApplication 14 | -------------------------------------------------------------------------------- /scripts/script-gradle.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | echo "Running gradle build..." 6 | set -x 7 | 8 | echo "sdk.dir=${ANDROID_HOME}" > local.properties 9 | gradle build 10 | 11 | set +x 12 | echo "Signing release APK..." 13 | set -x 14 | ${ANDROID_HOME}/build-tools/30.0.3/apksigner \ 15 | sign \ 16 | --in app/build/outputs/apk/release/app-release-unsigned.apk \ 17 | --out app/build/outputs/apk/release/app-release.apk \ 18 | --ks $HOME/.android/debug.keystore \ 19 | --ks-key-alias androiddebugkey \ 20 | --ks-pass pass:android \ 21 | --key-pass pass:android 22 | 23 | set +x 24 | echo "Checking ZIP-alignment..." 25 | set -x 26 | ${ANDROID_HOME}/build-tools/30.0.3/zipalign \ 27 | -c -p 4 \ 28 | app/build/outputs/apk/release/app-release.apk 29 | 30 | mv app/build/outputs/apk/release/app-release-unsigned.apk ${HOME}/build/android-simd-app-release-unsigned.apk 31 | mv app/build/outputs/apk/release/app-release.apk ${HOME}/build/android-simd-app-release.apk 32 | mv app/build/outputs/apk/debug/app-debug.apk ${HOME}/build/android-simd-app-debug.apk 33 | 34 | unzip -lv ${HOME}/build/android-simd-app-release-unsigned.apk | grep -E "(lib|classes|files)" 35 | unzip -lv ${HOME}/build/android-simd-app-release.apk | grep -E "(lib|classes|files)" 36 | unzip -lv ${HOME}/build/android-simd-app-debug.apk | grep -E "(lib|classes|files)" 37 | 38 | set +x 39 | echo "Clearing up some space..." 40 | set -x 41 | 42 | rm -R ${HOME}/.gradle/* 43 | -------------------------------------------------------------------------------- /scripts/script-java-incorrect-arm64.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | echo "Size of libsimd.so" 6 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 7 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 8 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 9 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 10 | set -x 11 | 12 | cp -rf src/MyRustSimdApplication build/ 13 | cd build/MyRustSimdApplication 14 | 15 | cd app/src/main 16 | rm -Rf jniLibs 17 | mkdir jniLibs 18 | mkdir -p jniLibs/arm64 19 | mkdir -p jniLibs/armeabi 20 | mkdir -p jniLibs/x86 21 | mkdir -p jniLibs/x86_64 22 | 23 | ln -sf ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so jniLibs/arm64/libsimd.so 24 | ln -sf ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so jniLibs/armeabi/libsimd.so 25 | ln -sf ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so jniLibs/x86/libsimd.so 26 | ln -sf ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so jniLibs/x86_64/libsimd.so 27 | cd ../../.. 28 | 29 | ../../script-gradle.sh 30 | -------------------------------------------------------------------------------- /scripts/script-java-incorrect-x86_64.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | echo "Size of libsimd.so" 6 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 7 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 8 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 9 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 10 | set -x 11 | 12 | cp -rf src/MyRustSimdApplication build/ 13 | cd build/MyRustSimdApplication 14 | 15 | cd app/src/main 16 | rm -Rf jniLibs 17 | mkdir jniLibs 18 | mkdir -p jniLibs/arm64-v8a 19 | mkdir -p jniLibs/armeabi-v7a 20 | mkdir -p jniLibs/x86 21 | mkdir -p jniLibs/x64 22 | 23 | ln -sf ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so jniLibs/arm64-v8a/libsimd.so 24 | ln -sf ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so jniLibs/armeabi-v7a/libsimd.so 25 | ln -sf ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so jniLibs/x86/libsimd.so 26 | ln -sf ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so jniLibs/x64/libsimd.so 27 | cd ../../.. 28 | 29 | ../../script-gradle.sh 30 | -------------------------------------------------------------------------------- /scripts/script-java.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | echo "Size of libsimd.so" 6 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 7 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 8 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 9 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 10 | set -x 11 | 12 | cp -rf src/MyRustSimdApplication build/ 13 | cd build/MyRustSimdApplication 14 | 15 | cd app/src/main 16 | rm -Rf jniLibs 17 | mkdir jniLibs 18 | mkdir -p jniLibs/arm64-v8a 19 | mkdir -p jniLibs/armeabi-v7a 20 | mkdir -p jniLibs/x86 21 | mkdir -p jniLibs/x86_64 22 | 23 | ln -sf ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so jniLibs/arm64-v8a/libsimd.so 24 | ln -sf ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so jniLibs/armeabi-v7a/libsimd.so 25 | ln -sf ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so jniLibs/x86/libsimd.so 26 | ln -sf ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so jniLibs/x86_64/libsimd.so 27 | cd ../../.. 28 | 29 | ../../script-gradle.sh 30 | -------------------------------------------------------------------------------- /scripts/script-relinked.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | 7 | RUST_TOOLCHAIN=nightly ./build-relinked.sh 8 | 9 | cp -rf src/android-simd build/ 10 | cd build/android-simd 11 | 12 | cargo fmt --check 13 | 14 | export PATH=${PATH}:${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin 15 | RUSTFLAGS='-L /home/dev/build/relinked' cargo +nightly build --features relink --target aarch64-linux-android --release 16 | cargo +nightly build --target armv7-linux-androideabi --release 17 | cargo +nightly build --target i686-linux-android --release 18 | cargo +nightly build --target x86_64-linux-android --release 19 | 20 | cd ../.. 21 | 22 | set +x 23 | echo "Raw size of libsimd.so" 24 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 25 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 26 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 27 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 28 | set -x 29 | 30 | ./strip-rust.sh 31 | 32 | set +x 33 | echo "Cleaning up some space..." 34 | set -x 35 | 36 | rm build/relinked/*.a 37 | -------------------------------------------------------------------------------- /scripts/script-rust-default-nostrip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | cp -rf src/android-simd build/ 6 | cd build/android-simd 7 | 8 | export PATH=${PATH}:${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin 9 | cargo build --target aarch64-linux-android --release 10 | cargo build --target armv7-linux-androideabi --release 11 | cargo build --target i686-linux-android --release 12 | cargo build --target x86_64-linux-android --release 13 | 14 | cd ../.. 15 | 16 | set +x 17 | echo "Raw size of libsimd.so" 18 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 19 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 20 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 21 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 22 | 23 | echo "Clearing up some space..." 24 | set -x 25 | 26 | rm -Rf ${HOME}/.cargo/registry/* 27 | -------------------------------------------------------------------------------- /scripts/script-rust-default.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ./script-rust-default-nostrip.sh 6 | ./strip-rust.sh 7 | -------------------------------------------------------------------------------- /scripts/script-rust-nightly-nostrip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | 7 | cp -rf src/android-simd build/ 8 | cd build/android-simd 9 | 10 | cargo fmt --check 11 | 12 | export PATH=${PATH}:${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin 13 | cargo +nightly build --target aarch64-linux-android --release 14 | cargo +nightly build --target armv7-linux-androideabi --release 15 | cargo +nightly build --target i686-linux-android --release 16 | cargo +nightly build --target x86_64-linux-android --release 17 | 18 | cd ../.. 19 | 20 | set +x 21 | echo "Raw size of libsimd.so" 22 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 23 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 24 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 25 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 26 | set -x 27 | -------------------------------------------------------------------------------- /scripts/script-rust-nightly.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ./script-rust-nightly-nostrip.sh 6 | ./strip-rust.sh 7 | -------------------------------------------------------------------------------- /scripts/script-rust-stage1-nostrip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | export CARGO_UNSTABLE_SPARSE_REGISTRY=true 6 | 7 | cp -rf src/android-simd build/ 8 | cd build/android-simd 9 | 10 | export PATH=${PATH}:${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin 11 | cargo +stage1 build --target aarch64-linux-android --release 12 | cargo +stage1 build --target armv7-linux-androideabi --release 13 | cargo +stage1 build --target i686-linux-android --release 14 | cargo +stage1 build --target x86_64-linux-android --release 15 | 16 | cd ../.. 17 | 18 | set +x 19 | echo "Raw size of libsimd.so" 20 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 21 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 22 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 23 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 24 | set -x 25 | -------------------------------------------------------------------------------- /scripts/script-rust-stage1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ./script-rust-stage1-nostrip.sh 6 | ./strip-rust.sh 7 | -------------------------------------------------------------------------------- /scripts/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | ln -sf ~/rustup/toolchains/* .rustup/toolchains/ 6 | adb shell echo "Hello from Android device" || true 7 | -------------------------------------------------------------------------------- /scripts/stage0.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | cd rustc-build/rust 6 | cp ~/rustbuild-config.toml config.toml 7 | CARGOFLAGS="--timings" ./x.py build 8 | rustup toolchain link stage0 build/x86_64-unknown-linux-gnu/stage0 9 | -------------------------------------------------------------------------------- /scripts/stage1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | cd rustc-build/rust 6 | 7 | cd library/stdarch 8 | git apply ~/stdarch.patch 9 | cd ../.. 10 | 11 | CARGOFLAGS="--timings" ./x.py build --stage 1 \ 12 | --target x86_64-unknown-linux-gnu \ 13 | --target aarch64-linux-android \ 14 | --target armv7-linux-androideabi \ 15 | --target i686-linux-android \ 16 | --target x86_64-linux-android 17 | rustup toolchain link stage1 build/x86_64-unknown-linux-gnu/stage1 18 | -------------------------------------------------------------------------------- /scripts/strip-rust.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | set +x 6 | echo "Stripping Rust libraries..." 7 | set -x 8 | ${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so 9 | ${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so 10 | ${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so 11 | ${NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so 12 | 13 | set +x 14 | echo "Stripped size of libsimd.so" 15 | echo "- aarch64 : $(stat -c%s ${HOME}/build/android-simd/target/aarch64-linux-android/release/libsimd.so) bytes" 16 | echo "- armv7 : $(stat -c%s ${HOME}/build/android-simd/target/armv7-linux-androideabi/release/libsimd.so) bytes" 17 | echo "- i686 : $(stat -c%s ${HOME}/build/android-simd/target/i686-linux-android/release/libsimd.so) bytes" 18 | echo "- x86_64 : $(stat -c%s ${HOME}/build/android-simd/target/x86_64-linux-android/release/libsimd.so) bytes" 19 | set -x 20 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | namespace "com.example.myrustapplication" 5 | compileSdkVersion 33 6 | defaultConfig { 7 | applicationId "com.example.myrustapplication" 8 | minSdkVersion 23 9 | targetSdkVersion 33 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled true 16 | shrinkResources true 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | implementation 'androidx.appcompat:appcompat:1.5.1' 25 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 26 | } 27 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/java/com/example/myrustapplication/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.myrustapplication; 2 | 3 | import android.os.Build; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.widget.TextView; 7 | import androidx.appcompat.app.AppCompatActivity; 8 | 9 | import java.util.Arrays; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | private static final String TAG = "MyRustSimdApplication"; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | 20 | Log.i(TAG, "SUPPORTED_ABIS: " + Arrays.toString(Build.SUPPORTED_ABIS)); 21 | Log.i(TAG, "SUPPORTED_32_BIT_ABIS: " + Arrays.toString(Build.SUPPORTED_32_BIT_ABIS)); 22 | Log.i(TAG, "SUPPORTED_64_BIT_ABIS: " + Arrays.toString(Build.SUPPORTED_64_BIT_ABIS)); 23 | 24 | Log.i(TAG, "CPU_ABI [deprecated]: " + Build.CPU_ABI); 25 | Log.i(TAG, "CPU_ABI2 [deprecated]: " + Build.CPU_ABI2); 26 | Log.i(TAG, "OS.ARCH: " + System.getProperty("os.arch")); 27 | 28 | NativeLibrary lib = new NativeLibrary(); 29 | String message = lib.run(); 30 | ((TextView) findViewById(R.id.greetingField)).setText(message); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/java/com/example/myrustapplication/NativeLibrary.java: -------------------------------------------------------------------------------- 1 | package com.example.myrustapplication; 2 | 3 | public class NativeLibrary { 4 | 5 | static { 6 | System.loadLibrary("simd"); 7 | } 8 | 9 | public String run() { 10 | return nativeRun(); 11 | } 12 | 13 | private static native String nativeRun(); 14 | } 15 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gendx/android-rust-library/f1c1d1c12ed5ccfcf96fc74291471278128d5714/src/MyRustSimdApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | My Rust Application 3 | 4 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:7.3.0' 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | google() 18 | mavenCentral() 19 | } 20 | gradle.projectsEvaluated { 21 | tasks.withType(JavaCompile) { 22 | options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" 23 | } 24 | } 25 | } 26 | 27 | task clean(type: Delete) { 28 | delete rootProject.buildDir 29 | } 30 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | android.useAndroidX=true 16 | -------------------------------------------------------------------------------- /src/MyRustSimdApplication/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /src/android-simd/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /src/android-simd/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "simd" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [features] 7 | default = [] 8 | relink = [] 9 | 10 | [target.'cfg(target_os="android")'.dependencies] 11 | jni = { version = "0.20", default-features = false } 12 | libc = "0.2.137" 13 | 14 | [lib] 15 | crate-type = ["dylib"] 16 | 17 | # Various optimizations of the final binary's size. Interestingly, 18 | # opt-level = 'z' doesn't actually decrease the size in this case. 19 | [profile.release] 20 | codegen-units = 1 21 | lto = true 22 | panic = "abort" 23 | 24 | # LTO triggers linker errors in benchmarks ("duplicate symbol: rust_eh_personality") 25 | [profile.bench] 26 | lto = false 27 | 28 | # LTO triggers linker errors in tests ("duplicate symbol: rust_eh_personality") 29 | [profile.release-nolto] 30 | inherits = "release" 31 | lto = false 32 | -------------------------------------------------------------------------------- /src/android-simd/src/aes.rs: -------------------------------------------------------------------------------- 1 | use std::arch::is_aarch64_feature_detected; 2 | 3 | pub fn aesenc(block: &mut [u8; 16], key: &[u8; 16]) -> &'static str { 4 | let block_ptr: *mut u8 = block.as_mut_ptr(); 5 | let key_ptr: *const u8 = key.as_ptr(); 6 | 7 | let status = if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 8 | unsafe { aesenc_simd(block_ptr, key_ptr) } 9 | } else { 10 | unsafe { aesenc_fallback(block_ptr, key_ptr) } 11 | }; 12 | match status { 13 | 1 => "fallback", 14 | 2 => "simd", 15 | _ => "unknown", 16 | } 17 | } 18 | 19 | #[link(name = "fallback")] 20 | extern "C" { 21 | fn aesenc_fallback(block: *mut u8, key: *const u8) -> u32; 22 | } 23 | 24 | #[link(name = "simd")] 25 | extern "C" { 26 | fn aesenc_simd(block: *mut u8, key: *const u8) -> u32; 27 | } 28 | 29 | #[cfg(test)] 30 | pub mod tests { 31 | use super::*; 32 | use std::hint::black_box; 33 | use test::Bencher; 34 | 35 | fn aesenc_fallback_wrapper(block: &mut [u8; 16], key: &[u8; 16]) { 36 | let block_ptr: *mut u8 = block.as_mut_ptr(); 37 | let key_ptr: *const u8 = key.as_ptr(); 38 | unsafe { aesenc_fallback(block_ptr, key_ptr) }; 39 | } 40 | 41 | fn aesenc_simd_wrapper(block: &mut [u8; 16], key: &[u8; 16]) { 42 | let block_ptr: *mut u8 = block.as_mut_ptr(); 43 | let key_ptr: *const u8 = key.as_ptr(); 44 | unsafe { aesenc_simd(block_ptr, key_ptr) }; 45 | } 46 | 47 | #[target_feature(enable = "neon", enable = "aes")] 48 | unsafe fn aesenc_direct(block: &mut [u8; 16], key: &[u8; 16]) { 49 | use std::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, vdupq_n_u8, veorq_u8}; 50 | use std::mem::transmute; 51 | 52 | let mut simd_block: uint8x16_t = transmute(*block); 53 | let simd_key: uint8x16_t = transmute(*key); 54 | 55 | let zero = vdupq_n_u8(0); 56 | let x = vaeseq_u8(simd_block, zero); 57 | let y = vaesmcq_u8(x); 58 | simd_block = veorq_u8(y, simd_key); 59 | 60 | *block = transmute(simd_block); 61 | } 62 | 63 | #[test] 64 | fn test_aesenc() { 65 | let mut block = [1; 16]; 66 | let key = [2; 16]; 67 | aesenc(&mut block, &key); 68 | assert_eq!(block, [0x7e; 16]); 69 | } 70 | 71 | #[test] 72 | fn test_fallback() { 73 | let mut block = [1; 16]; 74 | let key = [2; 16]; 75 | aesenc_fallback_wrapper(&mut block, &key); 76 | assert_eq!(block, [0x7e; 16]); 77 | } 78 | 79 | #[test] 80 | fn test_aesenc_simd() { 81 | let mut block = [1; 16]; 82 | let key = [2; 16]; 83 | aesenc_simd_wrapper(&mut block, &key); 84 | assert_eq!(block, [0x7e; 16]); 85 | } 86 | 87 | #[test] 88 | fn test_aesenc_direct() { 89 | let mut block = [1; 16]; 90 | let key = [2; 16]; 91 | unsafe { aesenc_direct(&mut block, &key) }; 92 | assert_eq!(block, [0x7e; 16]); 93 | } 94 | 95 | #[bench] 96 | fn bench_aesenc(b: &mut Bencher) { 97 | let mut block = [1; 16]; 98 | let key = [2; 16]; 99 | b.iter(|| aesenc(black_box(&mut block), black_box(&key))); 100 | } 101 | 102 | #[bench] 103 | fn bench_aesenc_simd(b: &mut Bencher) { 104 | let mut block = [1; 16]; 105 | let key = [2; 16]; 106 | b.iter(|| aesenc_simd_wrapper(black_box(&mut block), black_box(&key))); 107 | } 108 | 109 | #[bench] 110 | fn bench_aesenc_fallback(b: &mut Bencher) { 111 | let mut block = [1; 16]; 112 | let key = [2; 16]; 113 | b.iter(|| aesenc_fallback_wrapper(black_box(&mut block), black_box(&key))); 114 | } 115 | 116 | #[bench] 117 | fn bench_aesenc_direct(b: &mut Bencher) { 118 | let mut block = [1; 16]; 119 | let key = [2; 16]; 120 | b.iter(|| unsafe { aesenc_direct(black_box(&mut block), black_box(&key)) }); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/android-simd/src/cpu.rs: -------------------------------------------------------------------------------- 1 | use crate::logger::Logger; 2 | #[cfg(target_arch = "aarch64")] 3 | use std::arch::asm; 4 | use std::collections::HashSet; 5 | use std::fs::File; 6 | use std::io; 7 | use std::io::{BufRead, BufReader, Read}; 8 | 9 | pub fn get_arch_name() -> &'static str { 10 | #[cfg(target_arch = "x86")] 11 | return "x86"; 12 | 13 | #[cfg(target_arch = "x86_64")] 14 | return "x86_64"; 15 | 16 | #[cfg(target_arch = "arm")] 17 | return "arm"; 18 | 19 | #[cfg(target_arch = "aarch64")] 20 | return "aarch64"; 21 | 22 | #[cfg(not(any( 23 | target_arch = "x86", 24 | target_arch = "x86_64", 25 | target_arch = "arm", 26 | target_arch = "aarch64", 27 | )))] 28 | return "unknown"; 29 | } 30 | 31 | fn cat_cpuinfo() -> io::Result { 32 | let mut file = File::open("/proc/cpuinfo")?; 33 | let mut contents = String::new(); 34 | file.read_to_string(&mut contents)?; 35 | Ok(contents) 36 | } 37 | 38 | fn parse_cpuinfo_features() -> io::Result> { 39 | let file = File::open("/proc/cpuinfo")?; 40 | let reader = BufReader::new(file); 41 | 42 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 43 | const EXPECTED_HEADER: &str = "flags"; 44 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] 45 | const EXPECTED_HEADER: &str = "Features"; 46 | 47 | let mut result = HashSet::new(); 48 | for line in reader.lines() { 49 | let line = line?; 50 | let mut tokens = line.split_whitespace(); 51 | if let Some(header) = tokens.next() 52 | && header == EXPECTED_HEADER 53 | { 54 | for token in tokens { 55 | if token != ":" { 56 | result.insert(token.to_owned()); 57 | } 58 | } 59 | } 60 | } 61 | Ok(result) 62 | } 63 | 64 | fn cat_auxv() -> io::Result> { 65 | let mut file = File::open("/proc/self/auxv")?; 66 | let mut contents = Vec::new(); 67 | file.read_to_end(&mut contents)?; 68 | 69 | let (chunks, _remainder) = contents.as_chunks::<{ std::mem::size_of::() }>(); 70 | let integers = chunks 71 | .iter() 72 | .map(|x| usize::from_ne_bytes(*x)) 73 | .collect::>(); 74 | Ok(integers 75 | .as_chunks::<2>() 76 | .0 77 | .iter() 78 | .map(|&[key, value]| (key, value)) 79 | .collect()) 80 | } 81 | 82 | fn parse_auxv() -> io::Result { 83 | let mut file = File::open("/proc/self/auxv")?; 84 | let mut contents = Vec::new(); 85 | file.read_to_end(&mut contents)?; 86 | 87 | let (chunks, _remainder) = contents.as_chunks::<{ std::mem::size_of::() }>(); 88 | let integers = chunks 89 | .iter() 90 | .map(|x| usize::from_ne_bytes(*x)) 91 | .collect::>(); 92 | for &[key, value] in integers.as_chunks::<2>().0 { 93 | if key == 16 { 94 | return Ok(value); 95 | } 96 | } 97 | 98 | Err(io::Error::new( 99 | io::ErrorKind::Other, 100 | "Invalid format for /proc/self/auxv", 101 | )) 102 | } 103 | 104 | // FIXME: Rust's libc doesn't have getauxval on "arm" for Android. 105 | #[cfg(target_arch = "aarch64")] 106 | fn get_auxval_hwcap() -> u64 { 107 | unsafe { libc::getauxval(16) } 108 | } 109 | 110 | // Note: This crashed on Android. 111 | #[allow(dead_code)] 112 | #[cfg(target_arch = "aarch64")] 113 | fn parse_mrs() -> u64 { 114 | // ID_AA64ISAR0_EL1 - Instruction Set Attribute Register 0 115 | let aa64isar0: u64; 116 | unsafe { 117 | asm!( 118 | "mrs {}, ID_AA64ISAR0_EL1", 119 | out(reg) aa64isar0, 120 | options(pure, nomem, preserves_flags, nostack) 121 | ); 122 | } 123 | aa64isar0 124 | } 125 | 126 | fn format_string_array>(values: &[S]) -> String { 127 | let mut result = String::new(); 128 | result.push('['); 129 | for (i, x) in values.iter().enumerate() { 130 | if i != 0 { 131 | result.push_str(", "); 132 | } 133 | result.push_str(x.as_ref()); 134 | } 135 | result.push(']'); 136 | result 137 | } 138 | 139 | fn display_features( 140 | logger: &L, 141 | enabled: &[&str], 142 | disabled: &[&str], 143 | ) -> Result<(), L::E> { 144 | logger.d(format!( 145 | "Detected {} enabled features:\n {}", 146 | enabled.len(), 147 | format_string_array(enabled) 148 | ))?; 149 | logger.d(format!( 150 | "Detected {} disabled features:\n {}", 151 | disabled.len(), 152 | format_string_array(disabled) 153 | ))?; 154 | Ok(()) 155 | } 156 | 157 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 158 | macro_rules! print_x86_features { 159 | ( $logger:ident, $($feature:tt,)* ) => { 160 | let mut enabled = Vec::new(); 161 | let mut disabled = Vec::new(); 162 | $( 163 | if is_x86_feature_detected!($feature) { 164 | enabled.push($feature); 165 | } else { 166 | disabled.push($feature); 167 | } 168 | )* 169 | display_features($logger, &enabled, &disabled)?; 170 | } 171 | } 172 | 173 | #[cfg(target_arch = "arm")] 174 | macro_rules! print_arm_features { 175 | ( $logger:ident, $($feature:tt,)* ) => { 176 | use std::arch::is_arm_feature_detected; 177 | let mut enabled = Vec::new(); 178 | let mut disabled = Vec::new(); 179 | $( 180 | if is_arm_feature_detected!($feature) { 181 | enabled.push($feature); 182 | } else { 183 | disabled.push($feature); 184 | } 185 | )* 186 | display_features($logger, &enabled, &disabled)?; 187 | } 188 | } 189 | 190 | #[cfg(target_arch = "aarch64")] 191 | macro_rules! print_aarch64_features { 192 | ( $logger:ident, $($feature:tt,)* ) => { 193 | use std::arch::is_aarch64_feature_detected; 194 | let mut enabled = Vec::new(); 195 | let mut disabled = Vec::new(); 196 | $( 197 | if is_aarch64_feature_detected!($feature) { 198 | enabled.push($feature); 199 | } else { 200 | disabled.push($feature); 201 | } 202 | )* 203 | display_features($logger, &enabled, &disabled)?; 204 | } 205 | } 206 | 207 | pub fn print_cpu_features(logger: &L) -> Result<(), L::E> { 208 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 209 | print_x86_features!( 210 | logger, 211 | "abm", 212 | "adx", 213 | "aes", 214 | "avx", 215 | "avx2", 216 | "avx512bf16", 217 | "avx512bitalg", 218 | "avx512bw", 219 | "avx512cd", 220 | "avx512dq", 221 | "avx512er", 222 | "avx512f", 223 | "avx512gfni", 224 | "avx512ifma", 225 | "avx512pf", 226 | "avx512vaes", 227 | "avx512vbmi", 228 | "avx512vbmi2", 229 | "avx512vl", 230 | "avx512vnni", 231 | "avx512vp2intersect", 232 | "avx512vpclmulqdq", 233 | "avx512vpopcntdq", 234 | "bmi1", 235 | "bmi2", 236 | "cmpxchg16b", 237 | "f16c", 238 | "fma", 239 | "fxsr", 240 | "lzcnt", 241 | "mmx", 242 | "pclmulqdq", 243 | "popcnt", 244 | "rdrand", 245 | "rdseed", 246 | "rtm", 247 | "sha", 248 | "sse", 249 | "sse2", 250 | "sse3", 251 | "sse4.1", 252 | "sse4.2", 253 | "sse4a", 254 | "ssse3", 255 | "tbm", 256 | "tsc", 257 | "xsave", 258 | "xsavec", 259 | "xsaveopt", 260 | "xsaves", 261 | ); 262 | 263 | #[cfg(target_arch = "arm")] 264 | print_arm_features!(logger, "aes", "crc", "i8mm", "neon", "pmull", "sha2",); 265 | 266 | #[cfg(target_arch = "aarch64")] 267 | print_aarch64_features!( 268 | logger, 269 | "aes", 270 | "asimd", 271 | "bf16", 272 | "bti", 273 | "crc", 274 | "dit", 275 | "dotprod", 276 | "dpb", 277 | "dpb2", 278 | "f32mm", 279 | "f64mm", 280 | "fcma", 281 | "fhm", 282 | "flagm", 283 | "fp", 284 | "fp16", 285 | "frintts", 286 | "i8mm", 287 | "jsconv", 288 | "lse", 289 | "lse2", 290 | "mte", 291 | "neon", 292 | "paca", 293 | "pacg", 294 | "pmull", 295 | "rand", 296 | "rcpc", 297 | "rcpc2", 298 | "rdm", 299 | "sb", 300 | "sha2", 301 | "sha3", 302 | "sm4", 303 | "ssbs", 304 | "sve", 305 | "sve2", 306 | "sve2-aes", 307 | "sve2-bitperm", 308 | "sve2-sha3", 309 | "sve2-sm4", 310 | "tme", 311 | ); 312 | 313 | // Note: This crashed on Android. 314 | // #[cfg(target_arch = "aarch64")] 315 | // { 316 | // let isar0 = parse_mrs(); 317 | // logger.d(format!( 318 | // "Features found in Instruction Set Attribute Register 0: {:016x}", 319 | // isar0 320 | // ))?; 321 | // } 322 | 323 | #[cfg(target_arch = "aarch64")] 324 | { 325 | let hwcap = get_auxval_hwcap(); 326 | logger.d(format!("HWCAP features found in getauxval: {:016x}", hwcap))?; 327 | } 328 | 329 | match parse_auxv() { 330 | Ok(hwcap) => logger.d(format!( 331 | "HWCAP features found in /proc/self/auxv ({} bits are set): {hwcap:0nibbles$x} / {hwcap:0bits$b}", 332 | hwcap.count_ones(), 333 | nibbles = 2 * std::mem::size_of::(), 334 | bits = 8 * std::mem::size_of::(), 335 | ))?, 336 | Err(e) => logger.d(format!("Failed to parse /proc/self/auxv: {:?}", e))?, 337 | } 338 | 339 | match parse_cpuinfo_features() { 340 | Ok(features) => { 341 | let mut sorted = features.iter().collect::>(); 342 | sorted.sort_unstable(); 343 | logger.d(format!( 344 | "Found {} features in /proc/cpuinfo:\n {}", 345 | sorted.len(), 346 | format_string_array(&sorted) 347 | ))?; 348 | } 349 | Err(e) => logger.d(format!("Failed to parse /proc/cpuinfo: {:?}", e))?, 350 | } 351 | 352 | match cat_auxv() { 353 | Ok(mut map) => { 354 | logger.d("Contents of /proc/self/auxv:")?; 355 | map.sort_by_key(|(key, _)| *key); 356 | for (key, value) in map { 357 | logger.d(format!( 358 | " {key:2} = {value:0nibbles$x} / {value:0bits$b}", 359 | nibbles = 2 * std::mem::size_of::(), 360 | bits = 8 * std::mem::size_of::(), 361 | ))?; 362 | } 363 | } 364 | Err(e) => logger.d(format!("Failed to read /proc/self/auxv: {:?}", e))?, 365 | } 366 | 367 | match cat_cpuinfo() { 368 | Ok(contents) => { 369 | logger.d(format!("Contents of /proc/cpuinfo:\n{}", contents))?; 370 | } 371 | Err(e) => logger.d(format!("Failed to read /proc/cpuinfo: {:?}", e))?, 372 | } 373 | 374 | Ok(()) 375 | } 376 | -------------------------------------------------------------------------------- /src/android-simd/src/gf2n.rs: -------------------------------------------------------------------------------- 1 | use std::arch::is_aarch64_feature_detected; 2 | 3 | pub fn gf256_shamir_split_10(secret: &[u8; 32], output: &mut [u8; 640]) { 4 | let secret_ptr = secret as *const [u8; 32]; 5 | let output_ptr = output.as_mut_ptr(); 6 | 7 | if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 8 | unsafe { gf256_shamir_split_10_simd(secret_ptr, output_ptr) } 9 | } else { 10 | unsafe { gf256_shamir_split_10_fallback(secret_ptr, output_ptr) } 11 | }; 12 | } 13 | 14 | pub fn gf64_invert(data: &mut [u64; 1]) { 15 | let data_ptr: *mut u64 = data.as_mut_ptr(); 16 | 17 | if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 18 | unsafe { gf64_invert_simd(data_ptr) } 19 | } else { 20 | unsafe { gf64_invert_fallback(data_ptr) } 21 | }; 22 | } 23 | 24 | pub fn gf128_invert(data: &mut [u64; 2]) { 25 | let data_ptr: *mut u64 = data.as_mut_ptr(); 26 | 27 | if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 28 | unsafe { gf128_invert_simd(data_ptr) } 29 | } else { 30 | unsafe { gf128_invert_fallback(data_ptr) } 31 | }; 32 | } 33 | 34 | pub fn gf256_invert(data: &mut [u64; 4]) { 35 | let data_ptr: *mut u64 = data.as_mut_ptr(); 36 | 37 | if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 38 | unsafe { gf256_invert_simd(data_ptr) } 39 | } else { 40 | unsafe { gf256_invert_fallback(data_ptr) } 41 | }; 42 | } 43 | 44 | #[link(name = "fallback")] 45 | extern "C" { 46 | fn gf256_shamir_split_10_fallback(secret: *const [u8; 32], output: *mut u8); 47 | fn gf64_invert_fallback(bytes: *mut u64); 48 | fn gf128_invert_fallback(bytes: *mut u64); 49 | fn gf256_invert_fallback(bytes: *mut u64); 50 | } 51 | 52 | #[link(name = "simd")] 53 | extern "C" { 54 | fn gf256_shamir_split_10_simd(secret: *const [u8; 32], output: *mut u8); 55 | fn gf64_invert_simd(bytes: *mut u64); 56 | fn gf128_invert_simd(bytes: *mut u64); 57 | fn gf256_invert_simd(bytes: *mut u64); 58 | } 59 | 60 | #[cfg(test)] 61 | pub mod tests { 62 | use super::*; 63 | use std::hint::black_box; 64 | use test::Bencher; 65 | 66 | #[bench] 67 | fn bench_gf256_shamir_split_10(b: &mut Bencher) { 68 | let data = [1; 32]; 69 | let mut output = [0; 640]; 70 | b.iter(|| { 71 | gf256_shamir_split_10(black_box(&data), &mut output); 72 | output 73 | }); 74 | } 75 | 76 | #[bench] 77 | fn bench_gf64_invert(b: &mut Bencher) { 78 | let mut data = [1]; 79 | b.iter(|| gf64_invert(black_box(&mut data))); 80 | } 81 | 82 | #[bench] 83 | fn bench_gf128_invert(b: &mut Bencher) { 84 | let mut data = [1, 2]; 85 | b.iter(|| gf128_invert(black_box(&mut data))); 86 | } 87 | 88 | #[bench] 89 | fn bench_gf256_invert(b: &mut Bencher) { 90 | let mut data = [1, 2, 3, 4]; 91 | b.iter(|| gf256_invert(black_box(&mut data))); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/android-simd/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(test, feature(never_type, test))] 2 | #![cfg_attr( 3 | any( 4 | target_arch = "arm", 5 | all(test, feature = "relink", target_arch = "aarch64") 6 | ), 7 | feature(stdsimd) 8 | )] 9 | #![feature(let_chains, slice_as_chunks)] 10 | 11 | #[cfg(all(feature = "relink", target_arch = "aarch64"))] 12 | mod aes; 13 | mod cpu; 14 | #[cfg(all(feature = "relink", target_arch = "aarch64"))] 15 | mod gf2n; 16 | mod logger; 17 | mod pmul; 18 | 19 | #[cfg(test)] 20 | extern crate test; 21 | 22 | use cpu::{get_arch_name, print_cpu_features}; 23 | use logger::Logger; 24 | use pmul::{pmul_strategy, pmul_strategy_cheat, pmul_strategy_nosimd}; 25 | 26 | #[cfg(target_os = "android")] 27 | #[allow(non_snake_case)] 28 | pub mod android { 29 | use super::hello; 30 | use crate::logger::AndroidLogger; 31 | use jni::objects::JClass; 32 | use jni::sys::jstring; 33 | use jni::JNIEnv; 34 | 35 | #[no_mangle] 36 | pub unsafe extern "C" fn Java_com_example_myrustapplication_NativeLibrary_nativeRun( 37 | env: JNIEnv, 38 | _: JClass, 39 | ) -> jstring { 40 | println!("Hello from Rust's stdout. This message is sent to /dev/null by Android."); 41 | 42 | let logger = AndroidLogger::new(env, "MyRustSimdApplication") 43 | .expect("Couldn't create logger object"); 44 | let your_arch = hello(&logger); 45 | 46 | let output = env 47 | .new_string(&your_arch) 48 | .expect("Couldn't create Java string!"); 49 | output.into_raw() 50 | } 51 | } 52 | 53 | #[cfg(test)] 54 | pub mod tests { 55 | use super::hello; 56 | use crate::logger::PrintlnLogger; 57 | 58 | #[test] 59 | fn test() { 60 | let logger = PrintlnLogger {}; 61 | hello(&logger); 62 | // Hack to have the test's stdout be displayed. 63 | assert!(false); 64 | } 65 | } 66 | 67 | pub fn hello(logger: &L) -> String { 68 | let your_arch = format!("Your CPU architecture is {}", get_arch_name()); 69 | 70 | logger.d("Hello Rust world").expect("Failed to log"); 71 | logger.d(&your_arch).expect("Failed to log"); 72 | 73 | print_cpu_features(logger).expect("Failed to log"); 74 | 75 | logger 76 | .d("Testing polynomial multiplication instructions") 77 | .expect("Failed to log"); 78 | let a: u64 = 0x1234567890abcdef; 79 | let b: u64 = 0xfedcba0987654321; 80 | 81 | let (product, strategy) = pmul_strategy_nosimd(a, b); 82 | logger 83 | .d(format!( 84 | "pmul_nosimd({a:016x?}, {b:016x?}) = {product:032x?} [strategy = {strategy}]" 85 | )) 86 | .expect("Failed to log"); 87 | 88 | let (product, strategy) = pmul_strategy_cheat(a, b); 89 | logger 90 | .d(format!( 91 | "pmul_cheat({a:016x?}, {b:016x?}) = {product:032x?} [strategy = {strategy}]" 92 | )) 93 | .expect("Failed to log"); 94 | 95 | let (product, strategy) = pmul_strategy(a, b); 96 | logger 97 | .d(format!( 98 | "pmul({a:016x?}, {b:016x?}) = {product:032x?} [strategy = {strategy}]" 99 | )) 100 | .expect("Failed to log"); 101 | 102 | #[cfg(all(feature = "relink", target_arch = "aarch64"))] 103 | { 104 | logger 105 | .d("Testing aesenc implementation") 106 | .expect("Failed to log"); 107 | let src = [1; 16]; 108 | let key = [2; 16]; 109 | let mut dst = src; 110 | let strategy = aes::aesenc(&mut dst, &key); 111 | logger 112 | .d(format!( 113 | "aesenc({src:02x?}, {key:02x?}) = {dst:02x?} [strategy = {strategy}]" 114 | )) 115 | .expect("Failed to log"); 116 | 117 | logger 118 | .d("Testing GF(2^n) implementation") 119 | .expect("Failed to log"); 120 | 121 | let src = [1]; 122 | let mut dst = src; 123 | gf2n::gf64_invert(&mut dst); 124 | logger 125 | .d(format!("invert({src:016x?}) = {dst:016x?}")) 126 | .expect("Failed to log"); 127 | 128 | let src = [1, 2]; 129 | let mut dst = src; 130 | gf2n::gf128_invert(&mut dst); 131 | logger 132 | .d(format!("invert({src:016x?}) = {dst:016x?}")) 133 | .expect("Failed to log"); 134 | 135 | let src = [1, 2, 3, 4]; 136 | let mut dst = src; 137 | gf2n::gf256_invert(&mut dst); 138 | logger 139 | .d(format!("invert({src:016x?}) = {dst:016x?}")) 140 | .expect("Failed to log"); 141 | 142 | logger 143 | .d("Testing Shamir implementation") 144 | .expect("Failed to log"); 145 | 146 | let src = [1; 32]; 147 | let mut dst = [0; 640]; 148 | gf2n::gf256_shamir_split_10(&src, &mut dst); 149 | logger 150 | .d(format!("gf256_shamir_split_10({src:02x?}) = {dst:02x?}")) 151 | .expect("Failed to log"); 152 | } 153 | 154 | your_arch 155 | } 156 | -------------------------------------------------------------------------------- /src/android-simd/src/logger.rs: -------------------------------------------------------------------------------- 1 | #[cfg(target_os = "android")] 2 | use jni::objects::{JClass, JObject, JString, JValue}; 3 | #[cfg(target_os = "android")] 4 | use jni::JNIEnv; 5 | use std::fmt::Debug; 6 | 7 | pub trait Logger { 8 | type E: Debug; 9 | 10 | /// Prints a message at the debug level. 11 | fn d(&self, message: impl AsRef) -> Result<(), Self::E>; 12 | } 13 | 14 | #[cfg(test)] 15 | pub struct PrintlnLogger {} 16 | 17 | #[cfg(test)] 18 | impl Logger for PrintlnLogger { 19 | type E = !; 20 | 21 | fn d(&self, message: impl AsRef) -> Result<(), Self::E> { 22 | println!("{}", message.as_ref()); 23 | Ok(()) 24 | } 25 | } 26 | 27 | #[cfg(target_os = "android")] 28 | pub struct AndroidLogger<'a> { 29 | /// JNI environment. 30 | env: JNIEnv<'a>, 31 | /// Reference to the android.util.Log class. 32 | log_class: JClass<'a>, 33 | /// Tag for log messages. 34 | tag: JString<'a>, 35 | } 36 | 37 | #[cfg(target_os = "android")] 38 | impl<'a> AndroidLogger<'a> { 39 | pub fn new(env: JNIEnv<'a>, tag: &str) -> Result { 40 | Ok(Self { 41 | env, 42 | log_class: env.find_class("android/util/Log")?, 43 | tag: env.new_string(tag)?, 44 | }) 45 | } 46 | } 47 | 48 | #[cfg(target_os = "android")] 49 | impl<'a> Logger for AndroidLogger<'a> { 50 | type E = jni::errors::Error; 51 | 52 | fn d(&self, message: impl AsRef) -> Result<(), Self::E> { 53 | self.env.call_static_method( 54 | self.log_class, 55 | "d", 56 | "(Ljava/lang/String;Ljava/lang/String;)I", 57 | &[ 58 | JValue::Object(JObject::from(self.tag)), 59 | JValue::Object(JObject::from(self.env.new_string(message)?)), 60 | ], 61 | )?; 62 | Ok(()) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/android-simd/src/pmul.rs: -------------------------------------------------------------------------------- 1 | pub fn pmul_strategy(a: u64, b: u64) -> (u128, &'static str) { 2 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 3 | { 4 | if is_x86_feature_detected!("sse2") && is_x86_feature_detected!("pclmulqdq") { 5 | // Safety: target_features "sse2" and "pclmulqdq" are available in this block. 6 | return unsafe { pmul_strategy_x86_clmul(a, b) }; 7 | } 8 | } 9 | #[cfg(target_arch = "aarch64")] 10 | { 11 | use std::arch::is_aarch64_feature_detected; 12 | if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 13 | // Safety: target_features "neon" and "aes" are available in this block. 14 | return unsafe { pmul_strategy_aarch64_neon(a, b) }; 15 | } 16 | } 17 | pmul_strategy_nosimd(a, b) 18 | } 19 | 20 | pub fn pmul_strategy_cheat(a: u64, b: u64) -> (u128, &'static str) { 21 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 22 | { 23 | if is_x86_feature_detected!("sse2") && is_x86_feature_detected!("pclmulqdq") { 24 | // Safety: target_features "sse2" and "pclmulqdq" are available in this block. 25 | return unsafe { pmul_strategy_x86_clmul(a, b) }; 26 | } 27 | } 28 | #[cfg(target_arch = "aarch64")] 29 | { 30 | use std::arch::is_aarch64_feature_detected; 31 | // FIXME: Here we cheat and omit to detect the "aes" feature. 32 | if is_aarch64_feature_detected!("neon") { 33 | // Safety: target_features "neon" and "aes" are available in this block. 34 | return unsafe { pmul_strategy_aarch64_neon(a, b) }; 35 | } 36 | } 37 | pmul_strategy_nosimd(a, b) 38 | } 39 | 40 | pub fn pmul_strategy_nosimd(a: u64, b: u64) -> (u128, &'static str) { 41 | let mut tmp: u128 = b as u128; 42 | let mut result: u128 = 0; 43 | for i in 0..64 { 44 | if a & (1 << i) != 0 { 45 | result ^= tmp; 46 | } 47 | tmp <<= 1; 48 | } 49 | (result, "nosimd") 50 | } 51 | 52 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 53 | #[target_feature(enable = "sse2", enable = "pclmulqdq")] 54 | unsafe fn pmul_strategy_x86_clmul(a: u64, b: u64) -> (u128, &'static str) { 55 | #[cfg(target_arch = "x86")] 56 | use std::arch::x86::{__m128i, _mm_clmulepi64_si128, _mm_set_epi64x, _mm_storeu_si128}; 57 | #[cfg(target_arch = "x86_64")] 58 | use std::arch::x86_64::{__m128i, _mm_clmulepi64_si128, _mm_set_epi64x, _mm_storeu_si128}; 59 | 60 | // Safety: target_feature "sse2" is available in this function. 61 | let x: __m128i = _mm_set_epi64x(0, a as i64); 62 | // Safety: target_feature "sse2" is available in this function. 63 | let y: __m128i = _mm_set_epi64x(0, b as i64); 64 | // Safety: target_feature "pclmulqdq" is available in this function. 65 | let clmul: __m128i = _mm_clmulepi64_si128(x, y, 0); 66 | 67 | let mut result: u128 = 0; 68 | // Safety: 69 | // - target_feature "sse2" is available in this function, 70 | // - result points to 128 bits (no alignment required by this function). 71 | _mm_storeu_si128(&mut result as *mut _ as *mut __m128i, clmul); 72 | 73 | (result, "x86_clmul") 74 | } 75 | 76 | #[cfg(target_arch = "aarch64")] 77 | #[target_feature(enable = "neon", enable = "aes")] 78 | unsafe fn pmul_strategy_aarch64_neon(a: u64, b: u64) -> (u128, &'static str) { 79 | use std::arch::aarch64::vmull_p64; 80 | 81 | // Safety: target_features "neon" and "aes" are available in this function. 82 | let result: u128 = vmull_p64(a, b); 83 | (result, "aarch64_neon") 84 | } 85 | 86 | #[cfg(test)] 87 | pub fn pmul(a: u64, b: u64) -> u128 { 88 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 89 | { 90 | if is_x86_feature_detected!("sse2") && is_x86_feature_detected!("pclmulqdq") { 91 | // Safety: target_features "sse2" and "pclmulqdq" are available in this block. 92 | return unsafe { pmul_x86_clmul(a, b) }; 93 | } 94 | } 95 | #[cfg(target_arch = "aarch64")] 96 | { 97 | use std::arch::is_aarch64_feature_detected; 98 | if is_aarch64_feature_detected!("neon") && is_aarch64_feature_detected!("aes") { 99 | // Safety: target_features "neon" and "aes" are available in this block. 100 | return unsafe { pmul_aarch64_neon(a, b) }; 101 | } 102 | } 103 | pmul_nosimd(a, b) 104 | } 105 | 106 | #[cfg(test)] 107 | pub fn pmul_cheat(a: u64, b: u64) -> u128 { 108 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 109 | { 110 | if is_x86_feature_detected!("sse2") && is_x86_feature_detected!("pclmulqdq") { 111 | // Safety: target_features "sse2" and "pclmulqdq" are available in this block. 112 | return unsafe { pmul_x86_clmul(a, b) }; 113 | } 114 | } 115 | #[cfg(target_arch = "aarch64")] 116 | { 117 | use std::arch::is_aarch64_feature_detected; 118 | // FIXME: Here we cheat and omit to detect the "aes" feature. 119 | if is_aarch64_feature_detected!("neon") { 120 | // Safety: target_features "neon" and "aes" are available in this block. 121 | return unsafe { pmul_aarch64_neon(a, b) }; 122 | } 123 | } 124 | pmul_nosimd(a, b) 125 | } 126 | 127 | #[cfg(test)] 128 | pub fn pmul_nosimd(a: u64, b: u64) -> u128 { 129 | let mut tmp: u128 = b as u128; 130 | let mut result: u128 = 0; 131 | for i in 0..64 { 132 | if a & (1 << i) != 0 { 133 | result ^= tmp; 134 | } 135 | tmp <<= 1; 136 | } 137 | result 138 | } 139 | 140 | #[cfg(test)] 141 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 142 | #[target_feature(enable = "sse2", enable = "pclmulqdq")] 143 | unsafe fn pmul_x86_clmul(a: u64, b: u64) -> u128 { 144 | #[cfg(target_arch = "x86")] 145 | use std::arch::x86::{__m128i, _mm_clmulepi64_si128, _mm_set_epi64x, _mm_storeu_si128}; 146 | #[cfg(target_arch = "x86_64")] 147 | use std::arch::x86_64::{__m128i, _mm_clmulepi64_si128, _mm_set_epi64x, _mm_storeu_si128}; 148 | 149 | // Safety: target_feature "sse2" is available in this function. 150 | let x: __m128i = _mm_set_epi64x(0, a as i64); 151 | // Safety: target_feature "sse2" is available in this function. 152 | let y: __m128i = _mm_set_epi64x(0, b as i64); 153 | // Safety: target_feature "pclmulqdq" is available in this function. 154 | let clmul: __m128i = _mm_clmulepi64_si128(x, y, 0); 155 | 156 | let mut result: u128 = 0; 157 | // Safety: 158 | // - target_feature "sse2" is available in this function, 159 | // - result points to 128 bits (no alignment required by this function). 160 | _mm_storeu_si128(&mut result as *mut _ as *mut __m128i, clmul); 161 | 162 | result 163 | } 164 | 165 | #[cfg(test)] 166 | #[cfg(target_arch = "aarch64")] 167 | #[target_feature(enable = "neon", enable = "aes")] 168 | unsafe fn pmul_aarch64_neon(a: u64, b: u64) -> u128 { 169 | use std::arch::aarch64::vmull_p64; 170 | 171 | // Safety: target_features "neon" and "aes" are available in this function. 172 | vmull_p64(a, b) 173 | } 174 | 175 | #[cfg(test)] 176 | pub mod tests { 177 | use super::*; 178 | use std::hint::black_box; 179 | use test::Bencher; 180 | 181 | #[test] 182 | fn test_pmul_strategy() { 183 | let (result, _strategy) = pmul_strategy(0x1234567890abcdef, 0xfedcba0987654321); 184 | assert_eq!(result, 0x0e038d8eab3af47a1f31f87ebb8c810f); 185 | #[cfg(target_arch = "aarch64")] 186 | assert_eq!(_strategy, "aarch64_neon"); 187 | } 188 | 189 | #[test] 190 | fn test_pmul_strategy_cheat() { 191 | let (result, _strategy) = pmul_strategy_cheat(0x1234567890abcdef, 0xfedcba0987654321); 192 | assert_eq!(result, 0x0e038d8eab3af47a1f31f87ebb8c810f); 193 | #[cfg(target_arch = "aarch64")] 194 | assert_eq!(_strategy, "aarch64_neon"); 195 | } 196 | 197 | #[test] 198 | fn test_pmul_strategy_nosimd() { 199 | let (result, strategy) = pmul_strategy_nosimd(0x1234567890abcdef, 0xfedcba0987654321); 200 | assert_eq!(result, 0x0e038d8eab3af47a1f31f87ebb8c810f); 201 | assert_eq!(strategy, "nosimd"); 202 | } 203 | 204 | #[test] 205 | fn test_pmul() { 206 | let result = pmul(0x1234567890abcdef, 0xfedcba0987654321); 207 | assert_eq!(result, 0x0e038d8eab3af47a1f31f87ebb8c810f); 208 | } 209 | 210 | #[test] 211 | fn test_pmul_cheat() { 212 | let result = pmul_cheat(0x1234567890abcdef, 0xfedcba0987654321); 213 | assert_eq!(result, 0x0e038d8eab3af47a1f31f87ebb8c810f); 214 | } 215 | 216 | #[test] 217 | fn test_pmul_nosimd() { 218 | let result = pmul_nosimd(0x1234567890abcdef, 0xfedcba0987654321); 219 | assert_eq!(result, 0x0e038d8eab3af47a1f31f87ebb8c810f); 220 | } 221 | 222 | #[bench] 223 | fn bench_pmul_strategy(b: &mut Bencher) { 224 | b.iter(|| pmul_strategy(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321))); 225 | } 226 | 227 | #[bench] 228 | fn bench_pmul_strategy_cheat(b: &mut Bencher) { 229 | b.iter(|| { 230 | pmul_strategy_cheat(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321)) 231 | }); 232 | } 233 | 234 | #[bench] 235 | fn bench_pmul_strategy_nosimd(b: &mut Bencher) { 236 | b.iter(|| { 237 | pmul_strategy_nosimd(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321)) 238 | }); 239 | } 240 | 241 | #[cfg(target_arch = "aarch64")] 242 | #[bench] 243 | fn bench_pmul_strategy_aarch64_neon(b: &mut Bencher) { 244 | b.iter(|| unsafe { 245 | pmul_strategy_aarch64_neon(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321)) 246 | }); 247 | } 248 | 249 | #[bench] 250 | fn bench_pmul(b: &mut Bencher) { 251 | b.iter(|| pmul(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321))); 252 | } 253 | 254 | #[bench] 255 | fn bench_pmul_cheat(b: &mut Bencher) { 256 | b.iter(|| pmul_cheat(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321))); 257 | } 258 | 259 | #[bench] 260 | fn bench_pmul_nosimd(b: &mut Bencher) { 261 | b.iter(|| pmul_nosimd(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321))); 262 | } 263 | 264 | #[cfg(target_arch = "aarch64")] 265 | #[bench] 266 | fn bench_pmul_aarch64_neon(b: &mut Bencher) { 267 | b.iter(|| unsafe { 268 | pmul_aarch64_neon(black_box(0x1234567890abcdef), black_box(0xfedcba0987654321)) 269 | }); 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /src/relinked/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /src/relinked/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "relinked" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [lib] 7 | crate-type = ["staticlib"] 8 | 9 | [profile.release] 10 | codegen-units = 1 11 | panic = "abort" 12 | 13 | [features] 14 | default = ["clmul"] 15 | clmul = ["horcrux/clmul"] 16 | 17 | [dependencies] 18 | horcrux = { git = "https://github.com/gendx/horcrux", rev = "0ca24ee3353c70dc7ea11a299826bdfc0f4eb173", default-features = false, features = ["parse"] } 19 | -------------------------------------------------------------------------------- /src/relinked/src/gf2n.rs: -------------------------------------------------------------------------------- 1 | //! Generic implementation of a finite field GF(2^n). 2 | //! 3 | //! This is based on the existence of a irreducible polynomial of the form 4 | //! `x^n + x^a + x^b + x^c + 1`, where `0 < c < b < a < n`. 5 | 6 | use std::convert::TryInto; 7 | use std::fmt::{Debug, Display}; 8 | use std::hash::{Hash, Hasher}; 9 | use std::ops::{Add, AddAssign, BitAnd, BitXor, BitXorAssign, Mul, MulAssign, Not, Shl, Shr, Sub}; 10 | 11 | /// Trait for words that can be used for the representation of elements of GF(2^n). 12 | pub trait Word: 13 | Copy 14 | + Eq 15 | + Hash 16 | + Debug 17 | + From 18 | + BitAnd 19 | + BitXorAssign 20 | + BitXor 21 | + Not 22 | + Shl 23 | + Shr 24 | { 25 | /// Zero. 26 | const ZERO: Self; 27 | /// One. 28 | const ONE: Self; 29 | /// Number of bytes in the size of the type. 30 | const NBYTES: usize = std::mem::size_of::(); 31 | /// Number of bits in the size of the type. 32 | const NBITS: usize = 8 * Self::NBYTES; 33 | 34 | /// Parses a word from a byte slice. Panics if the slice length is not `NBYTES`. 35 | fn from_bytes(bytes: &[u8]) -> Self; 36 | } 37 | 38 | impl Word for u64 { 39 | const ZERO: Self = 0; 40 | const ONE: Self = 1; 41 | 42 | fn from_bytes(bytes: &[u8]) -> Self { 43 | let array = bytes.try_into().unwrap(); 44 | u64::from_be_bytes(array) 45 | } 46 | } 47 | 48 | /// Implementation of a binary field GF(2^n), with `W::NBYTES * NWORDS` bits, using the 49 | /// irreducible polynomial `x^n + x^a + x^b + x^c + 1`. 50 | #[derive(Clone, Copy)] 51 | pub struct GF2n { 52 | words: [W; NWORDS], 53 | } 54 | 55 | /// Finite field GF(2^64) implemented with 64-bit words and using the following irreducible 56 | /// polynomial: `x^64 + x^4 + x^3 + x + 1`. 57 | pub type GF64 = GF2n; 58 | /// Finite field GF(2^128) implemented with 64-bit words and using the following irreducible 59 | /// polynomial: `x^128 + x^7 + x^2 + x + 1`. 60 | pub type GF128 = GF2n; 61 | /// Finite field GF(2^256) implemented with 64-bit words and using the following irreducible 62 | /// polynomial: `x^256 + x^10 + x^5 + x^2 + 1`. 63 | pub type GF256 = GF2n; 64 | 65 | impl Debug 66 | for GF2n 67 | { 68 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { 69 | match W::NBITS { 70 | 8 => f.write_fmt(format_args!("{:02x?}", &self.words as &[W])), 71 | 16 => f.write_fmt(format_args!("{:04x?}", &self.words as &[W])), 72 | 32 => f.write_fmt(format_args!("{:08x?}", &self.words as &[W])), 73 | 64 => f.write_fmt(format_args!("{:016x?}", &self.words as &[W])), 74 | 128 => f.write_fmt(format_args!("{:032x?}", &self.words as &[W])), 75 | _ => f.write_fmt(format_args!("{:x?}", &self.words as &[W])), 76 | } 77 | } 78 | } 79 | 80 | impl Display 81 | for GF2n 82 | { 83 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { 84 | for d in &self.words as &[W] { 85 | match W::NBITS { 86 | 8 => f.write_fmt(format_args!("{:02x?}", d))?, 87 | 16 => f.write_fmt(format_args!("{:04x?}", d))?, 88 | 32 => f.write_fmt(format_args!("{:08x?}", d))?, 89 | 64 => f.write_fmt(format_args!("{:016x?}", d))?, 90 | 128 => f.write_fmt(format_args!("{:032x?}", d))?, 91 | _ => unimplemented!(), 92 | } 93 | } 94 | Ok(()) 95 | } 96 | } 97 | 98 | impl PartialEq 99 | for GF2n 100 | { 101 | fn eq(&self, other: &Self) -> bool { 102 | &self.words as &[W] == &other.words as &[W] 103 | } 104 | } 105 | 106 | impl Eq 107 | for GF2n 108 | { 109 | } 110 | 111 | impl Hash 112 | for GF2n 113 | { 114 | fn hash(&self, state: &mut H) { 115 | (&self.words as &[W]).hash(state) 116 | } 117 | } 118 | 119 | #[cfg(all( 120 | feature = "clmul", 121 | target_arch = "x86_64", 122 | target_feature = "sse2", 123 | target_feature = "pclmulqdq" 124 | ))] 125 | fn mul_clmul_u64( 126 | x: &GF2n, 127 | y: &GF2n, 128 | ) -> GF2n { 129 | use core::arch::x86_64::{__m128i, _mm_clmulepi64_si128, _mm_set_epi64x, _mm_storeu_si128}; 130 | 131 | // Note: we cannot create an array of `NWORDS * 2` elements: 132 | // error: constant expression depends on a generic parameter 133 | let mut words = [0u64; NWORDS]; 134 | let mut carry = [0u64; NWORDS]; 135 | 136 | for i in 0..NWORDS { 137 | // Safety: target_feature "sse2" is available in this function. 138 | let xi: __m128i = unsafe { _mm_set_epi64x(0, x.words[i] as i64) }; 139 | for j in 0..NWORDS { 140 | // Safety: target_feature "sse2" is available in this function. 141 | let yj: __m128i = unsafe { _mm_set_epi64x(0, y.words[j] as i64) }; 142 | // Safety: target_feature "pclmulqdq" is available in this function. 143 | let clmul: __m128i = unsafe { _mm_clmulepi64_si128(xi, yj, 0) }; 144 | let mut cc: [u64; 2] = [0u64, 0u64]; 145 | // Safety: 146 | // - target_feature "sse2" is available in this function, 147 | // - cc points to 128 bits (no alignment required by this function). 148 | unsafe { _mm_storeu_si128(&mut cc as *mut _ as *mut __m128i, clmul) }; 149 | 150 | let ij = i + j; 151 | if ij < NWORDS { 152 | words[ij] ^= cc[0]; 153 | } else { 154 | carry[ij - NWORDS] ^= cc[0]; 155 | } 156 | 157 | let ij1 = ij + 1; 158 | if ij1 < NWORDS { 159 | words[ij1] ^= cc[1]; 160 | } else { 161 | carry[ij1 - NWORDS] ^= cc[1]; 162 | } 163 | } 164 | } 165 | 166 | GF2n::::propagate_carries(words, carry) 167 | } 168 | 169 | #[cfg(all(feature = "clmul", target_arch = "aarch64", target_feature = "aes"))] 170 | unsafe fn mul_clmul_u64( 171 | x: &GF2n, 172 | y: &GF2n, 173 | ) -> GF2n { 174 | use std::arch::aarch64::vmull_p64; 175 | 176 | // Note: we cannot create an array of `NWORDS * 2` elements: 177 | // error: constant expression depends on a generic parameter 178 | let mut words = [0u64; NWORDS]; 179 | let mut carry = [0u64; NWORDS]; 180 | 181 | for i in 0..NWORDS { 182 | let xi = x.words[i]; 183 | for j in 0..NWORDS { 184 | let yj = y.words[j]; 185 | // Safety: target_feature's "neon" and "aes" are available in this function. 186 | let clmul: u128 = vmull_p64(xi, yj); 187 | let low: u64 = clmul as u64; 188 | let high: u64 = (clmul >> 64) as u64; 189 | 190 | let ij = i + j; 191 | if ij < NWORDS { 192 | words[ij] ^= low; 193 | } else { 194 | carry[ij - NWORDS] ^= low; 195 | } 196 | 197 | let ij1 = ij + 1; 198 | if ij1 < NWORDS { 199 | words[ij1] ^= high; 200 | } else { 201 | carry[ij1 - NWORDS] ^= high; 202 | } 203 | } 204 | } 205 | 206 | GF2n::::propagate_carries(words, carry) 207 | } 208 | 209 | impl 210 | GF2n 211 | { 212 | const NBITS: usize = W::NBITS * NWORDS; 213 | 214 | const fn new_small(word: W) -> Self { 215 | let mut words = [W::ZERO; NWORDS]; 216 | words[0] = word; 217 | Self { words } 218 | } 219 | 220 | fn shl1(&mut self) { 221 | let mut carry = W::ZERO; 222 | for i in 0..NWORDS { 223 | let d = self.words[i]; 224 | self.words[i] = (d << 1) ^ carry; 225 | carry = d >> (W::NBITS - 1); 226 | } 227 | if carry != W::ZERO { 228 | self.words[0] ^= W::ONE ^ (W::ONE << A) ^ (W::ONE << B) ^ (W::ONE << C); 229 | } 230 | } 231 | 232 | fn mul_as_add(mut self, other: &Self) -> Self { 233 | let mut result = Self { 234 | words: [W::ZERO; NWORDS], 235 | }; 236 | for &word in &other.words as &[W] { 237 | for i in 0..W::NBITS { 238 | if word & (W::ONE << i) != W::ZERO { 239 | result += &self; 240 | } 241 | self.shl1(); 242 | } 243 | } 244 | result 245 | } 246 | 247 | #[cfg(any( 248 | all( 249 | feature = "clmul", 250 | target_arch = "x86_64", 251 | target_feature = "sse2", 252 | target_feature = "pclmulqdq" 253 | ), 254 | all( 255 | feature = "clmul", 256 | target_arch = "aarch64", 257 | target_feature = "neon", 258 | target_feature = "aes" 259 | ) 260 | ))] 261 | fn propagate_carries(mut words: [W; NWORDS], carry: [W; NWORDS]) -> Self { 262 | if NWORDS == 1 { 263 | let mut c = carry[0]; 264 | while c != W::ZERO { 265 | words[0] ^= c ^ (c << A) ^ (c << B) ^ (c << C); 266 | c = (c >> (W::NBITS - A)) ^ (c >> (W::NBITS - B)) ^ (c >> (W::NBITS - C)); 267 | } 268 | } else { 269 | for i in 0..NWORDS { 270 | let c = carry[i]; 271 | words[i] ^= c ^ (c << A) ^ (c << B) ^ (c << C); 272 | if i + 1 < NWORDS { 273 | words[i + 1] ^= 274 | (c >> (W::NBITS - A)) ^ (c >> (W::NBITS - B)) ^ (c >> (W::NBITS - C)); 275 | } else { 276 | let c = (c >> (W::NBITS - A)) ^ (c >> (W::NBITS - B)) ^ (c >> (W::NBITS - C)); 277 | words[0] ^= c ^ (c << A) ^ (c << B) ^ (c << C); 278 | words[1] ^= 279 | (c >> (W::NBITS - A)) ^ (c >> (W::NBITS - B)) ^ (c >> (W::NBITS - C)); 280 | } 281 | } 282 | } 283 | 284 | Self { words } 285 | } 286 | 287 | const ONE: Self = Self::new_small(W::ONE); 288 | 289 | pub fn invert(mut self) -> Self { 290 | // Compute x^(2^n - 2) 291 | let mut result = Self::ONE; 292 | for _ in 1..Self::NBITS { 293 | self = self * &self; 294 | result *= &self; 295 | } 296 | result 297 | } 298 | 299 | pub fn from_words(words: [W; NWORDS]) -> Self { 300 | Self { words } 301 | } 302 | 303 | pub fn to_words(self) -> [W; NWORDS] { 304 | self.words 305 | } 306 | } 307 | 308 | impl From 309 | for GF2n 310 | { 311 | fn from(word: u8) -> Self { 312 | let mut words = [W::ZERO; NWORDS]; 313 | words[0] = W::from(word); 314 | Self { words } 315 | } 316 | } 317 | 318 | impl Add 319 | for GF2n 320 | { 321 | type Output = Self; 322 | 323 | #[allow(clippy::suspicious_arithmetic_impl)] 324 | #[allow(clippy::needless_range_loop)] 325 | fn add(self, other: Self) -> Self { 326 | let mut words = [W::ZERO; NWORDS]; 327 | for i in 0..NWORDS { 328 | words[i] = self.words[i] ^ other.words[i]; 329 | } 330 | Self { words } 331 | } 332 | } 333 | 334 | impl AddAssign<&Self> 335 | for GF2n 336 | { 337 | #[allow(clippy::suspicious_op_assign_impl)] 338 | fn add_assign(&mut self, other: &Self) { 339 | for i in 0..NWORDS { 340 | self.words[i] ^= other.words[i]; 341 | } 342 | } 343 | } 344 | 345 | impl Sub 346 | for GF2n 347 | { 348 | type Output = Self; 349 | 350 | #[allow(clippy::suspicious_arithmetic_impl)] 351 | fn sub(self, other: Self) -> Self { 352 | self + other 353 | } 354 | } 355 | 356 | impl Mul<&Self> 357 | for GF2n 358 | { 359 | type Output = Self; 360 | 361 | fn mul(self, other: &Self) -> Self { 362 | #[cfg(all( 363 | feature = "clmul", 364 | target_arch = "x86_64", 365 | target_feature = "sse2", 366 | target_feature = "pclmulqdq" 367 | ))] 368 | if W::NBITS == 64 { 369 | // Safety: W == u64 when NBITS == 64. 370 | let x: &GF2n = unsafe { std::mem::transmute(&self) }; 371 | // Safety: W == u64 when NBITS == 64. 372 | let y: &GF2n = unsafe { std::mem::transmute(other) }; 373 | let tmp: GF2n = mul_clmul_u64(x, y); 374 | // Safety: W == u64 when NBITS == 64. 375 | let result: &Self = unsafe { std::mem::transmute(&tmp) }; 376 | return *result; 377 | } 378 | #[cfg(all( 379 | feature = "clmul", 380 | target_arch = "aarch64", 381 | target_feature = "neon", 382 | target_feature = "aes" 383 | ))] 384 | if W::NBITS == 64 { 385 | // Safety: W == u64 when NBITS == 64. 386 | let x: &GF2n = unsafe { std::mem::transmute(&self) }; 387 | // Safety: W == u64 when NBITS == 64. 388 | let y: &GF2n = unsafe { std::mem::transmute(other) }; 389 | let tmp: GF2n = unsafe { mul_clmul_u64(x, y) }; 390 | // Safety: W == u64 when NBITS == 64. 391 | let result: &Self = unsafe { std::mem::transmute(&tmp) }; 392 | return *result; 393 | } 394 | self.mul_as_add(other) 395 | } 396 | } 397 | 398 | impl MulAssign<&Self> 399 | for GF2n 400 | { 401 | fn mul_assign(&mut self, other: &Self) { 402 | *self = *self * other; 403 | } 404 | } 405 | -------------------------------------------------------------------------------- /src/relinked/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(stdsimd)] 2 | 3 | mod gf2n; 4 | 5 | use gf2n::{GF128, GF256, GF64}; 6 | use horcrux::field::Field; 7 | use horcrux::shamir::{RandomShamir, Shamir}; 8 | 9 | #[cfg(not(all(target_feature = "neon", target_feature = "aes")))] 10 | mod fallback { 11 | use super::*; 12 | 13 | #[no_mangle] 14 | pub unsafe extern "C" fn gf256_shamir_split_10_fallback( 15 | secret: *const [u8; 32], 16 | output: *mut u8, 17 | ) { 18 | let secret = horcrux::gf2n::GF256::from_bytes(&*secret).unwrap(); 19 | let shares = RandomShamir::split(&secret, 10, 10); 20 | let output_slice = std::slice::from_raw_parts_mut( 21 | output as *mut >::Share, 22 | 10, 23 | ); 24 | output_slice.copy_from_slice(shares.as_slice()); 25 | } 26 | 27 | #[no_mangle] 28 | pub unsafe extern "C" fn gf64_invert_fallback(bytes: *mut u64) { 29 | let bytes: &mut [u64; 1] = &mut *(bytes as *mut [u64; 1]); 30 | 31 | let x = GF64::from_words(*bytes); 32 | *bytes = x.invert().to_words(); 33 | } 34 | 35 | #[no_mangle] 36 | pub unsafe extern "C" fn gf128_invert_fallback(bytes: *mut u64) { 37 | let bytes: &mut [u64; 2] = &mut *(bytes as *mut [u64; 2]); 38 | 39 | let x = GF128::from_words(*bytes); 40 | *bytes = x.invert().to_words(); 41 | } 42 | 43 | #[no_mangle] 44 | pub unsafe extern "C" fn gf256_invert_fallback(bytes: *mut u64) { 45 | let bytes: &mut [u64; 4] = &mut *(bytes as *mut [u64; 4]); 46 | 47 | let x = GF256::from_words(*bytes); 48 | *bytes = x.invert().to_words(); 49 | } 50 | 51 | #[no_mangle] 52 | pub unsafe extern "C" fn aesenc_fallback(block: *mut u8, key: *const u8) -> u32 { 53 | let state: &mut [u8; 16] = &mut *(block as *mut [u8; 16]); 54 | let rkey: &[u8; 16] = &*(key as *const [u8; 16]); 55 | 56 | subbytes(state); 57 | shiftrows(state); 58 | mixcolumns(state); 59 | addroundkey(state, rkey); 60 | 61 | 1 62 | } 63 | 64 | fn subbytes(state: &mut [u8; 16]) { 65 | for x in state.iter_mut() { 66 | *x = AES_SBOX[*x as usize]; 67 | } 68 | } 69 | 70 | fn shiftrows(state: &mut [u8; 16]) { 71 | let tmp = state[1]; 72 | state[1] = state[5]; 73 | state[5] = state[9]; 74 | state[9] = state[13]; 75 | state[13] = tmp; 76 | 77 | let tmp = state[2]; 78 | state[2] = state[10]; 79 | state[10] = tmp; 80 | let tmp = state[6]; 81 | state[6] = state[14]; 82 | state[14] = tmp; 83 | 84 | let tmp = state[3]; 85 | state[3] = state[15]; 86 | state[15] = state[11]; 87 | state[11] = state[7]; 88 | state[7] = tmp; 89 | } 90 | 91 | // multiplication by 2 in GF(2^256) 92 | fn mul2(x: u8) -> u8 { 93 | (x << 1) ^ (((x >> 7) & 1) * 0x1b) 94 | } 95 | 96 | fn mixcolumns(state: &mut [u8; 16]) { 97 | for i in 0..4 { 98 | let x0 = state[4 * i]; 99 | let x1 = state[4 * i + 1]; 100 | let x2 = state[4 * i + 2]; 101 | let x3 = state[4 * i + 3]; 102 | let x = x0 ^ x1 ^ x2 ^ x3; 103 | state[4 * i] ^= mul2(x0 ^ x1) ^ x; 104 | state[4 * i + 1] ^= mul2(x1 ^ x2) ^ x; 105 | state[4 * i + 2] ^= mul2(x2 ^ x3) ^ x; 106 | state[4 * i + 3] ^= mul2(x3 ^ x0) ^ x; 107 | } 108 | } 109 | 110 | fn addroundkey(state: &mut [u8; 16], rkey: &[u8; 16]) { 111 | for i in 0..16 { 112 | state[i] ^= rkey[i]; 113 | } 114 | } 115 | 116 | static AES_SBOX: [u8; 256] = [ 117 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 118 | 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 119 | 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 120 | 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 121 | 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 122 | 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 123 | 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 124 | 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 125 | 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 126 | 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 127 | 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 128 | 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 129 | 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 130 | 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 131 | 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 132 | 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 133 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 134 | 0x16, 135 | ]; 136 | } 137 | 138 | #[cfg(all(target_feature = "neon", target_feature = "aes"))] 139 | mod simd { 140 | use super::*; 141 | use std::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, vdupq_n_u8, veorq_u8}; 142 | 143 | #[no_mangle] 144 | pub unsafe extern "C" fn gf256_shamir_split_10_simd(secret: *const [u8; 32], output: *mut u8) { 145 | let secret = horcrux::gf2n::GF256::from_bytes(&*secret).unwrap(); 146 | let shares = RandomShamir::split(&secret, 10, 10); 147 | let output_slice = std::slice::from_raw_parts_mut( 148 | output as *mut >::Share, 149 | 10, 150 | ); 151 | output_slice.copy_from_slice(shares.as_slice()); 152 | } 153 | 154 | #[no_mangle] 155 | pub unsafe extern "C" fn gf64_invert_simd(bytes: *mut u64) { 156 | let bytes: &mut [u64; 1] = &mut *(bytes as *mut [u64; 1]); 157 | 158 | let x = GF64::from_words(*bytes); 159 | *bytes = x.invert().to_words(); 160 | } 161 | 162 | #[no_mangle] 163 | pub unsafe extern "C" fn gf128_invert_simd(bytes: *mut u64) { 164 | let bytes: &mut [u64; 2] = &mut *(bytes as *mut [u64; 2]); 165 | 166 | let x = GF128::from_words(*bytes); 167 | *bytes = x.invert().to_words(); 168 | } 169 | 170 | #[no_mangle] 171 | pub unsafe extern "C" fn gf256_invert_simd(bytes: *mut u64) { 172 | let bytes: &mut [u64; 4] = &mut *(bytes as *mut [u64; 4]); 173 | 174 | let x = GF256::from_words(*bytes); 175 | *bytes = x.invert().to_words(); 176 | } 177 | 178 | #[no_mangle] 179 | pub unsafe extern "C" fn aesenc_simd(block: *mut u8, key: *const u8) -> u32 { 180 | let simd_block: &mut uint8x16_t = &mut *(block as *mut uint8x16_t); 181 | let simd_key: &uint8x16_t = &*(key as *const uint8x16_t); 182 | 183 | let zero = vdupq_n_u8(0); 184 | let x = vaeseq_u8(*simd_block, zero); 185 | let y = vaesmcq_u8(x); 186 | *simd_block = veorq_u8(y, *simd_key); 187 | 188 | 2 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /stdarch.patch: -------------------------------------------------------------------------------- 1 | diff --git a/crates/std_detect/src/detect/mod.rs b/crates/std_detect/src/detect/mod.rs 2 | index 2bca84c..664dd33 100644 3 | --- a/crates/std_detect/src/detect/mod.rs 4 | +++ b/crates/std_detect/src/detect/mod.rs 5 | @@ -47,7 +47,7 @@ cfg_if! { 6 | // On x86/x86_64 no OS specific functionality is required. 7 | #[path = "os/x86.rs"] 8 | mod os; 9 | - } else if #[cfg(all(target_os = "linux", feature = "libc"))] { 10 | + } else if #[cfg(all(any(target_os = "linux", target_os = "android"), feature = "libc"))] { 11 | #[path = "os/linux/mod.rs"] 12 | mod os; 13 | } else if #[cfg(all(target_os = "freebsd", feature = "libc"))] { 14 | -------------------------------------------------------------------------------- /tools/flamedisk/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /tools/flamedisk/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "flamedisk" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /tools/flamedisk/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | use std::fs::read_dir; 3 | use std::io; 4 | use std::os::unix::fs::MetadataExt; 5 | use std::path::PathBuf; 6 | 7 | fn main() -> io::Result<()> { 8 | visit( 9 | "".into(), 10 | "/home/dev/rustc-build/rust/".into(), 11 | &mut HashSet::new(), 12 | ) 13 | } 14 | 15 | fn visit(output_name: String, path: PathBuf, inodes: &mut HashSet) -> io::Result<()> { 16 | for entry in read_dir(path)? { 17 | let entry = entry?; 18 | let metadata = entry.metadata()?; 19 | let inode = metadata.ino(); 20 | 21 | if !inodes.insert(inode) { 22 | eprintln!("Skipping hard link (inode = {}): {:?}", inode, entry.path()); 23 | continue; 24 | } 25 | 26 | // TODO: sanitize file name. 27 | let file_name = entry.file_name().into_string().unwrap(); 28 | let item_name = if output_name.is_empty() { 29 | file_name 30 | } else { 31 | output_name.clone() + ";" + &file_name 32 | }; 33 | 34 | if metadata.is_dir() { 35 | visit(item_name, entry.path(), inodes)?; 36 | } else if metadata.is_file() { 37 | println!("{} {}", item_name, metadata.len()); 38 | } 39 | } 40 | Ok(()) 41 | } 42 | --------------------------------------------------------------------------------