├── .github └── workflows │ ├── Release.yml │ ├── ci-macos-brew.yml │ ├── ci-macos.yml │ ├── ci-win-msys2.yml │ ├── ci.yml │ └── test-mac.yml ├── .gitignore ├── DEBIAN └── control ├── LICENSE ├── Makefile ├── Makefile-wasm ├── Makefile-wasm.config ├── Makefile.config ├── Makefile.config.msys2 ├── Makefile.config.raspberry-pi-2 ├── README.FreeBSD.md ├── README.Mac.md ├── README.Windows.md ├── README.md ├── README.wasm.md ├── build-wasm.sh ├── ck-polyfill.c ├── ck-polyfill.h ├── cyclone.c ├── dist ├── PKGBUILD ├── PKGBUILD-master └── notes.txt ├── ffi.c ├── gc.c ├── hashset.c ├── icyc.c ├── icyc.scm ├── include └── cyclone │ ├── bignum.h │ ├── ck_ht_hash.h │ ├── hashset.h │ ├── runtime-main.h │ ├── runtime.h │ └── types.h ├── install.sh ├── libs ├── cyclone │ ├── concurrent.c │ ├── concurrent.sld │ ├── foreign.c │ ├── foreign.meta │ ├── foreign.sld │ ├── match.c │ ├── match.meta │ ├── match.sld │ ├── test.c │ ├── test.meta │ ├── test.scm │ └── test.sld ├── test-atoms.scm └── test-threads.scm ├── mstreams.c ├── runtime.c ├── scheme ├── base.c ├── base.sld ├── case-lambda.c ├── case-lambda.sld ├── char.c ├── char.sld ├── complex.c ├── complex.sld ├── cxr.c ├── cxr.sld ├── cyclone │ ├── ast.c │ ├── ast.sld │ ├── cgen.c │ ├── cgen.sld │ ├── common.c │ ├── common.sld │ ├── cps-opt-analyze-call-graph.scm │ ├── cps-opt-local-var-redux.scm │ ├── cps-opt-memoize-pure-fncs.scm │ ├── cps-optimizations.c │ ├── cps-optimizations.sld │ ├── hashset.c │ ├── hashset.sld │ ├── libraries.c │ ├── libraries.sld │ ├── macros.c │ ├── macros.sld │ ├── pass-validate-syntax.scm │ ├── pretty-print.c │ ├── pretty-print.sld │ ├── primitives.c │ ├── primitives.sld │ ├── transforms.c │ ├── transforms.sld │ ├── util.c │ └── util.sld ├── eval.c ├── eval.sld ├── file.c ├── file.sld ├── inexact.c ├── inexact.sld ├── lazy.c ├── lazy.sld ├── load.c ├── load.sld ├── process-context.c ├── process-context.sld ├── read.c ├── read.sld ├── repl.c ├── repl.sld ├── time.c ├── time.sld ├── write.c └── write.sld ├── scripts ├── debug.sh ├── debug2.sh └── timing.sh ├── setup.mac.sh ├── setup.mysys.sh ├── srfi ├── 1.c ├── 1.scm ├── 1.sld ├── 106.c ├── 106.sld ├── 111.c ├── 111.sld ├── 113.c ├── 113.sld ├── 117.c ├── 117.sld ├── 121.c ├── 121.scm ├── 121.sld ├── 128.c ├── 128.meta ├── 128.sld ├── 132.c ├── 132.sld ├── 133.c ├── 133.sld ├── 143.c ├── 143.sld ├── 18.c ├── 18.sld ├── 2.c ├── 2.meta ├── 2.scm ├── 2.sld ├── 27.c ├── 27.sld ├── 28.c ├── 28.scm ├── 28.sld ├── 60.c ├── 60.scm ├── 60.sld ├── 69.c ├── 69.sld ├── list-queues │ ├── list-queues-impl.scm │ └── list-queues-test.scm ├── sets │ ├── comparators-shim.scm │ ├── sets-impl.scm │ └── sets-test.scm ├── sorting │ ├── delndups.scm │ ├── lmsort.scm │ ├── median.scm │ ├── merge.scm │ ├── select.scm │ ├── sort.scm │ ├── sortfaster.scm │ ├── sorting-test.scm │ ├── sortp.scm │ ├── vbinsearch.scm │ ├── vector-util.scm │ ├── vhsort.scm │ ├── visort.scm │ ├── vmsort.scm │ ├── vqsort2.scm │ └── vqsort3.scm └── vectors-impl.scm ├── tests ├── base.scm ├── test.scm ├── threading.scm └── unit-tests.scm ├── third-party └── libtommath-1.2.0 │ ├── LICENSE │ ├── README.md │ ├── appveyor.yml │ ├── astylerc │ ├── bn_cutoffs.c │ ├── bn_deprecated.c │ ├── bn_mp_2expt.c │ ├── bn_mp_abs.c │ ├── bn_mp_add.c │ ├── bn_mp_add_d.c │ ├── bn_mp_addmod.c │ ├── bn_mp_and.c │ ├── bn_mp_clamp.c │ ├── bn_mp_clear.c │ ├── bn_mp_clear_multi.c │ ├── bn_mp_cmp.c │ ├── bn_mp_cmp_d.c │ ├── bn_mp_cmp_mag.c │ ├── bn_mp_cnt_lsb.c │ ├── bn_mp_complement.c │ ├── bn_mp_copy.c │ ├── bn_mp_count_bits.c │ ├── bn_mp_decr.c │ ├── bn_mp_div.c │ ├── bn_mp_div_2.c │ ├── bn_mp_div_2d.c │ ├── bn_mp_div_3.c │ ├── bn_mp_div_d.c │ ├── bn_mp_dr_is_modulus.c │ ├── bn_mp_dr_reduce.c │ ├── bn_mp_dr_setup.c │ ├── bn_mp_error_to_string.c │ ├── bn_mp_exch.c │ ├── bn_mp_expt_u32.c │ ├── bn_mp_exptmod.c │ ├── bn_mp_exteuclid.c │ ├── bn_mp_fread.c │ ├── bn_mp_from_sbin.c │ ├── bn_mp_from_ubin.c │ ├── bn_mp_fwrite.c │ ├── bn_mp_gcd.c │ ├── bn_mp_get_double.c │ ├── bn_mp_get_i32.c │ ├── bn_mp_get_i64.c │ ├── bn_mp_get_l.c │ ├── bn_mp_get_ll.c │ ├── bn_mp_get_mag_u32.c │ ├── bn_mp_get_mag_u64.c │ ├── bn_mp_get_mag_ul.c │ ├── bn_mp_get_mag_ull.c │ ├── bn_mp_grow.c │ ├── bn_mp_incr.c │ ├── bn_mp_init.c │ ├── bn_mp_init_copy.c │ ├── bn_mp_init_i32.c │ ├── bn_mp_init_i64.c │ ├── bn_mp_init_l.c │ ├── bn_mp_init_ll.c │ ├── bn_mp_init_multi.c │ ├── bn_mp_init_set.c │ ├── bn_mp_init_size.c │ ├── bn_mp_init_u32.c │ ├── bn_mp_init_u64.c │ ├── bn_mp_init_ul.c │ ├── bn_mp_init_ull.c │ ├── bn_mp_invmod.c │ ├── bn_mp_is_square.c │ ├── bn_mp_iseven.c │ ├── bn_mp_isodd.c │ ├── bn_mp_kronecker.c │ ├── bn_mp_lcm.c │ ├── bn_mp_log_u32.c │ ├── bn_mp_lshd.c │ ├── bn_mp_mod.c │ ├── bn_mp_mod_2d.c │ ├── bn_mp_mod_d.c │ ├── bn_mp_montgomery_calc_normalization.c │ ├── bn_mp_montgomery_reduce.c │ ├── bn_mp_montgomery_setup.c │ ├── bn_mp_mul.c │ ├── bn_mp_mul_2.c │ ├── bn_mp_mul_2d.c │ ├── bn_mp_mul_d.c │ ├── bn_mp_mulmod.c │ ├── bn_mp_neg.c │ ├── bn_mp_or.c │ ├── bn_mp_pack.c │ ├── bn_mp_pack_count.c │ ├── bn_mp_prime_fermat.c │ ├── bn_mp_prime_frobenius_underwood.c │ ├── bn_mp_prime_is_prime.c │ ├── bn_mp_prime_miller_rabin.c │ ├── bn_mp_prime_next_prime.c │ ├── bn_mp_prime_rabin_miller_trials.c │ ├── bn_mp_prime_rand.c │ ├── bn_mp_prime_strong_lucas_selfridge.c │ ├── bn_mp_radix_size.c │ ├── bn_mp_radix_smap.c │ ├── bn_mp_rand.c │ ├── bn_mp_read_radix.c │ ├── bn_mp_reduce.c │ ├── bn_mp_reduce_2k.c │ ├── bn_mp_reduce_2k_l.c │ ├── bn_mp_reduce_2k_setup.c │ ├── bn_mp_reduce_2k_setup_l.c │ ├── bn_mp_reduce_is_2k.c │ ├── bn_mp_reduce_is_2k_l.c │ ├── bn_mp_reduce_setup.c │ ├── bn_mp_root_u32.c │ ├── bn_mp_rshd.c │ ├── bn_mp_sbin_size.c │ ├── bn_mp_set.c │ ├── bn_mp_set_double.c │ ├── bn_mp_set_i32.c │ ├── bn_mp_set_i64.c │ ├── bn_mp_set_l.c │ ├── bn_mp_set_ll.c │ ├── bn_mp_set_u32.c │ ├── bn_mp_set_u64.c │ ├── bn_mp_set_ul.c │ ├── bn_mp_set_ull.c │ ├── bn_mp_shrink.c │ ├── bn_mp_signed_rsh.c │ ├── bn_mp_sqr.c │ ├── bn_mp_sqrmod.c │ ├── bn_mp_sqrt.c │ ├── bn_mp_sqrtmod_prime.c │ ├── bn_mp_sub.c │ ├── bn_mp_sub_d.c │ ├── bn_mp_submod.c │ ├── bn_mp_to_radix.c │ ├── bn_mp_to_sbin.c │ ├── bn_mp_to_ubin.c │ ├── bn_mp_ubin_size.c │ ├── bn_mp_unpack.c │ ├── bn_mp_xor.c │ ├── bn_mp_zero.c │ ├── bn_prime_tab.c │ ├── bn_s_mp_add.c │ ├── bn_s_mp_balance_mul.c │ ├── bn_s_mp_exptmod.c │ ├── bn_s_mp_exptmod_fast.c │ ├── bn_s_mp_get_bit.c │ ├── bn_s_mp_invmod_fast.c │ ├── bn_s_mp_invmod_slow.c │ ├── bn_s_mp_karatsuba_mul.c │ ├── bn_s_mp_karatsuba_sqr.c │ ├── bn_s_mp_montgomery_reduce_fast.c │ ├── bn_s_mp_mul_digs.c │ ├── bn_s_mp_mul_digs_fast.c │ ├── bn_s_mp_mul_high_digs.c │ ├── bn_s_mp_mul_high_digs_fast.c │ ├── bn_s_mp_prime_is_divisible.c │ ├── bn_s_mp_rand_jenkins.c │ ├── bn_s_mp_rand_platform.c │ ├── bn_s_mp_reverse.c │ ├── bn_s_mp_sqr.c │ ├── bn_s_mp_sqr_fast.c │ ├── bn_s_mp_sub.c │ ├── bn_s_mp_toom_mul.c │ ├── bn_s_mp_toom_sqr.c │ ├── changes.txt │ ├── demo │ ├── mtest_opponent.c │ ├── shared.c │ ├── shared.h │ ├── test.c │ └── timing.c │ ├── doc │ ├── bn.tex │ └── makefile │ ├── etc │ ├── 2kprime.1 │ ├── 2kprime.c │ ├── drprime.c │ ├── drprimes.28 │ ├── drprimes.txt │ ├── makefile │ ├── makefile.icc │ ├── makefile.msvc │ ├── mersenne.c │ ├── mont.c │ ├── pprime.c │ ├── prime.1024 │ ├── prime.512 │ ├── timer.asm │ ├── tune.c │ └── tune_it.sh │ ├── gen.pl │ ├── helper.pl │ ├── libtommath.pc.in │ ├── libtommath_VS2008.sln │ ├── libtommath_VS2008.vcproj │ ├── logs │ ├── Makefile │ ├── README │ ├── add.log │ ├── addsub.png │ ├── expt.log │ ├── expt.png │ ├── expt_2k.log │ ├── expt_2kl.log │ ├── expt_dr.log │ ├── graphs.dem │ ├── index.html │ ├── invmod.log │ ├── invmod.png │ ├── mult.log │ ├── mult.png │ ├── mult_kara.log │ ├── sqr.log │ ├── sqr_kara.log │ └── sub.log │ ├── makefile │ ├── makefile.mingw │ ├── makefile.msvc │ ├── makefile.shared │ ├── makefile.unix │ ├── makefile_include.mk │ ├── mtest │ ├── logtab.h │ ├── mpi-config.h │ ├── mpi-types.h │ ├── mpi.c │ ├── mpi.h │ └── mtest.c │ ├── testme.sh │ ├── tommath.def │ ├── tommath.h │ ├── tommath_class.h │ ├── tommath_cutoffs.h │ ├── tommath_private.h │ └── tommath_superclass.h └── tools └── winds ├── libs ├── common.c ├── file.c ├── index.c ├── levenshtein.h ├── lock.c ├── metadata.c ├── package.c ├── semantic.c ├── system-calls.c └── util.c └── winds.c /.github/workflows/Release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: [create] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | arch: [64] 13 | 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Install ck 17 | run: sudo apt-get install libck-dev 18 | - name: make 19 | run: make 20 | - name: make install 21 | run: sudo make install 22 | - name: make test 23 | run: make test 24 | - name: make deb 25 | run: make deb 26 | 27 | - name: upload deb 28 | if: matrix.arch == '64' 29 | uses: actions/upload-artifact@v1 30 | with: 31 | name: cyclone-scheme deb 32 | path: DEBIAN/cyclone-scheme_0.36.0_amd64.deb 33 | 34 | -------------------------------------------------------------------------------- /.github/workflows/ci-macos-brew.yml: -------------------------------------------------------------------------------- 1 | name: MacOS Homebrew Build 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: macOS-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Install 13 | run: brew update; brew tap cyclone-scheme/cyclone ; brew -v install cyclone-scheme/cyclone/cyclone-bootstrap 14 | - name: test 15 | run: ls -d /usr/local/share/cyclone/* && ls /usr/local/share/cyclone/cyclone/* ; cd tests; cyclone unit-tests.scm ; ./unit-tests && winds && cyclone threading.scm && ./threading && cyclone base.scm && ./base && cyclone test.scm && ./test 16 | -------------------------------------------------------------------------------- /.github/workflows/ci-macos.yml: -------------------------------------------------------------------------------- 1 | name: MacOS Build 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: macos-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Install ck 13 | run: brew update; brew install -v concurrencykit; 14 | - name: make 15 | run: make 16 | - name: make install 17 | run: sudo make install 18 | - name: make test 19 | run: make test 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Ubuntu Linux Build 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | arch: [32, 64] 13 | 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Install ck 17 | run: sudo apt-get install libck-dev 18 | - name: make 19 | run: make 20 | - name: make install 21 | run: sudo make install 22 | - name: make test 23 | run: make test 24 | - name: make deb 25 | run: make deb 26 | 27 | #- name: upload deb 28 | # if: matrix.arch == '64' 29 | # uses: actions/upload-artifact@v1 30 | # with: 31 | # name: cyclone-scheme deb 32 | # path: DEBIAN/cyclone-scheme_0.15_amd64.deb 33 | 34 | -------------------------------------------------------------------------------- /.github/workflows/test-mac.yml: -------------------------------------------------------------------------------- 1 | name: Test MacOS 2 | 3 | on: [fork] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: macOS-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Install ck 13 | run: brew update; brew install -v concurrencykit; 14 | - name: make 15 | run: make PREFIX=tmp ; make PREFIX=tmp install 16 | - name: test 17 | run: ./cyclone -d unit-tests.scm ; make PREFIX=tmp test 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | -------------------------------------------------------------------------------- /DEBIAN/control: -------------------------------------------------------------------------------- 1 | Package: cyclone-scheme 2 | Version: 0.36.0 3 | Priority: optional 4 | Section: devel 5 | Maintainer: Justin Ethier 6 | Architecture: amd64 7 | Depends: libck-dev, make, gcc 8 | Description: A brand-new compiler that allows practical application development using R7RS Scheme. 9 | Homepage: http://justinethier.github.io/cyclone/ 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Justin Ethier 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.FreeBSD.md: -------------------------------------------------------------------------------- 1 | Instructions for building on FreeBSD 2 | ------------------------------------ 3 | 4 | ### Compiling from source 5 | 6 | #### Install dependencies. 7 | 8 | FreeBSD (as other BSDs) have a different `make` program we also need to install GNU Make. 9 | 10 | Run the following command: 11 | 12 | sudo pkg install libtommath concurrencykit gmake 13 | 14 | ### Build and install cyclone-bootstrap 15 | 16 | Run the following commands: 17 | 18 | 1. `git clone https://github.com/justinethier/cyclone-bootstrap.git` 19 | 2. `cd cyclone-bootstrap` 20 | 3. `./install.sh` 21 | 22 | You will have `cyclone` and `icyc` executables generated in the local directory and installed in /usr/local/bin! 23 | 24 | PS: Note that `install.sh` is already set to use `gmake` instead of `make` on FreeBSD 12. 25 | -------------------------------------------------------------------------------- /README.Mac.md: -------------------------------------------------------------------------------- 1 | Instructions for building on Mac 2 | -------------------------------- 3 | 4 | ### Installing via Homebrew 5 | 6 | This will download the latest release of cyclone-bootstrap, compile it from source, install it, 7 | and then use it to compile cyclone from source and install that. 8 | 9 | 1. Follow the instructions at https://brew.sh/ to install the homebrew package manager. 10 | 2. Run the following commands: 11 | - brew tap cyclone-scheme/cyclone 12 | - brew install cyclone-scheme/cyclone/cyclone-bootstrap 13 | 14 | 15 | ### Compiling from source 16 | 17 | #### Install Homebrew, tools, and dependencies. 18 | 19 | 1. Follow the instructions at https://brew.sh/ to install the homebrew package manager. 20 | 2. Run the following commands: 21 | - `brew install git gcc libtool` 22 | - `brew install libtommath ck` 23 | 24 | 25 | ### Build and install cyclone-bootstrap 26 | 27 | Run the following commands: 28 | 29 | 1. `git clone https://github.com/justinethier/cyclone-bootstrap.git` 30 | 2. `cd cyclone-bootstrap` 31 | 3. `make` 32 | 4. `make install` 33 | 5. `make test` 34 | 35 | You will have `cyclone` and `icyc` executables generated in the local directory and installed in /usr/local/bin! 36 | 37 | -------------------------------------------------------------------------------- /README.Windows.md: -------------------------------------------------------------------------------- 1 | Instructions for building on Windows using MSYS 2 | ----------------------------------------------- 3 | 4 | ### Install MSYS2 5 | 1. Download msys2 from http://www.msys2.org/ 6 | 2. Install msys2 to a location of your choice - say `C:\msys64` (the steps should work for 32bit too) 7 | 3. Run `C:\msys64\msys2.exe` 8 | 4. Run `pacman -Syu` (Hit 'Y' when asked - Proceed with installation?). Just close the terminal window if you get the warning about "terminating msys2 without returning to shell" - there is a [known issue with msys](https://github.com/StephanTLavavej/mingw-distro/issues/20). 9 | 10 | ### Install necessary tools in MSYS2 11 | 1. Run `C:\msys64\msys2.exe` and execute the following commands in the msys terminal: 12 | 2. `pacman -Su` (Hit 'Y' when asked - Proceed with installation?) 13 | 3. `pacman -S gcc make autoconf git` (Hit 'Y' when asked - Proceed with installation?) 14 | 15 | ### Build and install cyclone-bootstrap 16 | 1. Run the following commands: 17 | 2. `git clone https://github.com/justinethier/cyclone-bootstrap.git` 18 | 3. `cd cyclone-bootstrap` 19 | 4. `./setup.mysys.sh` - this will fetch and build all the dependencies. 20 | 21 | You will have `cyclone.exe` and `icyc.exe` generated at `/usr/local/bin`! 22 | -------------------------------------------------------------------------------- /README.wasm.md: -------------------------------------------------------------------------------- 1 | # WASM Build Instructions 2 | 3 | These instructions may be used to compile cyclone to web assembly (WASM) using emscripten. 4 | 5 | Note this is still a work in progress and things may not work out of the gate. It may also be necessary to do additional integration work to get things to work on the web. For example, we use wasm-terminal as a web-based REPL rather than a drop-in compiled version of `icyc`. 6 | 7 | Anyway, to build: 8 | 9 | * Get latest cyclone-bootstrap (this repo) 10 | * Install emscripten sdk: https://emscripten.org/docs/getting_started/downloads.html 11 | * Load emscripten environment: `source ~/Documents/emsdk/emsdk_env.sh` 12 | * Do ck-polyfill replacements. This is necessary as there were issues porting libck to WASM, so we completely replaced it with equivalent functionality that is slower and not necessarily atomic, but that works in a WASM environment: `emmake make -f Makefile-wasm polyfill` 13 | * Build libraries and the runtime: `emmake make -f Makefile-wasm` 14 | 15 | This is all automated by `build-wasm.sh`. 16 | -------------------------------------------------------------------------------- /build-wasm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Re-compile everything using EmScripten. This allows our libraries to be linked to Web Assembly programs. 3 | source ~/Documents/emsdk/emsdk_env.sh && emmake make -f Makefile-wasm polyfill && emmake make -f Makefile-wasm 4 | -------------------------------------------------------------------------------- /dist/PKGBUILD: -------------------------------------------------------------------------------- 1 | # Maintainer: Justin Ethier 2 | pkgname=cyclone-scheme 3 | pkgver=0.7 4 | pkgrel=2 5 | pkgdesc="A brand-new compiler that allows practical application development using R7RS Scheme" 6 | arch=('i686' 'x86_64') 7 | url="https://github.com/justinethier/cyclone-bootstrap" 8 | license=('MIT') 9 | groups=() 10 | depends=('libck' 'libtommath' 'gcc' 'make') 11 | makedepends=() 12 | optdepends=() 13 | provides=() 14 | conflicts=() 15 | replaces=() 16 | backup=() 17 | options=() 18 | install= 19 | changelog= 20 | source=($url/archive/v$pkgver.tar.gz) 21 | noextract=() 22 | #autofill using updpkgsums 23 | md5sums=('751a72f84918d75ded3e3b2f09ce50a5') 24 | 25 | build() { 26 | cd "cyclone-bootstrap-$pkgver" 27 | make libcyclone.a PREFIX=/usr CFLAGS="-O2 -fPIC -rdynamic -Wall -Iinclude -L." LDFLAGS="-L. -Wl,--export-dynamic" 28 | make cyclone PREFIX=/usr CFLAGS="-O2 -fPIC -rdynamic -Wall -Iinclude -L." LDFLAGS="-L. -Wl,--export-dynamic" 29 | make icyc-c PREFIX=/usr CFLAGS="-O2 -fPIC -rdynamic -Wall -Iinclude -L." LDFLAGS="-L. -Wl,--export-dynamic" 30 | } 31 | 32 | package() { 33 | cd "cyclone-bootstrap-$pkgver" 34 | make DESTDIR="$pkgdir" PREFIX=/usr CFLAGS="-O2 -fPIC -rdynamic -Wall -Iinclude -L." LDFLAGS="-L. -Wl,--export-dynamic" 35 | make DESTDIR="$pkgdir" PREFIX=/usr install 36 | } 37 | -------------------------------------------------------------------------------- /dist/PKGBUILD-master: -------------------------------------------------------------------------------- 1 | # Maintainer: Justin Ethier 2 | # A version of PKGBUILD to get the latest snapshot from github. Thanks wasamasa! 3 | pkgname=cyclone-scheme-git 4 | pkgver=v0.6.2.r14.gecc9c32 5 | pkgrel=1 6 | pkgdesc="A brand-new compiler that allows practical application development using R7RS Scheme" 7 | arch=('i686' 'x86_64') 8 | url="https://github.com/justinethier/cyclone-bootstrap" 9 | license=('MIT') 10 | groups=() 11 | depends=('libck' 'libtommath' 'gcc' 'make') 12 | makedepends=() 13 | optdepends=() 14 | provides=(cyclone-scheme) 15 | conflicts=(cyclone-scheme) 16 | source=('git+https://github.com/justinethier/cyclone-bootstrap.git') 17 | md5sums=('SKIP') 18 | 19 | pkgver() { 20 | cd cyclone-bootstrap 21 | git describe --long | sed 's/\([^-]*-g\)/r\1/;s/-/./g' 22 | } 23 | 24 | build() { 25 | cd cyclone-bootstrap 26 | make DESTDIR="$pkgdir" PREFIX=/usr CFLAGS="-O2 -fPIC -rdynamic -Wall -Iinclude -L." 27 | } 28 | 29 | package() { 30 | cd cyclone-bootstrap 31 | make DESTDIR="$pkgdir" PREFIX=/usr install 32 | } 33 | -------------------------------------------------------------------------------- /dist/notes.txt: -------------------------------------------------------------------------------- 1 | git clone https://aur.archlinux.org/libck.git 2 | cd libck/ 3 | makepkg libck 4 | sudo pacman -U libck-0.5.0-1-x86_64.pkg.tar.xz 5 | 6 | # To repackage a new cyclone build 7 | updpkgsums 8 | makepkg --printsrcinfo > .SRCINFO 9 | -------------------------------------------------------------------------------- /include/cyclone/hashset.h: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 | /* 3 | * Copyright 2012 Couchbase, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef HASHSET_H 19 | #define HASHSET_H 1 20 | 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | struct hashset_st { 28 | size_t nbits; 29 | size_t mask; 30 | 31 | size_t capacity; 32 | size_t *items; 33 | size_t nitems; 34 | size_t n_deleted_items; 35 | }; 36 | 37 | typedef struct hashset_st *hashset_t; 38 | 39 | /* create hashset instance */ 40 | hashset_t hashset_create(void); 41 | 42 | /* destroy hashset instance */ 43 | void hashset_destroy(hashset_t set); 44 | 45 | size_t hashset_num_items(hashset_t set); 46 | 47 | /* add item into the hashset. 48 | * 49 | * @note 0 and 1 is special values, meaning nil and deleted items. the 50 | * function will return -1 indicating error. 51 | * 52 | * returns zero if the item already in the set and non-zero otherwise 53 | */ 54 | int hashset_add(hashset_t set, void *item); 55 | 56 | /* remove item from the hashset 57 | * 58 | * returns non-zero if the item was removed and zero if the item wasn't 59 | * exist 60 | */ 61 | int hashset_remove(hashset_t set, void *item); 62 | 63 | /* check if existence of the item 64 | * 65 | * returns non-zero if the item exists and zero otherwise 66 | */ 67 | int hashset_is_member(hashset_t set, void *item); 68 | 69 | void hashset_to_array(hashset_t set, void **items); 70 | 71 | #ifdef __cplusplus 72 | } 73 | #endif 74 | #endif 75 | -------------------------------------------------------------------------------- /include/cyclone/runtime-main.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Cyclone Scheme 3 | * Copyright (c) 2014, Justin Ethier 4 | * All rights reserved. 5 | * 6 | * This file contains the C runtime code used only by the main program module. 7 | */ 8 | 9 | #ifndef CYCLONE_RUNTIME_MAIN_H 10 | #define CYCLONE_RUNTIME_MAIN_H 11 | 12 | long global_stack_size = 0; 13 | long global_heap_size = 0; 14 | 15 | static void c_entry_pt(void *data, object clo, int argc, object * args); 16 | static void Cyc_heap_init(long heap_size); 17 | 18 | static void Cyc_heap_init(long heap_size) 19 | { 20 | /* Allocate heap area for second generation. */ 21 | #if DEBUG_SHOW_DIAG 22 | printf("main: Allocating and initializing heap...\n"); 23 | #endif 24 | gc_init_heap(heap_size); 25 | gc_start_collector(); 26 | } 27 | 28 | #endif /* CYCLONE_RUNTIME_MAIN_H */ 29 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Cyclone Scheme 3 | # http://github.com/justinethier/cyclone 4 | # Copyright (c) 2014, Justin Ethier 5 | # All rights reserved. 6 | # 7 | # This script incrementally builds and installs Cyclone Scheme from scratch. 8 | # Multiple steps are required because of the required dependencies along the way. 9 | # For example, the compiler depends upon the runtime library, the interpreter 10 | # is built by the compiler, etc. 11 | # 12 | # USAGE: 13 | # 14 | # Run without arguments to install in the default location (/usr/local): 15 | # ./install.sh 16 | # 17 | # or include an argument to specify the install directory, EG: 18 | # ./install.sh /home/my-username 19 | # 20 | # NOTES: 21 | # 22 | # - This script requires superuser permissions and will prompt for them as needed. 23 | # - If you install to a custom location, you may need to add the bin, include, and lib 24 | # directories to your PATH in order to run cyclone and complete the installation. 25 | # 26 | 27 | if [ $# -gt 0 ] 28 | then 29 | PREFIX="PREFIX=$1" 30 | else 31 | PREFIX="" 32 | fi 33 | 34 | if [ $(uname) = 'FreeBSD' ]; then 35 | MAKE=gmake 36 | else 37 | MAKE=make 38 | fi 39 | 40 | $MAKE $PREFIX && \ 41 | $MAKE $PREFIX test && \ 42 | sudo $MAKE $PREFIX install 43 | -------------------------------------------------------------------------------- /libs/test-atoms.scm: -------------------------------------------------------------------------------- 1 | (import 2 | (scheme base) 3 | (scheme write) 4 | (cyclone concurrency) 5 | ) 6 | 7 | (define lis '(1 2)) 8 | (define a (make-atom lis)) 9 | (write 10 | (list 11 | a 12 | (ref a) 13 | (compare-and-set! a 1 lis) 14 | (ref a) 15 | (compare-and-set! a lis 1) 16 | (ref a) 17 | )) 18 | (newline) 19 | (write 20 | (list 21 | (make-shared '(1 (2 (3)))) 22 | (make-shared 1) 23 | (make-shared 1.0) 24 | (make-shared "this is a string") 25 | (make-shared #(a b c d)) 26 | (make-shared #u8(1 2 3 4)) 27 | )) 28 | -------------------------------------------------------------------------------- /libs/test-threads.scm: -------------------------------------------------------------------------------- 1 | ;;;; A simple example of using a condition variable to simulate thread-join 2 | (import (scheme base) 3 | (scheme read) 4 | (scheme write) 5 | (srfi 18)) 6 | 7 | ;(define cv (make-condition-variable)) 8 | ;(define m (make-mutex)) 9 | 10 | ;; Thread - Do something, then let main thread know when we are done 11 | (define t 12 | (make-thread 13 | (lambda () 14 | (display "started thread") 15 | (newline) 16 | (thread-sleep! 3) 17 | (display "thread done") 18 | (newline) 19 | ;(thread-terminate!) 20 | ))) 21 | 22 | (define t2 23 | (make-thread 24 | (lambda () 25 | (display "started thread2") 26 | (newline) 27 | (thread-sleep! 4) 28 | (display "thread2 done") 29 | (newline) 30 | ;(thread-terminate!) 31 | ))) 32 | (thread-start! t) 33 | (thread-start! t2) 34 | 35 | (write (thread? t)) 36 | (write (thread? t2)) 37 | (newline) 38 | ;; Main thread - wait for thread to broadcast it is done 39 | (thread-join! t) 40 | (thread-join! t2) 41 | (display "main thread done") 42 | (newline) 43 | -------------------------------------------------------------------------------- /scheme/case-lambda.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; A case-lambda implementation, based off the one from chibi scheme. 5 | ;;;; 6 | (define-library (scheme case-lambda) 7 | (import (scheme base)) 8 | (export 9 | case-lambda 10 | ; %case 11 | ) 12 | (begin 13 | 14 | (define-syntax %case 15 | (syntax-rules () 16 | ((%case args len n p ((params ...) . body) . rest) 17 | (if (= len (length '(params ...))) 18 | (apply (lambda (params ...) . body) args) 19 | (%case args len 0 () . rest))) 20 | ((%case args len n (p ...) ((x . y) . body) . rest) 21 | (%case args len (+ n 1) (p ... x) (y . body) . rest)) 22 | ((%case args len n (p ...) (y . body) . rest) 23 | (if (>= len n) 24 | (apply (lambda (p ... . y) . body) args) 25 | (%case args len 0 () . rest))) 26 | ((%case args len n p) 27 | (error "case-lambda: no cases matched")))) 28 | (define-syntax case-lambda 29 | (syntax-rules () 30 | ((case-lambda . clauses) 31 | (lambda args (let ((len (length args))) (%case args len 0 () . clauses)))))) 32 | ;(lambda args (let ((len (length* args))) (%case args len 0 () . clauses)))))) 33 | )) 34 | 35 | -------------------------------------------------------------------------------- /scheme/complex.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the complex library from r7rs. 8 | ;;;; 9 | (define-library (scheme complex) 10 | (export 11 | angle 12 | imag-part 13 | magnitude 14 | make-polar 15 | make-rectangular 16 | real-part 17 | ) 18 | (inline 19 | real-part 20 | imag-part) 21 | (import (scheme base) 22 | (scheme inexact)) 23 | (begin 24 | (define (real-part x) 25 | (if (complex? x) 26 | (%real-part x) 27 | x)) 28 | 29 | (define (imag-part x) 30 | (if (complex? x) 31 | (%imag-part x) 32 | 0)) 33 | 34 | (define (magnitude z) 35 | (sqrt (+ (* (real-part z) (real-part z)) 36 | (* (imag-part z) (imag-part z))))) 37 | 38 | (define (angle z) (atan (imag-part z) (real-part z))) 39 | 40 | (define (make-polar r phi) 41 | (make-rectangular (* r (cos phi)) (* r (sin phi)))) 42 | 43 | (define (make-rectangular x y) 44 | (%make-rect x y)) 45 | 46 | (define-c %real-part 47 | "(void *data, int argc, closure _, object k, object z)" 48 | " make_double(d, creal(complex_num_value(z))); 49 | return_closcall1(data, k, &d); ") 50 | 51 | (define-c %imag-part 52 | "(void *data, int argc, closure _, object k, object z)" 53 | " make_double(d, cimag(complex_num_value(z))); 54 | return_closcall1(data, k, &d); ") 55 | 56 | (define-c %make-rect 57 | "(void *data, int argc, closure _, object k, object r, object i)" 58 | " Cyc_check_num(data, r); 59 | Cyc_check_num(data, i); 60 | Cyc_make_rectangular(data, k, r, i); ") 61 | )) 62 | -------------------------------------------------------------------------------- /scheme/cyclone/common.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains definitions used by the compiler and interpreter. 8 | ;;;; 9 | (define-library (scheme cyclone common) 10 | (export 11 | *Cyc-version-banner* 12 | *version* 13 | *version-number* 14 | *version-name* 15 | *version-banner* 16 | *c-file-header-comment* 17 | *reader-source-db* 18 | memloc 19 | ) 20 | (begin 21 | (define *version-number* "0.37.0") 22 | (define *version-name* "") 23 | (define *version* (string-append *version-number* " " *version-name* "")) 24 | 25 | (define *version-banner* 26 | (string-append " 27 | :@ 28 | @@@ 29 | @@@@: 30 | `@@@@@+ 31 | .@@@+@@@ 32 | @@ @@ Cyclone Scheme->C compiler 33 | ,@ http://justinethier.github.io/cyclone/ 34 | '@ 35 | .@ (c) 2014-2025 Justin Ethier 36 | @@ #@ Version " *version* " 37 | `@@@#@@@. 38 | #@@@@@ 39 | +@@@+ 40 | @@# 41 | `@. 42 | 43 | ")) 44 | 45 | (define *Cyc-version-banner* *version-banner*) 46 | 47 | (define *c-file-header-comment* 48 | (string-append "/** 49 | ** This file was automatically generated by the Cyclone scheme compiler 50 | ** http://justinethier.github.io/cyclone/ 51 | ** 52 | ** (c) 2014-2024 Justin Ethier 53 | ** Version " *version* " 54 | ** 55 | **/ 56 | ")) 57 | 58 | (define *reader-source-db* '()) 59 | 60 | (define-c memloc 61 | "(void *data, int argc, closure _, object k, object obj)" 62 | " char str[32]; 63 | sprintf(str, \"%p\", obj); 64 | make_utf8_string(data, s, str); 65 | return_closcall1(data, k, &s);") 66 | 67 | )) 68 | -------------------------------------------------------------------------------- /scheme/cyclone/macros.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains code to deal with macros. 8 | ;;;; 9 | (define-library (scheme cyclone macros) 10 | (import (scheme base) 11 | ;(scheme write) ;; Debug only 12 | (scheme eval) 13 | (scheme cyclone util) 14 | ) 15 | (export 16 | ) 17 | (inline 18 | macro:macro?) 19 | (begin 20 | )) 21 | -------------------------------------------------------------------------------- /scheme/file.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the file library from r7rs. 8 | ;;;; 9 | (define-library (scheme file) 10 | (export 11 | call-with-input-file 12 | call-with-output-file 13 | ;delete-file 14 | ;file-exists? 15 | ;open-binary-input-file 16 | ;open-binary-output-file 17 | ;open-input-file 18 | ;open-output-file 19 | with-input-from-file 20 | with-output-to-file 21 | ) 22 | (import (scheme base)) 23 | (begin 24 | (define (call-with-input-file string proc) 25 | (call-with-port (open-input-file string) proc)) 26 | (define (call-with-output-file string proc) 27 | (call-with-port (open-output-file string) proc)) 28 | (define (with-input-from-file string thunk) 29 | ;; Have to do this the long way since parameterize is not available 30 | (let ((old (current-input-port)) 31 | (new ((current-input-port ') (open-input-file string)))) 32 | (dynamic-wind 33 | (lambda () (current-input-port ' new)) 34 | thunk 35 | (lambda () 36 | (close-port (current-input-port)) 37 | (current-input-port ' old))))) 38 | (define (with-output-to-file string thunk) 39 | ;; Have to do this the long way since parameterize is not available 40 | (let ((old (current-output-port)) 41 | (new ((current-output-port ') (open-output-file string)))) 42 | (dynamic-wind 43 | (lambda () (current-output-port ' new)) 44 | thunk 45 | (lambda () 46 | (close-port (current-output-port)) 47 | (current-output-port ' old))))) 48 | )) 49 | -------------------------------------------------------------------------------- /scheme/lazy.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the lazy library from r7rs. 8 | ;;;; 9 | (define-library (scheme lazy) 10 | (import (scheme base)) 11 | (export 12 | delay 13 | force 14 | delay-force 15 | make-promise 16 | promise? 17 | ) 18 | (begin 19 | 20 | ;; promise 21 | ;; ( tag value/obj ) 22 | 23 | (define *promise-tag* '(promise)) 24 | (define (promise? obj) 25 | (and (pair? obj) 26 | (eq? *promise-tag* (car obj)))) 27 | 28 | (define force 29 | (lambda (obj) 30 | (if (promise? obj) 31 | (force ((cdr obj))) 32 | obj))) 33 | 34 | (define-syntax delay 35 | (er-macro-transformer 36 | (lambda (expr rename compare) 37 | `(make-promise (lambda () ,(cadr expr)))))) 38 | 39 | (define-syntax delay-force 40 | (er-macro-transformer 41 | (lambda (expr rename compare) 42 | `(make-promise (lambda () ,(cadr expr)))))) 43 | 44 | (define make-promise 45 | (lambda (obj) 46 | (if (promise? obj) 47 | obj 48 | (let ((result-ready? #f) 49 | (result #f)) 50 | (cons 51 | *promise-tag* 52 | (lambda () 53 | (if result-ready? 54 | result 55 | (let ((x (if (procedure? obj) 56 | (obj) 57 | obj))) 58 | (if result-ready? 59 | result 60 | (begin (set! result x) 61 | (set! result-ready? #t) 62 | result)))))))))) 63 | 64 | )) 65 | -------------------------------------------------------------------------------- /scheme/load.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the load library from r7rs. 8 | ;;;; 9 | (define-library (scheme load) 10 | (export 11 | load) 12 | (import (scheme base) 13 | (scheme eval) 14 | (scheme file) 15 | (scheme read)) 16 | (begin 17 | (define (load filename . env) 18 | (let ((exprs (call-with-input-file filename 19 | (lambda (port) 20 | (read-all/source port filename))))) 21 | (for-each 22 | (lambda (expr) 23 | (apply eval (cons expr env))) 24 | exprs))))) 25 | -------------------------------------------------------------------------------- /scheme/process-context.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the process-context r7rs. 8 | ;;;; 9 | (define-library (scheme process-context) 10 | (export 11 | command-line 12 | ;exit - not needed because already defined as a primitive, at least for now 13 | emergency-exit 14 | get-environment-variable 15 | get-environment-variables 16 | ) 17 | (begin 18 | (define emergency-exit exit) 19 | (define-c command-line 20 | "(void *data, int argc, closure _, object k)" 21 | ;; TODO: consolidate with Cyc_command_line_arguments from runtime.c 22 | " int i; 23 | object lis = NULL; 24 | for (i = _cyc_argc; i > 0; i--) { 25 | object ps = alloca(sizeof(string_type)); 26 | object pl = alloca(sizeof(pair_type)); 27 | make_utf8_string(data, s, _cyc_argv[i - 1]); 28 | memcpy(ps, &s, sizeof(string_type)); 29 | ((list)pl)->hdr.mark = gc_color_red; 30 | ((list)pl)->hdr.grayed = 0; 31 | ((list)pl)->hdr.immutable = 0; 32 | ((list)pl)->tag = pair_tag; 33 | ((list)pl)->pair_car = ps; 34 | ((list)pl)->pair_cdr = lis; 35 | lis = pl; 36 | } 37 | return_closcall1(data, k, lis); ") 38 | (define-c get-environment-variable 39 | "(void *data, int argc, closure _, object k, object env_var)" 40 | ;; TODO: consolidate with Cyc_command_line_arguments from runtime.c 41 | " 42 | const char *v = NULL; 43 | Cyc_check_str(data, env_var); 44 | v = getenv(string_str(env_var)); 45 | if (v == NULL) { 46 | return_closcall1(data, k, boolean_f); 47 | } else { 48 | make_utf8_string(data, str, v); 49 | return_closcall1(data, k, &str); 50 | } 51 | ") 52 | (define-c get-environment-variables 53 | "(void *data, int argc, closure _, object k)" 54 | "pack_env_variables(data, k); 55 | ") 56 | )) 57 | -------------------------------------------------------------------------------- /scheme/repl.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the repl library from r7rs. 8 | ;;;; 9 | (define-library (scheme repl) 10 | (export 11 | interaction-environment 12 | repl) 13 | (import (scheme base) 14 | (scheme eval) 15 | (scheme read) 16 | (scheme write)) 17 | (begin 18 | (define (interaction-environment) 19 | (setup-environment)) 20 | (define (repl) 21 | (with-handler 22 | (lambda (obj) 23 | (cond 24 | ((error-object? obj) 25 | (display "Error: ") 26 | (display (error-object-message obj)) 27 | (if (not (null? (error-object-irritants obj))) 28 | (display ": ")) 29 | (for-each 30 | (lambda (o) 31 | (write o) 32 | (display " ")) 33 | (error-object-irritants obj))) 34 | (else 35 | (display "Error: ") 36 | (display obj))) 37 | (newline) 38 | (repl)) 39 | (display "cyclone> ") 40 | (flush-output-port) 41 | (let ((obj (read))) 42 | (if (eof-object? obj) 43 | (newline) ;; Quick way to exit REPL 44 | (let ((c (eval obj))) 45 | (if (eof-object? c) 46 | (display "") 47 | (write c)) 48 | (newline) 49 | (repl)))))))) 50 | -------------------------------------------------------------------------------- /scheme/time.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the time library from r7rs. 8 | ;;;; 9 | (define-library (scheme time) 10 | (export 11 | current-second 12 | current-jiffy 13 | jiffies-per-second) 14 | (include-c-header "") 15 | (import (scheme base)) 16 | (begin 17 | (define-c current-second 18 | "(void *data, int argc, closure _, object k)" 19 | " make_double(box, 0.0); 20 | time_t t = time(NULL); 21 | double_value(&box) = t; 22 | return_closcall1(data, k, &box); ") 23 | (define-c current-jiffy 24 | "(void *data, int argc, closure _, object k)" 25 | " struct timespec now; 26 | make_double(box, 0.0); 27 | clock_gettime(CLOCK_MONOTONIC, &now); 28 | long long jiffy = (now.tv_sec)*1000000LL + now.tv_nsec/1000; // nano->microseconds 29 | double_value(&box) = jiffy; 30 | return_closcall1(data, k, &box); ") 31 | (define-c jiffies-per-second 32 | "(void *data, int argc, closure _, object k)" 33 | " int n = 1000000; 34 | object obj = obj_int2obj(n); 35 | return_closcall1(data, k, obj); "))) 36 | -------------------------------------------------------------------------------- /scheme/write.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains the write library from r7rs. 8 | ;;;; 9 | (define-library (scheme write) 10 | (export 11 | display 12 | write 13 | write-shared 14 | write-simple) 15 | (import (scheme base)) 16 | (begin 17 | (define (display obj . port) 18 | (if (null? port) 19 | (Cyc-display obj (current-output-port)) 20 | (Cyc-display obj (car port)))) 21 | (define (write obj . port) 22 | (if (null? port) 23 | (Cyc-write obj (current-output-port)) 24 | (Cyc-write obj (car port)))) 25 | (define write-shared write) 26 | (define write-simple write) 27 | ) 28 | ) 29 | -------------------------------------------------------------------------------- /scripts/debug.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for ((n=0;n<100;n++)) 4 | do 5 | echo "Running cyclone $n" 6 | ./cyclone icyc.scm > debug.out 2>&1 7 | if [ $? -ne 0 ] 8 | then 9 | break 10 | fi 11 | done 12 | -------------------------------------------------------------------------------- /scripts/debug2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for ((n=0;n<100;n++)) 4 | do 5 | echo "Running cyclone $n" 6 | gdb -ex run --eval-command=quit --args ./cyclone icyc.scm 2>debug.out 7 | #gdb -ex run --args ./cyclone unit-tests.scm 2>debug.out 8 | if [ $? -ne 0 ] 9 | then 10 | break 11 | fi 12 | done 13 | -------------------------------------------------------------------------------- /scripts/timing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | time for i in {1..10}; do cyclone -d scheme/base.sld; done 3 | -------------------------------------------------------------------------------- /setup.mac.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | CYCLONE_BOOTSTRAP_SRC=$PWD 5 | 6 | # Root directory where everthing related 7 | # to cyclone-bootstrap will reside. 8 | export CYCLONE_ROOT=$HOME/CYCLONE_ROOT 9 | export INSTALL_DIR=$CYCLONE_ROOT/INSTALL 10 | export INSTALL_RELATIVE=/usr/local 11 | 12 | 13 | # Create the necessary directories 14 | mkdir $CYCLONE_ROOT $INSTALL_DIR 15 | 16 | # Build and install libtommath 17 | cd $CYCLONE_ROOT 18 | repo=https://github.com/libtom/libtommath.git 19 | echo "Cloning $repo" 20 | git clone $repo 21 | cd libtommath 22 | LIBPATH=$INSTALL_DIR/$INSTALL_RELATIVE/lib INCPATH=$INSTALL_DIR/$INSTALL_RELATIVE/include make 23 | LIBPATH=$INSTALL_DIR/$INSTALL_RELATIVE/lib INCPATH=$INSTALL_DIR/$INSTALL_RELATIVE/include make install 24 | 25 | 26 | # Build and install concurrencykit 27 | cd $CYCLONE_ROOT 28 | repo=https://github.com/concurrencykit/ck.git 29 | echo "Cloning $repo" 30 | git clone $repo 31 | cd ck 32 | ./configure 33 | make 34 | DESTDIR=$INSTALL_DIR make install 35 | 36 | 37 | # Get back to cyclone-bootstrap 38 | cd $CYCLONE_BOOTSTRAP_SRC 39 | cp -v Makefile.config.msys2 Makefile.config 40 | 41 | 42 | # Fixing up the Makefile - this is a temporary crude workaround 43 | perl -pi -e 's/-shared -rdynamic/-Wl,-undefined -Wl,dynamic_lookup -shared -rdynamic/' Makefile 44 | make 45 | DESTDIR=$INSTALL_DIR make install 46 | -------------------------------------------------------------------------------- /setup.mysys.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | CYCLONE_BOOTSTRAP_SRC=$PWD 5 | 6 | # Root directory where everthing related 7 | # to cyclone-bootstrap will reside. 8 | export CYCLONE_ROOT=$HOME/CYCLONE_ROOT 9 | export INSTALL_DIR=/ 10 | export INSTALL_RELATIVE=/usr/local 11 | 12 | # Create the necessary directories 13 | mkdir $CYCLONE_ROOT 14 | 15 | # Build and install libtommath 16 | cd $CYCLONE_ROOT 17 | repo=https://github.com/libtom/libtommath.git 18 | echo "Cloning $repo" 19 | git clone $repo 20 | cd libtommath 21 | LIBPATH=$INSTALL_RELATIVE/lib INCPATH=$INSTALL_RELATIVE/include make 22 | LIBPATH=$INSTALL_RELATIVE/lib INCPATH=$INSTALL_RELATIVE/include make install 23 | 24 | 25 | # Build and install concurrencykit 26 | cd $CYCLONE_ROOT 27 | repo=https://github.com/concurrencykit/ck.git 28 | echo "Cloning $repo" 29 | git clone $repo 30 | cd ck 31 | ./configure 32 | make 33 | DESTDIR=/ make install 34 | 35 | 36 | # Get back to cyclone-bootstrap 37 | cd $CYCLONE_BOOTSTRAP_SRC 38 | cp -v Makefile.config.msys2 Makefile.config 39 | 40 | 41 | # Fixing up the Makefile - this is a temporary crude workaround 42 | perl -pi -e 's/-shared /-Wl,-undefined -shared /' Makefile 43 | make 44 | make install 45 | make test 46 | -------------------------------------------------------------------------------- /srfi/1.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module is an interface to the list library. 8 | ;;;; 9 | (define-library (srfi 1) 10 | (import (scheme base) 11 | (scheme cxr)) 12 | (export 13 | xcons ;tree-copy 14 | make-list list-tabulate cons* list-copy 15 | proper-list? circular-list? dotted-list? not-pair? null-list? list= 16 | circular-list length+ 17 | iota 18 | first second third fourth fifth sixth seventh eighth ninth tenth 19 | car+cdr 20 | take drop 21 | take-right drop-right 22 | take! drop-right! 23 | split-at split-at! 24 | last last-pair 25 | zip unzip1 unzip2 unzip3 unzip4 unzip5 26 | count 27 | append! append-reverse append-reverse! concatenate concatenate! 28 | unfold fold pair-fold reduce 29 | unfold-right fold-right pair-fold-right reduce-right 30 | append-map append-map! map! pair-for-each filter-map map-in-order 31 | filter partition remove 32 | filter! partition! remove! 33 | find find-tail any every list-index 34 | take-while drop-while take-while! 35 | span break span! break! 36 | delete delete! 37 | alist-cons alist-copy 38 | delete-duplicates delete-duplicates! 39 | alist-delete alist-delete! 40 | reverse! 41 | lset<= lset= lset-adjoin 42 | lset-union lset-intersection lset-difference lset-xor lset-diff+intersection 43 | lset-union! lset-intersection! lset-difference! lset-xor! lset-diff+intersection! 44 | ) 45 | (inline 46 | tenth 47 | ninth 48 | eighth 49 | seventh 50 | sixth 51 | fifth 52 | not-pair? 53 | ) 54 | (include "1.scm") 55 | (begin) 56 | ) 57 | -------------------------------------------------------------------------------- /srfi/111.sld: -------------------------------------------------------------------------------- 1 | (define-library (srfi 111) 2 | (import (scheme base)) 3 | (export box box? unbox set-box!) 4 | (begin 5 | (define-record-type box-type 6 | (box value) 7 | box? 8 | (value unbox set-box!)))) 9 | -------------------------------------------------------------------------------- /srfi/113.sld: -------------------------------------------------------------------------------- 1 | (define-library (srfi 113) 2 | (import (scheme base)) 3 | (import (scheme case-lambda)) 4 | (import (scheme write)) 5 | (import (srfi 128)) ;(comparators)) 6 | (import (srfi 69)) 7 | 8 | (export set set-unfold) 9 | (export set? set-contains? set-empty? set-disjoint?) 10 | (export set-member set-element-comparator) 11 | (export set-adjoin set-adjoin! set-replace set-replace! 12 | set-delete set-delete! set-delete-all set-delete-all! set-search!) 13 | (export set-size set-find set-count set-any? set-every?) 14 | (export set-map set-for-each set-fold 15 | set-filter set-remove set-remove set-partition 16 | set-filter! set-remove! set-partition!) 17 | (export set-copy set->list list->set list->set!) 18 | (export set=? set? set<=? set>=?) 19 | (export set-union set-intersection set-difference set-xor 20 | set-union! set-intersection! set-difference! set-xor!) 21 | (export set-comparator) 22 | 23 | (export bag bag-unfold) 24 | (export bag? bag-contains? bag-empty? bag-disjoint?) 25 | (export bag-member bag-element-comparator) 26 | (export bag-adjoin bag-adjoin! bag-replace bag-replace! 27 | bag-delete bag-delete! bag-delete-all bag-delete-all! bag-search!) 28 | (export bag-size bag-find bag-count bag-any? bag-every?) 29 | (export bag-map bag-for-each bag-fold 30 | bag-filter bag-remove bag-partition 31 | bag-filter! bag-remove! bag-partition!) 32 | (export bag-copy bag->list list->bag list->bag!) 33 | (export bag=? bag? bag<=? bag>=?) 34 | (export bag-union bag-intersection bag-difference bag-xor 35 | bag-union! bag-intersection! bag-difference! bag-xor!) 36 | (export bag-comparator) 37 | 38 | 39 | (export bag-sum bag-sum! bag-product bag-product! 40 | bag-unique-size bag-element-count bag-for-each-unique bag-fold-unique 41 | bag-increment! bag-decrement! bag->set set->bag set->bag! 42 | bag->alist alist->bag) 43 | 44 | (include "sets/sets-impl.scm") 45 | ) 46 | -------------------------------------------------------------------------------- /srfi/117.sld: -------------------------------------------------------------------------------- 1 | (define-library (srfi 117) 2 | (import (scheme base) (scheme case-lambda)) 3 | (export make-list-queue list-queue list-queue-copy list-queue-unfold list-queue-unfold-right) 4 | (export list-queue? list-queue-empty?) 5 | (export list-queue-front list-queue-back list-queue-list list-queue-first-last) 6 | (export list-queue-add-front! list-queue-add-back! list-queue-remove-front! list-queue-remove-back!) 7 | (export list-queue-remove-all! list-queue-set-list!) 8 | (export list-queue-append list-queue-append! list-queue-concatenate) 9 | (export list-queue-append list-queue-append! list-queue-concatenate) 10 | (export list-queue-map list-queue-map! list-queue-for-each) 11 | (include "list-queues/list-queues-impl.scm") 12 | ) 13 | -------------------------------------------------------------------------------- /srfi/121.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2017, Justin Ethier 5 | ;;;; Copyright (c) 2017, Koz Ross 6 | ;;;; 7 | ;;;; This module is an interface to the Generators library. 8 | (define-library 9 | (srfi 121) 10 | (import 11 | (scheme base) 12 | (scheme case-lambda)) 13 | (export 14 | generator make-iota-generator make-range-generator 15 | make-coroutine-generator list->generator vector->generator 16 | reverse-vector->generator string->generator 17 | bytevector->generator 18 | make-for-each-generator make-unfold-generator) 19 | (export 20 | gcons* gappend gcombine gfilter gremove 21 | gtake gdrop gtake-while gdrop-while 22 | gdelete gdelete-neighbor-dups gindex gselect) 23 | (export 24 | generator->list generator->reverse-list 25 | generator->vector generator->vector! generator->string 26 | generator-fold generator-for-each generator-find 27 | generator-count generator-any generator-every generator-unfold) 28 | (include "121.scm")) 29 | -------------------------------------------------------------------------------- /srfi/128.sld: -------------------------------------------------------------------------------- 1 | (define-library (srfi 128) ;comparators) 2 | (import (scheme base)) 3 | (import (scheme case-lambda)) 4 | (import (scheme char) (scheme complex) (scheme inexact)) 5 | (inline 6 | boolean? <=? >=?) 19 | (export comparator-if<=>) 20 | (export %salt%) ;; Temporary workaround since we cannot preserve referential transparency for this 21 | (export comparator-max-in-list comparator-min-in-list 22 | comparator-max comparator-min 23 | default-comparator 24 | boolean-comparator 25 | real-comparator 26 | char-comparator 27 | char-ci-comparator 28 | string-comparator 29 | string-ci-comparator 30 | pair-comparator 31 | list-comparator 32 | vector-comparator 33 | eq-comparator 34 | eqv-comparator 35 | equal-comparator) 36 | (include "comparators/comparators-impl.scm") 37 | (include "comparators/default.scm") 38 | (include "comparators/162-impl.scm") 39 | ) 40 | -------------------------------------------------------------------------------- /srfi/132.sld: -------------------------------------------------------------------------------- 1 | (define-library (srfi 132) 2 | 3 | 4 | 5 | (import (except (scheme base) vector-copy vector-copy!) 6 | (rename (only (scheme base) vector-copy vector-copy! vector-fill!) 7 | (vector-copy r7rs-vector-copy) 8 | (vector-copy! r7rs-vector-copy!) 9 | (vector-fill! r7rs-vector-fill!) 10 | )) 11 | 12 | ; (import (scheme base)) 13 | (import (scheme cxr) 14 | (srfi 27)) 15 | (export list-sorted? vector-sorted? list-merge vector-merge list-sort vector-sort 16 | list-stable-sort vector-stable-sort list-merge! vector-merge! list-sort! vector-sort! 17 | list-stable-sort! vector-stable-sort! 18 | list-delete-neighbor-dups vector-delete-neighbor-dups 19 | list-delete-neighbor-dups! vector-delete-neighbor-dups! 20 | vector-find-median! vector-find-median 21 | vector-select! vector-select 22 | vector-separate! 23 | ) 24 | (include "sorting/delndups.scm") 25 | (include "sorting/lmsort.scm") 26 | (include "sorting/sortp.scm") 27 | (include "sorting/vector-util.scm") 28 | (include "sorting/vhsort.scm") 29 | (include "sorting/visort.scm") 30 | (include "sorting/vmsort.scm") 31 | (include "sorting/vqsort2.scm") 32 | (include "sorting/vqsort3.scm") 33 | (include "sorting/select.scm") 34 | (include "sorting/sort.scm") ; must be last 35 | ) 36 | -------------------------------------------------------------------------------- /srfi/133.sld: -------------------------------------------------------------------------------- 1 | (define-library (srfi 133) ;vectors) 2 | (import (scheme base)) 3 | (import (scheme cxr)) 4 | (inline 5 | unspecified-value 6 | between? 7 | nonneg-int? 8 | ) 9 | ;; Constructors 10 | (export vector-unfold vector-unfold-right vector-reverse-copy 11 | vector-concatenate vector-append-subvectors) 12 | ;; Predicates 13 | (export vector-empty? vector=) 14 | ;; Iteration 15 | (export vector-fold vector-fold-right vector-map! 16 | vector-count vector-cumulate) 17 | ;; Searching 18 | (export vector-index vector-index-right vector-skip vector-skip-right 19 | vector-binary-search vector-any vector-every vector-partition) 20 | ;; Mutators 21 | (export vector-swap! vector-reverse! 22 | vector-reverse-copy! vector-unfold! vector-unfold-right!) 23 | ;; Conversion 24 | (export reverse-vector->list reverse-list->vector) 25 | (include "vectors-impl.scm") 26 | ) 27 | -------------------------------------------------------------------------------- /srfi/2.meta: -------------------------------------------------------------------------------- 1 | ;; This file was automatically generated by the Cyclone Scheme compiler 2 | () -------------------------------------------------------------------------------- /srfi/2.scm: -------------------------------------------------------------------------------- 1 | ;;; 2 | ;;; husk-scheme 3 | ;;; http://justinethier.github.com/husk-scheme 4 | ;;; 5 | ;;; Implementation of SRFI-2: and-let* 6 | ;;; 7 | (define-syntax and-let* 8 | (syntax-rules () 9 | ; Special case 10 | ((and-let* ()) 11 | #t) 12 | ; No CLAWS, just body 13 | ((and-let* () body ...) 14 | (begin body ...)) 15 | ; Special cases of below - CLAWS with no body 16 | ((and-let* ((var expr)) ) 17 | (let ((var expr)) 18 | (and var))) 19 | ((and-let* ((expr))) 20 | (let ((tmp expr)) 21 | (and tmp ))) 22 | ((and-let* (expr)) 23 | (let ((tmp expr)) 24 | (and tmp ))) 25 | ; General case - CLAWS and body 26 | ((and-let* ((var expr) . rest) . body) 27 | (let ((var expr)) 28 | (and var (and-let* rest . body)))) 29 | ((and-let* ((expr) . rest) . body) 30 | (let ((tmp expr)) 31 | (and tmp (and-let* rest . body)))) 32 | ((and-let* (expr . rest) . body) 33 | (let ((tmp expr)) 34 | (and tmp (and-let* rest . body)))))) 35 | 36 | -------------------------------------------------------------------------------- /srfi/2.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2016, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module is an interface SRFI-2 and-let* 8 | ;;;; 9 | (define-library (srfi 2) 10 | (import (scheme base)) 11 | (export and-let*) 12 | (include "2.scm")) 13 | -------------------------------------------------------------------------------- /srfi/28.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2017, Koz Ross 5 | ;;;; 6 | ;;;; This module is an interface to the Basic Format Strings library. 7 | (define-library 8 | (srfi 28) 9 | (import 10 | (scheme base) 11 | (scheme write)) 12 | (export format) 13 | (include "28.scm")) 14 | 15 | -------------------------------------------------------------------------------- /srfi/60.sld: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2017, Justin Ethier 5 | ;;;; Copyright (c) 2017, Koz Ross 6 | ;;;; 7 | ;;;; This module is an interface to the Integers as Bits library. 8 | ;;;; 9 | ;;;; NOTE: This does not support arbitrary-precision numbers, as Cyclone 10 | ;;;; doesn't have them yet. This will need to be modified accordingly once 11 | ;;;; this support is provided. 12 | (define-library (srfi 60) 13 | (import 14 | (scheme base)) 15 | (export 16 | any-bits-set? arithmetic-shift ash 17 | bit-count bit-field bit-set? bitwise-and bitwise-if bitwise-ior 18 | bitwise-merge bitwise-not bitwise-xor booleans->integer 19 | copy-bit copy-bit-field 20 | first-set-bit 21 | integer-length integer->list 22 | list->integer log2-binary-factors logbit? logand logcount logior lognot 23 | logtest logxor 24 | reverse-bit-field rotate-bit-field) 25 | (include "60.scm") 26 | (begin) 27 | ) 28 | -------------------------------------------------------------------------------- /srfi/sorting/median.scm: -------------------------------------------------------------------------------- 1 | ;;;; Finding the median of a vector 2 | ;; This involves sorting the vector, which is why it's part 3 | ;; of this package. 4 | 5 | (define (vector-find-median < v knil . maybe-mean) 6 | (define mean (if (null? maybe-mean) 7 | (lambda (a b) (/ (+ a b) 2)) 8 | (car maybe-mean))) 9 | (define len (vector-length v)) 10 | (define newv (vector-sort < v)) 11 | (cond 12 | ((= len 0) knil) 13 | ((odd? len) (vector-ref newv (/ (- len 1) 2))) 14 | (else (mean 15 | (vector-ref newv (- (/ len 2) 1)) 16 | (vector-ref newv (/ len 2)))))) 17 | 18 | (define (vector-find-median! < v knil . maybe-mean) 19 | (define mean (if (null? maybe-mean) 20 | (lambda (a b) (/ (+ a b) 2)) 21 | (car maybe-mean))) 22 | (define len (vector-length v)) 23 | (define newv (vector-sort! < v)) 24 | (cond 25 | ((= len 0) knil) 26 | ((odd? len) (vector-ref newv (/ (- len 1) 2))) 27 | (else (mean 28 | (vector-ref newv (- (/ len 2) 1)) 29 | (vector-ref newv (/ len 2)))))) 30 | 31 | -------------------------------------------------------------------------------- /srfi/sorting/sort.scm: -------------------------------------------------------------------------------- 1 | ;;; The sort package -- general sort & merge procedures 2 | ;;; 3 | ;;; Copyright (c) 1998 by Olin Shivers. 4 | ;;; You may do as you please with this code, as long as you do not delete this 5 | ;;; notice or hold me responsible for any outcome related to its use. 6 | ;;; Olin Shivers 10/98. 7 | 8 | ;;; This file just defines the general sort API in terms of some 9 | ;;; algorithm-specific calls. 10 | 11 | (define (list-sort < l) ; Sort lists by converting to 12 | (let ((v (list->vector l))) ; a vector and sorting that. 13 | (vector-heap-sort! < v) 14 | (vector->list v))) 15 | 16 | (define list-sort! list-merge-sort!) 17 | 18 | (define list-stable-sort list-merge-sort) 19 | (define list-stable-sort! list-merge-sort!) 20 | 21 | (define vector-sort vector-quick-sort) 22 | (define vector-sort! vector-quick-sort!) 23 | 24 | (define vector-stable-sort vector-merge-sort) 25 | (define vector-stable-sort! vector-merge-sort!) 26 | 27 | -------------------------------------------------------------------------------- /srfi/sorting/sortfaster.scm: -------------------------------------------------------------------------------- 1 | ;;; SRFI 132 specifies these eight procedures. 2 | ;;; 3 | ;;; Benchmarking has shown that the (rnrs sorting) procedures 4 | ;;; are faster than the sorting procedures defined by SRFI 132's 5 | ;;; reference implementation, so the R6RS procedures are used here. 6 | ;;; 7 | ;;; This file is a plug-and-play alternative to sort.scm in the 8 | ;;; same directory. 9 | 10 | (define list-sort r6rs-list-sort) 11 | (define list-sort! r6rs-list-sort) 12 | (define list-stable-sort r6rs-list-sort) 13 | (define list-stable-sort! r6rs-list-sort) 14 | 15 | (define (vector-sort < v . rest) 16 | (cond ((null? rest) 17 | (r6rs-vector-sort < v)) 18 | ((null? (cdr rest)) 19 | (r6rs-vector-sort < (r7rs-vector-copy v (car rest)))) 20 | ((null? (cddr rest)) 21 | (r6rs-vector-sort < (r7rs-vector-copy v (car rest) (cadr rest)))) 22 | (else 23 | (error 'vector-sort 24 | "too many arguments" 25 | (cons < (cons v rest)))))) 26 | 27 | (define vector-stable-sort vector-sort) 28 | 29 | (define (vector-sort! < v . rest) 30 | (cond ((null? rest) 31 | (r6rs-vector-sort! < v)) 32 | ((null? (cdr rest)) 33 | (let* ((start (car rest)) 34 | (v2 (r7rs-vector-copy v start))) 35 | (r6rs-vector-sort! < v2) 36 | (r7rs-vector-copy! v start v2 0))) 37 | ((null? (cddr rest)) 38 | (let* ((start (car rest)) 39 | (end (cadr rest)) 40 | (v2 (r7rs-vector-copy v start end))) 41 | (r6rs-vector-sort! < v2) 42 | (r7rs-vector-copy! v start v2 0))) 43 | (else 44 | (error 'vector-sort! 45 | "too many arguments" 46 | (cons < (cons v rest)))))) 47 | 48 | (define vector-stable-sort! vector-sort!) 49 | 50 | -------------------------------------------------------------------------------- /srfi/sorting/sortp.scm: -------------------------------------------------------------------------------- 1 | ;;; The sort package -- sorted predicates 2 | ;;; Olin Shivers 10/98. 3 | ;;; 4 | ;;; (list-sorted? < lis) -> boolean 5 | ;;; (vector-sorted? < v [start end]) -> boolean 6 | 7 | (define (list-sorted? < list) 8 | (or (not (pair? list)) 9 | (let lp ((prev (car list)) (tail (cdr list))) 10 | (or (not (pair? tail)) 11 | (let ((next (car tail))) 12 | (and (not (< next prev)) 13 | (lp next (cdr tail)))))))) 14 | 15 | (define (vector-sorted? elt< v . maybe-start+end) 16 | (call-with-values 17 | (lambda () (vector-start+end v maybe-start+end)) 18 | (lambda (start end) 19 | (or (>= start end) ; Empty range 20 | (let lp ((i (+ start 1)) (vi-1 (vector-ref v start))) 21 | (or (>= i end) 22 | (let ((vi (vector-ref v i))) 23 | (and (not (elt< vi vi-1)) 24 | (lp (+ i 1) vi))))))))) 25 | 26 | ;;; Copyright and porting non-notices 27 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 28 | ;;; Give me a break. It's fifteen lines of code. I place this code in the 29 | ;;; public domain; help yourself. 30 | ;;; 31 | ;;; If your Scheme has a faster mechanism for handling optional arguments 32 | ;;; (e.g., Chez), you should definitely port over to it. Note that argument 33 | ;;; defaulting and error-checking are interleaved -- you don't have to 34 | ;;; error-check defaulted START/END args to see if they are fixnums that are 35 | ;;; legal vector indices for the corresponding vector, etc. 36 | -------------------------------------------------------------------------------- /srfi/sorting/vbinsearch.scm: -------------------------------------------------------------------------------- 1 | ;;; The sort package -- binary search -*- Scheme -*- 2 | ;;; Copyright (c) 1998 by Olin Shivers. 3 | ;;; This code is in the public domain. 4 | ;;; Olin Shivers 98/11 5 | 6 | ;;; Returns the index of the matching element. 7 | ;;; (vector-binary-search < car 4 '#((1 . one) (3 . three) 8 | ;;; (4 . four) (25 . twenty-five))) 9 | ;;; => 2 10 | 11 | (define (vector-binary-search key< elt->key key v . maybe-start+end) 12 | (call-with-values 13 | (lambda () (vector-start+end v maybe-start+end)) 14 | (lambda (start end) 15 | (let lp ((left start) (right end)) ; Search V[left,right). 16 | (and (< left right) 17 | (let* ((m (quotient (+ left right) 2)) 18 | (elt (vector-ref v m)) 19 | (elt-key (elt->key elt))) 20 | (cond ((key< key elt-key) (lp left m)) 21 | ((key< elt-key key) (lp (+ m 1) right)) 22 | (else m)))))))) 23 | 24 | (define (vector-binary-search3 compare v . maybe-start+end) 25 | (call-with-values 26 | (lambda () (vector-start+end v maybe-start+end)) 27 | (lambda (start end) 28 | (let lp ((left start) (right end)) ; Search V[left,right). 29 | (and (< left right) 30 | (let* ((m (quotient (+ left right) 2)) 31 | (sign (compare (vector-ref v m)))) 32 | (cond ((> sign 0) (lp left m)) 33 | ((< sign 0) (lp (+ m 1) right)) 34 | (else m)))))))) 35 | -------------------------------------------------------------------------------- /srfi/sorting/vector-util.scm: -------------------------------------------------------------------------------- 1 | ;;; This code is 2 | ;;; Copyright (c) 1998 by Olin Shivers. 3 | ;;; The terms are: You may do as you please with this code, as long as 4 | ;;; you do not delete this notice or hold me responsible for any outcome 5 | ;;; related to its use. 6 | ;;; 7 | ;;; Blah blah blah. Don't you think source files should contain more lines 8 | ;;; of code than copyright notice? 9 | 10 | (define (vector-portion-copy vec start end) 11 | (let* ((len (vector-length vec)) 12 | (new-len (- end start)) 13 | (new (make-vector new-len))) 14 | (do ((i start (+ i 1)) 15 | (j 0 (+ j 1))) 16 | ((= i end) new) 17 | (vector-set! new j (vector-ref vec i))))) 18 | 19 | (define (vector-copy vec) 20 | (vector-portion-copy vec 0 (vector-length vec))) 21 | 22 | (define (vector-portion-copy! target src start end) 23 | (let ((len (- end start))) 24 | (do ((i (- len 1) (- i 1)) 25 | (j (- end 1) (- j 1))) 26 | ((< i 0)) 27 | (vector-set! target i (vector-ref src j))))) 28 | 29 | (define (has-element list index) 30 | (cond 31 | ((zero? index) 32 | (if (pair? list) 33 | (values #t (car list)) 34 | (values #f #f))) 35 | ((null? list) 36 | (values #f #f)) 37 | (else 38 | (has-element (cdr list) (- index 1))))) 39 | 40 | (define (list-ref-or-default list index default) 41 | (call-with-values 42 | (lambda () (has-element list index)) 43 | (lambda (has? maybe) 44 | (if has? 45 | maybe 46 | default)))) 47 | 48 | (define (vector-start+end vector maybe-start+end) 49 | (let ((start (list-ref-or-default maybe-start+end 50 | 0 0)) 51 | (end (list-ref-or-default maybe-start+end 52 | 1 (vector-length vector)))) 53 | (values start end))) 54 | 55 | (define (vectors-start+end-2 vector-1 vector-2 maybe-start+end) 56 | (let ((start-1 (list-ref-or-default maybe-start+end 57 | 0 0)) 58 | (end-1 (list-ref-or-default maybe-start+end 59 | 1 (vector-length vector-1))) 60 | (start-2 (list-ref-or-default maybe-start+end 61 | 2 0)) 62 | (end-2 (list-ref-or-default maybe-start+end 63 | 3 (vector-length vector-2)))) 64 | (values start-1 end-1 65 | start-2 end-2))) 66 | -------------------------------------------------------------------------------- /tests/test.scm: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2021, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains unit tests for (cyclone test) 8 | ;;;; 9 | 10 | (import 11 | (scheme base) 12 | (cyclone test)) 13 | 14 | (test-group 15 | "assert" 16 | (test-assert #t) 17 | ) 18 | 19 | (test-group 20 | "not" 21 | (test-not #f) 22 | ) 23 | 24 | (test-exit) 25 | 26 | 27 | -------------------------------------------------------------------------------- /tests/threading.scm: -------------------------------------------------------------------------------- 1 | ;;;; Cyclone Scheme 2 | ;;;; https://github.com/justinethier/cyclone 3 | ;;;; 4 | ;;;; Copyright (c) 2014-2021, Justin Ethier 5 | ;;;; All rights reserved. 6 | ;;;; 7 | ;;;; This module contains unit tests for threading / SRFI 18. 8 | ;;;; 9 | 10 | (import 11 | (scheme base) 12 | (srfi 18) 13 | (cyclone test)) 14 | 15 | 16 | (test-group 17 | "thread-join!" 18 | (let ((t (thread-start! (make-thread (lambda () (expt 2 100)))))) 19 | (test (expt 2 100) (thread-join! t))) 20 | ) 21 | 22 | (test-exit) 23 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/LICENSE: -------------------------------------------------------------------------------- 1 | The LibTom license 2 | 3 | This is free and unencumbered software released into the public domain. 4 | 5 | Anyone is free to copy, modify, publish, use, compile, sell, or 6 | distribute this software, either in source code form or as a compiled 7 | binary, for any purpose, commercial or non-commercial, and by any 8 | means. 9 | 10 | In jurisdictions that recognize copyright laws, the author or authors 11 | of this software dedicate any and all copyright interest in the 12 | software to the public domain. We make this dedication for the benefit 13 | of the public at large and to the detriment of our heirs and 14 | successors. We intend this dedication to be an overt act of 15 | relinquishment in perpetuity of all present and future rights to this 16 | software under copyright law. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | For more information, please refer to 27 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.2.0-{build} 2 | branches: 3 | only: 4 | - master 5 | - develop 6 | - /^release/ 7 | - /^travis/ 8 | image: 9 | - Visual Studio 2019 10 | - Visual Studio 2017 11 | - Visual Studio 2015 12 | build_script: 13 | - cmd: >- 14 | if "Visual Studio 2019"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" 15 | if "Visual Studio 2017"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" 16 | if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 17 | if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64 18 | nmake -f makefile.msvc all 19 | test_script: 20 | - cmd: test.exe 21 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/astylerc: -------------------------------------------------------------------------------- 1 | # Artistic Style, see http://astyle.sourceforge.net/ 2 | # full documentation, see: http://astyle.sourceforge.net/astyle.html 3 | # 4 | # usage: 5 | # astyle --options=astylerc *.[ch] 6 | 7 | # Do not create backup, annonying in the times of git 8 | suffix=none 9 | 10 | ## Bracket Style Options 11 | style=kr 12 | 13 | ## Tab Options 14 | indent=spaces=3 15 | 16 | ## Bracket Modify Options 17 | 18 | ## Indentation Options 19 | min-conditional-indent=0 20 | 21 | ## Padding Options 22 | pad-header 23 | unpad-paren 24 | align-pointer=name 25 | 26 | ## Formatting Options 27 | break-after-logical 28 | max-code-length=120 29 | convert-tabs 30 | mode=c 31 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_cutoffs.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_CUTOFFS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | #ifndef MP_FIXED_CUTOFFS 7 | #include "tommath_cutoffs.h" 8 | int KARATSUBA_MUL_CUTOFF = MP_DEFAULT_KARATSUBA_MUL_CUTOFF, 9 | KARATSUBA_SQR_CUTOFF = MP_DEFAULT_KARATSUBA_SQR_CUTOFF, 10 | TOOM_MUL_CUTOFF = MP_DEFAULT_TOOM_MUL_CUTOFF, 11 | TOOM_SQR_CUTOFF = MP_DEFAULT_TOOM_SQR_CUTOFF; 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_2expt.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_2EXPT_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* computes a = 2**b 7 | * 8 | * Simple algorithm which zeroes the int, grows it then just sets one bit 9 | * as required. 10 | */ 11 | mp_err mp_2expt(mp_int *a, int b) 12 | { 13 | mp_err err; 14 | 15 | /* zero a as per default */ 16 | mp_zero(a); 17 | 18 | /* grow a to accomodate the single bit */ 19 | if ((err = mp_grow(a, (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) { 20 | return err; 21 | } 22 | 23 | /* set the used count of where the bit will go */ 24 | a->used = (b / MP_DIGIT_BIT) + 1; 25 | 26 | /* put the single bit in its place */ 27 | a->dp[b / MP_DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT); 28 | 29 | return MP_OKAY; 30 | } 31 | #endif 32 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_abs.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ABS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* b = |a| 7 | * 8 | * Simple function copies the input and fixes the sign to positive 9 | */ 10 | mp_err mp_abs(const mp_int *a, mp_int *b) 11 | { 12 | mp_err err; 13 | 14 | /* copy a to b */ 15 | if (a != b) { 16 | if ((err = mp_copy(a, b)) != MP_OKAY) { 17 | return err; 18 | } 19 | } 20 | 21 | /* force the sign of b to positive */ 22 | b->sign = MP_ZPOS; 23 | 24 | return MP_OKAY; 25 | } 26 | #endif 27 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_add.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ADD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* high level addition (handles signs) */ 7 | mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | mp_sign sa, sb; 10 | mp_err err; 11 | 12 | /* get sign of both inputs */ 13 | sa = a->sign; 14 | sb = b->sign; 15 | 16 | /* handle two cases, not four */ 17 | if (sa == sb) { 18 | /* both positive or both negative */ 19 | /* add their magnitudes, copy the sign */ 20 | c->sign = sa; 21 | err = s_mp_add(a, b, c); 22 | } else { 23 | /* one positive, the other negative */ 24 | /* subtract the one with the greater magnitude from */ 25 | /* the one of the lesser magnitude. The result gets */ 26 | /* the sign of the one with the greater magnitude. */ 27 | if (mp_cmp_mag(a, b) == MP_LT) { 28 | c->sign = sb; 29 | err = s_mp_sub(b, a, c); 30 | } else { 31 | c->sign = sa; 32 | err = s_mp_sub(a, b, c); 33 | } 34 | } 35 | return err; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_add_d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ADD_D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* single digit addition */ 7 | mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) 8 | { 9 | mp_err err; 10 | int ix, oldused; 11 | mp_digit *tmpa, *tmpc; 12 | 13 | /* grow c as required */ 14 | if (c->alloc < (a->used + 1)) { 15 | if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) { 16 | return err; 17 | } 18 | } 19 | 20 | /* if a is negative and |a| >= b, call c = |a| - b */ 21 | if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) { 22 | mp_int a_ = *a; 23 | /* temporarily fix sign of a */ 24 | a_.sign = MP_ZPOS; 25 | 26 | /* c = |a| - b */ 27 | err = mp_sub_d(&a_, b, c); 28 | 29 | /* fix sign */ 30 | c->sign = MP_NEG; 31 | 32 | /* clamp */ 33 | mp_clamp(c); 34 | 35 | return err; 36 | } 37 | 38 | /* old number of used digits in c */ 39 | oldused = c->used; 40 | 41 | /* source alias */ 42 | tmpa = a->dp; 43 | 44 | /* destination alias */ 45 | tmpc = c->dp; 46 | 47 | /* if a is positive */ 48 | if (a->sign == MP_ZPOS) { 49 | /* add digits, mu is carry */ 50 | mp_digit mu = b; 51 | for (ix = 0; ix < a->used; ix++) { 52 | *tmpc = *tmpa++ + mu; 53 | mu = *tmpc >> MP_DIGIT_BIT; 54 | *tmpc++ &= MP_MASK; 55 | } 56 | /* set final carry */ 57 | ix++; 58 | *tmpc++ = mu; 59 | 60 | /* setup size */ 61 | c->used = a->used + 1; 62 | } else { 63 | /* a was negative and |a| < b */ 64 | c->used = 1; 65 | 66 | /* the result is a single digit */ 67 | if (a->used == 1) { 68 | *tmpc++ = b - a->dp[0]; 69 | } else { 70 | *tmpc++ = b; 71 | } 72 | 73 | /* setup count so the clearing of oldused 74 | * can fall through correctly 75 | */ 76 | ix = 1; 77 | } 78 | 79 | /* sign always positive */ 80 | c->sign = MP_ZPOS; 81 | 82 | /* now zero to oldused */ 83 | MP_ZERO_DIGITS(tmpc, oldused - ix); 84 | mp_clamp(c); 85 | 86 | return MP_OKAY; 87 | } 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_addmod.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ADDMOD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* d = a + b (mod c) */ 7 | mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) 8 | { 9 | mp_err err; 10 | mp_int t; 11 | 12 | if ((err = mp_init(&t)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | if ((err = mp_add(a, b, &t)) != MP_OKAY) { 17 | goto LBL_ERR; 18 | } 19 | err = mp_mod(&t, c, d); 20 | 21 | LBL_ERR: 22 | mp_clear(&t); 23 | return err; 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_and.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_AND_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* two complement and */ 7 | mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | int used = MP_MAX(a->used, b->used) + 1, i; 10 | mp_err err; 11 | mp_digit ac = 1, bc = 1, cc = 1; 12 | mp_sign csign = ((a->sign == MP_NEG) && (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS; 13 | 14 | if (c->alloc < used) { 15 | if ((err = mp_grow(c, used)) != MP_OKAY) { 16 | return err; 17 | } 18 | } 19 | 20 | for (i = 0; i < used; i++) { 21 | mp_digit x, y; 22 | 23 | /* convert to two complement if negative */ 24 | if (a->sign == MP_NEG) { 25 | ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK); 26 | x = ac & MP_MASK; 27 | ac >>= MP_DIGIT_BIT; 28 | } else { 29 | x = (i >= a->used) ? 0uL : a->dp[i]; 30 | } 31 | 32 | /* convert to two complement if negative */ 33 | if (b->sign == MP_NEG) { 34 | bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK); 35 | y = bc & MP_MASK; 36 | bc >>= MP_DIGIT_BIT; 37 | } else { 38 | y = (i >= b->used) ? 0uL : b->dp[i]; 39 | } 40 | 41 | c->dp[i] = x & y; 42 | 43 | /* convert to to sign-magnitude if negative */ 44 | if (csign == MP_NEG) { 45 | cc += ~c->dp[i] & MP_MASK; 46 | c->dp[i] = cc & MP_MASK; 47 | cc >>= MP_DIGIT_BIT; 48 | } 49 | } 50 | 51 | c->used = used; 52 | c->sign = csign; 53 | mp_clamp(c); 54 | return MP_OKAY; 55 | } 56 | #endif 57 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_clamp.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CLAMP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* trim unused digits 7 | * 8 | * This is used to ensure that leading zero digits are 9 | * trimed and the leading "used" digit will be non-zero 10 | * Typically very fast. Also fixes the sign if there 11 | * are no more leading digits 12 | */ 13 | void mp_clamp(mp_int *a) 14 | { 15 | /* decrease used while the most significant digit is 16 | * zero. 17 | */ 18 | while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) { 19 | --(a->used); 20 | } 21 | 22 | /* reset the sign flag if used == 0 */ 23 | if (a->used == 0) { 24 | a->sign = MP_ZPOS; 25 | } 26 | } 27 | #endif 28 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_clear.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CLEAR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* clear one (frees) */ 7 | void mp_clear(mp_int *a) 8 | { 9 | /* only do anything if a hasn't been freed previously */ 10 | if (a->dp != NULL) { 11 | /* free ram */ 12 | MP_FREE_DIGITS(a->dp, a->alloc); 13 | 14 | /* reset members to make debugging easier */ 15 | a->dp = NULL; 16 | a->alloc = a->used = 0; 17 | a->sign = MP_ZPOS; 18 | } 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_clear_multi.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CLEAR_MULTI_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | #include 7 | 8 | void mp_clear_multi(mp_int *mp, ...) 9 | { 10 | mp_int *next_mp = mp; 11 | va_list args; 12 | va_start(args, mp); 13 | while (next_mp != NULL) { 14 | mp_clear(next_mp); 15 | next_mp = va_arg(args, mp_int *); 16 | } 17 | va_end(args); 18 | } 19 | #endif 20 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_cmp.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CMP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* compare two ints (signed)*/ 7 | mp_ord mp_cmp(const mp_int *a, const mp_int *b) 8 | { 9 | /* compare based on sign */ 10 | if (a->sign != b->sign) { 11 | if (a->sign == MP_NEG) { 12 | return MP_LT; 13 | } else { 14 | return MP_GT; 15 | } 16 | } 17 | 18 | /* compare digits */ 19 | if (a->sign == MP_NEG) { 20 | /* if negative compare opposite direction */ 21 | return mp_cmp_mag(b, a); 22 | } else { 23 | return mp_cmp_mag(a, b); 24 | } 25 | } 26 | #endif 27 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_cmp_d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CMP_D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* compare a digit */ 7 | mp_ord mp_cmp_d(const mp_int *a, mp_digit b) 8 | { 9 | /* compare based on sign */ 10 | if (a->sign == MP_NEG) { 11 | return MP_LT; 12 | } 13 | 14 | /* compare based on magnitude */ 15 | if (a->used > 1) { 16 | return MP_GT; 17 | } 18 | 19 | /* compare the only digit of a to b */ 20 | if (a->dp[0] > b) { 21 | return MP_GT; 22 | } else if (a->dp[0] < b) { 23 | return MP_LT; 24 | } else { 25 | return MP_EQ; 26 | } 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_cmp_mag.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CMP_MAG_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* compare maginitude of two ints (unsigned) */ 7 | mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) 8 | { 9 | int n; 10 | const mp_digit *tmpa, *tmpb; 11 | 12 | /* compare based on # of non-zero digits */ 13 | if (a->used > b->used) { 14 | return MP_GT; 15 | } 16 | 17 | if (a->used < b->used) { 18 | return MP_LT; 19 | } 20 | 21 | /* alias for a */ 22 | tmpa = a->dp + (a->used - 1); 23 | 24 | /* alias for b */ 25 | tmpb = b->dp + (a->used - 1); 26 | 27 | /* compare based on digits */ 28 | for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { 29 | if (*tmpa > *tmpb) { 30 | return MP_GT; 31 | } 32 | 33 | if (*tmpa < *tmpb) { 34 | return MP_LT; 35 | } 36 | } 37 | return MP_EQ; 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_cnt_lsb.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_CNT_LSB_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | static const int lnz[16] = { 7 | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 8 | }; 9 | 10 | /* Counts the number of lsbs which are zero before the first zero bit */ 11 | int mp_cnt_lsb(const mp_int *a) 12 | { 13 | int x; 14 | mp_digit q, qq; 15 | 16 | /* easy out */ 17 | if (MP_IS_ZERO(a)) { 18 | return 0; 19 | } 20 | 21 | /* scan lower digits until non-zero */ 22 | for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {} 23 | q = a->dp[x]; 24 | x *= MP_DIGIT_BIT; 25 | 26 | /* now scan this digit until a 1 is found */ 27 | if ((q & 1u) == 0u) { 28 | do { 29 | qq = q & 15u; 30 | x += lnz[qq]; 31 | q >>= 4; 32 | } while (qq == 0u); 33 | } 34 | return x; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_complement.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_COMPLEMENT_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* b = ~a */ 7 | mp_err mp_complement(const mp_int *a, mp_int *b) 8 | { 9 | mp_err err = mp_neg(a, b); 10 | return (err == MP_OKAY) ? mp_sub_d(b, 1uL, b) : err; 11 | } 12 | #endif 13 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_copy.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_COPY_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* copy, b = a */ 7 | mp_err mp_copy(const mp_int *a, mp_int *b) 8 | { 9 | int n; 10 | mp_digit *tmpa, *tmpb; 11 | mp_err err; 12 | 13 | /* if dst == src do nothing */ 14 | if (a == b) { 15 | return MP_OKAY; 16 | } 17 | 18 | /* grow dest */ 19 | if (b->alloc < a->used) { 20 | if ((err = mp_grow(b, a->used)) != MP_OKAY) { 21 | return err; 22 | } 23 | } 24 | 25 | /* zero b and copy the parameters over */ 26 | /* pointer aliases */ 27 | 28 | /* source */ 29 | tmpa = a->dp; 30 | 31 | /* destination */ 32 | tmpb = b->dp; 33 | 34 | /* copy all the digits */ 35 | for (n = 0; n < a->used; n++) { 36 | *tmpb++ = *tmpa++; 37 | } 38 | 39 | /* clear high digits */ 40 | MP_ZERO_DIGITS(tmpb, b->used - n); 41 | 42 | /* copy used count and sign */ 43 | b->used = a->used; 44 | b->sign = a->sign; 45 | return MP_OKAY; 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_count_bits.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_COUNT_BITS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* returns the number of bits in an int */ 7 | int mp_count_bits(const mp_int *a) 8 | { 9 | int r; 10 | mp_digit q; 11 | 12 | /* shortcut */ 13 | if (MP_IS_ZERO(a)) { 14 | return 0; 15 | } 16 | 17 | /* get number of digits and add that */ 18 | r = (a->used - 1) * MP_DIGIT_BIT; 19 | 20 | /* take the last digit and count the bits in it */ 21 | q = a->dp[a->used - 1]; 22 | while (q > 0u) { 23 | ++r; 24 | q >>= 1u; 25 | } 26 | return r; 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_decr.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DECR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* Decrement "a" by one like "a--". Changes input! */ 7 | mp_err mp_decr(mp_int *a) 8 | { 9 | if (MP_IS_ZERO(a)) { 10 | mp_set(a,1uL); 11 | a->sign = MP_NEG; 12 | return MP_OKAY; 13 | } else if (a->sign == MP_NEG) { 14 | mp_err err; 15 | a->sign = MP_ZPOS; 16 | if ((err = mp_incr(a)) != MP_OKAY) { 17 | return err; 18 | } 19 | /* There is no -0 in LTM */ 20 | if (!MP_IS_ZERO(a)) { 21 | a->sign = MP_NEG; 22 | } 23 | return MP_OKAY; 24 | } else if (a->dp[0] > 1uL) { 25 | a->dp[0]--; 26 | if (a->dp[0] == 0u) { 27 | mp_zero(a); 28 | } 29 | return MP_OKAY; 30 | } else { 31 | return mp_sub_d(a, 1uL,a); 32 | } 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_div_2.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DIV_2_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* b = a/2 */ 7 | mp_err mp_div_2(const mp_int *a, mp_int *b) 8 | { 9 | int x, oldused; 10 | mp_digit r, rr, *tmpa, *tmpb; 11 | mp_err err; 12 | 13 | /* copy */ 14 | if (b->alloc < a->used) { 15 | if ((err = mp_grow(b, a->used)) != MP_OKAY) { 16 | return err; 17 | } 18 | } 19 | 20 | oldused = b->used; 21 | b->used = a->used; 22 | 23 | /* source alias */ 24 | tmpa = a->dp + b->used - 1; 25 | 26 | /* dest alias */ 27 | tmpb = b->dp + b->used - 1; 28 | 29 | /* carry */ 30 | r = 0; 31 | for (x = b->used - 1; x >= 0; x--) { 32 | /* get the carry for the next iteration */ 33 | rr = *tmpa & 1u; 34 | 35 | /* shift the current digit, add in carry and store */ 36 | *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1)); 37 | 38 | /* forward carry to next iteration */ 39 | r = rr; 40 | } 41 | 42 | /* zero excess digits */ 43 | MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used); 44 | 45 | b->sign = a->sign; 46 | mp_clamp(b); 47 | return MP_OKAY; 48 | } 49 | #endif 50 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_div_2d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DIV_2D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ 7 | mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) 8 | { 9 | mp_digit D, r, rr; 10 | int x; 11 | mp_err err; 12 | 13 | /* if the shift count is <= 0 then we do no work */ 14 | if (b <= 0) { 15 | err = mp_copy(a, c); 16 | if (d != NULL) { 17 | mp_zero(d); 18 | } 19 | return err; 20 | } 21 | 22 | /* copy */ 23 | if ((err = mp_copy(a, c)) != MP_OKAY) { 24 | return err; 25 | } 26 | /* 'a' should not be used after here - it might be the same as d */ 27 | 28 | /* get the remainder */ 29 | if (d != NULL) { 30 | if ((err = mp_mod_2d(a, b, d)) != MP_OKAY) { 31 | return err; 32 | } 33 | } 34 | 35 | /* shift by as many digits in the bit count */ 36 | if (b >= MP_DIGIT_BIT) { 37 | mp_rshd(c, b / MP_DIGIT_BIT); 38 | } 39 | 40 | /* shift any bit count < MP_DIGIT_BIT */ 41 | D = (mp_digit)(b % MP_DIGIT_BIT); 42 | if (D != 0u) { 43 | mp_digit *tmpc, mask, shift; 44 | 45 | /* mask */ 46 | mask = ((mp_digit)1 << D) - 1uL; 47 | 48 | /* shift for lsb */ 49 | shift = (mp_digit)MP_DIGIT_BIT - D; 50 | 51 | /* alias */ 52 | tmpc = c->dp + (c->used - 1); 53 | 54 | /* carry */ 55 | r = 0; 56 | for (x = c->used - 1; x >= 0; x--) { 57 | /* get the lower bits of this word in a temp */ 58 | rr = *tmpc & mask; 59 | 60 | /* shift the current word and mix in the carry bits from the previous word */ 61 | *tmpc = (*tmpc >> D) | (r << shift); 62 | --tmpc; 63 | 64 | /* set the carry to the carry bits of the current word found above */ 65 | r = rr; 66 | } 67 | } 68 | mp_clamp(c); 69 | return MP_OKAY; 70 | } 71 | #endif 72 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_div_3.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DIV_3_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* divide by three (based on routine from MPI and the GMP manual) */ 7 | mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) 8 | { 9 | mp_int q; 10 | mp_word w, t; 11 | mp_digit b; 12 | mp_err err; 13 | int ix; 14 | 15 | /* b = 2**MP_DIGIT_BIT / 3 */ 16 | b = ((mp_word)1 << (mp_word)MP_DIGIT_BIT) / (mp_word)3; 17 | 18 | if ((err = mp_init_size(&q, a->used)) != MP_OKAY) { 19 | return err; 20 | } 21 | 22 | q.used = a->used; 23 | q.sign = a->sign; 24 | w = 0; 25 | for (ix = a->used - 1; ix >= 0; ix--) { 26 | w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix]; 27 | 28 | if (w >= 3u) { 29 | /* multiply w by [1/3] */ 30 | t = (w * (mp_word)b) >> (mp_word)MP_DIGIT_BIT; 31 | 32 | /* now subtract 3 * [w/3] from w, to get the remainder */ 33 | w -= t+t+t; 34 | 35 | /* fixup the remainder as required since 36 | * the optimization is not exact. 37 | */ 38 | while (w >= 3u) { 39 | t += 1u; 40 | w -= 3u; 41 | } 42 | } else { 43 | t = 0; 44 | } 45 | q.dp[ix] = (mp_digit)t; 46 | } 47 | 48 | /* [optional] store the remainder */ 49 | if (d != NULL) { 50 | *d = (mp_digit)w; 51 | } 52 | 53 | /* [optional] store the quotient */ 54 | if (c != NULL) { 55 | mp_clamp(&q); 56 | mp_exch(&q, c); 57 | } 58 | mp_clear(&q); 59 | 60 | return err; 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_div_d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DIV_D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* single digit division (based on routine from MPI) */ 7 | mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) 8 | { 9 | mp_int q; 10 | mp_word w; 11 | mp_digit t; 12 | mp_err err; 13 | int ix; 14 | 15 | /* cannot divide by zero */ 16 | if (b == 0u) { 17 | return MP_VAL; 18 | } 19 | 20 | /* quick outs */ 21 | if ((b == 1u) || MP_IS_ZERO(a)) { 22 | if (d != NULL) { 23 | *d = 0; 24 | } 25 | if (c != NULL) { 26 | return mp_copy(a, c); 27 | } 28 | return MP_OKAY; 29 | } 30 | 31 | /* power of two ? */ 32 | if ((b & (b - 1u)) == 0u) { 33 | ix = 1; 34 | while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<dp[0] & (((mp_digit)1<<(mp_digit)ix) - 1uL); 39 | } 40 | if (c != NULL) { 41 | return mp_div_2d(a, ix, c, NULL); 42 | } 43 | return MP_OKAY; 44 | } 45 | 46 | /* three? */ 47 | if (MP_HAS(MP_DIV_3) && (b == 3u)) { 48 | return mp_div_3(a, c, d); 49 | } 50 | 51 | /* no easy answer [c'est la vie]. Just division */ 52 | if ((err = mp_init_size(&q, a->used)) != MP_OKAY) { 53 | return err; 54 | } 55 | 56 | q.used = a->used; 57 | q.sign = a->sign; 58 | w = 0; 59 | for (ix = a->used - 1; ix >= 0; ix--) { 60 | w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix]; 61 | 62 | if (w >= b) { 63 | t = (mp_digit)(w / b); 64 | w -= (mp_word)t * (mp_word)b; 65 | } else { 66 | t = 0; 67 | } 68 | q.dp[ix] = t; 69 | } 70 | 71 | if (d != NULL) { 72 | *d = (mp_digit)w; 73 | } 74 | 75 | if (c != NULL) { 76 | mp_clamp(&q); 77 | mp_exch(&q, c); 78 | } 79 | mp_clear(&q); 80 | 81 | return err; 82 | } 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_dr_is_modulus.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DR_IS_MODULUS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines if a number is a valid DR modulus */ 7 | mp_bool mp_dr_is_modulus(const mp_int *a) 8 | { 9 | int ix; 10 | 11 | /* must be at least two digits */ 12 | if (a->used < 2) { 13 | return MP_NO; 14 | } 15 | 16 | /* must be of the form b**k - a [a <= b] so all 17 | * but the first digit must be equal to -1 (mod b). 18 | */ 19 | for (ix = 1; ix < a->used; ix++) { 20 | if (a->dp[ix] != MP_MASK) { 21 | return MP_NO; 22 | } 23 | } 24 | return MP_YES; 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_dr_setup.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_DR_SETUP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines the setup value */ 7 | void mp_dr_setup(const mp_int *a, mp_digit *d) 8 | { 9 | /* the casts are required if MP_DIGIT_BIT is one less than 10 | * the number of bits in a mp_digit [e.g. MP_DIGIT_BIT==31] 11 | */ 12 | *d = (mp_digit)(((mp_word)1 << (mp_word)MP_DIGIT_BIT) - (mp_word)a->dp[0]); 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_error_to_string.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ERROR_TO_STRING_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* return a char * string for a given code */ 7 | const char *mp_error_to_string(mp_err code) 8 | { 9 | switch (code) { 10 | case MP_OKAY: 11 | return "Successful"; 12 | case MP_ERR: 13 | return "Unknown error"; 14 | case MP_MEM: 15 | return "Out of heap"; 16 | case MP_VAL: 17 | return "Value out of range"; 18 | case MP_ITER: 19 | return "Max. iterations reached"; 20 | case MP_BUF: 21 | return "Buffer overflow"; 22 | default: 23 | return "Invalid error code"; 24 | } 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_exch.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_EXCH_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* swap the elements of two integers, for cases where you can't simply swap the 7 | * mp_int pointers around 8 | */ 9 | void mp_exch(mp_int *a, mp_int *b) 10 | { 11 | mp_int t; 12 | 13 | t = *a; 14 | *a = *b; 15 | *b = t; 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_expt_u32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_EXPT_U32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* calculate c = a**b using a square-multiply algorithm */ 7 | mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) 8 | { 9 | mp_err err; 10 | 11 | mp_int g; 12 | 13 | if ((err = mp_init_copy(&g, a)) != MP_OKAY) { 14 | return err; 15 | } 16 | 17 | /* set initial result */ 18 | mp_set(c, 1uL); 19 | 20 | while (b > 0u) { 21 | /* if the bit is set multiply */ 22 | if ((b & 1u) != 0u) { 23 | if ((err = mp_mul(c, &g, c)) != MP_OKAY) { 24 | goto LBL_ERR; 25 | } 26 | } 27 | 28 | /* square */ 29 | if (b > 1u) { 30 | if ((err = mp_sqr(&g, &g)) != MP_OKAY) { 31 | goto LBL_ERR; 32 | } 33 | } 34 | 35 | /* shift to next bit */ 36 | b >>= 1; 37 | } 38 | 39 | err = MP_OKAY; 40 | 41 | LBL_ERR: 42 | mp_clear(&g); 43 | return err; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_fread.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_FREAD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | #ifndef MP_NO_FILE 7 | /* read a bigint from a file stream in ASCII */ 8 | mp_err mp_fread(mp_int *a, int radix, FILE *stream) 9 | { 10 | mp_err err; 11 | mp_sign neg; 12 | 13 | /* if first digit is - then set negative */ 14 | int ch = fgetc(stream); 15 | if (ch == (int)'-') { 16 | neg = MP_NEG; 17 | ch = fgetc(stream); 18 | } else { 19 | neg = MP_ZPOS; 20 | } 21 | 22 | /* no digits, return error */ 23 | if (ch == EOF) { 24 | return MP_ERR; 25 | } 26 | 27 | /* clear a */ 28 | mp_zero(a); 29 | 30 | do { 31 | int y; 32 | unsigned pos = (unsigned)(ch - (int)'('); 33 | if (mp_s_rmap_reverse_sz < pos) { 34 | break; 35 | } 36 | 37 | y = (int)mp_s_rmap_reverse[pos]; 38 | 39 | if ((y == 0xff) || (y >= radix)) { 40 | break; 41 | } 42 | 43 | /* shift up and add */ 44 | if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { 45 | return err; 46 | } 47 | if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { 48 | return err; 49 | } 50 | } while ((ch = fgetc(stream)) != EOF); 51 | 52 | if (a->used != 0) { 53 | a->sign = neg; 54 | } 55 | 56 | return MP_OKAY; 57 | } 58 | #endif 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_from_sbin.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_FROM_SBIN_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* read signed bin, big endian, first byte is 0==positive or 1==negative */ 7 | mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) 8 | { 9 | mp_err err; 10 | 11 | /* read magnitude */ 12 | if ((err = mp_from_ubin(a, buf + 1, size - 1u)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | /* first byte is 0 for positive, non-zero for negative */ 17 | if (buf[0] == (unsigned char)0) { 18 | a->sign = MP_ZPOS; 19 | } else { 20 | a->sign = MP_NEG; 21 | } 22 | 23 | return MP_OKAY; 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_from_ubin.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_FROM_UBIN_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* reads a unsigned char array, assumes the msb is stored first [big endian] */ 7 | mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) 8 | { 9 | mp_err err; 10 | 11 | /* make sure there are at least two digits */ 12 | if (a->alloc < 2) { 13 | if ((err = mp_grow(a, 2)) != MP_OKAY) { 14 | return err; 15 | } 16 | } 17 | 18 | /* zero the int */ 19 | mp_zero(a); 20 | 21 | /* read the bytes in */ 22 | while (size-- > 0u) { 23 | if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) { 24 | return err; 25 | } 26 | 27 | #ifndef MP_8BIT 28 | a->dp[0] |= *buf++; 29 | a->used += 1; 30 | #else 31 | a->dp[0] = (*buf & MP_MASK); 32 | a->dp[1] |= ((*buf++ >> 7) & 1u); 33 | a->used += 2; 34 | #endif 35 | } 36 | mp_clamp(a); 37 | return MP_OKAY; 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_fwrite.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_FWRITE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | #ifndef MP_NO_FILE 7 | mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) 8 | { 9 | char *buf; 10 | mp_err err; 11 | int len; 12 | size_t written; 13 | 14 | /* TODO: this function is not in this PR */ 15 | if (MP_HAS(MP_RADIX_SIZE_OVERESTIMATE)) { 16 | /* if ((err = mp_radix_size_overestimate(&t, base, &len)) != MP_OKAY) goto LBL_ERR; */ 17 | } else { 18 | if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { 19 | return err; 20 | } 21 | } 22 | 23 | buf = (char *) MP_MALLOC((size_t)len); 24 | if (buf == NULL) { 25 | return MP_MEM; 26 | } 27 | 28 | if ((err = mp_to_radix(a, buf, (size_t)len, &written, radix)) != MP_OKAY) { 29 | goto LBL_ERR; 30 | } 31 | 32 | if (fwrite(buf, written, 1uL, stream) != 1uL) { 33 | err = MP_ERR; 34 | goto LBL_ERR; 35 | } 36 | err = MP_OKAY; 37 | 38 | 39 | LBL_ERR: 40 | MP_FREE_BUFFER(buf, (size_t)len); 41 | return err; 42 | } 43 | #endif 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_double.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_DOUBLE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | double mp_get_double(const mp_int *a) 7 | { 8 | int i; 9 | double d = 0.0, fac = 1.0; 10 | for (i = 0; i < MP_DIGIT_BIT; ++i) { 11 | fac *= 2.0; 12 | } 13 | for (i = a->used; i --> 0;) { 14 | d = (d * fac) + (double)a->dp[i]; 15 | } 16 | return (a->sign == MP_NEG) ? -d : d; 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_i32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_I32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_SIGNED(mp_get_i32, mp_get_mag_u32, int32_t, uint32_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_i64.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_I64_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_SIGNED(mp_get_i64, mp_get_mag_u64, int64_t, uint64_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_l.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_L_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_SIGNED(mp_get_l, mp_get_mag_ul, long, unsigned long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_ll.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_LL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_SIGNED(mp_get_ll, mp_get_mag_ull, long long, unsigned long long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_mag_u32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_MAG_U32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_MAG(mp_get_mag_u32, uint32_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_mag_u64.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_MAG_U64_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_MAG(mp_get_mag_u64, uint64_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_mag_ul.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_MAG_UL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_MAG(mp_get_mag_ul, unsigned long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_get_mag_ull.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GET_MAG_ULL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_GET_MAG(mp_get_mag_ull, unsigned long long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_grow.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_GROW_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* grow as required */ 7 | mp_err mp_grow(mp_int *a, int size) 8 | { 9 | int i; 10 | mp_digit *tmp; 11 | 12 | /* if the alloc size is smaller alloc more ram */ 13 | if (a->alloc < size) { 14 | /* reallocate the array a->dp 15 | * 16 | * We store the return in a temporary variable 17 | * in case the operation failed we don't want 18 | * to overwrite the dp member of a. 19 | */ 20 | tmp = (mp_digit *) MP_REALLOC(a->dp, 21 | (size_t)a->alloc * sizeof(mp_digit), 22 | (size_t)size * sizeof(mp_digit)); 23 | if (tmp == NULL) { 24 | /* reallocation failed but "a" is still valid [can be freed] */ 25 | return MP_MEM; 26 | } 27 | 28 | /* reallocation succeeded so set a->dp */ 29 | a->dp = tmp; 30 | 31 | /* zero excess digits */ 32 | i = a->alloc; 33 | a->alloc = size; 34 | MP_ZERO_DIGITS(a->dp + i, a->alloc - i); 35 | } 36 | return MP_OKAY; 37 | } 38 | #endif 39 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_incr.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INCR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* Increment "a" by one like "a++". Changes input! */ 7 | mp_err mp_incr(mp_int *a) 8 | { 9 | if (MP_IS_ZERO(a)) { 10 | mp_set(a,1uL); 11 | return MP_OKAY; 12 | } else if (a->sign == MP_NEG) { 13 | mp_err err; 14 | a->sign = MP_ZPOS; 15 | if ((err = mp_decr(a)) != MP_OKAY) { 16 | return err; 17 | } 18 | /* There is no -0 in LTM */ 19 | if (!MP_IS_ZERO(a)) { 20 | a->sign = MP_NEG; 21 | } 22 | return MP_OKAY; 23 | } else if (a->dp[0] < MP_DIGIT_MAX) { 24 | a->dp[0]++; 25 | return MP_OKAY; 26 | } else { 27 | return mp_add_d(a, 1uL,a); 28 | } 29 | } 30 | #endif 31 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* init a new mp_int */ 7 | mp_err mp_init(mp_int *a) 8 | { 9 | /* allocate memory required and clear it */ 10 | a->dp = (mp_digit *) MP_CALLOC((size_t)MP_PREC, sizeof(mp_digit)); 11 | if (a->dp == NULL) { 12 | return MP_MEM; 13 | } 14 | 15 | /* set the used to zero, allocated digits to the default precision 16 | * and sign to positive */ 17 | a->used = 0; 18 | a->alloc = MP_PREC; 19 | a->sign = MP_ZPOS; 20 | 21 | return MP_OKAY; 22 | } 23 | #endif 24 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_copy.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_COPY_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* creates "a" then copies b into it */ 7 | mp_err mp_init_copy(mp_int *a, const mp_int *b) 8 | { 9 | mp_err err; 10 | 11 | if ((err = mp_init_size(a, b->used)) != MP_OKAY) { 12 | return err; 13 | } 14 | 15 | if ((err = mp_copy(b, a)) != MP_OKAY) { 16 | mp_clear(a); 17 | } 18 | 19 | return err; 20 | } 21 | #endif 22 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_i32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_I32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_i32, mp_set_i32, int32_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_i64.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_I64_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_i64, mp_set_i64, int64_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_l.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_L_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_l, mp_set_l, long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_ll.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_LL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_ll, mp_set_ll, long long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_multi.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_MULTI_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | #include 7 | 8 | mp_err mp_init_multi(mp_int *mp, ...) 9 | { 10 | mp_err err = MP_OKAY; /* Assume ok until proven otherwise */ 11 | int n = 0; /* Number of ok inits */ 12 | mp_int *cur_arg = mp; 13 | va_list args; 14 | 15 | va_start(args, mp); /* init args to next argument from caller */ 16 | while (cur_arg != NULL) { 17 | if (mp_init(cur_arg) != MP_OKAY) { 18 | /* Oops - error! Back-track and mp_clear what we already 19 | succeeded in init-ing, then return error. 20 | */ 21 | va_list clean_args; 22 | 23 | /* now start cleaning up */ 24 | cur_arg = mp; 25 | va_start(clean_args, mp); 26 | while (n-- != 0) { 27 | mp_clear(cur_arg); 28 | cur_arg = va_arg(clean_args, mp_int *); 29 | } 30 | va_end(clean_args); 31 | err = MP_MEM; 32 | break; 33 | } 34 | n++; 35 | cur_arg = va_arg(args, mp_int *); 36 | } 37 | va_end(args); 38 | return err; /* Assumed ok, if error flagged above. */ 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_set.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_SET_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* initialize and set a digit */ 7 | mp_err mp_init_set(mp_int *a, mp_digit b) 8 | { 9 | mp_err err; 10 | if ((err = mp_init(a)) != MP_OKAY) { 11 | return err; 12 | } 13 | mp_set(a, b); 14 | return err; 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_size.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_SIZE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* init an mp_init for a given size */ 7 | mp_err mp_init_size(mp_int *a, int size) 8 | { 9 | size = MP_MAX(MP_MIN_PREC, size); 10 | 11 | /* alloc mem */ 12 | a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit)); 13 | if (a->dp == NULL) { 14 | return MP_MEM; 15 | } 16 | 17 | /* set the members */ 18 | a->used = 0; 19 | a->alloc = size; 20 | a->sign = MP_ZPOS; 21 | 22 | return MP_OKAY; 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_u32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_U32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_u32, mp_set_u32, uint32_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_u64.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_U64_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_ul.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_UL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_ul, mp_set_ul, unsigned long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_init_ull.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INIT_ULL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_INIT_INT(mp_init_ull, mp_set_ull, unsigned long long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_invmod.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_INVMOD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* hac 14.61, pp608 */ 7 | mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | /* b cannot be negative and has to be >1 */ 10 | if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) { 11 | return MP_VAL; 12 | } 13 | 14 | /* if the modulus is odd we can use a faster routine instead */ 15 | if (MP_HAS(S_MP_INVMOD_FAST) && MP_IS_ODD(b)) { 16 | return s_mp_invmod_fast(a, b, c); 17 | } 18 | 19 | return MP_HAS(S_MP_INVMOD_SLOW) 20 | ? s_mp_invmod_slow(a, b, c) 21 | : MP_VAL; 22 | } 23 | #endif 24 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_iseven.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ISEVEN_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | mp_bool mp_iseven(const mp_int *a) 7 | { 8 | return MP_IS_EVEN(a) ? MP_YES : MP_NO; 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_isodd.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ISODD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | mp_bool mp_isodd(const mp_int *a) 7 | { 8 | return MP_IS_ODD(a) ? MP_YES : MP_NO; 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_lcm.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_LCM_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* computes least common multiple as |a*b|/(a, b) */ 7 | mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | mp_err err; 10 | mp_int t1, t2; 11 | 12 | 13 | if ((err = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) { 14 | return err; 15 | } 16 | 17 | /* t1 = get the GCD of the two inputs */ 18 | if ((err = mp_gcd(a, b, &t1)) != MP_OKAY) { 19 | goto LBL_T; 20 | } 21 | 22 | /* divide the smallest by the GCD */ 23 | if (mp_cmp_mag(a, b) == MP_LT) { 24 | /* store quotient in t2 such that t2 * b is the LCM */ 25 | if ((err = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { 26 | goto LBL_T; 27 | } 28 | err = mp_mul(b, &t2, c); 29 | } else { 30 | /* store quotient in t2 such that t2 * a is the LCM */ 31 | if ((err = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { 32 | goto LBL_T; 33 | } 34 | err = mp_mul(a, &t2, c); 35 | } 36 | 37 | /* fix the sign to positive */ 38 | c->sign = MP_ZPOS; 39 | 40 | LBL_T: 41 | mp_clear_multi(&t1, &t2, NULL); 42 | return err; 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_lshd.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_LSHD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* shift left a certain amount of digits */ 7 | mp_err mp_lshd(mp_int *a, int b) 8 | { 9 | int x; 10 | mp_err err; 11 | mp_digit *top, *bottom; 12 | 13 | /* if its less than zero return */ 14 | if (b <= 0) { 15 | return MP_OKAY; 16 | } 17 | /* no need to shift 0 around */ 18 | if (MP_IS_ZERO(a)) { 19 | return MP_OKAY; 20 | } 21 | 22 | /* grow to fit the new digits */ 23 | if (a->alloc < (a->used + b)) { 24 | if ((err = mp_grow(a, a->used + b)) != MP_OKAY) { 25 | return err; 26 | } 27 | } 28 | 29 | /* increment the used by the shift amount then copy upwards */ 30 | a->used += b; 31 | 32 | /* top */ 33 | top = a->dp + a->used - 1; 34 | 35 | /* base */ 36 | bottom = (a->dp + a->used - 1) - b; 37 | 38 | /* much like mp_rshd this is implemented using a sliding window 39 | * except the window goes the otherway around. Copying from 40 | * the bottom to the top. see bn_mp_rshd.c for more info. 41 | */ 42 | for (x = a->used - 1; x >= b; x--) { 43 | *top-- = *bottom--; 44 | } 45 | 46 | /* zero the lower digits */ 47 | MP_ZERO_DIGITS(a->dp, b); 48 | 49 | return MP_OKAY; 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mod.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MOD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ 7 | mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | mp_int t; 10 | mp_err err; 11 | 12 | if ((err = mp_init_size(&t, b->used)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) { 17 | goto LBL_ERR; 18 | } 19 | 20 | if (MP_IS_ZERO(&t) || (t.sign == b->sign)) { 21 | err = MP_OKAY; 22 | mp_exch(&t, c); 23 | } else { 24 | err = mp_add(b, &t, c); 25 | } 26 | 27 | LBL_ERR: 28 | mp_clear(&t); 29 | return err; 30 | } 31 | #endif 32 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mod_2d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MOD_2D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* calc a value mod 2**b */ 7 | mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) 8 | { 9 | int x; 10 | mp_err err; 11 | 12 | /* if b is <= 0 then zero the int */ 13 | if (b <= 0) { 14 | mp_zero(c); 15 | return MP_OKAY; 16 | } 17 | 18 | /* if the modulus is larger than the value than return */ 19 | if (b >= (a->used * MP_DIGIT_BIT)) { 20 | return mp_copy(a, c); 21 | } 22 | 23 | /* copy */ 24 | if ((err = mp_copy(a, c)) != MP_OKAY) { 25 | return err; 26 | } 27 | 28 | /* zero digits above the last digit of the modulus */ 29 | x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1); 30 | MP_ZERO_DIGITS(c->dp + x, c->used - x); 31 | 32 | /* clear the digit that is not completely outside/inside the modulus */ 33 | c->dp[b / MP_DIGIT_BIT] &= 34 | ((mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT)) - (mp_digit)1; 35 | mp_clamp(c); 36 | return MP_OKAY; 37 | } 38 | #endif 39 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mod_d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MOD_D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) 7 | { 8 | return mp_div_d(a, b, NULL, c); 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_montgomery_calc_normalization.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* 7 | * shifts with subtractions when the result is greater than b. 8 | * 9 | * The method is slightly modified to shift B unconditionally upto just under 10 | * the leading bit of b. This saves alot of multiple precision shifting. 11 | */ 12 | mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) 13 | { 14 | int x, bits; 15 | mp_err err; 16 | 17 | /* how many bits of last digit does b use */ 18 | bits = mp_count_bits(b) % MP_DIGIT_BIT; 19 | 20 | if (b->used > 1) { 21 | if ((err = mp_2expt(a, ((b->used - 1) * MP_DIGIT_BIT) + bits - 1)) != MP_OKAY) { 22 | return err; 23 | } 24 | } else { 25 | mp_set(a, 1uL); 26 | bits = 1; 27 | } 28 | 29 | 30 | /* now compute C = A * B mod b */ 31 | for (x = bits - 1; x < (int)MP_DIGIT_BIT; x++) { 32 | if ((err = mp_mul_2(a, a)) != MP_OKAY) { 33 | return err; 34 | } 35 | if (mp_cmp_mag(a, b) != MP_LT) { 36 | if ((err = s_mp_sub(a, b, a)) != MP_OKAY) { 37 | return err; 38 | } 39 | } 40 | } 41 | 42 | return MP_OKAY; 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_montgomery_setup.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MONTGOMERY_SETUP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* setups the montgomery reduction stuff */ 7 | mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) 8 | { 9 | mp_digit x, b; 10 | 11 | /* fast inversion mod 2**k 12 | * 13 | * Based on the fact that 14 | * 15 | * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) 16 | * => 2*X*A - X*X*A*A = 1 17 | * => 2*(1) - (1) = 1 18 | */ 19 | b = n->dp[0]; 20 | 21 | if ((b & 1u) == 0u) { 22 | return MP_VAL; 23 | } 24 | 25 | x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */ 26 | x *= 2u - (b * x); /* here x*a==1 mod 2**8 */ 27 | #if !defined(MP_8BIT) 28 | x *= 2u - (b * x); /* here x*a==1 mod 2**16 */ 29 | #endif 30 | #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) 31 | x *= 2u - (b * x); /* here x*a==1 mod 2**32 */ 32 | #endif 33 | #ifdef MP_64BIT 34 | x *= 2u - (b * x); /* here x*a==1 mod 2**64 */ 35 | #endif 36 | 37 | /* rho = -1/m mod b */ 38 | *rho = (mp_digit)(((mp_word)1 << (mp_word)MP_DIGIT_BIT) - x) & MP_MASK; 39 | 40 | return MP_OKAY; 41 | } 42 | #endif 43 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mul_2.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MUL_2_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* b = a*2 */ 7 | mp_err mp_mul_2(const mp_int *a, mp_int *b) 8 | { 9 | int x, oldused; 10 | mp_err err; 11 | 12 | /* grow to accomodate result */ 13 | if (b->alloc < (a->used + 1)) { 14 | if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) { 15 | return err; 16 | } 17 | } 18 | 19 | oldused = b->used; 20 | b->used = a->used; 21 | 22 | { 23 | mp_digit r, rr, *tmpa, *tmpb; 24 | 25 | /* alias for source */ 26 | tmpa = a->dp; 27 | 28 | /* alias for dest */ 29 | tmpb = b->dp; 30 | 31 | /* carry */ 32 | r = 0; 33 | for (x = 0; x < a->used; x++) { 34 | 35 | /* get what will be the *next* carry bit from the 36 | * MSB of the current digit 37 | */ 38 | rr = *tmpa >> (mp_digit)(MP_DIGIT_BIT - 1); 39 | 40 | /* now shift up this digit, add in the carry [from the previous] */ 41 | *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK; 42 | 43 | /* copy the carry that would be from the source 44 | * digit into the next iteration 45 | */ 46 | r = rr; 47 | } 48 | 49 | /* new leading digit? */ 50 | if (r != 0u) { 51 | /* add a MSB which is always 1 at this point */ 52 | *tmpb = 1; 53 | ++(b->used); 54 | } 55 | 56 | /* now zero any excess digits on the destination 57 | * that we didn't write to 58 | */ 59 | MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used); 60 | } 61 | b->sign = a->sign; 62 | return MP_OKAY; 63 | } 64 | #endif 65 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mul_2d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MUL_2D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* shift left by a certain bit count */ 7 | mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) 8 | { 9 | mp_digit d; 10 | mp_err err; 11 | 12 | /* copy */ 13 | if (a != c) { 14 | if ((err = mp_copy(a, c)) != MP_OKAY) { 15 | return err; 16 | } 17 | } 18 | 19 | if (c->alloc < (c->used + (b / MP_DIGIT_BIT) + 1)) { 20 | if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) { 21 | return err; 22 | } 23 | } 24 | 25 | /* shift by as many digits in the bit count */ 26 | if (b >= MP_DIGIT_BIT) { 27 | if ((err = mp_lshd(c, b / MP_DIGIT_BIT)) != MP_OKAY) { 28 | return err; 29 | } 30 | } 31 | 32 | /* shift any bit count < MP_DIGIT_BIT */ 33 | d = (mp_digit)(b % MP_DIGIT_BIT); 34 | if (d != 0u) { 35 | mp_digit *tmpc, shift, mask, r, rr; 36 | int x; 37 | 38 | /* bitmask for carries */ 39 | mask = ((mp_digit)1 << d) - (mp_digit)1; 40 | 41 | /* shift for msbs */ 42 | shift = (mp_digit)MP_DIGIT_BIT - d; 43 | 44 | /* alias */ 45 | tmpc = c->dp; 46 | 47 | /* carry */ 48 | r = 0; 49 | for (x = 0; x < c->used; x++) { 50 | /* get the higher bits of the current word */ 51 | rr = (*tmpc >> shift) & mask; 52 | 53 | /* shift the current word and OR in the carry */ 54 | *tmpc = ((*tmpc << d) | r) & MP_MASK; 55 | ++tmpc; 56 | 57 | /* set the carry to the carry bits of the current word */ 58 | r = rr; 59 | } 60 | 61 | /* set final carry */ 62 | if (r != 0u) { 63 | c->dp[(c->used)++] = r; 64 | } 65 | } 66 | mp_clamp(c); 67 | return MP_OKAY; 68 | } 69 | #endif 70 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mul_d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MUL_D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* multiply by a digit */ 7 | mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) 8 | { 9 | mp_digit u, *tmpa, *tmpc; 10 | mp_word r; 11 | mp_err err; 12 | int ix, olduse; 13 | 14 | /* make sure c is big enough to hold a*b */ 15 | if (c->alloc < (a->used + 1)) { 16 | if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) { 17 | return err; 18 | } 19 | } 20 | 21 | /* get the original destinations used count */ 22 | olduse = c->used; 23 | 24 | /* set the sign */ 25 | c->sign = a->sign; 26 | 27 | /* alias for a->dp [source] */ 28 | tmpa = a->dp; 29 | 30 | /* alias for c->dp [dest] */ 31 | tmpc = c->dp; 32 | 33 | /* zero carry */ 34 | u = 0; 35 | 36 | /* compute columns */ 37 | for (ix = 0; ix < a->used; ix++) { 38 | /* compute product and carry sum for this term */ 39 | r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b); 40 | 41 | /* mask off higher bits to get a single digit */ 42 | *tmpc++ = (mp_digit)(r & (mp_word)MP_MASK); 43 | 44 | /* send carry into next iteration */ 45 | u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT); 46 | } 47 | 48 | /* store final carry [if any] and increment ix offset */ 49 | *tmpc++ = u; 50 | ++ix; 51 | 52 | /* now zero digits above the top */ 53 | MP_ZERO_DIGITS(tmpc, olduse - ix); 54 | 55 | /* set used count */ 56 | c->used = a->used + 1; 57 | mp_clamp(c); 58 | 59 | return MP_OKAY; 60 | } 61 | #endif 62 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_mulmod.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_MULMOD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* d = a * b (mod c) */ 7 | mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) 8 | { 9 | mp_err err; 10 | mp_int t; 11 | 12 | if ((err = mp_init_size(&t, c->used)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | if ((err = mp_mul(a, b, &t)) != MP_OKAY) { 17 | goto LBL_ERR; 18 | } 19 | err = mp_mod(&t, c, d); 20 | 21 | LBL_ERR: 22 | mp_clear(&t); 23 | return err; 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_neg.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_NEG_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* b = -a */ 7 | mp_err mp_neg(const mp_int *a, mp_int *b) 8 | { 9 | mp_err err; 10 | if (a != b) { 11 | if ((err = mp_copy(a, b)) != MP_OKAY) { 12 | return err; 13 | } 14 | } 15 | 16 | if (!MP_IS_ZERO(b)) { 17 | b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS; 18 | } else { 19 | b->sign = MP_ZPOS; 20 | } 21 | 22 | return MP_OKAY; 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_or.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_OR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* two complement or */ 7 | mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | int used = MP_MAX(a->used, b->used) + 1, i; 10 | mp_err err; 11 | mp_digit ac = 1, bc = 1, cc = 1; 12 | mp_sign csign = ((a->sign == MP_NEG) || (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS; 13 | 14 | if (c->alloc < used) { 15 | if ((err = mp_grow(c, used)) != MP_OKAY) { 16 | return err; 17 | } 18 | } 19 | 20 | for (i = 0; i < used; i++) { 21 | mp_digit x, y; 22 | 23 | /* convert to two complement if negative */ 24 | if (a->sign == MP_NEG) { 25 | ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK); 26 | x = ac & MP_MASK; 27 | ac >>= MP_DIGIT_BIT; 28 | } else { 29 | x = (i >= a->used) ? 0uL : a->dp[i]; 30 | } 31 | 32 | /* convert to two complement if negative */ 33 | if (b->sign == MP_NEG) { 34 | bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK); 35 | y = bc & MP_MASK; 36 | bc >>= MP_DIGIT_BIT; 37 | } else { 38 | y = (i >= b->used) ? 0uL : b->dp[i]; 39 | } 40 | 41 | c->dp[i] = x | y; 42 | 43 | /* convert to to sign-magnitude if negative */ 44 | if (csign == MP_NEG) { 45 | cc += ~c->dp[i] & MP_MASK; 46 | c->dp[i] = cc & MP_MASK; 47 | cc >>= MP_DIGIT_BIT; 48 | } 49 | } 50 | 51 | c->used = used; 52 | c->sign = csign; 53 | mp_clamp(c); 54 | return MP_OKAY; 55 | } 56 | #endif 57 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_pack.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_PACK_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* based on gmp's mpz_export. 7 | * see http://gmplib.org/manual/Integer-Import-and-Export.html 8 | */ 9 | mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size, 10 | mp_endian endian, size_t nails, const mp_int *op) 11 | { 12 | mp_err err; 13 | size_t odd_nails, nail_bytes, i, j, count; 14 | unsigned char odd_nail_mask; 15 | 16 | mp_int t; 17 | 18 | count = mp_pack_count(op, nails, size); 19 | 20 | if (count > maxcount) { 21 | return MP_BUF; 22 | } 23 | 24 | if ((err = mp_init_copy(&t, op)) != MP_OKAY) { 25 | return err; 26 | } 27 | 28 | if (endian == MP_NATIVE_ENDIAN) { 29 | MP_GET_ENDIANNESS(endian); 30 | } 31 | 32 | odd_nails = (nails % 8u); 33 | odd_nail_mask = 0xff; 34 | for (i = 0u; i < odd_nails; ++i) { 35 | odd_nail_mask ^= (unsigned char)(1u << (7u - i)); 36 | } 37 | nail_bytes = nails / 8u; 38 | 39 | for (i = 0u; i < count; ++i) { 40 | for (j = 0u; j < size; ++j) { 41 | unsigned char *byte = (unsigned char *)rop + 42 | (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) + 43 | ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j)); 44 | 45 | if (j >= (size - nail_bytes)) { 46 | *byte = 0; 47 | continue; 48 | } 49 | 50 | *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL)); 51 | 52 | if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) { 53 | goto LBL_ERR; 54 | } 55 | 56 | } 57 | } 58 | 59 | if (written != NULL) { 60 | *written = count; 61 | } 62 | err = MP_OKAY; 63 | 64 | LBL_ERR: 65 | mp_clear(&t); 66 | return err; 67 | } 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_pack_count.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_PACK_COUNT_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) 7 | { 8 | size_t bits = (size_t)mp_count_bits(a); 9 | return ((bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u)); 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_prime_fermat.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_PRIME_FERMAT_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* performs one Fermat test. 7 | * 8 | * If "a" were prime then b**a == b (mod a) since the order of 9 | * the multiplicative sub-group would be phi(a) = a-1. That means 10 | * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). 11 | * 12 | * Sets result to 1 if the congruence holds, or zero otherwise. 13 | */ 14 | mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) 15 | { 16 | mp_int t; 17 | mp_err err; 18 | 19 | /* default to composite */ 20 | *result = MP_NO; 21 | 22 | /* ensure b > 1 */ 23 | if (mp_cmp_d(b, 1uL) != MP_GT) { 24 | return MP_VAL; 25 | } 26 | 27 | /* init t */ 28 | if ((err = mp_init(&t)) != MP_OKAY) { 29 | return err; 30 | } 31 | 32 | /* compute t = b**a mod a */ 33 | if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) { 34 | goto LBL_T; 35 | } 36 | 37 | /* is it equal to b? */ 38 | if (mp_cmp(&t, b) == MP_EQ) { 39 | *result = MP_YES; 40 | } 41 | 42 | err = MP_OKAY; 43 | LBL_T: 44 | mp_clear(&t); 45 | return err; 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_prime_rabin_miller_trials.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | static const struct { 7 | int k, t; 8 | } sizes[] = { 9 | { 80, -1 }, /* Use deterministic algorithm for size <= 80 bits */ 10 | { 81, 37 }, /* max. error = 2^(-96)*/ 11 | { 96, 32 }, /* max. error = 2^(-96)*/ 12 | { 128, 40 }, /* max. error = 2^(-112)*/ 13 | { 160, 35 }, /* max. error = 2^(-112)*/ 14 | { 256, 27 }, /* max. error = 2^(-128)*/ 15 | { 384, 16 }, /* max. error = 2^(-128)*/ 16 | { 512, 18 }, /* max. error = 2^(-160)*/ 17 | { 768, 11 }, /* max. error = 2^(-160)*/ 18 | { 896, 10 }, /* max. error = 2^(-160)*/ 19 | { 1024, 12 }, /* max. error = 2^(-192)*/ 20 | { 1536, 8 }, /* max. error = 2^(-192)*/ 21 | { 2048, 6 }, /* max. error = 2^(-192)*/ 22 | { 3072, 4 }, /* max. error = 2^(-192)*/ 23 | { 4096, 5 }, /* max. error = 2^(-256)*/ 24 | { 5120, 4 }, /* max. error = 2^(-256)*/ 25 | { 6144, 4 }, /* max. error = 2^(-256)*/ 26 | { 8192, 3 }, /* max. error = 2^(-256)*/ 27 | { 9216, 3 }, /* max. error = 2^(-256)*/ 28 | { 10240, 2 } /* For bigger keysizes use always at least 2 Rounds */ 29 | }; 30 | 31 | /* returns # of RM trials required for a given bit size */ 32 | int mp_prime_rabin_miller_trials(int size) 33 | { 34 | int x; 35 | 36 | for (x = 0; x < (int)(sizeof(sizes)/(sizeof(sizes[0]))); x++) { 37 | if (sizes[x].k == size) { 38 | return sizes[x].t; 39 | } else if (sizes[x].k > size) { 40 | return (x == 0) ? sizes[0].t : sizes[x - 1].t; 41 | } 42 | } 43 | return sizes[x-1].t; 44 | } 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_radix_size.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_RADIX_SIZE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* returns size of ASCII representation */ 7 | mp_err mp_radix_size(const mp_int *a, int radix, int *size) 8 | { 9 | mp_err err; 10 | int digs; 11 | mp_int t; 12 | mp_digit d; 13 | 14 | *size = 0; 15 | 16 | /* make sure the radix is in range */ 17 | if ((radix < 2) || (radix > 64)) { 18 | return MP_VAL; 19 | } 20 | 21 | if (MP_IS_ZERO(a)) { 22 | *size = 2; 23 | return MP_OKAY; 24 | } 25 | 26 | /* special case for binary */ 27 | if (radix == 2) { 28 | *size = (mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1); 29 | return MP_OKAY; 30 | } 31 | 32 | /* digs is the digit count */ 33 | digs = 0; 34 | 35 | /* if it's negative add one for the sign */ 36 | if (a->sign == MP_NEG) { 37 | ++digs; 38 | } 39 | 40 | /* init a copy of the input */ 41 | if ((err = mp_init_copy(&t, a)) != MP_OKAY) { 42 | return err; 43 | } 44 | 45 | /* force temp to positive */ 46 | t.sign = MP_ZPOS; 47 | 48 | /* fetch out all of the digits */ 49 | while (!MP_IS_ZERO(&t)) { 50 | if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { 51 | goto LBL_ERR; 52 | } 53 | ++digs; 54 | } 55 | 56 | /* return digs + 1, the 1 is for the NULL byte that would be required. */ 57 | *size = digs + 1; 58 | err = MP_OKAY; 59 | 60 | LBL_ERR: 61 | mp_clear(&t); 62 | return err; 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_radix_smap.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_RADIX_SMAP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* chars used in radix conversions */ 7 | const char *const mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; 8 | const uint8_t mp_s_rmap_reverse[] = { 9 | 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */ 10 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */ 11 | 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */ 12 | 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */ 13 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */ 14 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */ 15 | 0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */ 16 | 0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */ 17 | 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */ 18 | 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */ 19 | 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */ 20 | }; 21 | const size_t mp_s_rmap_reverse_sz = sizeof(mp_s_rmap_reverse); 22 | #endif 23 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_rand.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_RAND_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | mp_err(*s_mp_rand_source)(void *out, size_t size) = s_mp_rand_platform; 7 | 8 | void mp_rand_source(mp_err(*source)(void *out, size_t size)) 9 | { 10 | s_mp_rand_source = (source == NULL) ? s_mp_rand_platform : source; 11 | } 12 | 13 | mp_err mp_rand(mp_int *a, int digits) 14 | { 15 | int i; 16 | mp_err err; 17 | 18 | mp_zero(a); 19 | 20 | if (digits <= 0) { 21 | return MP_OKAY; 22 | } 23 | 24 | if ((err = mp_grow(a, digits)) != MP_OKAY) { 25 | return err; 26 | } 27 | 28 | if ((err = s_mp_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) { 29 | return err; 30 | } 31 | 32 | /* TODO: We ensure that the highest digit is nonzero. Should this be removed? */ 33 | while ((a->dp[digits - 1] & MP_MASK) == 0u) { 34 | if ((err = s_mp_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) { 35 | return err; 36 | } 37 | } 38 | 39 | a->used = digits; 40 | for (i = 0; i < digits; ++i) { 41 | a->dp[i] &= MP_MASK; 42 | } 43 | 44 | return MP_OKAY; 45 | } 46 | #endif 47 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_2k.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_2K_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* reduces a modulo n where n is of the form 2**p - d */ 7 | mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) 8 | { 9 | mp_int q; 10 | mp_err err; 11 | int p; 12 | 13 | if ((err = mp_init(&q)) != MP_OKAY) { 14 | return err; 15 | } 16 | 17 | p = mp_count_bits(n); 18 | top: 19 | /* q = a/2**p, a = a mod 2**p */ 20 | if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) { 21 | goto LBL_ERR; 22 | } 23 | 24 | if (d != 1u) { 25 | /* q = q * d */ 26 | if ((err = mp_mul_d(&q, d, &q)) != MP_OKAY) { 27 | goto LBL_ERR; 28 | } 29 | } 30 | 31 | /* a = a + q */ 32 | if ((err = s_mp_add(a, &q, a)) != MP_OKAY) { 33 | goto LBL_ERR; 34 | } 35 | 36 | if (mp_cmp_mag(a, n) != MP_LT) { 37 | if ((err = s_mp_sub(a, n, a)) != MP_OKAY) { 38 | goto LBL_ERR; 39 | } 40 | goto top; 41 | } 42 | 43 | LBL_ERR: 44 | mp_clear(&q); 45 | return err; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_2k_l.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_2K_L_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* reduces a modulo n where n is of the form 2**p - d 7 | This differs from reduce_2k since "d" can be larger 8 | than a single digit. 9 | */ 10 | mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) 11 | { 12 | mp_int q; 13 | mp_err err; 14 | int p; 15 | 16 | if ((err = mp_init(&q)) != MP_OKAY) { 17 | return err; 18 | } 19 | 20 | p = mp_count_bits(n); 21 | top: 22 | /* q = a/2**p, a = a mod 2**p */ 23 | if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) { 24 | goto LBL_ERR; 25 | } 26 | 27 | /* q = q * d */ 28 | if ((err = mp_mul(&q, d, &q)) != MP_OKAY) { 29 | goto LBL_ERR; 30 | } 31 | 32 | /* a = a + q */ 33 | if ((err = s_mp_add(a, &q, a)) != MP_OKAY) { 34 | goto LBL_ERR; 35 | } 36 | 37 | if (mp_cmp_mag(a, n) != MP_LT) { 38 | if ((err = s_mp_sub(a, n, a)) != MP_OKAY) { 39 | goto LBL_ERR; 40 | } 41 | goto top; 42 | } 43 | 44 | LBL_ERR: 45 | mp_clear(&q); 46 | return err; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_2k_setup.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_2K_SETUP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines the setup value */ 7 | mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) 8 | { 9 | mp_err err; 10 | mp_int tmp; 11 | int p; 12 | 13 | if ((err = mp_init(&tmp)) != MP_OKAY) { 14 | return err; 15 | } 16 | 17 | p = mp_count_bits(a); 18 | if ((err = mp_2expt(&tmp, p)) != MP_OKAY) { 19 | mp_clear(&tmp); 20 | return err; 21 | } 22 | 23 | if ((err = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) { 24 | mp_clear(&tmp); 25 | return err; 26 | } 27 | 28 | *d = tmp.dp[0]; 29 | mp_clear(&tmp); 30 | return MP_OKAY; 31 | } 32 | #endif 33 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_2k_setup_l.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_2K_SETUP_L_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines the setup value */ 7 | mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) 8 | { 9 | mp_err err; 10 | mp_int tmp; 11 | 12 | if ((err = mp_init(&tmp)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | if ((err = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { 17 | goto LBL_ERR; 18 | } 19 | 20 | if ((err = s_mp_sub(&tmp, a, d)) != MP_OKAY) { 21 | goto LBL_ERR; 22 | } 23 | 24 | LBL_ERR: 25 | mp_clear(&tmp); 26 | return err; 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_is_2k.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_IS_2K_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines if mp_reduce_2k can be used */ 7 | mp_bool mp_reduce_is_2k(const mp_int *a) 8 | { 9 | int ix, iy, iw; 10 | mp_digit iz; 11 | 12 | if (a->used == 0) { 13 | return MP_NO; 14 | } else if (a->used == 1) { 15 | return MP_YES; 16 | } else if (a->used > 1) { 17 | iy = mp_count_bits(a); 18 | iz = 1; 19 | iw = 1; 20 | 21 | /* Test every bit from the second digit up, must be 1 */ 22 | for (ix = MP_DIGIT_BIT; ix < iy; ix++) { 23 | if ((a->dp[iw] & iz) == 0u) { 24 | return MP_NO; 25 | } 26 | iz <<= 1; 27 | if (iz > MP_DIGIT_MAX) { 28 | ++iw; 29 | iz = 1; 30 | } 31 | } 32 | return MP_YES; 33 | } else { 34 | return MP_YES; 35 | } 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_is_2k_l.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_IS_2K_L_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines if reduce_2k_l can be used */ 7 | mp_bool mp_reduce_is_2k_l(const mp_int *a) 8 | { 9 | int ix, iy; 10 | 11 | if (a->used == 0) { 12 | return MP_NO; 13 | } else if (a->used == 1) { 14 | return MP_YES; 15 | } else if (a->used > 1) { 16 | /* if more than half of the digits are -1 we're sold */ 17 | for (iy = ix = 0; ix < a->used; ix++) { 18 | if (a->dp[ix] == MP_DIGIT_MAX) { 19 | ++iy; 20 | } 21 | } 22 | return (iy >= (a->used/2)) ? MP_YES : MP_NO; 23 | } else { 24 | return MP_NO; 25 | } 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_reduce_setup.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_REDUCE_SETUP_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* pre-calculate the value required for Barrett reduction 7 | * For a given modulus "b" it calulates the value required in "a" 8 | */ 9 | mp_err mp_reduce_setup(mp_int *a, const mp_int *b) 10 | { 11 | mp_err err; 12 | if ((err = mp_2expt(a, b->used * 2 * MP_DIGIT_BIT)) != MP_OKAY) { 13 | return err; 14 | } 15 | return mp_div(a, b, a, NULL); 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_rshd.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_RSHD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* shift right a certain amount of digits */ 7 | void mp_rshd(mp_int *a, int b) 8 | { 9 | int x; 10 | mp_digit *bottom, *top; 11 | 12 | /* if b <= 0 then ignore it */ 13 | if (b <= 0) { 14 | return; 15 | } 16 | 17 | /* if b > used then simply zero it and return */ 18 | if (a->used <= b) { 19 | mp_zero(a); 20 | return; 21 | } 22 | 23 | /* shift the digits down */ 24 | 25 | /* bottom */ 26 | bottom = a->dp; 27 | 28 | /* top [offset into digits] */ 29 | top = a->dp + b; 30 | 31 | /* this is implemented as a sliding window where 32 | * the window is b-digits long and digits from 33 | * the top of the window are copied to the bottom 34 | * 35 | * e.g. 36 | 37 | b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> 38 | /\ | ----> 39 | \-------------------/ ----> 40 | */ 41 | for (x = 0; x < (a->used - b); x++) { 42 | *bottom++ = *top++; 43 | } 44 | 45 | /* zero the top digits */ 46 | MP_ZERO_DIGITS(bottom, a->used - x); 47 | 48 | /* remove excess digits */ 49 | a->used -= b; 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_sbin_size.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SBIN_SIZE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* get the size for an signed equivalent */ 7 | size_t mp_sbin_size(const mp_int *a) 8 | { 9 | return 1u + mp_ubin_size(a); 10 | } 11 | #endif 12 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* set to a digit */ 7 | void mp_set(mp_int *a, mp_digit b) 8 | { 9 | a->dp[0] = b & MP_MASK; 10 | a->sign = MP_ZPOS; 11 | a->used = (a->dp[0] != 0u) ? 1 : 0; 12 | MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_double.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_DOUBLE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) 7 | mp_err mp_set_double(mp_int *a, double b) 8 | { 9 | uint64_t frac; 10 | int exp; 11 | mp_err err; 12 | union { 13 | double dbl; 14 | uint64_t bits; 15 | } cast; 16 | cast.dbl = b; 17 | 18 | exp = (int)((unsigned)(cast.bits >> 52) & 0x7FFu); 19 | frac = (cast.bits & ((1uLL << 52) - 1uLL)) | (1uLL << 52); 20 | 21 | if (exp == 0x7FF) { /* +-inf, NaN */ 22 | return MP_VAL; 23 | } 24 | exp -= 1023 + 52; 25 | 26 | mp_set_u64(a, frac); 27 | 28 | err = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); 29 | if (err != MP_OKAY) { 30 | return err; 31 | } 32 | 33 | if (((cast.bits >> 63) != 0uLL) && !MP_IS_ZERO(a)) { 34 | a->sign = MP_NEG; 35 | } 36 | 37 | return MP_OKAY; 38 | } 39 | #else 40 | /* pragma message() not supported by several compilers (in mostly older but still used versions) */ 41 | # ifdef _MSC_VER 42 | # pragma message("mp_set_double implementation is only available on platforms with IEEE754 floating point format") 43 | # else 44 | # warning "mp_set_double implementation is only available on platforms with IEEE754 floating point format" 45 | # endif 46 | #endif 47 | #endif 48 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_i32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_I32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_SIGNED(mp_set_i32, mp_set_u32, int32_t, uint32_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_i64.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_I64_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_SIGNED(mp_set_i64, mp_set_u64, int64_t, uint64_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_l.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_L_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_SIGNED(mp_set_l, mp_set_ul, long, unsigned long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_ll.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_LL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_SIGNED(mp_set_ll, mp_set_ull, long long, unsigned long long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_u32.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_U32_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_UNSIGNED(mp_set_u32, uint32_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_u64.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_U64_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_UNSIGNED(mp_set_u64, uint64_t) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_ul.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_UL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_UNSIGNED(mp_set_ul, unsigned long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_set_ull.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SET_ULL_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | MP_SET_UNSIGNED(mp_set_ull, unsigned long long) 7 | #endif 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_shrink.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SHRINK_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* shrink a bignum */ 7 | mp_err mp_shrink(mp_int *a) 8 | { 9 | mp_digit *tmp; 10 | int alloc = MP_MAX(MP_MIN_PREC, a->used); 11 | if (a->alloc != alloc) { 12 | if ((tmp = (mp_digit *) MP_REALLOC(a->dp, 13 | (size_t)a->alloc * sizeof(mp_digit), 14 | (size_t)alloc * sizeof(mp_digit))) == NULL) { 15 | return MP_MEM; 16 | } 17 | a->dp = tmp; 18 | a->alloc = alloc; 19 | } 20 | return MP_OKAY; 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_signed_rsh.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SIGNED_RSH_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* shift right by a certain bit count with sign extension */ 7 | mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) 8 | { 9 | mp_err res; 10 | if (a->sign == MP_ZPOS) { 11 | return mp_div_2d(a, b, c, NULL); 12 | } 13 | 14 | res = mp_add_d(a, 1uL, c); 15 | if (res != MP_OKAY) { 16 | return res; 17 | } 18 | 19 | res = mp_div_2d(c, b, c, NULL); 20 | return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res; 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_sqr.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SQR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* computes b = a*a */ 7 | mp_err mp_sqr(const mp_int *a, mp_int *b) 8 | { 9 | mp_err err; 10 | if (MP_HAS(S_MP_TOOM_SQR) && /* use Toom-Cook? */ 11 | (a->used >= MP_TOOM_SQR_CUTOFF)) { 12 | err = s_mp_toom_sqr(a, b); 13 | } else if (MP_HAS(S_MP_KARATSUBA_SQR) && /* Karatsuba? */ 14 | (a->used >= MP_KARATSUBA_SQR_CUTOFF)) { 15 | err = s_mp_karatsuba_sqr(a, b); 16 | } else if (MP_HAS(S_MP_SQR_FAST) && /* can we use the fast comba multiplier? */ 17 | (((a->used * 2) + 1) < MP_WARRAY) && 18 | (a->used < (MP_MAXFAST / 2))) { 19 | err = s_mp_sqr_fast(a, b); 20 | } else if (MP_HAS(S_MP_SQR)) { 21 | err = s_mp_sqr(a, b); 22 | } else { 23 | err = MP_VAL; 24 | } 25 | b->sign = MP_ZPOS; 26 | return err; 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_sqrmod.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SQRMOD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* c = a * a (mod b) */ 7 | mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | mp_err err; 10 | mp_int t; 11 | 12 | if ((err = mp_init(&t)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | if ((err = mp_sqr(a, &t)) != MP_OKAY) { 17 | goto LBL_ERR; 18 | } 19 | err = mp_mod(&t, b, c); 20 | 21 | LBL_ERR: 22 | mp_clear(&t); 23 | return err; 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_sqrt.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SQRT_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* this function is less generic than mp_n_root, simpler and faster */ 7 | mp_err mp_sqrt(const mp_int *arg, mp_int *ret) 8 | { 9 | mp_err err; 10 | mp_int t1, t2; 11 | 12 | /* must be positive */ 13 | if (arg->sign == MP_NEG) { 14 | return MP_VAL; 15 | } 16 | 17 | /* easy out */ 18 | if (MP_IS_ZERO(arg)) { 19 | mp_zero(ret); 20 | return MP_OKAY; 21 | } 22 | 23 | if ((err = mp_init_copy(&t1, arg)) != MP_OKAY) { 24 | return err; 25 | } 26 | 27 | if ((err = mp_init(&t2)) != MP_OKAY) { 28 | goto E2; 29 | } 30 | 31 | /* First approx. (not very bad for large arg) */ 32 | mp_rshd(&t1, t1.used/2); 33 | 34 | /* t1 > 0 */ 35 | if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) { 36 | goto E1; 37 | } 38 | if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) { 39 | goto E1; 40 | } 41 | if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) { 42 | goto E1; 43 | } 44 | /* And now t1 > sqrt(arg) */ 45 | do { 46 | if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) { 47 | goto E1; 48 | } 49 | if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) { 50 | goto E1; 51 | } 52 | if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) { 53 | goto E1; 54 | } 55 | /* t1 >= sqrt(arg) >= t2 at this point */ 56 | } while (mp_cmp_mag(&t1, &t2) == MP_GT); 57 | 58 | mp_exch(&t1, ret); 59 | 60 | E1: 61 | mp_clear(&t2); 62 | E2: 63 | mp_clear(&t1); 64 | return err; 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_sub.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SUB_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* high level subtraction (handles signs) */ 7 | mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | mp_sign sa = a->sign, sb = b->sign; 10 | mp_err err; 11 | 12 | if (sa != sb) { 13 | /* subtract a negative from a positive, OR */ 14 | /* subtract a positive from a negative. */ 15 | /* In either case, ADD their magnitudes, */ 16 | /* and use the sign of the first number. */ 17 | c->sign = sa; 18 | err = s_mp_add(a, b, c); 19 | } else { 20 | /* subtract a positive from a positive, OR */ 21 | /* subtract a negative from a negative. */ 22 | /* First, take the difference between their */ 23 | /* magnitudes, then... */ 24 | if (mp_cmp_mag(a, b) != MP_LT) { 25 | /* Copy the sign from the first */ 26 | c->sign = sa; 27 | /* The first has a larger or equal magnitude */ 28 | err = s_mp_sub(a, b, c); 29 | } else { 30 | /* The result has the *opposite* sign from */ 31 | /* the first number. */ 32 | c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; 33 | /* The second has a larger magnitude */ 34 | err = s_mp_sub(b, a, c); 35 | } 36 | } 37 | return err; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_sub_d.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SUB_D_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* single digit subtraction */ 7 | mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) 8 | { 9 | mp_digit *tmpa, *tmpc; 10 | mp_err err; 11 | int ix, oldused; 12 | 13 | /* grow c as required */ 14 | if (c->alloc < (a->used + 1)) { 15 | if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) { 16 | return err; 17 | } 18 | } 19 | 20 | /* if a is negative just do an unsigned 21 | * addition [with fudged signs] 22 | */ 23 | if (a->sign == MP_NEG) { 24 | mp_int a_ = *a; 25 | a_.sign = MP_ZPOS; 26 | err = mp_add_d(&a_, b, c); 27 | c->sign = MP_NEG; 28 | 29 | /* clamp */ 30 | mp_clamp(c); 31 | 32 | return err; 33 | } 34 | 35 | /* setup regs */ 36 | oldused = c->used; 37 | tmpa = a->dp; 38 | tmpc = c->dp; 39 | 40 | /* if a <= b simply fix the single digit */ 41 | if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) { 42 | if (a->used == 1) { 43 | *tmpc++ = b - *tmpa; 44 | } else { 45 | *tmpc++ = b; 46 | } 47 | ix = 1; 48 | 49 | /* negative/1digit */ 50 | c->sign = MP_NEG; 51 | c->used = 1; 52 | } else { 53 | mp_digit mu = b; 54 | 55 | /* positive/size */ 56 | c->sign = MP_ZPOS; 57 | c->used = a->used; 58 | 59 | /* subtract digits, mu is carry */ 60 | for (ix = 0; ix < a->used; ix++) { 61 | *tmpc = *tmpa++ - mu; 62 | mu = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); 63 | *tmpc++ &= MP_MASK; 64 | } 65 | } 66 | 67 | /* zero excess digits */ 68 | MP_ZERO_DIGITS(tmpc, oldused - ix); 69 | 70 | mp_clamp(c); 71 | return MP_OKAY; 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_submod.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_SUBMOD_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* d = a - b (mod c) */ 7 | mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) 8 | { 9 | mp_err err; 10 | mp_int t; 11 | 12 | if ((err = mp_init(&t)) != MP_OKAY) { 13 | return err; 14 | } 15 | 16 | if ((err = mp_sub(a, b, &t)) != MP_OKAY) { 17 | goto LBL_ERR; 18 | } 19 | err = mp_mod(&t, c, d); 20 | 21 | LBL_ERR: 22 | mp_clear(&t); 23 | return err; 24 | } 25 | #endif 26 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_to_sbin.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_TO_SBIN_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* store in signed [big endian] format */ 7 | mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) 8 | { 9 | mp_err err; 10 | if (maxlen == 0u) { 11 | return MP_BUF; 12 | } 13 | if ((err = mp_to_ubin(a, buf + 1, maxlen - 1u, written)) != MP_OKAY) { 14 | return err; 15 | } 16 | if (written != NULL) { 17 | (*written)++; 18 | } 19 | buf[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1; 20 | return MP_OKAY; 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_to_ubin.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_TO_UBIN_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* store in unsigned [big endian] format */ 7 | mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) 8 | { 9 | size_t x, count; 10 | mp_err err; 11 | mp_int t; 12 | 13 | count = mp_ubin_size(a); 14 | if (count > maxlen) { 15 | return MP_BUF; 16 | } 17 | 18 | if ((err = mp_init_copy(&t, a)) != MP_OKAY) { 19 | return err; 20 | } 21 | 22 | for (x = count; x --> 0u;) { 23 | #ifndef MP_8BIT 24 | buf[x] = (unsigned char)(t.dp[0] & 255u); 25 | #else 26 | buf[x] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7)); 27 | #endif 28 | if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) { 29 | goto LBL_ERR; 30 | } 31 | } 32 | 33 | if (written != NULL) { 34 | *written = count; 35 | } 36 | 37 | LBL_ERR: 38 | mp_clear(&t); 39 | return err; 40 | } 41 | #endif 42 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_ubin_size.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_UBIN_SIZE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* get the size for an unsigned equivalent */ 7 | size_t mp_ubin_size(const mp_int *a) 8 | { 9 | size_t size = (size_t)mp_count_bits(a); 10 | return (size / 8u) + (((size & 7u) != 0u) ? 1u : 0u); 11 | } 12 | #endif 13 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_unpack.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_UNPACK_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* based on gmp's mpz_import. 7 | * see http://gmplib.org/manual/Integer-Import-and-Export.html 8 | */ 9 | mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, 10 | mp_endian endian, size_t nails, const void *op) 11 | { 12 | mp_err err; 13 | size_t odd_nails, nail_bytes, i, j; 14 | unsigned char odd_nail_mask; 15 | 16 | mp_zero(rop); 17 | 18 | if (endian == MP_NATIVE_ENDIAN) { 19 | MP_GET_ENDIANNESS(endian); 20 | } 21 | 22 | odd_nails = (nails % 8u); 23 | odd_nail_mask = 0xff; 24 | for (i = 0; i < odd_nails; ++i) { 25 | odd_nail_mask ^= (unsigned char)(1u << (7u - i)); 26 | } 27 | nail_bytes = nails / 8u; 28 | 29 | for (i = 0; i < count; ++i) { 30 | for (j = 0; j < (size - nail_bytes); ++j) { 31 | unsigned char byte = *((const unsigned char *)op + 32 | (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) + 33 | ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); 34 | 35 | if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) { 36 | return err; 37 | } 38 | 39 | rop->dp[0] |= (j == 0u) ? (mp_digit)(byte & odd_nail_mask) : (mp_digit)byte; 40 | rop->used += 1; 41 | } 42 | } 43 | 44 | mp_clamp(rop); 45 | 46 | return MP_OKAY; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_xor.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_XOR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* two complement xor */ 7 | mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | int used = MP_MAX(a->used, b->used) + 1, i; 10 | mp_err err; 11 | mp_digit ac = 1, bc = 1, cc = 1; 12 | mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS; 13 | 14 | if (c->alloc < used) { 15 | if ((err = mp_grow(c, used)) != MP_OKAY) { 16 | return err; 17 | } 18 | } 19 | 20 | for (i = 0; i < used; i++) { 21 | mp_digit x, y; 22 | 23 | /* convert to two complement if negative */ 24 | if (a->sign == MP_NEG) { 25 | ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK); 26 | x = ac & MP_MASK; 27 | ac >>= MP_DIGIT_BIT; 28 | } else { 29 | x = (i >= a->used) ? 0uL : a->dp[i]; 30 | } 31 | 32 | /* convert to two complement if negative */ 33 | if (b->sign == MP_NEG) { 34 | bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK); 35 | y = bc & MP_MASK; 36 | bc >>= MP_DIGIT_BIT; 37 | } else { 38 | y = (i >= b->used) ? 0uL : b->dp[i]; 39 | } 40 | 41 | c->dp[i] = x ^ y; 42 | 43 | /* convert to to sign-magnitude if negative */ 44 | if (csign == MP_NEG) { 45 | cc += ~c->dp[i] & MP_MASK; 46 | c->dp[i] = cc & MP_MASK; 47 | cc >>= MP_DIGIT_BIT; 48 | } 49 | } 50 | 51 | c->used = used; 52 | c->sign = csign; 53 | mp_clamp(c); 54 | return MP_OKAY; 55 | } 56 | #endif 57 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_mp_zero.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_MP_ZERO_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* set to zero */ 7 | void mp_zero(mp_int *a) 8 | { 9 | a->sign = MP_ZPOS; 10 | a->used = 0; 11 | MP_ZERO_DIGITS(a->dp, a->alloc); 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_get_bit.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_GET_BIT_C 3 | 4 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 5 | /* SPDX-License-Identifier: Unlicense */ 6 | 7 | /* Get bit at position b and return MP_YES if the bit is 1, MP_NO if it is 0 */ 8 | mp_bool s_mp_get_bit(const mp_int *a, unsigned int b) 9 | { 10 | mp_digit bit; 11 | int limb = (int)(b / MP_DIGIT_BIT); 12 | 13 | if (limb >= a->used) { 14 | return MP_NO; 15 | } 16 | 17 | bit = (mp_digit)1 << (b % MP_DIGIT_BIT); 18 | return ((a->dp[limb] & bit) != 0u) ? MP_YES : MP_NO; 19 | } 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_mul_high_digs.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_MUL_HIGH_DIGS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* multiplies |a| * |b| and does not compute the lower digs digits 7 | * [meant to get the higher part of the product] 8 | */ 9 | mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) 10 | { 11 | mp_int t; 12 | int pa, pb, ix, iy; 13 | mp_err err; 14 | mp_digit u; 15 | mp_word r; 16 | mp_digit tmpx, *tmpt, *tmpy; 17 | 18 | /* can we use the fast multiplier? */ 19 | if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST) 20 | && ((a->used + b->used + 1) < MP_WARRAY) 21 | && (MP_MIN(a->used, b->used) < MP_MAXFAST)) { 22 | return s_mp_mul_high_digs_fast(a, b, c, digs); 23 | } 24 | 25 | if ((err = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) { 26 | return err; 27 | } 28 | t.used = a->used + b->used + 1; 29 | 30 | pa = a->used; 31 | pb = b->used; 32 | for (ix = 0; ix < pa; ix++) { 33 | /* clear the carry */ 34 | u = 0; 35 | 36 | /* left hand side of A[ix] * B[iy] */ 37 | tmpx = a->dp[ix]; 38 | 39 | /* alias to the address of where the digits will be stored */ 40 | tmpt = &(t.dp[digs]); 41 | 42 | /* alias for where to read the right hand side from */ 43 | tmpy = b->dp + (digs - ix); 44 | 45 | for (iy = digs - ix; iy < pb; iy++) { 46 | /* calculate the double precision result */ 47 | r = (mp_word)*tmpt + 48 | ((mp_word)tmpx * (mp_word)*tmpy++) + 49 | (mp_word)u; 50 | 51 | /* get the lower part */ 52 | *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK); 53 | 54 | /* carry the carry */ 55 | u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT); 56 | } 57 | *tmpt = u; 58 | } 59 | mp_clamp(&t); 60 | mp_exch(&t, c); 61 | mp_clear(&t); 62 | return MP_OKAY; 63 | } 64 | #endif 65 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_prime_is_divisible.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_PRIME_IS_DIVISIBLE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* determines if an integers is divisible by one 7 | * of the first PRIME_SIZE primes or not 8 | * 9 | * sets result to 0 if not, 1 if yes 10 | */ 11 | mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result) 12 | { 13 | int ix; 14 | mp_err err; 15 | mp_digit res; 16 | 17 | /* default to not */ 18 | *result = MP_NO; 19 | 20 | for (ix = 0; ix < PRIVATE_MP_PRIME_TAB_SIZE; ix++) { 21 | /* what is a mod LBL_prime_tab[ix] */ 22 | if ((err = mp_mod_d(a, s_mp_prime_tab[ix], &res)) != MP_OKAY) { 23 | return err; 24 | } 25 | 26 | /* is the residue zero? */ 27 | if (res == 0u) { 28 | *result = MP_YES; 29 | return MP_OKAY; 30 | } 31 | } 32 | 33 | return MP_OKAY; 34 | } 35 | #endif 36 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_rand_jenkins.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_RAND_JENKINS_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */ 7 | /* Chosen for speed and a good "mix" */ 8 | typedef struct { 9 | uint64_t a; 10 | uint64_t b; 11 | uint64_t c; 12 | uint64_t d; 13 | } ranctx; 14 | 15 | static ranctx jenkins_x; 16 | 17 | #define rot(x,k) (((x)<<(k))|((x)>>(64-(k)))) 18 | static uint64_t s_rand_jenkins_val(void) 19 | { 20 | uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7); 21 | jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13); 22 | jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37); 23 | jenkins_x.c = jenkins_x.d + e; 24 | jenkins_x.d = e + jenkins_x.a; 25 | return jenkins_x.d; 26 | } 27 | 28 | void s_mp_rand_jenkins_init(uint64_t seed) 29 | { 30 | uint64_t i; 31 | jenkins_x.a = 0xf1ea5eedULL; 32 | jenkins_x.b = jenkins_x.c = jenkins_x.d = seed; 33 | for (i = 0uLL; i < 20uLL; ++i) { 34 | (void)s_rand_jenkins_val(); 35 | } 36 | } 37 | 38 | mp_err s_mp_rand_jenkins(void *p, size_t n) 39 | { 40 | char *q = (char *)p; 41 | while (n > 0u) { 42 | int i; 43 | uint64_t x = s_rand_jenkins_val(); 44 | for (i = 0; (i < 8) && (n > 0u); ++i, --n) { 45 | *q++ = (char)(x & 0xFFuLL); 46 | x >>= 8; 47 | } 48 | } 49 | return MP_OKAY; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_reverse.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_REVERSE_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* reverse an array, used for radix code */ 7 | void s_mp_reverse(unsigned char *s, size_t len) 8 | { 9 | size_t ix, iy; 10 | unsigned char t; 11 | 12 | ix = 0u; 13 | iy = len - 1u; 14 | while (ix < iy) { 15 | t = s[ix]; 16 | s[ix] = s[iy]; 17 | s[iy] = t; 18 | ++ix; 19 | --iy; 20 | } 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_sqr.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_SQR_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ 7 | mp_err s_mp_sqr(const mp_int *a, mp_int *b) 8 | { 9 | mp_int t; 10 | int ix, iy, pa; 11 | mp_err err; 12 | mp_word r; 13 | mp_digit u, tmpx, *tmpt; 14 | 15 | pa = a->used; 16 | if ((err = mp_init_size(&t, (2 * pa) + 1)) != MP_OKAY) { 17 | return err; 18 | } 19 | 20 | /* default used is maximum possible size */ 21 | t.used = (2 * pa) + 1; 22 | 23 | for (ix = 0; ix < pa; ix++) { 24 | /* first calculate the digit at 2*ix */ 25 | /* calculate double precision result */ 26 | r = (mp_word)t.dp[2*ix] + 27 | ((mp_word)a->dp[ix] * (mp_word)a->dp[ix]); 28 | 29 | /* store lower part in result */ 30 | t.dp[ix+ix] = (mp_digit)(r & (mp_word)MP_MASK); 31 | 32 | /* get the carry */ 33 | u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT); 34 | 35 | /* left hand side of A[ix] * A[iy] */ 36 | tmpx = a->dp[ix]; 37 | 38 | /* alias for where to store the results */ 39 | tmpt = t.dp + ((2 * ix) + 1); 40 | 41 | for (iy = ix + 1; iy < pa; iy++) { 42 | /* first calculate the product */ 43 | r = (mp_word)tmpx * (mp_word)a->dp[iy]; 44 | 45 | /* now calculate the double precision result, note we use 46 | * addition instead of *2 since it's easier to optimize 47 | */ 48 | r = (mp_word)*tmpt + r + r + (mp_word)u; 49 | 50 | /* store lower part */ 51 | *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK); 52 | 53 | /* get carry */ 54 | u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT); 55 | } 56 | /* propagate upwards */ 57 | while (u != 0uL) { 58 | r = (mp_word)*tmpt + (mp_word)u; 59 | *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK); 60 | u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT); 61 | } 62 | } 63 | 64 | mp_clamp(&t); 65 | mp_exch(&t, b); 66 | mp_clear(&t); 67 | return MP_OKAY; 68 | } 69 | #endif 70 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/bn_s_mp_sub.c: -------------------------------------------------------------------------------- 1 | #include "tommath_private.h" 2 | #ifdef BN_S_MP_SUB_C 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 | /* SPDX-License-Identifier: Unlicense */ 5 | 6 | /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ 7 | mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) 8 | { 9 | int olduse, min, max; 10 | mp_err err; 11 | 12 | /* find sizes */ 13 | min = b->used; 14 | max = a->used; 15 | 16 | /* init result */ 17 | if (c->alloc < max) { 18 | if ((err = mp_grow(c, max)) != MP_OKAY) { 19 | return err; 20 | } 21 | } 22 | olduse = c->used; 23 | c->used = max; 24 | 25 | { 26 | mp_digit u, *tmpa, *tmpb, *tmpc; 27 | int i; 28 | 29 | /* alias for digit pointers */ 30 | tmpa = a->dp; 31 | tmpb = b->dp; 32 | tmpc = c->dp; 33 | 34 | /* set carry to zero */ 35 | u = 0; 36 | for (i = 0; i < min; i++) { 37 | /* T[i] = A[i] - B[i] - U */ 38 | *tmpc = (*tmpa++ - *tmpb++) - u; 39 | 40 | /* U = carry bit of T[i] 41 | * Note this saves performing an AND operation since 42 | * if a carry does occur it will propagate all the way to the 43 | * MSB. As a result a single shift is enough to get the carry 44 | */ 45 | u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); 46 | 47 | /* Clear carry from T[i] */ 48 | *tmpc++ &= MP_MASK; 49 | } 50 | 51 | /* now copy higher words if any, e.g. if A has more digits than B */ 52 | for (; i < max; i++) { 53 | /* T[i] = A[i] - U */ 54 | *tmpc = *tmpa++ - u; 55 | 56 | /* U = carry bit of T[i] */ 57 | u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); 58 | 59 | /* Clear carry from T[i] */ 60 | *tmpc++ &= MP_MASK; 61 | } 62 | 63 | /* clear digits above used (since we may not have grown result above) */ 64 | MP_ZERO_DIGITS(tmpc, olduse - c->used); 65 | } 66 | 67 | mp_clamp(c); 68 | return MP_OKAY; 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/demo/shared.c: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | void ndraw(mp_int *a, const char *name) 4 | { 5 | char *buf = NULL; 6 | int size; 7 | 8 | mp_radix_size(a, 10, &size); 9 | buf = (char *)malloc((size_t) size); 10 | if (buf == NULL) { 11 | fprintf(stderr, "\nndraw: malloc(%d) failed\n", size); 12 | exit(EXIT_FAILURE); 13 | } 14 | 15 | printf("%s: ", name); 16 | mp_to_decimal(a, buf, (size_t) size); 17 | printf("%s\n", buf); 18 | mp_to_hex(a, buf, (size_t) size); 19 | printf("0x%s\n", buf); 20 | 21 | free(buf); 22 | } 23 | 24 | void print_header(void) 25 | { 26 | #ifdef MP_8BIT 27 | printf("Digit size 8 Bit \n"); 28 | #endif 29 | #ifdef MP_16BIT 30 | printf("Digit size 16 Bit \n"); 31 | #endif 32 | #ifdef MP_32BIT 33 | printf("Digit size 32 Bit \n"); 34 | #endif 35 | #ifdef MP_64BIT 36 | printf("Digit size 64 Bit \n"); 37 | #endif 38 | printf("Size of mp_digit: %u\n", (unsigned int)sizeof(mp_digit)); 39 | printf("Size of mp_word: %u\n", (unsigned int)sizeof(mp_word)); 40 | printf("MP_DIGIT_BIT: %d\n", MP_DIGIT_BIT); 41 | printf("MP_PREC: %d\n", MP_PREC); 42 | } 43 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/demo/shared.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | * Configuration 7 | */ 8 | #ifndef LTM_DEMO_TEST_REDUCE_2K_L 9 | /* This test takes a moment so we disable it by default, but it can be: 10 | * 0 to disable testing 11 | * 1 to make the test with P = 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF 12 | * 2 to make the test with P = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F 13 | */ 14 | #define LTM_DEMO_TEST_REDUCE_2K_L 0 15 | #endif 16 | 17 | #define MP_WUR /* TODO: result checks disabled for now */ 18 | #include "tommath_private.h" 19 | 20 | extern void ndraw(mp_int* a, const char* name); 21 | extern void print_header(void); 22 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/doc/makefile: -------------------------------------------------------------------------------- 1 | ifeq ($V,1) 2 | silent_stdout= 3 | else 4 | silent_stdout= > /dev/null 5 | endif 6 | 7 | PLATFORM := $(shell uname | sed -e 's/_.*//') 8 | ifeq ($(PLATFORM), Darwin) 9 | err: 10 | $(error Docs can't be built on Mac) 11 | 12 | docs mandvi manual: err 13 | endif 14 | 15 | docs: manual 16 | 17 | #LTM user manual 18 | mandvi: bn.tex 19 | cp bn.tex bn.bak 20 | touch --reference=bn.tex bn.bak 21 | (printf "%s" "\def\fixedpdfdate{"; date +'D:%Y%m%d%H%M%S%:z' -d @$$(stat --format=%Y bn.tex) | sed "s/:\([0-9][0-9]\)$$/'\1'}/g") > bn-deterministic.tex 22 | printf "%s\n" "\pdfinfo{" >> bn-deterministic.tex 23 | printf "%s\n" " /CreationDate (\fixedpdfdate)" >> bn-deterministic.tex 24 | printf "%s\n}\n" " /ModDate (\fixedpdfdate)" >> bn-deterministic.tex 25 | cat bn.tex >> bn-deterministic.tex 26 | mv bn-deterministic.tex bn.tex 27 | touch --reference=bn.bak bn.tex 28 | echo "hello" > bn.ind 29 | latex bn ${silent_stdout} 30 | latex bn ${silent_stdout} 31 | makeindex bn 32 | latex bn ${silent_stdout} 33 | 34 | #LTM user manual [pdf] 35 | manual: mandvi 36 | pdflatex bn >/dev/null 37 | sed -b -i 's,^/ID \[.*\]$$,/ID [<0> <0>],g' bn.pdf 38 | mv bn.bak bn.tex 39 | rm -f bn.aux bn.dvi bn.log bn.idx bn.lof bn.out bn.toc 40 | 41 | clean: 42 | rm -f *.idx *.toc *.log *.aux *.dvi *.lof *.ind *.ilg *.ps *.log tommath.tex 43 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/2kprime.1: -------------------------------------------------------------------------------- 1 | 256-bits (k = 36113) = 115792089237316195423570985008687907853269984665640564039457584007913129603823 2 | 512-bits (k = 38117) = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006045979 3 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/2kprime.c: -------------------------------------------------------------------------------- 1 | /* Makes safe primes of a 2k nature */ 2 | #include 3 | #include 4 | 5 | static int sizes[] = {256, 512, 768, 1024, 1536, 2048, 3072, 4096}; 6 | 7 | int main(void) 8 | { 9 | char buf[2000]; 10 | size_t x; 11 | mp_bool y; 12 | mp_int q, p; 13 | FILE *out; 14 | clock_t t1; 15 | mp_digit z; 16 | 17 | mp_init_multi(&q, &p, NULL); 18 | 19 | out = fopen("2kprime.1", "w"); 20 | if (out != NULL) { 21 | for (x = 0; x < (sizeof(sizes) / sizeof(sizes[0])); x++) { 22 | top: 23 | mp_2expt(&q, sizes[x]); 24 | mp_add_d(&q, 3uL, &q); 25 | z = -3; 26 | 27 | t1 = clock(); 28 | for (;;) { 29 | mp_sub_d(&q, 4uL, &q); 30 | z += 4uL; 31 | 32 | if (z > MP_MASK) { 33 | printf("No primes of size %d found\n", sizes[x]); 34 | break; 35 | } 36 | 37 | if ((clock() - t1) > CLOCKS_PER_SEC) { 38 | printf("."); 39 | fflush(stdout); 40 | /* sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC); */ 41 | t1 = clock(); 42 | } 43 | 44 | /* quick test on q */ 45 | mp_prime_is_prime(&q, 1, &y); 46 | if (y == MP_NO) { 47 | continue; 48 | } 49 | 50 | /* find (q-1)/2 */ 51 | mp_sub_d(&q, 1uL, &p); 52 | mp_div_2(&p, &p); 53 | mp_prime_is_prime(&p, 3, &y); 54 | if (y == MP_NO) { 55 | continue; 56 | } 57 | 58 | /* test on q */ 59 | mp_prime_is_prime(&q, 3, &y); 60 | if (y == MP_NO) { 61 | continue; 62 | } 63 | 64 | break; 65 | } 66 | 67 | if (y == MP_NO) { 68 | ++sizes[x]; 69 | goto top; 70 | } 71 | 72 | mp_to_decimal(&q, buf, sizeof(buf)); 73 | printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf); 74 | fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf); 75 | fflush(out); 76 | } 77 | fclose(out); 78 | } 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/drprime.c: -------------------------------------------------------------------------------- 1 | /* Makes safe primes of a DR nature */ 2 | #include 3 | 4 | static int sizes[] = { 1+256/MP_DIGIT_BIT, 1+512/MP_DIGIT_BIT, 1+768/MP_DIGIT_BIT, 1+1024/MP_DIGIT_BIT, 1+2048/MP_DIGIT_BIT, 1+4096/MP_DIGIT_BIT }; 5 | 6 | int main(void) 7 | { 8 | mp_bool res; 9 | int x, y; 10 | char buf[4096]; 11 | FILE *out; 12 | mp_int a, b; 13 | 14 | mp_init(&a); 15 | mp_init(&b); 16 | 17 | out = fopen("drprimes.txt", "w"); 18 | if (out != NULL) { 19 | for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) { 20 | top: 21 | printf("Seeking a %d-bit safe prime\n", sizes[x] * MP_DIGIT_BIT); 22 | mp_grow(&a, sizes[x]); 23 | mp_zero(&a); 24 | for (y = 1; y < sizes[x]; y++) { 25 | a.dp[y] = MP_MASK; 26 | } 27 | 28 | /* make a DR modulus */ 29 | a.dp[0] = -1; 30 | a.used = sizes[x]; 31 | 32 | /* now loop */ 33 | res = MP_NO; 34 | for (;;) { 35 | a.dp[0] += 4uL; 36 | if (a.dp[0] >= MP_MASK) break; 37 | mp_prime_is_prime(&a, 1, &res); 38 | if (res == MP_NO) continue; 39 | printf("."); 40 | fflush(stdout); 41 | mp_sub_d(&a, 1uL, &b); 42 | mp_div_2(&b, &b); 43 | mp_prime_is_prime(&b, 3, &res); 44 | if (res == MP_NO) continue; 45 | mp_prime_is_prime(&a, 3, &res); 46 | if (res == MP_YES) break; 47 | } 48 | 49 | if (res != MP_YES) { 50 | printf("Error not DR modulus\n"); 51 | sizes[x] += 1; 52 | goto top; 53 | } else { 54 | mp_to_decimal(&a, buf, sizeof(buf)); 55 | printf("\n\np == %s\n\n", buf); 56 | fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf); 57 | fflush(out); 58 | } 59 | } 60 | fclose(out); 61 | } 62 | 63 | mp_clear(&a); 64 | mp_clear(&b); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/drprimes.txt: -------------------------------------------------------------------------------- 1 | 300-bit prime: 2 | p == 2037035976334486086268445688409378161051468393665936250636140449354381298610415201576637819 3 | 4 | 540-bit prime: 5 | p == 3599131035634557106248430806148785487095757694641533306480604458089470064537190296255232548883112685719936728506816716098566612844395439751206810991770626477344739 6 | 7 | 780-bit prime: 8 | p == 6359114106063703798370219984742410466332205126109989319225557147754704702203399726411277962562135973685197744935448875852478791860694279747355800678568677946181447581781401213133886609947027230004277244697462656003655947791725966271167 9 | 10 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/makefile: -------------------------------------------------------------------------------- 1 | LTM_CFLAGS += -Wall -W -Wextra -Wshadow -O3 -I../ 2 | LTM_CFLAGS += $(CFLAGS) 3 | 4 | # default lib name (requires install with root) 5 | # LIBNAME=-ltommath 6 | 7 | # libname when you can't install the lib with install 8 | LIBNAME=../libtommath.a 9 | 10 | #provable primes 11 | pprime: pprime.o 12 | $(CC) $(LTM_CFLAGS) pprime.o $(LIBNAME) -o pprime 13 | 14 | # portable [well requires clock()] tuning app 15 | tune: tune.o 16 | $(CC) $(LTM_CFLAGS) tune.o $(LIBNAME) -o tune 17 | ./tune_it.sh 18 | 19 | test_standalone: tune.o 20 | # The benchmark program works as a testtool, too 21 | $(CC) $(LTM_CFLAGS) tune.o $(LIBNAME) -o test 22 | 23 | # spits out mersenne primes 24 | mersenne: mersenne.o 25 | $(CC) $(LTM_CFLAGS) mersenne.o $(LIBNAME) -o mersenne 26 | 27 | # finds DR safe primes for the given config 28 | drprime: drprime.o 29 | $(CC) $(LTM_CFLAGS) drprime.o $(LIBNAME) -o drprime 30 | 31 | # finds 2k safe primes for the given config 32 | 2kprime: 2kprime.o 33 | $(CC) $(LTM_CFLAGS) 2kprime.o $(LIBNAME) -o 2kprime 34 | 35 | mont: mont.o 36 | $(CC) $(LTM_CFLAGS) mont.o $(LIBNAME) -o mont 37 | 38 | 39 | clean: 40 | rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime mont 2kprime pprime.dat \ 41 | tuning_list multiplying squaring test *.da *.dyn *.dpi *~ 42 | rm -rf .libs 43 | 44 | .PHONY: tune 45 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/makefile.icc: -------------------------------------------------------------------------------- 1 | CC = icc 2 | 3 | CFLAGS += -I../ 4 | 5 | # optimize for SPEED 6 | # 7 | # -mcpu= can be pentium, pentiumpro (covers PII through PIII) or pentium4 8 | # -ax? specifies make code specifically for ? but compatible with IA-32 9 | # -x? specifies compile solely for ? [not specifically IA-32 compatible] 10 | # 11 | # where ? is 12 | # K - PIII 13 | # W - first P4 [Williamette] 14 | # N - P4 Northwood 15 | # P - P4 Prescott 16 | # B - Blend of P4 and PM [mobile] 17 | # 18 | # Default to just generic max opts 19 | CFLAGS += -O3 -xP -ip 20 | 21 | # default lib name (requires install with root) 22 | # LIBNAME=-ltommath 23 | 24 | # libname when you can't install the lib with install 25 | LIBNAME=../libtommath.a 26 | 27 | #provable primes 28 | pprime: pprime.o 29 | $(CC) pprime.o $(LIBNAME) -o pprime 30 | 31 | tune: tune.o 32 | $(CC) $(CFLAGS) tune.o $(LIBNAME) -o tune 33 | ./tune_it.sh 34 | 35 | # same app but using RDTSC for higher precision [requires 80586+], coff based gcc installs [e.g. ming, cygwin, djgpp] 36 | tune86: tune.c 37 | nasm -f coff timer.asm 38 | $(CC) -DX86_TIMER $(CFLAGS) tune.c timer.o $(LIBNAME) -o tune86 39 | 40 | # for cygwin 41 | tune86c: tune.c 42 | nasm -f gnuwin32 timer.asm 43 | $(CC) -DX86_TIMER $(CFLAGS) tune.c timer.o $(LIBNAME) -o tune86 44 | 45 | #make tune86 for linux or any ELF format 46 | tune86l: tune.c 47 | nasm -f elf -DUSE_ELF timer.asm 48 | $(CC) -DX86_TIMER $(CFLAGS) tune.c timer.o $(LIBNAME) -o tune86l 49 | 50 | # spits out mersenne primes 51 | mersenne: mersenne.o 52 | $(CC) mersenne.o $(LIBNAME) -o mersenne 53 | 54 | # fines DR safe primes for the given config 55 | drprime: drprime.o 56 | $(CC) drprime.o $(LIBNAME) -o drprime 57 | 58 | # fines 2k safe primes for the given config 59 | 2kprime: 2kprime.o 60 | $(CC) 2kprime.o $(LIBNAME) -o 2kprime 61 | 62 | mont: mont.o 63 | $(CC) mont.o $(LIBNAME) -o mont 64 | 65 | 66 | clean: 67 | rm -f *.log *.o *.obj *.exe pprime tune mersenne drprime tune86 tune86l mont 2kprime pprime.dat *.il tuning_list 68 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/makefile.msvc: -------------------------------------------------------------------------------- 1 | #MSVC Makefile 2 | # 3 | #Tom St Denis 4 | 5 | CFLAGS = /I../ /Ox /DWIN32 /W3 6 | 7 | pprime: pprime.obj 8 | cl pprime.obj ../tommath.lib 9 | 10 | mersenne: mersenne.obj 11 | cl mersenne.obj ../tommath.lib 12 | 13 | tune: tune.obj 14 | cl tune.obj ../tommath.lib 15 | 16 | 17 | mont: mont.obj 18 | cl mont.obj ../tommath.lib 19 | 20 | drprime: drprime.obj 21 | cl drprime.obj ../tommath.lib 22 | 23 | 2kprime: 2kprime.obj 24 | cl 2kprime.obj ../tommath.lib 25 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/mont.c: -------------------------------------------------------------------------------- 1 | /* tests the montgomery routines */ 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | mp_int modulus, R, p, pp; 9 | mp_digit mp; 10 | int x, y; 11 | 12 | srand(time(NULL)); 13 | mp_init_multi(&modulus, &R, &p, &pp, NULL); 14 | 15 | /* loop through various sizes */ 16 | for (x = 4; x < 256; x++) { 17 | printf("DIGITS == %3d...", x); 18 | fflush(stdout); 19 | 20 | /* make up the odd modulus */ 21 | mp_rand(&modulus, x); 22 | modulus.dp[0] |= 1uL; 23 | 24 | /* now find the R value */ 25 | mp_montgomery_calc_normalization(&R, &modulus); 26 | mp_montgomery_setup(&modulus, &mp); 27 | 28 | /* now run through a bunch tests */ 29 | for (y = 0; y < 1000; y++) { 30 | mp_rand(&p, x/2); /* p = random */ 31 | mp_mul(&p, &R, &pp); /* pp = R * p */ 32 | mp_montgomery_reduce(&pp, &modulus, mp); 33 | 34 | /* should be equal to p */ 35 | if (mp_cmp(&pp, &p) != MP_EQ) { 36 | printf("FAILURE!\n"); 37 | exit(-1); 38 | } 39 | } 40 | printf("PASSED\n"); 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/etc/timer.asm: -------------------------------------------------------------------------------- 1 | ; x86 timer in NASM 2 | ; 3 | ; Tom St Denis, tomstdenis@iahu.ca 4 | [bits 32] 5 | [section .data] 6 | time dd 0, 0 7 | 8 | [section .text] 9 | 10 | %ifdef USE_ELF 11 | [global t_start] 12 | t_start: 13 | %else 14 | [global _t_start] 15 | _t_start: 16 | %endif 17 | push edx 18 | push eax 19 | rdtsc 20 | mov [time+0],edx 21 | mov [time+4],eax 22 | pop eax 23 | pop edx 24 | ret 25 | 26 | %ifdef USE_ELF 27 | [global t_read] 28 | t_read: 29 | %else 30 | [global _t_read] 31 | _t_read: 32 | %endif 33 | rdtsc 34 | sub eax,[time+4] 35 | sbb edx,[time+0] 36 | ret 37 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/gen.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | # 3 | # Generates a "single file" you can use to quickly 4 | # add the whole source without any makefile troubles 5 | # 6 | use strict; 7 | use warnings; 8 | 9 | open(my $out, '>', 'mpi.c') or die "Couldn't open mpi.c for writing: $!"; 10 | foreach my $filename (glob 'bn*.c') { 11 | open(my $src, '<', $filename) or die "Couldn't open $filename for reading: $!"; 12 | print {$out} "/* Start: $filename */\n"; 13 | print {$out} $_ while <$src>; 14 | print {$out} "\n/* End: $filename */\n\n"; 15 | close $src or die "Error closing $filename after reading: $!"; 16 | } 17 | print {$out} "\n/* EOF */\n"; 18 | close $out or die "Error closing mpi.c after writing: $!"; 19 | 20 | system('perl -pli -e "s/\s*$//" mpi.c'); 21 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/libtommath.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@to-be-replaced@ 2 | exec_prefix=${prefix} 3 | libdir=${exec_prefix}/lib 4 | includedir=${prefix}/include 5 | 6 | Name: LibTomMath 7 | Description: public domain library for manipulating large integer numbers 8 | Version: @to-be-replaced@ 9 | Libs: -L${libdir} -ltommath 10 | Cflags: -I${includedir} 11 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/libtommath_VS2008.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tommath", "libtommath_VS2008.vcproj", "{42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Debug|x64 = Debug|x64 10 | Release|Win32 = Release|Win32 11 | Release|x64 = Release|x64 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Debug|Win32.Build.0 = Debug|Win32 16 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Debug|x64.ActiveCfg = Debug|x64 17 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Debug|x64.Build.0 = Debug|x64 18 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Release|Win32.ActiveCfg = Release|Win32 19 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Release|Win32.Build.0 = Release|Win32 20 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Release|x64.ActiveCfg = Release|x64 21 | {42109FEE-B0B9-4FCD-9E56-2863BF8C55D2}.Release|x64.Build.0 = Release|x64 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | GlobalSection(ExtensibilityGlobals) = postSolution 27 | SolutionGuid = {83B84178-7B4F-4B78-9C5D-17B8201D5B61} 28 | EndGlobalSection 29 | EndGlobal 30 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gnuplot graphs.dem 3 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/README: -------------------------------------------------------------------------------- 1 | To use the pretty graphs you have to first build/run the ltmtest from the root directory of the package. 2 | Todo this type 3 | 4 | make timing ; ltmtest 5 | 6 | in the root. It will run for a while [about ten minutes on most PCs] and produce a series of .log files in logs/. 7 | 8 | After doing that run "gnuplot graphs.dem" to make the PNGs. If you managed todo that all so far just open index.html to view 9 | them all :-) 10 | 11 | Have fun 12 | 13 | Tom -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/add.log: -------------------------------------------------------------------------------- 1 | 480 48 2 | 960 61 3 | 1440 82 4 | 1920 97 5 | 2400 106 6 | 2880 112 7 | 3360 127 8 | 3840 130 9 | 4320 146 10 | 4800 157 11 | 5280 174 12 | 5760 185 13 | 6240 200 14 | 6720 214 15 | 7200 230 16 | 7680 244 17 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/addsub.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justinethier/cyclone-bootstrap/baa67e1f32f30b450878aff317d04a5213f6bc0e/third-party/libtommath-1.2.0/logs/addsub.png -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/expt.log: -------------------------------------------------------------------------------- 1 | 513 446633 2 | 769 1110301 3 | 1025 2414927 4 | 2049 14870787 5 | 2561 26299761 6 | 3073 44323310 7 | 4097 98934292 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/expt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justinethier/cyclone-bootstrap/baa67e1f32f30b450878aff317d04a5213f6bc0e/third-party/libtommath-1.2.0/logs/expt.png -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/expt_2k.log: -------------------------------------------------------------------------------- 1 | 521 533515 2 | 607 675230 3 | 1279 2560713 4 | 2203 7468422 5 | 3217 17314246 6 | 4253 33899969 7 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/expt_2kl.log: -------------------------------------------------------------------------------- 1 | 1024 2210287 2 | 2048 7940364 3 | 4096 35903891 4 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/expt_dr.log: -------------------------------------------------------------------------------- 1 | 532 642330 2 | 784 1138699 3 | 1036 1972796 4 | 1540 3912241 5 | 2072 7075836 6 | 3080 16420867 7 | 4116 32477173 8 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/graphs.dem: -------------------------------------------------------------------------------- 1 | set terminal png 2 | set ylabel "Cycles per Operation" 3 | set xlabel "Operand size (bits)" 4 | 5 | set output "addsub.png" 6 | plot 'add.log' smooth bezier title "Addition", 'sub.log' smooth bezier title "Subtraction" 7 | 8 | set output "mult.png" 9 | plot 'sqr.log' smooth bezier title "Squaring (without Karatsuba)", 'sqr_kara.log' smooth bezier title "Squaring (Karatsuba)", 'mult.log' smooth bezier title "Multiplication (without Karatsuba)", 'mult_kara.log' smooth bezier title "Multiplication (Karatsuba)" 10 | 11 | set output "expt.png" 12 | plot 'expt.log' smooth bezier title "Exptmod (Montgomery)", 'expt_dr.log' smooth bezier title "Exptmod (Dimminished Radix)", 'expt_2k.log' smooth bezier title "Exptmod (2k Reduction)" 13 | 14 | set output "invmod.png" 15 | plot 'invmod.log' smooth bezier title "Modular Inverse" 16 | 17 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | LibTomMath Log Plots 4 | 5 | 6 | 7 |

Addition and Subtraction

8 |
9 |
10 | 11 |

Multipliers

12 |
13 |
14 | 15 |

Exptmod

16 |
17 |
18 | 19 |

Modular Inverse

20 |
21 |
22 | 23 | 24 | 25 | /* $Source: /cvs/libtom/libtommath/logs/index.html,v $ */ 26 | /* $Revision: 1.2 $ */ 27 | /* $Date: 2005/05/05 14:38:47 $ */ 28 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/invmod.log: -------------------------------------------------------------------------------- 1 | 240 58197 2 | 480 86617 3 | 720 255279 4 | 960 399626 5 | 1200 533330 6 | 1440 470046 7 | 1680 906754 8 | 1920 1132009 9 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/invmod.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justinethier/cyclone-bootstrap/baa67e1f32f30b450878aff317d04a5213f6bc0e/third-party/libtommath-1.2.0/logs/invmod.png -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/mult.log: -------------------------------------------------------------------------------- 1 | 240 114 2 | 360 182 3 | 480 273 4 | 600 364 5 | 717 471 6 | 840 597 7 | 960 737 8 | 1080 881 9 | 1196 1041 10 | 1318 1215 11 | 1438 1405 12 | 1559 1600 13 | 1679 1812 14 | 1800 2045 15 | 1918 2289 16 | 2040 2534 17 | 2160 2800 18 | 2280 3067 19 | 2397 3375 20 | 2520 3678 21 | 2640 4007 22 | 2759 4325 23 | 2880 4851 24 | 3000 5200 25 | 3117 5557 26 | 3239 5938 27 | 3356 6332 28 | 3480 6741 29 | 3600 7168 30 | 3720 7597 31 | 3840 8054 32 | 3960 8514 33 | 4079 8956 34 | 4198 9451 35 | 4320 9976 36 | 4440 10457 37 | 4560 10973 38 | 4679 11535 39 | 4797 12085 40 | 4920 12639 41 | 5040 13235 42 | 5160 13832 43 | 5279 14454 44 | 5400 15063 45 | 5519 15657 46 | 5640 16339 47 | 5760 16992 48 | 5879 17676 49 | 6000 18380 50 | 6120 18022 51 | 6237 18567 52 | 6360 19259 53 | 6479 19929 54 | 6599 20686 55 | 6719 21180 56 | 6840 22121 57 | 6960 22741 58 | 7080 23664 59 | 7200 24315 60 | 7320 25107 61 | 7439 25945 62 | 7560 26557 63 | 7680 27527 64 | 7799 28341 65 | 7920 30761 66 | 8040 31648 67 | 8159 32628 68 | 8280 33498 69 | 8400 34508 70 | 8520 33657 71 | 8640 34566 72 | 8758 35203 73 | 8878 36356 74 | 9000 37379 75 | 9119 38072 76 | 9240 39390 77 | 9360 48931 78 | 9475 66682 79 | 9600 72564 80 | 9719 51493 81 | 9840 52637 82 | 9960 48247 83 | 10080 49030 84 | 10195 50592 85 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/mult.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justinethier/cyclone-bootstrap/baa67e1f32f30b450878aff317d04a5213f6bc0e/third-party/libtommath-1.2.0/logs/mult.png -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/mult_kara.log: -------------------------------------------------------------------------------- 1 | 240 133 2 | 360 250 3 | 474 396 4 | 599 585 5 | 720 637 6 | 840 1045 7 | 960 1212 8 | 1080 1543 9 | 1196 1780 10 | 1320 2005 11 | 1436 2274 12 | 1560 2446 13 | 1680 1985 14 | 1800 2368 15 | 1920 2791 16 | 2038 3620 17 | 2160 3763 18 | 2278 3444 19 | 2400 4158 20 | 2516 5869 21 | 2640 6368 22 | 2753 5384 23 | 2876 7449 24 | 3000 6471 25 | 3114 8540 26 | 3240 7217 27 | 3360 9685 28 | 3476 6759 29 | 3599 8518 30 | 3714 8911 31 | 3840 12345 32 | 3960 9787 33 | 4079 11018 34 | 4196 12033 35 | 4319 12740 36 | 4440 12471 37 | 4558 15251 38 | 4678 13353 39 | 4798 15998 40 | 4920 13395 41 | 5040 13699 42 | 5160 14552 43 | 5280 14972 44 | 5400 15825 45 | 5520 16512 46 | 5639 17379 47 | 5757 17596 48 | 5879 18350 49 | 6000 18976 50 | 6115 19601 51 | 6240 20076 52 | 6354 20515 53 | 6480 21670 54 | 6600 22312 55 | 6716 22647 56 | 6839 23437 57 | 6960 24164 58 | 7080 24723 59 | 7199 25454 60 | 7320 26092 61 | 7440 26912 62 | 7557 27521 63 | 7677 28015 64 | 7800 28885 65 | 7919 29483 66 | 8040 30115 67 | 8160 31236 68 | 8280 31975 69 | 8400 30835 70 | 8520 31565 71 | 8639 32380 72 | 8760 32760 73 | 8879 33590 74 | 8996 34553 75 | 9119 35185 76 | 9239 36146 77 | 9358 36815 78 | 9480 39630 79 | 9596 43022 80 | 9720 41219 81 | 9840 41596 82 | 9960 42354 83 | 10080 43352 84 | 10200 43915 85 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/sqr.log: -------------------------------------------------------------------------------- 1 | 240 114 2 | 359 174 3 | 478 241 4 | 600 311 5 | 720 399 6 | 840 494 7 | 960 599 8 | 1080 799 9 | 1200 931 10 | 1320 911 11 | 1440 1016 12 | 1560 1143 13 | 1680 1281 14 | 1800 1459 15 | 1918 1617 16 | 2039 1763 17 | 2159 1913 18 | 2279 2071 19 | 2399 2240 20 | 2518 2412 21 | 2640 2600 22 | 2760 2792 23 | 2877 3008 24 | 2999 3220 25 | 3119 3405 26 | 3239 3637 27 | 3359 3859 28 | 3480 4094 29 | 3600 4328 30 | 3717 4571 31 | 3838 4840 32 | 3960 5098 33 | 4080 5349 34 | 4200 5617 35 | 4320 5891 36 | 4440 6147 37 | 4560 6444 38 | 4680 6745 39 | 4800 7057 40 | 4918 7317 41 | 5039 7637 42 | 5160 12833 43 | 5280 10098 44 | 5397 8666 45 | 5520 8999 46 | 5639 9376 47 | 5758 9727 48 | 5880 9996 49 | 6000 10427 50 | 6118 10868 51 | 6240 12218 52 | 6359 14010 53 | 6478 14838 54 | 6593 16135 55 | 6719 16503 56 | 6840 13267 57 | 6960 13648 58 | 7080 14118 59 | 7199 14525 60 | 7320 14803 61 | 7439 15378 62 | 7558 15871 63 | 7680 57530 64 | 7800 59550 65 | 7916 61091 66 | 8039 63004 67 | 8160 61136 68 | 8279 62803 69 | 8398 68671 70 | 8520 71001 71 | 8638 71537 72 | 8759 74757 73 | 8880 77164 74 | 9000 78963 75 | 9119 80982 76 | 9239 83142 77 | 9357 85292 78 | 9480 88190 79 | 9600 90343 80 | 9718 86710 81 | 9840 88818 82 | 9954 91034 83 | 10079 93350 84 | 10197 95592 85 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/sqr_kara.log: -------------------------------------------------------------------------------- 1 | 240 115 2 | 360 175 3 | 480 241 4 | 600 312 5 | 719 397 6 | 839 494 7 | 960 597 8 | 1080 696 9 | 1200 794 10 | 1320 908 11 | 1439 1022 12 | 1560 1141 13 | 1678 1284 14 | 1797 1461 15 | 1918 1590 16 | 2040 1764 17 | 2160 1911 18 | 2278 2072 19 | 2399 2263 20 | 2516 2425 21 | 2640 2627 22 | 2756 2809 23 | 2880 3017 24 | 3000 3220 25 | 3119 3413 26 | 3239 3627 27 | 3359 3864 28 | 3479 4087 29 | 3600 4327 30 | 3720 4603 31 | 3840 4867 32 | 3957 5095 33 | 4079 5079 34 | 4200 5623 35 | 4319 5878 36 | 4439 6177 37 | 4560 6467 38 | 4679 6749 39 | 4800 7056 40 | 4920 7384 41 | 5039 7681 42 | 5159 8004 43 | 5280 8332 44 | 5399 8664 45 | 5520 8929 46 | 5638 9340 47 | 5760 9631 48 | 5879 10109 49 | 5999 10458 50 | 6118 10816 51 | 6240 11215 52 | 6359 11550 53 | 6478 11958 54 | 6600 12390 55 | 6718 12801 56 | 6838 13197 57 | 6959 13609 58 | 7079 14033 59 | 7199 16182 60 | 7320 16539 61 | 7440 16952 62 | 7559 16255 63 | 7679 17593 64 | 7800 17107 65 | 7920 17362 66 | 8037 17723 67 | 8159 18072 68 | 8280 19804 69 | 8399 18966 70 | 8519 19510 71 | 8640 19958 72 | 8760 20364 73 | 8878 20674 74 | 9000 21682 75 | 9120 21665 76 | 9237 21945 77 | 9359 22394 78 | 9480 23105 79 | 9598 23334 80 | 9718 25301 81 | 9840 26053 82 | 9960 26565 83 | 10079 26812 84 | 10200 27300 85 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/logs/sub.log: -------------------------------------------------------------------------------- 1 | 480 36 2 | 960 51 3 | 1440 64 4 | 1920 78 5 | 2400 90 6 | 2880 105 7 | 3360 118 8 | 3840 133 9 | 4320 146 10 | 4800 161 11 | 5280 182 12 | 5760 201 13 | 6240 201 14 | 6720 214 15 | 7200 228 16 | 7680 243 17 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/mtest/logtab.h: -------------------------------------------------------------------------------- 1 | const float s_logv_2[] = { 2 | 0.000000000, 0.000000000, 1.000000000, 0.630929754, /* 0 1 2 3 */ 3 | 0.500000000, 0.430676558, 0.386852807, 0.356207187, /* 4 5 6 7 */ 4 | 0.333333333, 0.315464877, 0.301029996, 0.289064826, /* 8 9 10 11 */ 5 | 0.278942946, 0.270238154, 0.262649535, 0.255958025, /* 12 13 14 15 */ 6 | 0.250000000, 0.244650542, 0.239812467, 0.235408913, /* 16 17 18 19 */ 7 | 0.231378213, 0.227670249, 0.224243824, 0.221064729, /* 20 21 22 23 */ 8 | 0.218104292, 0.215338279, 0.212746054, 0.210309918, /* 24 25 26 27 */ 9 | 0.208014598, 0.205846832, 0.203795047, 0.201849087, /* 28 29 30 31 */ 10 | 0.200000000, 0.198239863, 0.196561632, 0.194959022, /* 32 33 34 35 */ 11 | 0.193426404, 0.191958720, 0.190551412, 0.189200360, /* 36 37 38 39 */ 12 | 0.187901825, 0.186652411, 0.185449023, 0.184288833, /* 40 41 42 43 */ 13 | 0.183169251, 0.182087900, 0.181042597, 0.180031327, /* 44 45 46 47 */ 14 | 0.179052232, 0.178103594, 0.177183820, 0.176291434, /* 48 49 50 51 */ 15 | 0.175425064, 0.174583430, 0.173765343, 0.172969690, /* 52 53 54 55 */ 16 | 0.172195434, 0.171441601, 0.170707280, 0.169991616, /* 56 57 58 59 */ 17 | 0.169293808, 0.168613099, 0.167948779, 0.167300179, /* 60 61 62 63 */ 18 | 0.166666667 19 | }; 20 | 21 | 22 | /* $Source$ */ 23 | /* $Revision$ */ 24 | /* $Date$ */ 25 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/mtest/mpi-types.h: -------------------------------------------------------------------------------- 1 | /* Type definitions generated by 'types.pl' */ 2 | typedef char mp_sign; 3 | typedef unsigned short mp_digit; /* 2 byte type */ 4 | typedef unsigned int mp_word; /* 4 byte type */ 5 | typedef unsigned int mp_size; 6 | typedef int mp_err; 7 | 8 | #define MP_DIGIT_BIT (CHAR_BIT*sizeof(mp_digit)) 9 | #define MP_DIGIT_MAX USHRT_MAX 10 | #define MP_WORD_BIT (CHAR_BIT*sizeof(mp_word)) 11 | #define MP_WORD_MAX UINT_MAX 12 | 13 | #define MP_DIGIT_SIZE 2 14 | #define DIGIT_FMT "%04X" 15 | #define RADIX (MP_DIGIT_MAX+1) 16 | 17 | 18 | /* $Source$ */ 19 | /* $Revision$ */ 20 | /* $Date$ */ 21 | -------------------------------------------------------------------------------- /third-party/libtommath-1.2.0/tommath_cutoffs.h: -------------------------------------------------------------------------------- 1 | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | /* 4 | Current values evaluated on an AMD A8-6600K (64-bit). 5 | Type "make tune" to optimize them for your machine but 6 | be aware that it may take a long time. It took 2:30 minutes 7 | on the aforementioned machine for example. 8 | */ 9 | 10 | #define MP_DEFAULT_KARATSUBA_MUL_CUTOFF 80 11 | #define MP_DEFAULT_KARATSUBA_SQR_CUTOFF 120 12 | #define MP_DEFAULT_TOOM_MUL_CUTOFF 350 13 | #define MP_DEFAULT_TOOM_SQR_CUTOFF 400 14 | -------------------------------------------------------------------------------- /tools/winds/libs/levenshtein.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) 5 | 6 | size_t levenshtein(char const* s1, char const* s2) { 7 | 8 | size_t s1_len, s2_len, x, y, last_diagonal, old_diagonal; 9 | 10 | s1_len = strlen(s1); 11 | s2_len = strlen(s2); 12 | 13 | size_t column[s1_len + 1]; 14 | 15 | for (y = 1; y <= s1_len; y++) 16 | column[y] = y; 17 | 18 | for (x = 1; x <= s2_len; x++) { 19 | column[0] = x; 20 | for (y = 1, last_diagonal = x - 1; y <= s1_len; y++) { 21 | old_diagonal = column[y]; 22 | column[y] = MIN3(column[y] + 1, column[y - 1] + 1, last_diagonal + (s1[y-1] == s2[x - 1] ? 0 : 1)); 23 | last_diagonal = old_diagonal; 24 | } 25 | } 26 | 27 | return column[s1_len]; 28 | } 29 | --------------------------------------------------------------------------------