MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

migrate away from libuuid and libsodium

+148 -253
+2 -14
.github/actions/build-native-deps/action.yml
··· 1 1 name: Build Native Dependencies 2 - description: Build libsodium and zlib as needed 2 + description: Build zlib as needed 3 3 4 4 inputs: 5 5 deps: 6 - description: Space-separated list of deps to build (libsodium zlib) 6 + description: Space-separated list of deps to build (zlib) 7 7 required: true 8 8 prefix: 9 9 description: Install prefix ··· 27 27 description: CMake generator (Ninja, MinGW Makefiles, etc) 28 28 required: false 29 29 default: '' 30 - libsodium_version: 31 - required: true 32 30 zlib_version: 33 31 required: true 34 32 ··· 59 57 fi 60 58 echo "prefix=$PREFIX" >> $GITHUB_OUTPUT 61 59 mkdir -p "$PREFIX" 62 - 63 - - name: Build libsodium 64 - if: steps.cache.outputs.cache-hit != 'true' && contains(inputs.deps, 'libsodium') 65 - shell: bash 66 - run: | 67 - git clone --depth 1 --branch ${{ inputs.libsodium_version }}-RELEASE https://github.com/jedisct1/libsodium.git /tmp/libsodium 68 - cd /tmp/libsodium 69 - ./configure CC=${{ inputs.cc }} --prefix=${{ steps.prep.outputs.prefix }} --disable-shared --enable-static 70 - make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4) 71 - make install 72 60 73 61 - name: Build zlib 74 62 if: steps.cache.outputs.cache-hit != 'true' && contains(inputs.deps, 'zlib')
-11
.github/actions/build-project/action.yml
··· 142 142 else 143 143 ${{ inputs.strip }} --strip-unneeded build/${{ inputs.binary_name }} 144 144 fi 145 - 146 - - name: Bundle Windows DLLs 147 - if: runner.os == 'Windows' 148 - shell: bash 149 - run: | 150 - MINGW_BIN=$(cygpath "$RUNNER_TEMP/msys64/clang64/bin") 151 - cp "$MINGW_BIN/libssl-3-x64.dll" build/ 152 - cp "$MINGW_BIN/libcrypto-3-x64.dll" build/ 153 - cp "$MINGW_BIN/libsodium-26.dll" build/ 154 - echo "Bundled DLLs:" 155 - ls -la build/*.dll
+2 -3
.github/versions.json
··· 5 5 "node": "22" 6 6 }, 7 7 "dependencies": { 8 - "libsodium": "1.0.20", 9 8 "zlib": "1.3.1" 10 9 }, 11 10 "targets": { ··· 64 63 "rust_target": "x86_64-apple-darwin", 65 64 "artifact_name": "ant-darwin-x64", 66 65 "binary_name": "ant", 67 - "deps": ["libsodium"], 66 + "deps": [], 68 67 "static_link": false, 69 68 "cmake_generator": null, 70 69 "extra_meson_args": null ··· 76 75 "rust_target": "aarch64-apple-darwin", 77 76 "artifact_name": "ant-darwin-aarch64", 78 77 "binary_name": "ant", 79 - "deps": ["libsodium"], 78 + "deps": [], 80 79 "static_link": false, 81 80 "cmake_generator": null, 82 81 "extra_meson_args": null
+3 -5
.github/workflows/build-platform.yml
··· 70 70 fi 71 71 apt-get update 72 72 apt-get install -y git ca-certificates gnupg wget software-properties-common curl \ 73 - python3-pip ninja-build cmake pkg-config uuid-dev libsodium-dev jq 73 + python3-pip ninja-build cmake pkg-config jq 74 74 curl -fsSL https://deb.nodesource.com/setup_22.x | bash - 75 75 apt-get install -y nodejs 76 76 pip3 install meson tomli ··· 81 81 if: startsWith(inputs.platform, 'linux-musl') 82 82 run: | 83 83 apk add --no-cache git clang lld llvm meson ninja cmake pkgconf curl npm nodejs \ 84 - musl-dev libsodium-dev libsodium-static \ 84 + musl-dev \ 85 85 util-linux-dev util-linux-static linux-headers libunwind-dev libunwind-static \ 86 86 py3-tomli tar xz zstd jq bash 87 87 git config --global --add safe.directory "$GITHUB_WORKSPACE" ··· 108 108 cache: true 109 109 install: >- 110 110 mingw-w64-clang-x86_64-toolchain mingw-w64-clang-x86_64-meson mingw-w64-clang-x86_64-ninja 111 - mingw-w64-clang-x86_64-cmake mingw-w64-clang-x86_64-libsodium 111 + mingw-w64-clang-x86_64-cmake 112 112 mingw-w64-clang-x86_64-lld mingw-w64-clang-x86_64-nodejs git jq 113 113 114 114 - name: Configure git line endings ··· 152 152 echo "deps=$DEPS_LIST" >> $GITHUB_OUTPUT 153 153 154 154 # Dep versions 155 - echo "libsodium_version=$(echo $DEPS | jq -r '.libsodium')" >> $GITHUB_OUTPUT 156 155 echo "zlib_version=$(echo $DEPS | jq -r '.zlib')" >> $GITHUB_OUTPUT 157 156 158 157 # === SETUP ZIG === ··· 191 190 ar: ${{ steps.llvm.outputs.ar }} 192 191 ranlib: ${{ steps.llvm.outputs.ranlib }} 193 192 cmake_generator: ${{ steps.config.outputs.cmake_generator }} 194 - libsodium_version: ${{ steps.config.outputs.libsodium_version }} 195 193 zlib_version: ${{ steps.config.outputs.zlib_version }} 196 194 197 195 # === BUILD PROJECT ===
+5 -21
BUILDING.md
··· 87 87 - **[Zig](https://ziglang.org/)** >= 0.15 (builds the package manager component) 88 88 - **Git** 89 89 90 - System libraries required: 91 - 92 - - **libsodium** 93 - - **libuuid** (Linux/macOS) 94 - 95 - The remaining dependencies are vendored as Meson subprojects under `vendor/` 90 + Dependencies are vendored as Meson subprojects under `vendor/` 96 91 and are fetched automatically: 97 92 98 93 - aklomp-base64 0.5.2 ··· 127 122 128 123 ```bash 129 124 sudo apt-get install python3 python3-pip gcc-14 g++-14 ninja-build cmake \ 130 - pkg-config uuid-dev libsodium-dev nodejs npm 125 + pkg-config nodejs npm 131 126 pip3 install meson 132 127 ``` 133 128 ··· 135 130 136 131 ```bash 137 132 sudo dnf install python3 gcc gcc-c++ ninja-build cmake pkgconf \ 138 - libuuid-devel libsodium-devel nodejs npm 133 + nodejs npm 139 134 pip3 install meson 140 135 ``` 141 136 142 137 - Alpine (musl): 143 138 ```sh 144 139 apk add clang lld llvm meson ninja cmake pkgconf nodejs npm \ 145 - musl-dev libsodium-dev libsodium-static \ 140 + musl-dev \ 146 141 util-linux-dev util-linux-static linux-headers libunwind-dev libunwind-static 147 142 ``` 148 143 ··· 167 162 - Install remaining tools via [Homebrew](https://brew.sh): 168 163 169 164 ```bash 170 - brew install meson ninja llvm libsodium node 165 + brew install meson ninja llvm node 171 166 ``` 172 167 173 168 - Rust and Zig: ··· 378 373 ```bash 379 374 pacman -S mingw-w64-x86_64-toolchain mingw-w64-x86_64-meson \ 380 375 mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake \ 381 - mingw-w64-x86_64-libsodium \ 382 376 mingw-w64-x86_64-lld mingw-w64-x86_64-nodejs git 383 377 ``` 384 378 3. Install Rust via [rustup](https://rustup.rs/) (select the ··· 406 400 ```bash 407 401 ./build/ant.exe --version 408 402 ``` 409 - 410 - > [!WARNING] 411 - > Windows builds require bundling the following DLLs alongside `ant.exe`: 412 - > 413 - > - `libssl-3-x64.dll` 414 - > - `libcrypto-3-x64.dll` 415 - > - `libsodium-26.dll` 416 - > 417 - > These are found in the MSYS2 MINGW64 bin directory 418 - > (`/mingw64/bin/` or equivalent). 419 403 420 404 ## Meson build options 421 405
-61
flake.lock
··· 1 - { 2 - "nodes": { 3 - "flake-utils": { 4 - "inputs": { 5 - "systems": "systems" 6 - }, 7 - "locked": { 8 - "lastModified": 1731533236, 9 - "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", 10 - "owner": "numtide", 11 - "repo": "flake-utils", 12 - "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", 13 - "type": "github" 14 - }, 15 - "original": { 16 - "owner": "numtide", 17 - "repo": "flake-utils", 18 - "type": "github" 19 - } 20 - }, 21 - "nixpkgs": { 22 - "locked": { 23 - "lastModified": 1774273680, 24 - "narHash": "sha256-a++tZ1RQsDb1I0NHrFwdGuRlR5TORvCEUksM459wKUA=", 25 - "owner": "NixOS", 26 - "repo": "nixpkgs", 27 - "rev": "fdc7b8f7b30fdbedec91b71ed82f36e1637483ed", 28 - "type": "github" 29 - }, 30 - "original": { 31 - "owner": "NixOS", 32 - "ref": "nixpkgs-unstable", 33 - "repo": "nixpkgs", 34 - "type": "github" 35 - } 36 - }, 37 - "root": { 38 - "inputs": { 39 - "flake-utils": "flake-utils", 40 - "nixpkgs": "nixpkgs" 41 - } 42 - }, 43 - "systems": { 44 - "locked": { 45 - "lastModified": 1681028828, 46 - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", 47 - "owner": "nix-systems", 48 - "repo": "default", 49 - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", 50 - "type": "github" 51 - }, 52 - "original": { 53 - "owner": "nix-systems", 54 - "repo": "default", 55 - "type": "github" 56 - } 57 - } 58 - }, 59 - "root": "root", 60 - "version": 7 61 - }
-18
flake.nix
··· 1 - { description = "ant dev shell"; 2 - 3 - inputs = { 4 - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; 5 - flake-utils.url = "github:numtide/flake-utils"; 6 - }; 7 - 8 - outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: 9 - let pkgs = nixpkgs.legacyPackages.${system}; in { 10 - devShells.default = pkgs.mkShell { 11 - packages = with pkgs; [ pkg-config libossp_uuid libsodium ]; 12 - 13 - shellHook = '' 14 - export CC="ccache clang" 15 - export MACOSX_DEPLOYMENT_TARGET="15.0" 16 - ''; 17 - }; 18 - }); }
+2 -1
include/modules/crypto.h
··· 1 1 #ifndef CRYPTO_H 2 2 #define CRYPTO_H 3 3 4 + #include <stddef.h> 4 5 #include "types.h" 5 6 6 7 void init_crypto_module(void); 7 - int ensure_crypto_init(void); 8 + int crypto_fill_random(void *buf, size_t len); 8 9 9 10 ant_value_t crypto_library(ant_t *js); 10 11
+3
maidfile.toml
··· 1 + [env] 2 + CC = "ccache clang" 3 + 1 4 [tasks.build] 2 5 path = "build" 3 6 script = "meson compile"
+11 -8
meson/deps/meson.build
··· 8 8 'INSTALL_ENABLED': '0', 9 9 }) 10 10 11 + if host_machine.system() == 'darwin' 12 + deployment_flag = '-mmacosx-version-min=15.0' 13 + boringssl_opts.add_cmake_defines({ 14 + 'CMAKE_EXE_LINKER_FLAGS': deployment_flag, 15 + 'CMAKE_SHARED_LINKER_FLAGS': deployment_flag, 16 + 'CMAKE_MODULE_LINKER_FLAGS': deployment_flag, 17 + }) 18 + endif 19 + 11 20 boringssl_warning_flag = cc.get_argument_syntax() == 'msvc' ? '/W0' : '-w' 12 21 boringssl_opts.append_compile_args('c', boringssl_warning_flag) 13 22 boringssl_opts.append_compile_args('cpp', boringssl_warning_flag) ··· 20 29 21 30 meson.override_dependency('ssl', ssl_dep) 22 31 meson.override_dependency('crypto', crypto_dep) 23 - 24 - libsodium_dep = dependency('libsodium', required: true, static: is_static) 25 32 26 33 if host_machine.system() == 'windows' 27 - uuid_dep = cc.find_library('rpcrt4', required: true) 28 34 secur32_dep = cc.find_library('secur32', required: true) 29 35 ntdll_dep = cc.find_library('ntdll', required: true) 30 36 crypt32_dep = cc.find_library('crypt32', required: true) ··· 33 39 iphlpapi_dep = cc.find_library('iphlpapi', required: true) 34 40 win_deps = [secur32_dep, ntdll_dep, crypt32_dep, userenv_dep, ws2_32_dep, iphlpapi_dep] 35 41 elif host_machine.system() == 'darwin' 36 - uuid_dep = dependency('uuid', required: true) 37 42 security_dep = dependency('appleframeworks', modules: ['Security', 'CoreFoundation'], required: true) 38 43 win_deps = [] 39 44 else 40 - uuid_dep = dependency('uuid', required: true, static: is_static) 41 45 win_deps = [] 42 46 endif 43 47 ··· 218 222 pkg_build_dir = meson.current_build_dir() 219 223 220 224 ant_deps = [ 221 - libffi_dep, uuid_dep, crprintf_dep, 225 + libffi_dep, crprintf_dep, argtable3_dep, 222 226 llhttp, pcre2_dep, libuv_dep, base64_dep, 223 - argtable3_dep, tlsuv_dep, libsodium_dep, 224 - yyjson_dep, minicoro_dep, uuidv7_dep, 227 + yyjson_dep, minicoro_dep, uuidv7_dep, tlsuv_dep, 225 228 uriparser_dep, utf8proc_dep, ssl_dep, crypto_dep, 226 229 zlib_dep, brotli_common_dep, brotli_dec_dep, 227 230 brotli_enc_dep, uthash_dep, lmdb_dep, wamr_dep,
+6
meson/meson.build
··· 24 24 language: 'c' 25 25 ) 26 26 27 + if host_machine.system() == 'darwin' 28 + deployment_flag = '-mmacosx-version-min=15.0' 29 + add_project_arguments(deployment_flag, language: ['c', 'cpp']) 30 + add_project_link_arguments(deployment_flag, language: ['c', 'cpp']) 31 + endif 32 + 27 33 if host_machine.system() == 'linux' 28 34 add_project_arguments('-fno-pie', language: ['c', 'cpp']) 29 35 add_project_link_arguments('-no-pie', language: ['c', 'cpp'])
+74 -67
src/modules/crypto.c
··· 1 - #include <sodium.h> 2 1 #include <string.h> 3 2 #include <time.h> 3 + #include <limits.h> 4 4 #include <openssl/evp.h> 5 + #include <openssl/rand.h> 5 6 6 7 #pragma GCC diagnostic push 7 8 #pragma GCC diagnostic ignored "-Wimplicit-int-conversion" 8 9 #include <uuidv7.h> 9 10 #pragma GCC diagnostic pop 10 - 11 - #ifdef _WIN32 12 - #include <rpc.h> 13 - #else 14 - #include <uuid/uuid.h> 15 - #endif 16 11 17 12 #include "ant.h" 18 13 #include "base64.h" ··· 36 31 unsigned int digest_len; 37 32 bool finalized; 38 33 } ant_hash_state_t; 34 + 35 + int crypto_fill_random(void *buf, size_t len) { 36 + if (len == 0) return 0; 37 + if (len > (size_t)INT_MAX) return -1; 38 + return RAND_bytes((uint8_t *)buf, (int)len) == 1 ? 0 : -1; 39 + } 40 + 41 + static inline ant_value_t crypto_random_error(ant_t *js) { 42 + return js_mkerr(js, "secure random generation failed"); 43 + } 44 + 45 + static ant_value_t crypto_format_uuid_v4(ant_t *js, const uint8_t uuid[16]) { 46 + static char lut[256][2]; 47 + static bool lut_init = false; 48 + char uuid_str[36]; 49 + 50 + if (!lut_init) { 51 + static const char hex[] = "0123456789abcdef"; 52 + for (int i = 0; i < 256; i++) { 53 + lut[i][0] = hex[(unsigned)i >> 4]; 54 + lut[i][1] = hex[(unsigned)i & 0x0f]; 55 + } 56 + lut_init = true; 57 + } 58 + 59 + memcpy(uuid_str + 0, lut[uuid[0]], 2); 60 + memcpy(uuid_str + 2, lut[uuid[1]], 2); 61 + memcpy(uuid_str + 4, lut[uuid[2]], 2); 62 + memcpy(uuid_str + 6, lut[uuid[3]], 2); 63 + 64 + uuid_str[8] = '-'; 65 + memcpy(uuid_str + 9, lut[uuid[4]], 2); 66 + memcpy(uuid_str + 11, lut[uuid[5]], 2); 67 + 68 + uuid_str[13] = '-'; 69 + memcpy(uuid_str + 14, lut[uuid[6]], 2); 70 + memcpy(uuid_str + 16, lut[uuid[7]], 2); 71 + 72 + uuid_str[18] = '-'; 73 + memcpy(uuid_str + 19, lut[uuid[8]], 2); 74 + memcpy(uuid_str + 21, lut[uuid[9]], 2); 75 + 76 + uuid_str[23] = '-'; 77 + memcpy(uuid_str + 24, lut[uuid[10]], 2); 78 + memcpy(uuid_str + 26, lut[uuid[11]], 2); 79 + memcpy(uuid_str + 28, lut[uuid[12]], 2); 80 + memcpy(uuid_str + 30, lut[uuid[13]], 2); 81 + memcpy(uuid_str + 32, lut[uuid[14]], 2); 82 + memcpy(uuid_str + 34, lut[uuid[15]], 2); 83 + 84 + return js_mkstr(js, uuid_str, sizeof(uuid_str)); 85 + } 39 86 40 87 static ant_value_t crypto_make_buffer(ant_t *js, const uint8_t *data, size_t len) { 41 88 ArrayBufferData *buffer = create_array_buffer_data(len); ··· 228 275 } 229 276 230 277 return crypto_make_buffer(js, digest, digest_len); 231 - } 232 - 233 - int ensure_crypto_init(void) { 234 - static int crypto_initialized = 0; 235 - 236 - if (!crypto_initialized) { 237 - if (sodium_init() < 0) return -1; 238 - crypto_initialized = 1; 239 - } 240 - return 0; 241 278 } 242 279 243 280 int uuidv7_new(uint8_t *uuid_out) { ··· 250 287 uint64_t unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000; 251 288 252 289 if (n_rand_consumed > sizeof(rand_bytes) - 10) { 253 - randombytes_buf(rand_bytes, sizeof(rand_bytes)); 290 + if (crypto_fill_random(rand_bytes, sizeof(rand_bytes)) < 0) return -1; 254 291 n_rand_consumed = 0; 255 292 } 256 293 ··· 263 300 264 301 // crypto.random() 265 302 static ant_value_t js_crypto_random(ant_t *js, ant_value_t *args, int nargs) { 266 - if (ensure_crypto_init() < 0) { 267 - return js_mkerr(js, "libsodium initialization failed"); 268 - } 269 - 270 - unsigned int value = randombytes_random(); 303 + unsigned int value = 0; 304 + if (crypto_fill_random(&value, sizeof(value)) < 0) return crypto_random_error(js); 271 305 return js_mknum((double)value); 272 306 } 273 307 ··· 276 310 if (nargs < 1) { 277 311 return js_mkerr(js, "randomBytes requires a length argument"); 278 312 } 279 - 280 - if (ensure_crypto_init() < 0) { 281 - return js_mkerr(js, "libsodium initialization failed"); 282 - } 283 - 313 + 284 314 int length = (int)js_getnum(args[0]); 285 - 286 315 if (length <= 0 || length > 65536) { 287 316 return js_mkerr(js, "invalid length"); 288 317 } ··· 292 321 return js_mkerr(js, "memory allocation failed"); 293 322 } 294 323 295 - randombytes_buf(random_bytes, length); 324 + if (crypto_fill_random(random_bytes, (size_t)length) < 0) { 325 + free(random_bytes); 326 + return crypto_random_error(js); 327 + } 328 + 296 329 ant_value_t array = crypto_make_buffer(js, random_bytes, (size_t)length); 297 330 free(random_bytes); 298 331 ··· 301 334 302 335 // crypto.randomUUID() 303 336 static ant_value_t js_crypto_random_uuid(ant_t *js, ant_value_t *args, int nargs) { 304 - if (ensure_crypto_init() < 0) { 305 - return js_mkerr(js, "libsodium initialization failed"); 306 - } 307 - 308 - char uuid_str[37]; 309 - 310 - #ifdef _WIN32 311 - UUID uuid; 312 - UuidCreate(&uuid); 313 - snprintf(uuid_str, sizeof(uuid_str), 314 - "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 315 - uuid.Data1, uuid.Data2, uuid.Data3, 316 - uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3], 317 - uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]); 318 - #else 319 - uuid_t uuid; 320 - uuid_generate_random(uuid); 321 - uuid_unparse_lower(uuid, uuid_str); 322 - #endif 323 - 324 - return js_mkstr(js, uuid_str, strlen(uuid_str)); 337 + uint8_t uuid[16]; 338 + if (crypto_fill_random(uuid, sizeof(uuid)) < 0) return crypto_random_error(js); 339 + 340 + uuid[6] = (uint8_t)((uuid[6] & 0x0f) | 0x40); 341 + uuid[8] = (uint8_t)((uuid[8] & 0x3f) | 0x80); 342 + 343 + return crypto_format_uuid_v4(js, uuid); 325 344 } 326 345 327 346 // crypto.randomUUIDv7() 328 347 static ant_value_t js_crypto_random_uuidv7(ant_t *js, ant_value_t *args, int nargs) { 329 - if (ensure_crypto_init() < 0) { 330 - return js_mkerr(js, "libsodium initialization failed"); 331 - } 332 - 333 348 uint8_t uuid[16]; 334 349 char uuid_str[37]; 335 350 336 351 int result = uuidv7_new(uuid); 337 - if (result < 0) { 338 - return js_mkerr(js, "UUIDv7 generation failed"); 339 - } 352 + if (result < 0) return js_mkerr(js, "UUIDv7 generation failed"); 340 353 341 354 uuidv7_to_string(uuid, uuid_str); 342 - 343 355 return js_mkstr(js, uuid_str, strlen(uuid_str)); 344 356 } 345 357 ··· 348 360 if (nargs < 1) { 349 361 return js_mkerr(js, "getRandomValues requires a TypedArray argument"); 350 362 } 351 - 352 - if (ensure_crypto_init() < 0) { 353 - return js_mkerr(js, "libsodium initialization failed"); 354 - } 355 - 363 + 356 364 TypedArrayData *ta_data = buffer_get_typedarray_data(args[0]); 357 365 if (!ta_data || !ta_data->buffer) { 358 366 return js_mkerr(js, "argument must be a TypedArray"); ··· 363 371 } 364 372 365 373 uint8_t *ptr = ta_data->buffer->data + ta_data->byte_offset; 366 - randombytes_buf(ptr, ta_data->byte_length); 374 + if (crypto_fill_random(ptr, ta_data->byte_length) < 0) return crypto_random_error(js); 367 375 368 376 return args[0]; 369 377 } 370 378 371 379 static ant_value_t js_crypto_random_fill_sync(ant_t *js, ant_value_t *args, int nargs) { 372 380 if (nargs < 1) return js_mkerr(js, "randomFillSync requires a target"); 373 - if (ensure_crypto_init() < 0) return js_mkerr(js, "libsodium initialization failed"); 374 381 375 382 uint8_t *bytes = NULL; 376 383 size_t len = 0; ··· 396 403 return js_mkerr(js, "randomFillSync range is out of bounds"); 397 404 } 398 405 399 - randombytes_buf(bytes + offset, size); 406 + if (crypto_fill_random(bytes + offset, size) < 0) return crypto_random_error(js); 400 407 return args[0]; 401 408 } 402 409
+40 -44
src/modules/math.c
··· 1 - #include <sodium.h> 2 1 #include <float.h> 3 2 4 3 #include "ant.h" ··· 21 20 static const double math_random_scale = 22 21 1.0 / (double)(UINT64_C(1) << MATH_RANDOM_MANTISSA_BITS); 23 22 24 - static ant_value_t builtin_Math_abs(ant_t *js, ant_value_t *args, int nargs) { 23 + static ant_value_t builtin_Math_abs(ant_params_t) { 25 24 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 26 25 if (isnan(x)) return tov(JS_NAN); 27 26 return tov(fabs(x)); 28 27 } 29 28 30 - static ant_value_t builtin_Math_acos(ant_t *js, ant_value_t *args, int nargs) { 29 + static ant_value_t builtin_Math_acos(ant_params_t) { 31 30 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 32 31 if (isnan(x)) return tov(JS_NAN); 33 32 return tov(acos(x)); 34 33 } 35 34 36 - static ant_value_t builtin_Math_acosh(ant_t *js, ant_value_t *args, int nargs) { 35 + static ant_value_t builtin_Math_acosh(ant_params_t) { 37 36 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 38 37 if (isnan(x)) return tov(JS_NAN); 39 38 return tov(acosh(x)); 40 39 } 41 40 42 - static ant_value_t builtin_Math_asin(ant_t *js, ant_value_t *args, int nargs) { 41 + static ant_value_t builtin_Math_asin(ant_params_t) { 43 42 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 44 43 if (isnan(x)) return tov(JS_NAN); 45 44 return tov(asin(x)); 46 45 } 47 46 48 - static ant_value_t builtin_Math_asinh(ant_t *js, ant_value_t *args, int nargs) { 47 + static ant_value_t builtin_Math_asinh(ant_params_t) { 49 48 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 50 49 if (isnan(x)) return tov(JS_NAN); 51 50 return tov(asinh(x)); 52 51 } 53 52 54 - static ant_value_t builtin_Math_atan(ant_t *js, ant_value_t *args, int nargs) { 53 + static ant_value_t builtin_Math_atan(ant_params_t) { 55 54 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 56 55 if (isnan(x)) return tov(JS_NAN); 57 56 return tov(atan(x)); 58 57 } 59 58 60 - static ant_value_t builtin_Math_atanh(ant_t *js, ant_value_t *args, int nargs) { 59 + static ant_value_t builtin_Math_atanh(ant_params_t) { 61 60 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 62 61 if (isnan(x)) return tov(JS_NAN); 63 62 return tov(atanh(x)); 64 63 } 65 64 66 - static ant_value_t builtin_Math_atan2(ant_t *js, ant_value_t *args, int nargs) { 65 + static ant_value_t builtin_Math_atan2(ant_params_t) { 67 66 double y = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 68 67 double x = (nargs < 2) ? JS_NAN : js_to_number(js, args[1]); 69 68 if (isnan(y) || isnan(x)) return tov(JS_NAN); 70 69 return tov(atan2(y, x)); 71 70 } 72 71 73 - static ant_value_t builtin_Math_cbrt(ant_t *js, ant_value_t *args, int nargs) { 72 + static ant_value_t builtin_Math_cbrt(ant_params_t) { 74 73 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 75 74 if (isnan(x)) return tov(JS_NAN); 76 75 return tov(cbrt(x)); 77 76 } 78 77 79 - static ant_value_t builtin_Math_ceil(ant_t *js, ant_value_t *args, int nargs) { 78 + static ant_value_t builtin_Math_ceil(ant_params_t) { 80 79 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 81 80 if (isnan(x)) return tov(JS_NAN); 82 81 return tov(ceil(x)); 83 82 } 84 83 85 - static ant_value_t builtin_Math_clz32(ant_t *js, ant_value_t *args, int nargs) { 84 + static ant_value_t builtin_Math_clz32(ant_params_t) { 86 85 if (nargs < 1) return tov(32); 87 86 uint32_t n = js_to_uint32(js_to_number(js, args[0])); 88 87 if (n == 0) return tov(32); ··· 93 92 return tov((double)lz); 94 93 } 95 94 96 - static ant_value_t builtin_Math_cos(ant_t *js, ant_value_t *args, int nargs) { 95 + static ant_value_t builtin_Math_cos(ant_params_t) { 97 96 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 98 97 if (isnan(x)) return tov(JS_NAN); 99 98 return tov(cos(x)); 100 99 } 101 100 102 - static ant_value_t builtin_Math_cosh(ant_t *js, ant_value_t *args, int nargs) { 101 + static ant_value_t builtin_Math_cosh(ant_params_t) { 103 102 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 104 103 if (isnan(x)) return tov(JS_NAN); 105 104 return tov(cosh(x)); 106 105 } 107 106 108 - static ant_value_t builtin_Math_exp(ant_t *js, ant_value_t *args, int nargs) { 107 + static ant_value_t builtin_Math_exp(ant_params_t) { 109 108 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 110 109 if (isnan(x)) return tov(JS_NAN); 111 110 return tov(exp(x)); 112 111 } 113 112 114 - static ant_value_t builtin_Math_expm1(ant_t *js, ant_value_t *args, int nargs) { 113 + static ant_value_t builtin_Math_expm1(ant_params_t) { 115 114 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 116 115 if (isnan(x)) return tov(JS_NAN); 117 116 return tov(expm1(x)); 118 117 } 119 118 120 - static ant_value_t builtin_Math_floor(ant_t *js, ant_value_t *args, int nargs) { 119 + static ant_value_t builtin_Math_floor(ant_params_t) { 121 120 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 122 121 if (isnan(x)) return tov(JS_NAN); 123 122 return tov(floor(x)); 124 123 } 125 124 126 - static ant_value_t builtin_Math_fround(ant_t *js, ant_value_t *args, int nargs) { 125 + static ant_value_t builtin_Math_fround(ant_params_t) { 127 126 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 128 127 if (isnan(x)) return tov(JS_NAN); 129 128 return tov((double)(float)x); 130 129 } 131 130 132 - static ant_value_t builtin_Math_hypot(ant_t *js, ant_value_t *args, int nargs) { 131 + static ant_value_t builtin_Math_hypot(ant_params_t) { 133 132 if (nargs == 0) return tov(0.0); 134 133 double acc = 0.0; 135 134 bool saw_nan = false; ··· 143 142 return tov(acc); 144 143 } 145 144 146 - static ant_value_t builtin_Math_imul(ant_t *js, ant_value_t *args, int nargs) { 145 + static ant_value_t builtin_Math_imul(ant_params_t) { 147 146 if (nargs < 2) return tov(0); 148 147 int32_t a = js_to_int32(js_to_number(js, args[0])); 149 148 int32_t b = js_to_int32(js_to_number(js, args[1])); 150 149 return tov((double)((int32_t)((uint32_t)a * (uint32_t)b))); 151 150 } 152 151 153 - static ant_value_t builtin_Math_log(ant_t *js, ant_value_t *args, int nargs) { 152 + static ant_value_t builtin_Math_log(ant_params_t) { 154 153 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 155 154 if (isnan(x)) return tov(JS_NAN); 156 155 return tov(log(x)); 157 156 } 158 157 159 - static ant_value_t builtin_Math_log1p(ant_t *js, ant_value_t *args, int nargs) { 158 + static ant_value_t builtin_Math_log1p(ant_params_t) { 160 159 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 161 160 if (isnan(x)) return tov(JS_NAN); 162 161 return tov(log1p(x)); 163 162 } 164 163 165 - static ant_value_t builtin_Math_log10(ant_t *js, ant_value_t *args, int nargs) { 164 + static ant_value_t builtin_Math_log10(ant_params_t) { 166 165 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 167 166 if (isnan(x)) return tov(JS_NAN); 168 167 return tov(log10(x)); 169 168 } 170 169 171 - static ant_value_t builtin_Math_log2(ant_t *js, ant_value_t *args, int nargs) { 170 + static ant_value_t builtin_Math_log2(ant_params_t) { 172 171 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 173 172 if (isnan(x)) return tov(JS_NAN); 174 173 return tov(log2(x)); 175 174 } 176 175 177 - static ant_value_t builtin_Math_max(ant_t *js, ant_value_t *args, int nargs) { 176 + static ant_value_t builtin_Math_max(ant_params_t) { 178 177 if (nargs == 0) return tov(JS_NEG_INF); 179 178 double max_val = js_to_number(js, args[0]); 180 179 if (isnan(max_val)) return tov(JS_NAN); ··· 182 181 double v = js_to_number(js, args[i]); 183 182 if (isnan(v)) return tov(JS_NAN); 184 183 if (v > max_val) { max_val = v; continue; } 185 - if (v == 0.0 && max_val == 0.0 && !signbit(v) && signbit(max_val)) { 186 - max_val = v; 187 - } 184 + if (v == 0.0 && max_val == 0.0 && !signbit(v) && signbit(max_val)) max_val = v; 188 185 } 189 186 return tov(max_val); 190 187 } 191 188 192 - static ant_value_t builtin_Math_min(ant_t *js, ant_value_t *args, int nargs) { 189 + static ant_value_t builtin_Math_min(ant_params_t) { 193 190 if (nargs == 0) return tov(JS_INF); 194 191 double min_val = js_to_number(js, args[0]); 195 192 if (isnan(min_val)) return tov(JS_NAN); ··· 209 206 return tov(min_val); 210 207 } 211 208 212 - static ant_value_t builtin_Math_pow(ant_t *js, ant_value_t *args, int nargs) { 209 + static ant_value_t builtin_Math_pow(ant_params_t) { 213 210 double base = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 214 211 double exp = (nargs < 2) ? JS_NAN : js_to_number(js, args[1]); 215 212 if (isnan(base) || isnan(exp)) return tov(JS_NAN); 216 213 return tov(pow(base, exp)); 217 214 } 218 215 219 - static ant_value_t builtin_Math_random(ant_t *js, ant_value_t *args, int nargs) { 220 - if (ensure_crypto_init() < 0) { 221 - return js_mkerr(js, "libsodium initialization failed"); 216 + static ant_value_t builtin_Math_random(ant_params_t) { 217 + uint64_t r = 0; 218 + if (crypto_fill_random(&r, sizeof(r)) < 0) { 219 + return js_mkerr(js, "secure random generation failed"); 222 220 } 223 - 224 - uint64_t r = 0; randombytes_buf(&r, sizeof(r)); 225 - uint64_t fraction = r >> MATH_RANDOM_DISCARD_BITS; 226 221 222 + uint64_t fraction = r >> MATH_RANDOM_DISCARD_BITS; 227 223 return tov((double)fraction * math_random_scale); 228 224 } 229 225 230 - static ant_value_t builtin_Math_round(ant_t *js, ant_value_t *args, int nargs) { 226 + static ant_value_t builtin_Math_round(ant_params_t) { 231 227 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 232 228 if (isnan(x) || isinf(x) || x == 0.0) return tov(x); 233 229 if (x < 0.0 && x >= -0.5) return tov(-0.0); 234 230 return tov(floor(x + 0.5)); 235 231 } 236 232 237 - static ant_value_t builtin_Math_sign(ant_t *js, ant_value_t *args, int nargs) { 233 + static ant_value_t builtin_Math_sign(ant_params_t) { 238 234 double v = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 239 235 if (isnan(v)) return tov(JS_NAN); 240 236 if (v > 0) return tov(1.0); ··· 242 238 return tov(v); 243 239 } 244 240 245 - static ant_value_t builtin_Math_sin(ant_t *js, ant_value_t *args, int nargs) { 241 + static ant_value_t builtin_Math_sin(ant_params_t) { 246 242 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 247 243 if (isnan(x)) return tov(JS_NAN); 248 244 return tov(sin(x)); 249 245 } 250 246 251 - static ant_value_t builtin_Math_sinh(ant_t *js, ant_value_t *args, int nargs) { 247 + static ant_value_t builtin_Math_sinh(ant_params_t) { 252 248 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 253 249 if (isnan(x)) return tov(JS_NAN); 254 250 return tov(sinh(x)); 255 251 } 256 252 257 - static ant_value_t builtin_Math_sqrt(ant_t *js, ant_value_t *args, int nargs) { 253 + static ant_value_t builtin_Math_sqrt(ant_params_t) { 258 254 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 259 255 if (isnan(x)) return tov(JS_NAN); 260 256 return tov(sqrt(x)); 261 257 } 262 258 263 - static ant_value_t builtin_Math_tan(ant_t *js, ant_value_t *args, int nargs) { 259 + static ant_value_t builtin_Math_tan(ant_params_t) { 264 260 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 265 261 if (isnan(x)) return tov(JS_NAN); 266 262 return tov(tan(x)); 267 263 } 268 264 269 - static ant_value_t builtin_Math_tanh(ant_t *js, ant_value_t *args, int nargs) { 265 + static ant_value_t builtin_Math_tanh(ant_params_t) { 270 266 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 271 267 if (isnan(x)) return tov(JS_NAN); 272 268 return tov(tanh(x)); 273 269 } 274 270 275 - static ant_value_t builtin_Math_trunc(ant_t *js, ant_value_t *args, int nargs) { 271 + static ant_value_t builtin_Math_trunc(ant_params_t) { 276 272 double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 277 273 if (isnan(x)) return tov(JS_NAN); 278 274 return tov(trunc(x));