@mtcute/core 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/base-client.js +4 -1
- package/cjs/base-client.js.map +1 -1
- package/cjs/network/auth-key.d.ts +3 -3
- package/cjs/network/auth-key.js +12 -12
- package/cjs/network/auth-key.js.map +1 -1
- package/cjs/network/auth-key.test.d.ts +1 -0
- package/cjs/network/auth-key.test.js +111 -0
- package/cjs/network/auth-key.test.js.map +1 -0
- package/cjs/network/authorization.js +62 -57
- package/cjs/network/authorization.js.map +1 -1
- package/cjs/network/config-manager.js +2 -1
- package/cjs/network/config-manager.js.map +1 -1
- package/cjs/network/config-manager.test.d.ts +1 -0
- package/cjs/network/config-manager.test.js +170 -0
- package/cjs/network/config-manager.test.js.map +1 -0
- package/cjs/network/mtproto-session.d.ts +2 -2
- package/cjs/network/mtproto-session.js +2 -2
- package/cjs/network/mtproto-session.js.map +1 -1
- package/cjs/network/multi-session-connection.d.ts +1 -1
- package/cjs/network/multi-session-connection.js +2 -2
- package/cjs/network/multi-session-connection.js.map +1 -1
- package/cjs/network/network-manager.d.ts +1 -0
- package/cjs/network/network-manager.js +21 -27
- package/cjs/network/network-manager.js.map +1 -1
- package/cjs/network/persistent-connection.test.d.ts +1 -0
- package/cjs/network/persistent-connection.test.js +157 -0
- package/cjs/network/persistent-connection.test.js.map +1 -0
- package/cjs/network/session-connection.d.ts +2 -1
- package/cjs/network/session-connection.js +51 -37
- package/cjs/network/session-connection.js.map +1 -1
- package/cjs/network/transports/intermediate.d.ts +3 -0
- package/cjs/network/transports/intermediate.js +5 -3
- package/cjs/network/transports/intermediate.js.map +1 -1
- package/cjs/network/transports/intermediate.test.d.ts +1 -0
- package/cjs/network/transports/intermediate.test.js +86 -0
- package/cjs/network/transports/intermediate.test.js.map +1 -0
- package/cjs/network/transports/obfuscated.js +15 -19
- package/cjs/network/transports/obfuscated.js.map +1 -1
- package/cjs/network/transports/obfuscated.test.d.ts +1 -0
- package/cjs/network/transports/obfuscated.test.js +131 -0
- package/cjs/network/transports/obfuscated.test.js.map +1 -0
- package/cjs/network/transports/tcp.js +1 -1
- package/cjs/network/transports/tcp.js.map +1 -1
- package/cjs/network/transports/tcp.test.d.ts +1 -0
- package/cjs/network/transports/tcp.test.js +97 -0
- package/cjs/network/transports/tcp.test.js.map +1 -0
- package/cjs/network/transports/websocket.test.d.ts +1 -0
- package/cjs/network/transports/websocket.test.js +91 -0
- package/cjs/network/transports/websocket.test.js.map +1 -0
- package/cjs/storage/abstract.d.ts +23 -2
- package/cjs/storage/abstract.js.map +1 -1
- package/cjs/storage/json.js +8 -9
- package/cjs/storage/json.js.map +1 -1
- package/cjs/storage/json.test.d.ts +1 -0
- package/cjs/storage/json.test.js +37 -0
- package/cjs/storage/json.test.js.map +1 -0
- package/cjs/storage/localstorage.js +5 -2
- package/cjs/storage/localstorage.js.map +1 -1
- package/cjs/storage/localstorage.test.d.ts +1 -0
- package/cjs/storage/localstorage.test.js +23 -0
- package/cjs/storage/localstorage.test.js.map +1 -0
- package/cjs/storage/memory.d.ts +7 -8
- package/cjs/storage/memory.js +7 -7
- package/cjs/storage/memory.js.map +1 -1
- package/cjs/storage/memory.test.d.ts +1 -0
- package/cjs/storage/memory.test.js +47 -0
- package/cjs/storage/memory.test.js.map +1 -0
- package/cjs/utils/async-lock.test.d.ts +1 -0
- package/cjs/utils/async-lock.test.js +25 -0
- package/cjs/utils/async-lock.test.js.map +1 -0
- package/cjs/utils/bigint-utils.d.ts +18 -11
- package/cjs/utils/bigint-utils.js +148 -47
- package/cjs/utils/bigint-utils.js.map +1 -1
- package/cjs/utils/bigint-utils.test.d.ts +1 -0
- package/cjs/utils/bigint-utils.test.js +150 -0
- package/cjs/utils/bigint-utils.test.js.map +1 -0
- package/cjs/utils/buffer-utils.d.ts +7 -1
- package/cjs/utils/buffer-utils.js +16 -3
- package/cjs/utils/buffer-utils.js.map +1 -1
- package/cjs/utils/buffer-utils.test.d.ts +1 -0
- package/cjs/utils/buffer-utils.test.js +69 -0
- package/cjs/utils/buffer-utils.test.js.map +1 -0
- package/cjs/utils/condition-variable.test.d.ts +1 -0
- package/cjs/utils/condition-variable.test.js +27 -0
- package/cjs/utils/condition-variable.test.js.map +1 -0
- package/cjs/utils/controllable-promise.test.d.ts +1 -0
- package/cjs/utils/controllable-promise.test.js +17 -0
- package/cjs/utils/controllable-promise.test.js.map +1 -0
- package/cjs/utils/crypto/abstract.d.ts +16 -9
- package/cjs/utils/crypto/abstract.js +6 -9
- package/cjs/utils/crypto/abstract.js.map +1 -1
- package/cjs/utils/crypto/factorization.d.ts +2 -1
- package/cjs/utils/crypto/factorization.js +28 -36
- package/cjs/utils/crypto/factorization.js.map +1 -1
- package/cjs/utils/crypto/index.d.ts +4 -0
- package/cjs/utils/crypto/index.js +4 -0
- package/cjs/utils/crypto/index.js.map +1 -1
- package/cjs/utils/crypto/keys.d.ts +2 -2
- package/cjs/utils/crypto/keys.js +4 -4
- package/cjs/utils/crypto/keys.js.map +1 -1
- package/cjs/utils/crypto/keys.test.d.ts +1 -0
- package/cjs/utils/crypto/keys.test.js +40 -0
- package/cjs/utils/crypto/keys.test.js.map +1 -0
- package/cjs/utils/crypto/miller-rabin.d.ts +2 -2
- package/cjs/utils/crypto/miller-rabin.js +20 -22
- package/cjs/utils/crypto/miller-rabin.js.map +1 -1
- package/cjs/utils/crypto/miller-rabin.test.d.ts +1 -0
- package/cjs/utils/crypto/miller-rabin.test.js +60 -0
- package/cjs/utils/crypto/miller-rabin.test.js.map +1 -0
- package/cjs/utils/crypto/mtproto.d.ts +3 -3
- package/cjs/utils/crypto/mtproto.js +12 -12
- package/cjs/utils/crypto/mtproto.js.map +1 -1
- package/cjs/utils/crypto/mtproto.test.d.ts +1 -0
- package/cjs/utils/crypto/mtproto.test.js +47 -0
- package/cjs/utils/crypto/mtproto.test.js.map +1 -0
- package/cjs/utils/crypto/node.d.ts +21 -0
- package/cjs/utils/crypto/node.js +74 -0
- package/cjs/utils/crypto/node.js.map +1 -0
- package/cjs/utils/crypto/node.test.d.ts +1 -0
- package/cjs/utils/crypto/node.test.js +13 -0
- package/cjs/utils/crypto/node.test.js.map +1 -0
- package/cjs/utils/crypto/password.js +22 -33
- package/cjs/utils/crypto/password.js.map +1 -1
- package/cjs/utils/crypto/password.test.d.ts +1 -0
- package/cjs/utils/crypto/password.test.js +95 -0
- package/cjs/utils/crypto/password.test.js.map +1 -0
- package/cjs/utils/crypto/utils.test.d.ts +1 -0
- package/cjs/utils/crypto/utils.test.js +49 -0
- package/cjs/utils/crypto/utils.test.js.map +1 -0
- package/cjs/utils/crypto/wasm.d.ts +22 -0
- package/cjs/utils/crypto/wasm.js +41 -0
- package/cjs/utils/crypto/wasm.js.map +1 -0
- package/cjs/utils/crypto/web.d.ts +11 -0
- package/cjs/utils/crypto/web.js +40 -0
- package/cjs/utils/crypto/web.js.map +1 -0
- package/cjs/utils/crypto/web.test.d.ts +1 -0
- package/cjs/utils/crypto/web.test.js +17 -0
- package/cjs/utils/crypto/web.test.js.map +1 -0
- package/cjs/utils/deque.d.ts +1 -1
- package/cjs/utils/deque.js +9 -3
- package/cjs/utils/deque.js.map +1 -1
- package/cjs/utils/deque.test.d.ts +1 -0
- package/cjs/utils/deque.test.js +128 -0
- package/cjs/utils/deque.test.js.map +1 -0
- package/cjs/utils/function-utils.d.ts +4 -1
- package/cjs/utils/function-utils.js +8 -1
- package/cjs/utils/function-utils.js.map +1 -1
- package/cjs/utils/function-utils.test.d.ts +1 -0
- package/cjs/utils/function-utils.test.js +23 -0
- package/cjs/utils/function-utils.test.js.map +1 -0
- package/cjs/utils/index.d.ts +0 -1
- package/cjs/utils/index.js +0 -1
- package/cjs/utils/index.js.map +1 -1
- package/cjs/utils/linked-list.test.d.ts +1 -0
- package/cjs/utils/linked-list.test.js +36 -0
- package/cjs/utils/linked-list.test.js.map +1 -0
- package/cjs/utils/links/bots.test.d.ts +1 -0
- package/cjs/utils/links/bots.test.js +152 -0
- package/cjs/utils/links/bots.test.js.map +1 -0
- package/cjs/utils/links/chat.test.d.ts +1 -0
- package/cjs/utils/links/chat.test.js +84 -0
- package/cjs/utils/links/chat.test.js.map +1 -0
- package/cjs/utils/links/misc.test.d.ts +1 -0
- package/cjs/utils/links/misc.test.js +153 -0
- package/cjs/utils/links/misc.test.js.map +1 -0
- package/cjs/utils/links/proxy.test.d.ts +1 -0
- package/cjs/utils/links/proxy.test.js +74 -0
- package/cjs/utils/links/proxy.test.js.map +1 -0
- package/cjs/utils/links/user.test.d.ts +1 -0
- package/cjs/utils/links/user.test.js +63 -0
- package/cjs/utils/links/user.test.js.map +1 -0
- package/cjs/utils/logger.d.ts +0 -1
- package/cjs/utils/logger.js +3 -4
- package/cjs/utils/logger.js.map +1 -1
- package/cjs/utils/logger.test.d.ts +1 -0
- package/cjs/utils/logger.test.js +125 -0
- package/cjs/utils/logger.test.js.map +1 -0
- package/cjs/utils/long-utils.test.d.ts +1 -0
- package/cjs/utils/long-utils.test.js +158 -0
- package/cjs/utils/long-utils.test.js.map +1 -0
- package/cjs/utils/lru-map.d.ts +1 -0
- package/cjs/utils/lru-map.js +6 -0
- package/cjs/utils/lru-map.js.map +1 -1
- package/cjs/utils/lru-map.test.d.ts +1 -0
- package/cjs/utils/lru-map.test.js +96 -0
- package/cjs/utils/lru-map.test.js.map +1 -0
- package/cjs/utils/lru-set.test.d.ts +1 -0
- package/cjs/utils/lru-set.test.js +77 -0
- package/cjs/utils/lru-set.test.js.map +1 -0
- package/cjs/utils/peer-utils.js +5 -1
- package/cjs/utils/peer-utils.js.map +1 -1
- package/cjs/utils/peer-utils.test.d.ts +1 -0
- package/cjs/utils/peer-utils.test.js +122 -0
- package/cjs/utils/peer-utils.test.js.map +1 -0
- package/cjs/utils/platform/crypto.js +2 -2
- package/cjs/utils/platform/crypto.js.map +1 -1
- package/cjs/utils/platform/crypto.web.js +2 -2
- package/cjs/utils/platform/crypto.web.js.map +1 -1
- package/cjs/utils/sorted-array.test.d.ts +1 -0
- package/cjs/utils/sorted-array.test.js +66 -0
- package/cjs/utils/sorted-array.test.js.map +1 -0
- package/cjs/utils/string-session.js +4 -2
- package/cjs/utils/string-session.js.map +1 -1
- package/cjs/utils/string-session.test.d.ts +1 -0
- package/cjs/utils/string-session.test.js +106 -0
- package/cjs/utils/string-session.test.js.map +1 -0
- package/cjs/utils/tl-json.test.d.ts +1 -0
- package/cjs/utils/tl-json.test.js +83 -0
- package/cjs/utils/tl-json.test.js.map +1 -0
- package/cjs/utils/type-assertions.test.d.ts +1 -0
- package/cjs/utils/type-assertions.test.js +67 -0
- package/cjs/utils/type-assertions.test.js.map +1 -0
- package/esm/base-client.js +4 -1
- package/esm/base-client.js.map +1 -1
- package/esm/network/auth-key.d.ts +3 -3
- package/esm/network/auth-key.js +13 -13
- package/esm/network/auth-key.js.map +1 -1
- package/esm/network/auth-key.test.d.ts +1 -0
- package/esm/network/auth-key.test.js +106 -0
- package/esm/network/auth-key.test.js.map +1 -0
- package/esm/network/authorization.js +64 -59
- package/esm/network/authorization.js.map +1 -1
- package/esm/network/config-manager.js +2 -1
- package/esm/network/config-manager.js.map +1 -1
- package/esm/network/config-manager.test.d.ts +1 -0
- package/esm/network/config-manager.test.js +168 -0
- package/esm/network/config-manager.test.js.map +1 -0
- package/esm/network/mtproto-session.d.ts +2 -2
- package/esm/network/mtproto-session.js +2 -2
- package/esm/network/mtproto-session.js.map +1 -1
- package/esm/network/multi-session-connection.d.ts +1 -1
- package/esm/network/multi-session-connection.js +2 -2
- package/esm/network/multi-session-connection.js.map +1 -1
- package/esm/network/network-manager.d.ts +1 -0
- package/esm/network/network-manager.js +21 -27
- package/esm/network/network-manager.js.map +1 -1
- package/esm/network/persistent-connection.test.d.ts +1 -0
- package/esm/network/persistent-connection.test.js +155 -0
- package/esm/network/persistent-connection.test.js.map +1 -0
- package/esm/network/session-connection.d.ts +2 -1
- package/esm/network/session-connection.js +53 -39
- package/esm/network/session-connection.js.map +1 -1
- package/esm/network/transports/intermediate.d.ts +3 -0
- package/esm/network/transports/intermediate.js +6 -4
- package/esm/network/transports/intermediate.js.map +1 -1
- package/esm/network/transports/intermediate.test.d.ts +1 -0
- package/esm/network/transports/intermediate.test.js +84 -0
- package/esm/network/transports/intermediate.test.js.map +1 -0
- package/esm/network/transports/obfuscated.js +16 -20
- package/esm/network/transports/obfuscated.js.map +1 -1
- package/esm/network/transports/obfuscated.test.d.ts +1 -0
- package/esm/network/transports/obfuscated.test.js +129 -0
- package/esm/network/transports/obfuscated.test.js.map +1 -0
- package/esm/network/transports/tcp.js +1 -1
- package/esm/network/transports/tcp.js.map +1 -1
- package/esm/network/transports/tcp.test.d.ts +1 -0
- package/esm/network/transports/tcp.test.js +95 -0
- package/esm/network/transports/tcp.test.js.map +1 -0
- package/esm/network/transports/websocket.test.d.ts +1 -0
- package/esm/network/transports/websocket.test.js +89 -0
- package/esm/network/transports/websocket.test.js.map +1 -0
- package/esm/storage/abstract.d.ts +23 -2
- package/esm/storage/abstract.js.map +1 -1
- package/esm/storage/json.js +8 -9
- package/esm/storage/json.js.map +1 -1
- package/esm/storage/json.test.d.ts +1 -0
- package/esm/storage/json.test.js +35 -0
- package/esm/storage/json.test.js.map +1 -0
- package/esm/storage/localstorage.js +5 -2
- package/esm/storage/localstorage.js.map +1 -1
- package/esm/storage/localstorage.test.d.ts +1 -0
- package/esm/storage/localstorage.test.js +21 -0
- package/esm/storage/localstorage.test.js.map +1 -0
- package/esm/storage/memory.d.ts +7 -8
- package/esm/storage/memory.js +7 -7
- package/esm/storage/memory.js.map +1 -1
- package/esm/storage/memory.test.d.ts +1 -0
- package/esm/storage/memory.test.js +45 -0
- package/esm/storage/memory.test.js.map +1 -0
- package/esm/utils/async-lock.test.d.ts +1 -0
- package/esm/utils/async-lock.test.js +23 -0
- package/esm/utils/async-lock.test.js.map +1 -0
- package/esm/utils/bigint-utils.d.ts +18 -11
- package/esm/utils/bigint-utils.js +142 -44
- package/esm/utils/bigint-utils.js.map +1 -1
- package/esm/utils/bigint-utils.test.d.ts +1 -0
- package/esm/utils/bigint-utils.test.js +148 -0
- package/esm/utils/bigint-utils.test.js.map +1 -0
- package/esm/utils/buffer-utils.d.ts +7 -1
- package/esm/utils/buffer-utils.js +14 -1
- package/esm/utils/buffer-utils.js.map +1 -1
- package/esm/utils/buffer-utils.test.d.ts +1 -0
- package/esm/utils/buffer-utils.test.js +67 -0
- package/esm/utils/buffer-utils.test.js.map +1 -0
- package/esm/utils/condition-variable.test.d.ts +1 -0
- package/esm/utils/condition-variable.test.js +25 -0
- package/esm/utils/condition-variable.test.js.map +1 -0
- package/esm/utils/controllable-promise.test.d.ts +1 -0
- package/esm/utils/controllable-promise.test.js +15 -0
- package/esm/utils/controllable-promise.test.js.map +1 -0
- package/esm/utils/crypto/abstract.d.ts +16 -9
- package/esm/utils/crypto/abstract.js +6 -9
- package/esm/utils/crypto/abstract.js.map +1 -1
- package/esm/utils/crypto/factorization.d.ts +2 -1
- package/esm/utils/crypto/factorization.js +29 -34
- package/esm/utils/crypto/factorization.js.map +1 -1
- package/esm/utils/crypto/index.d.ts +4 -0
- package/esm/utils/crypto/index.js +4 -0
- package/esm/utils/crypto/index.js.map +1 -1
- package/esm/utils/crypto/keys.d.ts +2 -2
- package/esm/utils/crypto/keys.js +4 -4
- package/esm/utils/crypto/keys.js.map +1 -1
- package/esm/utils/crypto/keys.test.d.ts +1 -0
- package/esm/utils/crypto/keys.test.js +38 -0
- package/esm/utils/crypto/keys.test.js.map +1 -0
- package/esm/utils/crypto/miller-rabin.d.ts +2 -2
- package/esm/utils/crypto/miller-rabin.js +21 -20
- package/esm/utils/crypto/miller-rabin.js.map +1 -1
- package/esm/utils/crypto/miller-rabin.test.d.ts +1 -0
- package/esm/utils/crypto/miller-rabin.test.js +58 -0
- package/esm/utils/crypto/miller-rabin.test.js.map +1 -0
- package/esm/utils/crypto/mtproto.d.ts +3 -3
- package/esm/utils/crypto/mtproto.js +12 -12
- package/esm/utils/crypto/mtproto.js.map +1 -1
- package/esm/utils/crypto/mtproto.test.d.ts +1 -0
- package/esm/utils/crypto/mtproto.test.js +45 -0
- package/esm/utils/crypto/mtproto.test.js.map +1 -0
- package/esm/utils/crypto/node.d.ts +21 -0
- package/esm/utils/crypto/node.js +69 -0
- package/esm/utils/crypto/node.js.map +1 -0
- package/esm/utils/crypto/node.test.d.ts +1 -0
- package/esm/utils/crypto/node.test.js +11 -0
- package/esm/utils/crypto/node.test.js.map +1 -0
- package/esm/utils/crypto/password.js +24 -32
- package/esm/utils/crypto/password.js.map +1 -1
- package/esm/utils/crypto/password.test.d.ts +1 -0
- package/esm/utils/crypto/password.test.js +90 -0
- package/esm/utils/crypto/password.test.js.map +1 -0
- package/esm/utils/crypto/utils.test.d.ts +1 -0
- package/esm/utils/crypto/utils.test.js +47 -0
- package/esm/utils/crypto/utils.test.js.map +1 -0
- package/esm/utils/crypto/wasm.d.ts +22 -0
- package/esm/utils/crypto/wasm.js +37 -0
- package/esm/utils/crypto/wasm.js.map +1 -0
- package/esm/utils/crypto/web.d.ts +11 -0
- package/esm/utils/crypto/web.js +36 -0
- package/esm/utils/crypto/web.js.map +1 -0
- package/esm/utils/crypto/web.test.d.ts +1 -0
- package/esm/utils/crypto/web.test.js +15 -0
- package/esm/utils/crypto/web.test.js.map +1 -0
- package/esm/utils/deque.d.ts +1 -1
- package/esm/utils/deque.js +9 -3
- package/esm/utils/deque.js.map +1 -1
- package/esm/utils/deque.test.d.ts +1 -0
- package/esm/utils/deque.test.js +126 -0
- package/esm/utils/deque.test.js.map +1 -0
- package/esm/utils/function-utils.d.ts +4 -1
- package/esm/utils/function-utils.js +8 -1
- package/esm/utils/function-utils.js.map +1 -1
- package/esm/utils/function-utils.test.d.ts +1 -0
- package/esm/utils/function-utils.test.js +21 -0
- package/esm/utils/function-utils.test.js.map +1 -0
- package/esm/utils/index.d.ts +0 -1
- package/esm/utils/index.js +0 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/linked-list.test.d.ts +1 -0
- package/esm/utils/linked-list.test.js +34 -0
- package/esm/utils/linked-list.test.js.map +1 -0
- package/esm/utils/links/bots.test.d.ts +1 -0
- package/esm/utils/links/bots.test.js +150 -0
- package/esm/utils/links/bots.test.js.map +1 -0
- package/esm/utils/links/chat.test.d.ts +1 -0
- package/esm/utils/links/chat.test.js +82 -0
- package/esm/utils/links/chat.test.js.map +1 -0
- package/esm/utils/links/misc.test.d.ts +1 -0
- package/esm/utils/links/misc.test.js +151 -0
- package/esm/utils/links/misc.test.js.map +1 -0
- package/esm/utils/links/proxy.test.d.ts +1 -0
- package/esm/utils/links/proxy.test.js +72 -0
- package/esm/utils/links/proxy.test.js.map +1 -0
- package/esm/utils/links/user.test.d.ts +1 -0
- package/esm/utils/links/user.test.js +61 -0
- package/esm/utils/links/user.test.js.map +1 -0
- package/esm/utils/logger.d.ts +0 -1
- package/esm/utils/logger.js +3 -4
- package/esm/utils/logger.js.map +1 -1
- package/esm/utils/logger.test.d.ts +1 -0
- package/esm/utils/logger.test.js +120 -0
- package/esm/utils/logger.test.js.map +1 -0
- package/esm/utils/long-utils.test.d.ts +1 -0
- package/esm/utils/long-utils.test.js +153 -0
- package/esm/utils/long-utils.test.js.map +1 -0
- package/esm/utils/lru-map.d.ts +1 -0
- package/esm/utils/lru-map.js +6 -0
- package/esm/utils/lru-map.js.map +1 -1
- package/esm/utils/lru-map.test.d.ts +1 -0
- package/esm/utils/lru-map.test.js +94 -0
- package/esm/utils/lru-map.test.js.map +1 -0
- package/esm/utils/lru-set.test.d.ts +1 -0
- package/esm/utils/lru-set.test.js +72 -0
- package/esm/utils/lru-set.test.js.map +1 -0
- package/esm/utils/peer-utils.js +5 -1
- package/esm/utils/peer-utils.js.map +1 -1
- package/esm/utils/peer-utils.test.d.ts +1 -0
- package/esm/utils/peer-utils.test.js +117 -0
- package/esm/utils/peer-utils.test.js.map +1 -0
- package/esm/utils/platform/crypto.js +1 -1
- package/esm/utils/platform/crypto.js.map +1 -1
- package/esm/utils/platform/crypto.web.js +2 -2
- package/esm/utils/platform/crypto.web.js.map +1 -1
- package/esm/utils/sorted-array.test.d.ts +1 -0
- package/esm/utils/sorted-array.test.js +64 -0
- package/esm/utils/sorted-array.test.js.map +1 -0
- package/esm/utils/string-session.js +4 -2
- package/esm/utils/string-session.js.map +1 -1
- package/esm/utils/string-session.test.d.ts +1 -0
- package/esm/utils/string-session.test.js +104 -0
- package/esm/utils/string-session.test.js.map +1 -0
- package/esm/utils/tl-json.test.d.ts +1 -0
- package/esm/utils/tl-json.test.js +81 -0
- package/esm/utils/tl-json.test.js.map +1 -0
- package/esm/utils/type-assertions.test.d.ts +1 -0
- package/esm/utils/type-assertions.test.js +65 -0
- package/esm/utils/type-assertions.test.js.map +1 -0
- package/package.json +4 -5
- package/cjs/utils/crypto/common.d.ts +0 -12
- package/cjs/utils/crypto/common.js +0 -54
- package/cjs/utils/crypto/common.js.map +0 -1
- package/cjs/utils/crypto/node-crypto.d.ts +0 -10
- package/cjs/utils/crypto/node-crypto.js +0 -46
- package/cjs/utils/crypto/node-crypto.js.map +0 -1
- package/cjs/utils/crypto/subtle.d.ts +0 -12
- package/cjs/utils/crypto/subtle.js +0 -86
- package/cjs/utils/crypto/subtle.js.map +0 -1
- package/cjs/utils/flood-control.d.ts +0 -14
- package/cjs/utils/flood-control.js +0 -65
- package/cjs/utils/flood-control.js.map +0 -1
- package/cjs/utils/platform/random.d.ts +0 -1
- package/cjs/utils/platform/random.js +0 -7
- package/cjs/utils/platform/random.js.map +0 -1
- package/cjs/utils/platform/random.web.d.ts +0 -1
- package/cjs/utils/platform/random.web.js +0 -10
- package/cjs/utils/platform/random.web.js.map +0 -1
- package/esm/utils/crypto/common.d.ts +0 -12
- package/esm/utils/crypto/common.js +0 -50
- package/esm/utils/crypto/common.js.map +0 -1
- package/esm/utils/crypto/node-crypto.d.ts +0 -10
- package/esm/utils/crypto/node-crypto.js +0 -42
- package/esm/utils/crypto/node-crypto.js.map +0 -1
- package/esm/utils/crypto/subtle.d.ts +0 -12
- package/esm/utils/crypto/subtle.js +0 -59
- package/esm/utils/crypto/subtle.js.map +0 -1
- package/esm/utils/flood-control.d.ts +0 -14
- package/esm/utils/flood-control.js +0 -61
- package/esm/utils/flood-control.js.map +0 -1
- package/esm/utils/platform/random.d.ts +0 -1
- package/esm/utils/platform/random.js +0 -4
- package/esm/utils/platform/random.js.map +0 -1
- package/esm/utils/platform/random.web.d.ts +0 -1
- package/esm/utils/platform/random.web.js +0 -6
- package/esm/utils/platform/random.web.js.map +0 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { testStateStorage, testStorage } from '@mtcute/test';
|
|
3
|
+
import { MemoryStorage } from './memory.js';
|
|
4
|
+
describe('MemoryStorage', () => {
|
|
5
|
+
testStorage(new MemoryStorage());
|
|
6
|
+
testStateStorage(new MemoryStorage());
|
|
7
|
+
describe('extending', () => {
|
|
8
|
+
it('should allow populating from an object', () => {
|
|
9
|
+
class ExtendedMemoryStorage extends MemoryStorage {
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
this._setStateFrom({
|
|
13
|
+
$version: 1,
|
|
14
|
+
defaultDcs: null,
|
|
15
|
+
authKeys: new Map(),
|
|
16
|
+
authKeysTemp: new Map(),
|
|
17
|
+
authKeysTempExpiry: new Map(),
|
|
18
|
+
entities: new Map(),
|
|
19
|
+
phoneIndex: new Map(),
|
|
20
|
+
usernameIndex: new Map(),
|
|
21
|
+
gpts: [1, 2, 3, 4],
|
|
22
|
+
pts: new Map(),
|
|
23
|
+
fsm: new Map(),
|
|
24
|
+
rl: new Map(),
|
|
25
|
+
self: null,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const s = new ExtendedMemoryStorage();
|
|
30
|
+
expect(s.getUpdatesState()).toEqual([1, 2, 3, 4]);
|
|
31
|
+
});
|
|
32
|
+
it('should silently fail if version is wrong', () => {
|
|
33
|
+
class ExtendedMemoryStorage extends MemoryStorage {
|
|
34
|
+
constructor() {
|
|
35
|
+
super();
|
|
36
|
+
// eslint-disable-next-line
|
|
37
|
+
this._setStateFrom({ $version: 0 });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const s = new ExtendedMemoryStorage();
|
|
41
|
+
expect(s.getUpdatesState()).toEqual(null);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=memory.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.test.js","sourceRoot":"","sources":["../../../src/storage/memory.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC3B,WAAW,CAAC,IAAI,aAAa,EAAE,CAAC,CAAA;IAChC,gBAAgB,CAAC,IAAI,aAAa,EAAE,CAAC,CAAA;IAErC,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;QACvB,EAAE,CAAC,wCAAwC,EAAE,GAAG,EAAE;YAC9C,MAAM,qBAAsB,SAAQ,aAAa;gBAC7C;oBACI,KAAK,EAAE,CAAA;oBACP,IAAI,CAAC,aAAa,CAAC;wBACf,QAAQ,EAAE,CAAC;wBACX,UAAU,EAAE,IAAI;wBAChB,QAAQ,EAAE,IAAI,GAAG,EAAE;wBACnB,YAAY,EAAE,IAAI,GAAG,EAAE;wBACvB,kBAAkB,EAAE,IAAI,GAAG,EAAE;wBAC7B,QAAQ,EAAE,IAAI,GAAG,EAAE;wBACnB,UAAU,EAAE,IAAI,GAAG,EAAE;wBACrB,aAAa,EAAE,IAAI,GAAG,EAAE;wBACxB,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;wBAClB,GAAG,EAAE,IAAI,GAAG,EAAE;wBACd,GAAG,EAAE,IAAI,GAAG,EAAE;wBACd,EAAE,EAAE,IAAI,GAAG,EAAE;wBACb,IAAI,EAAE,IAAI;qBACb,CAAC,CAAA;gBACN,CAAC;aACJ;YAED,MAAM,CAAC,GAAG,IAAI,qBAAqB,EAAE,CAAA;YAErC,MAAM,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;QACrD,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,0CAA0C,EAAE,GAAG,EAAE;YAChD,MAAM,qBAAsB,SAAQ,aAAa;gBAC7C;oBACI,KAAK,EAAE,CAAA;oBACP,2BAA2B;oBAC3B,IAAI,CAAC,aAAa,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAS,CAAC,CAAA;gBAC9C,CAAC;aACJ;YAED,MAAM,CAAC,GAAG,IAAI,qBAAqB,EAAE,CAAA;YAErC,MAAM,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC7C,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA","sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport { testStateStorage, testStorage } from '@mtcute/test'\n\nimport { MemoryStorage } from './memory.js'\n\ndescribe('MemoryStorage', () => {\n testStorage(new MemoryStorage())\n testStateStorage(new MemoryStorage())\n\n describe('extending', () => {\n it('should allow populating from an object', () => {\n class ExtendedMemoryStorage extends MemoryStorage {\n constructor() {\n super()\n this._setStateFrom({\n $version: 1,\n defaultDcs: null,\n authKeys: new Map(),\n authKeysTemp: new Map(),\n authKeysTempExpiry: new Map(),\n entities: new Map(),\n phoneIndex: new Map(),\n usernameIndex: new Map(),\n gpts: [1, 2, 3, 4],\n pts: new Map(),\n fsm: new Map(),\n rl: new Map(),\n self: null,\n })\n }\n }\n\n const s = new ExtendedMemoryStorage()\n\n expect(s.getUpdatesState()).toEqual([1, 2, 3, 4])\n })\n\n it('should silently fail if version is wrong', () => {\n class ExtendedMemoryStorage extends MemoryStorage {\n constructor() {\n super()\n // eslint-disable-next-line\n this._setStateFrom({ $version: 0 } as any)\n }\n }\n\n const s = new ExtendedMemoryStorage()\n\n expect(s.getUpdatesState()).toEqual(null)\n })\n })\n})\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { AsyncLock } from './async-lock.js';
|
|
3
|
+
import { sleep } from './misc-utils.js';
|
|
4
|
+
describe('AsyncLock', () => {
|
|
5
|
+
it('should correctly lock execution', async () => {
|
|
6
|
+
const lock = new AsyncLock();
|
|
7
|
+
const log = [];
|
|
8
|
+
await Promise.all(Array.from({ length: 10 }, (_, idx) => lock.with(async () => {
|
|
9
|
+
await sleep(10 - idx);
|
|
10
|
+
log.push(idx);
|
|
11
|
+
})));
|
|
12
|
+
expect(log).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
13
|
+
});
|
|
14
|
+
it('should correctly propagate errors', async () => {
|
|
15
|
+
const lock = new AsyncLock();
|
|
16
|
+
await expect(async () => {
|
|
17
|
+
await lock.with(() => {
|
|
18
|
+
throw new Error('test');
|
|
19
|
+
});
|
|
20
|
+
}).rejects.toThrow('test');
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
//# sourceMappingURL=async-lock.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-lock.test.js","sourceRoot":"","sources":["../../../src/utils/async-lock.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,SAAS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ep } from './misc-utils.js'\n\ndescribe('AsyncLock', () => {\n it('should correctly lock execution', async () => {\n const lock = new AsyncLock()\n\n const log: number[] = []\n await Promise.all(\n Array.from({ length: 10 }, (_, idx) =>\n lock.with(async () => {\n await sleep(10 - idx)\n log.push(idx)\n }),\n ),\n )\n\n expect(log).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n })\n\n it('should correctly propagate errors', async () => {\n const lock = new AsyncLock()\n\n await expect(async () => {\n await lock.with(() => {\n throw new Error('test')\n })\n }).rejects.toThrow('test')\n })\n})\n"]}
|
|
@@ -1,40 +1,47 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ICryptoProvider } from './crypto/abstract.js';
|
|
2
|
+
/**
|
|
3
|
+
* Get the minimum number of bits required to represent a number
|
|
4
|
+
*/
|
|
5
|
+
export declare function bigIntBitLength(n: bigint): number;
|
|
2
6
|
/**
|
|
3
7
|
* Convert a big integer to a buffer
|
|
4
8
|
*
|
|
5
9
|
* @param value Value to convert
|
|
6
|
-
* @param length Length of the resulting buffer (by default it's
|
|
10
|
+
* @param length Length of the resulting buffer (by default it's the minimum required)
|
|
7
11
|
* @param le Whether to use little-endian encoding
|
|
8
12
|
*/
|
|
9
|
-
export declare function bigIntToBuffer(value:
|
|
13
|
+
export declare function bigIntToBuffer(value: bigint, length?: number, le?: boolean): Uint8Array;
|
|
10
14
|
/**
|
|
11
15
|
* Convert a buffer to a big integer
|
|
12
16
|
*
|
|
13
17
|
* @param buffer Buffer to convert
|
|
14
|
-
* @param offset Offset to start reading from
|
|
15
|
-
* @param length Length to read
|
|
16
18
|
* @param le Whether to use little-endian encoding
|
|
17
19
|
*/
|
|
18
|
-
export declare function bufferToBigInt(buffer: Uint8Array,
|
|
20
|
+
export declare function bufferToBigInt(buffer: Uint8Array, le?: boolean): bigint;
|
|
19
21
|
/**
|
|
20
|
-
* Generate a random big integer of the given size (in bytes)
|
|
22
|
+
* Generate a cryptographically safe random big integer of the given size (in bytes)
|
|
21
23
|
* @param size Size in bytes
|
|
22
24
|
*/
|
|
23
|
-
export declare function randomBigInt(size: number):
|
|
25
|
+
export declare function randomBigInt(crypto: ICryptoProvider, size: number): bigint;
|
|
24
26
|
/**
|
|
25
27
|
* Generate a random big integer of the given size (in bits)
|
|
26
28
|
* @param bits
|
|
27
29
|
*/
|
|
28
|
-
export declare function randomBigIntBits(bits: number):
|
|
30
|
+
export declare function randomBigIntBits(crypto: ICryptoProvider, bits: number): bigint;
|
|
29
31
|
/**
|
|
30
32
|
* Generate a random big integer in the range [min, max)
|
|
31
33
|
*
|
|
32
34
|
* @param max Maximum value (exclusive)
|
|
33
35
|
* @param min Minimum value (inclusive)
|
|
34
36
|
*/
|
|
35
|
-
export declare function randomBigIntInRange(max:
|
|
37
|
+
export declare function randomBigIntInRange(crypto: ICryptoProvider, max: bigint, min?: bigint): bigint;
|
|
36
38
|
/**
|
|
37
39
|
* Compute the multiplicity of 2 in the prime factorization of n
|
|
38
40
|
* @param n
|
|
39
41
|
*/
|
|
40
|
-
export declare function twoMultiplicity(n:
|
|
42
|
+
export declare function twoMultiplicity(n: bigint): bigint;
|
|
43
|
+
export declare function bigIntMin(a: bigint, b: bigint): bigint;
|
|
44
|
+
export declare function bigIntAbs(a: bigint): bigint;
|
|
45
|
+
export declare function bigIntGcd(a: bigint, b: bigint): bigint;
|
|
46
|
+
export declare function bigIntModPow(base: bigint, exp: bigint, mod: bigint): bigint;
|
|
47
|
+
export declare function bigIntModInv(a: bigint, n: bigint): bigint;
|
|
@@ -1,59 +1,87 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { bufferToReversed } from './buffer-utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Get the minimum number of bits required to represent a number
|
|
4
|
+
*/
|
|
5
|
+
export function bigIntBitLength(n) {
|
|
6
|
+
// not the fastest way, but at least not .toString(2) and not too complex
|
|
7
|
+
// taken from: https://stackoverflow.com/a/76616288/22656950
|
|
8
|
+
const i = (n.toString(16).length - 1) * 4;
|
|
9
|
+
return i + 32 - Math.clz32(Number(n >> BigInt(i)));
|
|
10
|
+
}
|
|
3
11
|
/**
|
|
4
12
|
* Convert a big integer to a buffer
|
|
5
13
|
*
|
|
6
14
|
* @param value Value to convert
|
|
7
|
-
* @param length Length of the resulting buffer (by default it's
|
|
15
|
+
* @param length Length of the resulting buffer (by default it's the minimum required)
|
|
8
16
|
* @param le Whether to use little-endian encoding
|
|
9
17
|
*/
|
|
10
18
|
export function bigIntToBuffer(value, length = 0, le = false) {
|
|
11
|
-
const
|
|
12
|
-
|
|
19
|
+
const bits = bigIntBitLength(value);
|
|
20
|
+
const bytes = Math.ceil(bits / 8);
|
|
21
|
+
if (length !== 0 && bytes > length) {
|
|
13
22
|
throw new Error('Value out of bounds');
|
|
14
23
|
}
|
|
15
|
-
if (length
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
if (length === 0)
|
|
25
|
+
length = bytes;
|
|
26
|
+
const buf = new ArrayBuffer(length);
|
|
27
|
+
const u8 = new Uint8Array(buf);
|
|
28
|
+
const unaligned = length % 8;
|
|
29
|
+
const dv = new DataView(buf, 0, length - unaligned);
|
|
30
|
+
// it is faster to work with 64-bit words than with bytes directly
|
|
31
|
+
for (let i = 0; i < dv.byteLength; i += 8) {
|
|
32
|
+
dv.setBigUint64(i, value & 0xffffffffffffffffn, true);
|
|
33
|
+
value >>= 64n;
|
|
19
34
|
}
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
if (unaligned > 0) {
|
|
36
|
+
for (let i = length - unaligned; i < length; i++) {
|
|
37
|
+
u8[i] = Number(value & 0xffn);
|
|
38
|
+
value >>= 8n;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!le)
|
|
42
|
+
u8.reverse();
|
|
43
|
+
return u8;
|
|
25
44
|
}
|
|
26
45
|
/**
|
|
27
46
|
* Convert a buffer to a big integer
|
|
28
47
|
*
|
|
29
48
|
* @param buffer Buffer to convert
|
|
30
|
-
* @param offset Offset to start reading from
|
|
31
|
-
* @param length Length to read
|
|
32
49
|
* @param le Whether to use little-endian encoding
|
|
33
50
|
*/
|
|
34
|
-
export function bufferToBigInt(buffer,
|
|
35
|
-
const arr = [...buffer.subarray(offset, offset + length)];
|
|
51
|
+
export function bufferToBigInt(buffer, le = false) {
|
|
36
52
|
if (le)
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
buffer = bufferToReversed(buffer);
|
|
54
|
+
const unaligned = buffer.length % 8;
|
|
55
|
+
const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength - unaligned);
|
|
56
|
+
let res = 0n;
|
|
57
|
+
// it is faster to work with 64-bit words than with bytes directly
|
|
58
|
+
for (let i = 0; i < dv.byteLength; i += 8) {
|
|
59
|
+
res = (res << 64n) | BigInt(dv.getBigUint64(i, false));
|
|
60
|
+
}
|
|
61
|
+
if (unaligned > 0) {
|
|
62
|
+
for (let i = buffer.length - unaligned; i < buffer.length; i++) {
|
|
63
|
+
res = (res << 8n) | BigInt(buffer[i]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return res;
|
|
39
67
|
}
|
|
40
68
|
/**
|
|
41
|
-
* Generate a random big integer of the given size (in bytes)
|
|
69
|
+
* Generate a cryptographically safe random big integer of the given size (in bytes)
|
|
42
70
|
* @param size Size in bytes
|
|
43
71
|
*/
|
|
44
|
-
export function randomBigInt(size) {
|
|
45
|
-
return bufferToBigInt(randomBytes(size));
|
|
72
|
+
export function randomBigInt(crypto, size) {
|
|
73
|
+
return bufferToBigInt(crypto.randomBytes(size));
|
|
46
74
|
}
|
|
47
75
|
/**
|
|
48
76
|
* Generate a random big integer of the given size (in bits)
|
|
49
77
|
* @param bits
|
|
50
78
|
*/
|
|
51
|
-
export function randomBigIntBits(bits) {
|
|
52
|
-
let num = randomBigInt(Math.ceil(bits / 8));
|
|
53
|
-
const bitLength = num
|
|
54
|
-
if (bitLength
|
|
55
|
-
const toTrim =
|
|
56
|
-
num
|
|
79
|
+
export function randomBigIntBits(crypto, bits) {
|
|
80
|
+
let num = randomBigInt(crypto, Math.ceil(bits / 8));
|
|
81
|
+
const bitLength = bigIntBitLength(num);
|
|
82
|
+
if (bitLength > bits) {
|
|
83
|
+
const toTrim = bitLength - bits;
|
|
84
|
+
num >>= BigInt(toTrim);
|
|
57
85
|
}
|
|
58
86
|
return num;
|
|
59
87
|
}
|
|
@@ -63,30 +91,100 @@ export function randomBigIntBits(bits) {
|
|
|
63
91
|
* @param max Maximum value (exclusive)
|
|
64
92
|
* @param min Minimum value (inclusive)
|
|
65
93
|
*/
|
|
66
|
-
export function randomBigIntInRange(max, min =
|
|
67
|
-
const interval = max
|
|
68
|
-
if (interval
|
|
94
|
+
export function randomBigIntInRange(crypto, max, min = 1n) {
|
|
95
|
+
const interval = max - min;
|
|
96
|
+
if (interval < 0n)
|
|
69
97
|
throw new Error('expected min < max');
|
|
70
|
-
const byteSize = interval
|
|
71
|
-
let result = randomBigInt(byteSize);
|
|
72
|
-
while (result
|
|
73
|
-
result
|
|
74
|
-
return min
|
|
98
|
+
const byteSize = bigIntBitLength(interval) / 8;
|
|
99
|
+
let result = randomBigInt(crypto, byteSize);
|
|
100
|
+
while (result > interval)
|
|
101
|
+
result -= interval;
|
|
102
|
+
return min + result;
|
|
75
103
|
}
|
|
76
104
|
/**
|
|
77
105
|
* Compute the multiplicity of 2 in the prime factorization of n
|
|
78
106
|
* @param n
|
|
79
107
|
*/
|
|
80
108
|
export function twoMultiplicity(n) {
|
|
81
|
-
if (n ===
|
|
82
|
-
return
|
|
83
|
-
let m =
|
|
84
|
-
let pow =
|
|
109
|
+
if (n === 0n)
|
|
110
|
+
return 0n;
|
|
111
|
+
let m = 0n;
|
|
112
|
+
let pow = 1n;
|
|
85
113
|
while (true) {
|
|
86
|
-
if (
|
|
114
|
+
if ((n & pow) !== 0n)
|
|
87
115
|
return m;
|
|
88
|
-
m
|
|
89
|
-
pow
|
|
116
|
+
m += 1n;
|
|
117
|
+
pow <<= 1n;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export function bigIntMin(a, b) {
|
|
121
|
+
return a < b ? a : b;
|
|
122
|
+
}
|
|
123
|
+
export function bigIntAbs(a) {
|
|
124
|
+
return a < 0n ? -a : a;
|
|
125
|
+
}
|
|
126
|
+
export function bigIntGcd(a, b) {
|
|
127
|
+
// using euclidean algorithm is fast enough on smaller numbers
|
|
128
|
+
// https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
|
|
129
|
+
while (b !== 0n) {
|
|
130
|
+
const t = b;
|
|
131
|
+
b = a % b;
|
|
132
|
+
a = t;
|
|
133
|
+
}
|
|
134
|
+
return a;
|
|
135
|
+
}
|
|
136
|
+
export function bigIntModPow(base, exp, mod) {
|
|
137
|
+
// using the binary method is good enough for our use case
|
|
138
|
+
// https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method
|
|
139
|
+
base %= mod;
|
|
140
|
+
let result = 1n;
|
|
141
|
+
while (exp > 0n) {
|
|
142
|
+
if (exp % 2n === 1n) {
|
|
143
|
+
result = (result * base) % mod;
|
|
144
|
+
}
|
|
145
|
+
exp >>= 1n;
|
|
146
|
+
base = base ** 2n % mod;
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
// below code is based on https://github.com/juanelas/bigint-mod-arith, MIT license
|
|
151
|
+
function eGcd(a, b) {
|
|
152
|
+
let x = 0n;
|
|
153
|
+
let y = 1n;
|
|
154
|
+
let u = 1n;
|
|
155
|
+
let v = 0n;
|
|
156
|
+
while (a !== 0n) {
|
|
157
|
+
const q = b / a;
|
|
158
|
+
const r = b % a;
|
|
159
|
+
const m = x - u * q;
|
|
160
|
+
const n = y - v * q;
|
|
161
|
+
b = a;
|
|
162
|
+
a = r;
|
|
163
|
+
x = u;
|
|
164
|
+
y = v;
|
|
165
|
+
u = m;
|
|
166
|
+
v = n;
|
|
167
|
+
}
|
|
168
|
+
return [b, x, y];
|
|
169
|
+
}
|
|
170
|
+
function toZn(a, n) {
|
|
171
|
+
if (typeof a === 'number')
|
|
172
|
+
a = BigInt(a);
|
|
173
|
+
if (typeof n === 'number')
|
|
174
|
+
n = BigInt(n);
|
|
175
|
+
if (n <= 0n) {
|
|
176
|
+
throw new RangeError('n must be > 0');
|
|
177
|
+
}
|
|
178
|
+
const aZn = a % n;
|
|
179
|
+
return aZn < 0n ? aZn + n : aZn;
|
|
180
|
+
}
|
|
181
|
+
export function bigIntModInv(a, n) {
|
|
182
|
+
const [g, x] = eGcd(toZn(a, n), n);
|
|
183
|
+
if (g !== 1n) {
|
|
184
|
+
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
return toZn(x, n);
|
|
90
188
|
}
|
|
91
189
|
}
|
|
92
190
|
//# sourceMappingURL=bigint-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bigint-utils.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAsB,MAAM,aAAa,CAAA;AAEhD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAE/C;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,KAAiB,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK;IACpE,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAA;IAEtC,IAAI,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,MAAM,EAAE;QACvC,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;KACzC;IAED,IAAI,MAAM,KAAK,CAAC,EAAE;QACd,UAAU;QACV,OAAO,KAAK,CAAC,MAAM,KAAK,MAAM;YAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA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;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAe,EAAE,GAAG,GAAG,MAAM,CAAC,GAAG;IACjE,MAAM,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC/B,IAAI,QAAQ,CAAC,UAAU,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;IAEhE,MAAM,QAAQ,GAAG,QAAQ,CAAC,SAAS,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAA;IAE5D,IAAI,MAAM,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAA;IACnC,OAAO,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC;QAAE,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;IAE3D,OAAO,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AAC3B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,CAAa;IACzC,IAAI,CAAC,KAAK,MAAM,CAAC,IAAI;QAAE,OAAO,MAAM,CAAC,IAAI,CAAA;IAEzC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAA;IACnB,IAAI,GAAG,GAAG,MAAM,CAAC,GAAG,CAAA;IAEpB,OAAO,IAAI,EAAE;QACT,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE;YAAE,OAAO,CAAC,CAAA;QAClC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACtB,GAAG,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;KACzB;AACL,CAAC","sourcesContent":["import bigInt, { BigInteger } from 'big-integer'\n\nimport { randomBytes } from './buffer-utils.js'\n\n/**\n * Convert a big integer to a buffer\n *\n * @param value Value to convert\n * @param length Length of the resulting buffer (by default it's computed automatically)\n * @param le Whether to use little-endian encoding\n */\nexport function bigIntToBuffer(value: BigInteger, length = 0, le = false): Uint8Array {\n const array = value.toArray(256).value\n\n if (length !== 0 && array.length > length) {\n throw new Error('Value out of bounds')\n }\n\n if (length !== 0) {\n // padding\n while (array.length !== length) array.unshift(0)\n }\n\n if (le) array.reverse()\n\n const buffer = new Uint8Array(length || array.length)\n buffer.set(array, 0)\n\n return buffer\n}\n\n/**\n * Convert a buffer to a big integer\n *\n * @param buffer Buffer to convert\n * @param offset Offset to start reading from\n * @param length Length to read\n * @param le Whether to use little-endian encoding\n */\nexport function bufferToBigInt(buffer: Uint8Array, offset = 0, length = buffer.length, le = false): BigInteger {\n const arr = [...buffer.subarray(offset, offset + length)]\n\n if (le) arr.reverse()\n\n return bigInt.fromArray(arr as unknown as number[], 256)\n}\n\n/**\n * Generate a random big integer of the given size (in bytes)\n * @param size Size in bytes\n */\nexport function randomBigInt(size: number): BigInteger {\n return bufferToBigInt(randomBytes(size))\n}\n\n/**\n * Generate a random big integer of the given size (in bits)\n * @param bits\n */\nexport function randomBigIntBits(bits: number): BigInteger {\n let num = randomBigInt(Math.ceil(bits / 8))\n\n const bitLength = num.bitLength()\n\n if (bitLength.gt(bits)) {\n const toTrim = bigInt.randBetween(bitLength.minus(bits), 8)\n num = num.shiftRight(toTrim)\n }\n\n return num\n}\n\n/**\n * Generate a random big integer in the range [min, max)\n *\n * @param max Maximum value (exclusive)\n * @param min Minimum value (inclusive)\n */\nexport function randomBigIntInRange(max: BigInteger, min = bigInt.one): BigInteger {\n const interval = max.minus(min)\n if (interval.isNegative()) throw new Error('expected min < max')\n\n const byteSize = interval.bitLength().divide(8).toJSNumber()\n\n let result = randomBigInt(byteSize)\n while (result.gt(interval)) result = result.minus(interval)\n\n return min.plus(result)\n}\n\n/**\n * Compute the multiplicity of 2 in the prime factorization of n\n * @param n\n */\nexport function twoMultiplicity(n: BigInteger): BigInteger {\n if (n === bigInt.zero) return bigInt.zero\n\n let m = bigInt.zero\n let pow = bigInt.one\n\n while (true) {\n if (!n.and(pow).isZero()) return m\n m = m.plus(bigInt.one)\n pow = pow.shiftLeft(1)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"bigint-utils.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAGpD;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,CAAS;IACrC,yEAAyE;IACzE,4DAA4D;IAE5D,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;IAEzC,OAAO,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACtD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK;IAChE,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,CAAC,CAAA;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAA;IAEjC,IAAI,MAAM,KAAK,CAAC,IAAI,KAAK,GAAG,MAAM,EAAE;QAChC,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;KACzC;IAED,IAAI,MAAM,KAAK,CAAC;QAAE,MAAM,GAAG,KAAK,CAAA;IAEhC,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAA;IACnC,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IAE9B,MAAM,SAAS,GAAG,MAAM,GAAG,CAAC,CAAA;IAC5B,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;IAEnD,k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G,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,CAAS,EAAE,CAAS;IAC7C,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAElC,IAAI,CAAC,KAAK,EAAE,EAAE;QACV,MAAM,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,iCAAiC,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA,CAAC,iCAAiC;KACzH;SAAM;QACH,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;KACpB;AACL,CAAC","sourcesContent":["import { bufferToReversed } from './buffer-utils.js'\nimport { ICryptoProvider } from './crypto/abstract.js'\n\n/**\n * Get the minimum number of bits required to represent a number\n */\nexport function bigIntBitLength(n: bigint) {\n // not the fastest way, but at least not .toString(2) and not too complex\n // taken from: https://stackoverflow.com/a/76616288/22656950\n\n const i = (n.toString(16).length - 1) * 4\n\n return i + 32 - Math.clz32(Number(n >> BigInt(i)))\n}\n\n/**\n * Convert a big integer to a buffer\n *\n * @param value Value to convert\n * @param length Length of the resulting buffer (by default it's the minimum required)\n * @param le Whether to use little-endian encoding\n */\nexport function bigIntToBuffer(value: bigint, length = 0, le = false): Uint8Array {\n const bits = bigIntBitLength(value)\n const bytes = Math.ceil(bits / 8)\n\n if (length !== 0 && bytes > length) {\n throw new Error('Value out of bounds')\n }\n\n if (length === 0) length = bytes\n\n const buf = new ArrayBuffer(length)\n const u8 = new Uint8Array(buf)\n\n const unaligned = length % 8\n const dv = new DataView(buf, 0, length - unaligned)\n\n // it is faster to work with 64-bit words than with bytes directly\n for (let i = 0; i < dv.byteLength; i += 8) {\n dv.setBigUint64(i, value & 0xffffffffffffffffn, true)\n value >>= 64n\n }\n\n if (unaligned > 0) {\n for (let i = length - unaligned; i < length; i++) {\n u8[i] = Number(value & 0xffn)\n value >>= 8n\n }\n }\n\n if (!le) u8.reverse()\n\n return u8\n}\n\n/**\n * Convert a buffer to a big integer\n *\n * @param buffer Buffer to convert\n * @param le Whether to use little-endian encoding\n */\nexport function bufferToBigInt(buffer: Uint8Array, le = false): bigint {\n if (le) buffer = bufferToReversed(buffer)\n\n const unaligned = buffer.length % 8\n const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength - unaligned)\n\n let res = 0n\n\n // it is faster to work with 64-bit words than with bytes directly\n for (let i = 0; i < dv.byteLength; i += 8) {\n res = (res << 64n) | BigInt(dv.getBigUint64(i, false))\n }\n\n if (unaligned > 0) {\n for (let i = buffer.length - unaligned; i < buffer.length; i++) {\n res = (res << 8n) | BigInt(buffer[i])\n }\n }\n\n return res\n}\n\n/**\n * Generate a cryptographically safe random big integer of the given size (in bytes)\n * @param size Size in bytes\n */\nexport function randomBigInt(crypto: ICryptoProvider, size: number): bigint {\n return bufferToBigInt(crypto.randomBytes(size))\n}\n\n/**\n * Generate a random big integer of the given size (in bits)\n * @param bits\n */\nexport function randomBigIntBits(crypto: ICryptoProvider, bits: number): bigint {\n let num = randomBigInt(crypto, Math.ceil(bits / 8))\n\n const bitLength = bigIntBitLength(num)\n\n if (bitLength > bits) {\n const toTrim = bitLength - bits\n num >>= BigInt(toTrim)\n }\n\n return num\n}\n\n/**\n * Generate a random big integer in the range [min, max)\n *\n * @param max Maximum value (exclusive)\n * @param min Minimum value (inclusive)\n */\nexport function randomBigIntInRange(crypto: ICryptoProvider, max: bigint, min = 1n): bigint {\n const interval = max - min\n if (interval < 0n) throw new Error('expected min < max')\n\n const byteSize = bigIntBitLength(interval) / 8\n\n let result = randomBigInt(crypto, byteSize)\n while (result > interval) result -= interval\n\n return min + result\n}\n\n/**\n * Compute the multiplicity of 2 in the prime factorization of n\n * @param n\n */\nexport function twoMultiplicity(n: bigint): bigint {\n if (n === 0n) return 0n\n\n let m = 0n\n let pow = 1n\n\n while (true) {\n if ((n & pow) !== 0n) return m\n m += 1n\n pow <<= 1n\n }\n}\n\nexport function bigIntMin(a: bigint, b: bigint): bigint {\n return a < b ? a : b\n}\n\nexport function bigIntAbs(a: bigint): bigint {\n return a < 0n ? -a : a\n}\n\nexport function bigIntGcd(a: bigint, b: bigint): bigint {\n // using euclidean algorithm is fast enough on smaller numbers\n // https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations\n\n while (b !== 0n) {\n const t = b\n b = a % b\n a = t\n }\n\n return a\n}\n\nexport function bigIntModPow(base: bigint, exp: bigint, mod: bigint): bigint {\n // using the binary method is good enough for our use case\n // https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method\n\n base %= mod\n\n let result = 1n\n\n while (exp > 0n) {\n if (exp % 2n === 1n) {\n result = (result * base) % mod\n }\n\n exp >>= 1n\n base = base ** 2n % mod\n }\n\n return result\n}\n\n// below code is based on https://github.com/juanelas/bigint-mod-arith, MIT license\n\nfunction eGcd(a: bigint, b: bigint): [bigint, bigint, bigint] {\n let x = 0n\n let y = 1n\n let u = 1n\n let v = 0n\n\n while (a !== 0n) {\n const q = b / a\n const r: bigint = b % a\n const m = x - u * q\n const n = y - v * q\n b = a\n a = r\n x = u\n y = v\n u = m\n v = n\n }\n\n return [b, x, y]\n}\n\nfunction toZn(a: number | bigint, n: number | bigint): bigint {\n if (typeof a === 'number') a = BigInt(a)\n if (typeof n === 'number') n = BigInt(n)\n\n if (n <= 0n) {\n throw new RangeError('n must be > 0')\n }\n\n const aZn = a % n\n\n return aZn < 0n ? aZn + n : aZn\n}\n\nexport function bigIntModInv(a: bigint, n: bigint): bigint {\n const [g, x] = eGcd(toZn(a, n), n)\n\n if (g !== 1n) {\n throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`) // modular inverse does not exist\n } else {\n return toZn(x, n)\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { defaultTestCryptoProvider } from '@mtcute/test';
|
|
3
|
+
import { hexDecodeToBuffer } from '@mtcute/tl-runtime';
|
|
4
|
+
import { bigIntBitLength, bigIntGcd, bigIntModInv, bigIntModPow, bigIntToBuffer, bufferToBigInt, randomBigInt, randomBigIntBits, randomBigIntInRange, twoMultiplicity, } from './index.js';
|
|
5
|
+
describe('bigIntBitLength', () => {
|
|
6
|
+
it('should correctly calculate bit length', () => {
|
|
7
|
+
expect(bigIntBitLength(0n)).eq(0);
|
|
8
|
+
expect(bigIntBitLength(1n)).eq(1);
|
|
9
|
+
expect(bigIntBitLength(2n)).eq(2);
|
|
10
|
+
expect(bigIntBitLength(255n)).eq(8);
|
|
11
|
+
expect(bigIntBitLength(256n)).eq(9);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
describe('bigIntToBuffer', () => {
|
|
15
|
+
it('should handle writing to BE', () => {
|
|
16
|
+
expect([...bigIntToBuffer(BigInt('10495708'), 0, false)]).eql([0xa0, 0x26, 0xdc]);
|
|
17
|
+
expect([...bigIntToBuffer(BigInt('10495708'), 4, false)]).eql([0x00, 0xa0, 0x26, 0xdc]);
|
|
18
|
+
expect([...bigIntToBuffer(BigInt('10495708'), 8, false)]).eql([0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x26, 0xdc]);
|
|
19
|
+
expect([...bigIntToBuffer(BigInt('3038102549'), 4, false)]).eql([0xb5, 0x15, 0xc4, 0x15]);
|
|
20
|
+
expect([...bigIntToBuffer(BigInt('9341376580368336208'), 8, false)]).eql([
|
|
21
|
+
...hexDecodeToBuffer('81A33C81D2020550'),
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
it('should handle writing to LE', () => {
|
|
25
|
+
expect([...bigIntToBuffer(BigInt('10495708'), 0, true)]).eql([0xdc, 0x26, 0xa0]);
|
|
26
|
+
expect([...bigIntToBuffer(BigInt('10495708'), 4, true)]).eql([0xdc, 0x26, 0xa0, 0x00]);
|
|
27
|
+
expect([...bigIntToBuffer(BigInt('10495708'), 8, true)]).eql([0xdc, 0x26, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00]);
|
|
28
|
+
expect([...bigIntToBuffer(BigInt('3038102549'), 4, true)]).eql([0x15, 0xc4, 0x15, 0xb5]);
|
|
29
|
+
expect([...bigIntToBuffer(BigInt('9341376580368336208'), 8, true)]).eql([
|
|
30
|
+
...hexDecodeToBuffer('81A33C81D2020550').reverse(),
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
it('should handle large integers', () => {
|
|
34
|
+
const buf = hexDecodeToBuffer('1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538');
|
|
35
|
+
const num = BigInt('0x1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538');
|
|
36
|
+
expect([...bigIntToBuffer(num, 0, false)]).eql([...buf]);
|
|
37
|
+
expect([...bigIntToBuffer(num, 0, true)]).eql([...buf.reverse()]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe('bufferToBigInt', () => {
|
|
41
|
+
it('should handle reading BE', () => {
|
|
42
|
+
expect(bufferToBigInt(new Uint8Array([0xa0, 0x26, 0xdc]), false).toString()).eq('10495708');
|
|
43
|
+
expect(bufferToBigInt(new Uint8Array([0x00, 0xa0, 0x26, 0xdc]), false).toString()).eq('10495708');
|
|
44
|
+
expect(bufferToBigInt(new Uint8Array([0xb5, 0x15, 0xc4, 0x15]), false).toString()).eq('3038102549');
|
|
45
|
+
});
|
|
46
|
+
it('should handle reading LE', () => {
|
|
47
|
+
expect(bufferToBigInt(new Uint8Array([0xdc, 0x26, 0xa0]), true).toString()).eq('10495708');
|
|
48
|
+
expect(bufferToBigInt(new Uint8Array([0xdc, 0x26, 0xa0, 0x00]), true).toString()).eq('10495708');
|
|
49
|
+
expect(bufferToBigInt(new Uint8Array([0x15, 0xc4, 0x15, 0xb5]), true).toString()).eq('3038102549');
|
|
50
|
+
});
|
|
51
|
+
it('should handle large integers', () => {
|
|
52
|
+
const buf = hexDecodeToBuffer('1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538');
|
|
53
|
+
const num = BigInt('0x1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538');
|
|
54
|
+
expect(bufferToBigInt(buf, false).toString()).eq(num.toString());
|
|
55
|
+
expect(bufferToBigInt(buf.reverse(), true).toString()).eq(num.toString());
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('randomBigInt', async () => {
|
|
59
|
+
const c = await defaultTestCryptoProvider();
|
|
60
|
+
it('should return a random bigint', () => {
|
|
61
|
+
const a = randomBigInt(c, 32);
|
|
62
|
+
const b = randomBigInt(c, 32);
|
|
63
|
+
expect(a).not.toEqual(b);
|
|
64
|
+
});
|
|
65
|
+
it('should return a random bigint up to specified byte length', () => {
|
|
66
|
+
const a = randomBigInt(c, 32);
|
|
67
|
+
const b = randomBigInt(c, 64);
|
|
68
|
+
expect(bigIntBitLength(a)).toBeLessThanOrEqual(32 * 8);
|
|
69
|
+
expect(bigIntBitLength(b)).toBeLessThanOrEqual(64 * 8);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
describe('randomBigIntBits', async () => {
|
|
73
|
+
const c = await defaultTestCryptoProvider();
|
|
74
|
+
it('should return a random bigint', () => {
|
|
75
|
+
const a = randomBigIntBits(c, 32);
|
|
76
|
+
const b = randomBigIntBits(c, 32);
|
|
77
|
+
expect(a).not.toEqual(b);
|
|
78
|
+
});
|
|
79
|
+
it('should return a random bigint up to specified bit length', () => {
|
|
80
|
+
const a = randomBigIntBits(c, 32);
|
|
81
|
+
const b = randomBigIntBits(c, 64);
|
|
82
|
+
expect(bigIntBitLength(a)).toBeLessThanOrEqual(32);
|
|
83
|
+
expect(bigIntBitLength(b)).toBeLessThanOrEqual(64);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe('randomBigIntInRange', async () => {
|
|
87
|
+
const c = await defaultTestCryptoProvider();
|
|
88
|
+
it('should return a random bigint', () => {
|
|
89
|
+
const a = randomBigIntInRange(c, 10000n);
|
|
90
|
+
const b = randomBigIntInRange(c, 10000n);
|
|
91
|
+
expect(a).not.toEqual(b);
|
|
92
|
+
});
|
|
93
|
+
it('should return a bigint within a given range', () => {
|
|
94
|
+
const a = randomBigIntInRange(c, 200n, 100n);
|
|
95
|
+
expect(a).toBeGreaterThanOrEqual(100n);
|
|
96
|
+
expect(a).toBeLessThan(200n);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('twoMultiplicity', () => {
|
|
100
|
+
it('should return the multiplicity of 2 in the prime factorization of n', () => {
|
|
101
|
+
expect(twoMultiplicity(0n)).toEqual(0n);
|
|
102
|
+
expect(twoMultiplicity(1n)).toEqual(0n);
|
|
103
|
+
expect(twoMultiplicity(2n)).toEqual(1n);
|
|
104
|
+
expect(twoMultiplicity(4n)).toEqual(2n);
|
|
105
|
+
expect(twoMultiplicity(65536n)).toEqual(16n);
|
|
106
|
+
expect(twoMultiplicity(65537n)).toEqual(0n);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
describe('bigIntGcd', () => {
|
|
110
|
+
it('should return the greatest common divisor of a and b', () => {
|
|
111
|
+
expect(bigIntGcd(123n, 456n)).toEqual(3n);
|
|
112
|
+
});
|
|
113
|
+
it('should correctly handle zeros', () => {
|
|
114
|
+
expect(bigIntGcd(0n, 0n)).toEqual(0n);
|
|
115
|
+
expect(bigIntGcd(0n, 1n)).toEqual(1n);
|
|
116
|
+
expect(bigIntGcd(1n, 0n)).toEqual(1n);
|
|
117
|
+
});
|
|
118
|
+
it('should correctly handle equal values', () => {
|
|
119
|
+
expect(bigIntGcd(1n, 1n)).toEqual(1n);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe('bigIntModPow', () => {
|
|
123
|
+
it('should correctly calculate modular exponentiation', () => {
|
|
124
|
+
expect(bigIntModPow(2n, 3n, 5n)).toEqual(3n);
|
|
125
|
+
expect(bigIntModPow(2n, 3n, 6n)).toEqual(2n);
|
|
126
|
+
expect(bigIntModPow(2n, 3n, 7n)).toEqual(1n);
|
|
127
|
+
expect(bigIntModPow(2n, 3n, 8n)).toEqual(0n);
|
|
128
|
+
});
|
|
129
|
+
it('should correctly handle very large numbers', () => {
|
|
130
|
+
// calculating this with BigInt would either take forever or error with "Maximum BigInt size exceeded
|
|
131
|
+
expect(bigIntModPow(2n, 100000000000n, 100n)).toEqual(76n);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
describe('bigIntModInv', () => {
|
|
135
|
+
it('should correctly calculate modular inverse', () => {
|
|
136
|
+
expect(bigIntModInv(2n, 5n)).toEqual(3n);
|
|
137
|
+
expect(bigIntModInv(2n, 7n)).toEqual(4n);
|
|
138
|
+
});
|
|
139
|
+
it("should error if there's no modular inverse", () => {
|
|
140
|
+
expect(() => bigIntModInv(2n, 6n)).toThrow(RangeError);
|
|
141
|
+
expect(() => bigIntModInv(2n, 8n)).toThrow(RangeError);
|
|
142
|
+
});
|
|
143
|
+
it('should correctly handle very large numbers', () => {
|
|
144
|
+
// calculating this with BigInt would either take forever or error with "Maximum BigInt size exceeded
|
|
145
|
+
expect(bigIntModInv(123123123123n, 1829n)).toEqual(318n);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
//# sourceMappingURL=bigint-utils.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bigint-utils.test.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.test.ts"],"names":[],"mappings":"A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QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QAClD,MAAM,CAAC,YAAY,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QACxC,MAAM,CAAC,YAAY,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;IAC5C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QAClD,MAAM,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;QACtD,MAAM,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IAC1D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QAClD,qGAAqG;QACrG,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5D,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA","sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport { defaultTestCryptoProvider } from '@mtcute/test'\nimport { hexDecodeToBuffer } from '@mtcute/tl-runtime'\n\nimport {\n bigIntBitLength,\n bigIntGcd,\n bigIntModInv,\n bigIntModPow,\n bigIntToBuffer,\n bufferToBigInt,\n randomBigInt,\n randomBigIntBits,\n randomBigIntInRange,\n twoMultiplicity,\n} from './index.js'\n\ndescribe('bigIntBitLength', () => {\n it('should correctly calculate bit length', () => {\n expect(bigIntBitLength(0n)).eq(0)\n expect(bigIntBitLength(1n)).eq(1)\n expect(bigIntBitLength(2n)).eq(2)\n expect(bigIntBitLength(255n)).eq(8)\n expect(bigIntBitLength(256n)).eq(9)\n })\n})\n\ndescribe('bigIntToBuffer', () => {\n it('should handle writing to BE', () => {\n expect([...bigIntToBuffer(BigInt('10495708'), 0, false)]).eql([0xa0, 0x26, 0xdc])\n expect([...bigIntToBuffer(BigInt('10495708'), 4, false)]).eql([0x00, 0xa0, 0x26, 0xdc])\n expect([...bigIntToBuffer(BigInt('10495708'), 8, false)]).eql([0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x26, 0xdc])\n expect([...bigIntToBuffer(BigInt('3038102549'), 4, false)]).eql([0xb5, 0x15, 0xc4, 0x15])\n expect([...bigIntToBuffer(BigInt('9341376580368336208'), 8, false)]).eql([\n ...hexDecodeToBuffer('81A33C81D2020550'),\n ])\n })\n\n it('should handle writing to LE', () => {\n expect([...bigIntToBuffer(BigInt('10495708'), 0, true)]).eql([0xdc, 0x26, 0xa0])\n expect([...bigIntToBuffer(BigInt('10495708'), 4, true)]).eql([0xdc, 0x26, 0xa0, 0x00])\n expect([...bigIntToBuffer(BigInt('10495708'), 8, true)]).eql([0xdc, 0x26, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00])\n expect([...bigIntToBuffer(BigInt('3038102549'), 4, true)]).eql([0x15, 0xc4, 0x15, 0xb5])\n expect([...bigIntToBuffer(BigInt('9341376580368336208'), 8, true)]).eql([\n ...hexDecodeToBuffer('81A33C81D2020550').reverse(),\n ])\n })\n\n it('should handle large integers', () => {\n const buf = hexDecodeToBuffer(\n '1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',\n )\n const num = BigInt(\n '0x1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',\n )\n\n expect([...bigIntToBuffer(num, 0, false)]).eql([...buf])\n expect([...bigIntToBuffer(num, 0, true)]).eql([...buf.reverse()])\n })\n})\n\ndescribe('bufferToBigInt', () => {\n it('should handle reading BE', () => {\n expect(bufferToBigInt(new Uint8Array([0xa0, 0x26, 0xdc]), false).toString()).eq('10495708')\n expect(bufferToBigInt(new Uint8Array([0x00, 0xa0, 0x26, 0xdc]), false).toString()).eq('10495708')\n expect(bufferToBigInt(new Uint8Array([0xb5, 0x15, 0xc4, 0x15]), false).toString()).eq('3038102549')\n })\n\n it('should handle reading LE', () => {\n expect(bufferToBigInt(new Uint8Array([0xdc, 0x26, 0xa0]), true).toString()).eq('10495708')\n expect(bufferToBigInt(new Uint8Array([0xdc, 0x26, 0xa0, 0x00]), true).toString()).eq('10495708')\n expect(bufferToBigInt(new Uint8Array([0x15, 0xc4, 0x15, 0xb5]), true).toString()).eq('3038102549')\n })\n\n it('should handle large integers', () => {\n const buf = hexDecodeToBuffer(\n '1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',\n )\n const num = BigInt(\n '0x1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',\n )\n\n expect(bufferToBigInt(buf, false).toString()).eq(num.toString())\n expect(bufferToBigInt(buf.reverse(), true).toString()).eq(num.toString())\n })\n})\n\ndescribe('randomBigInt', async () => {\n const c = await defaultTestCryptoProvider()\n\n it('should return a random bigint', () => {\n const a = randomBigInt(c, 32)\n const b = randomBigInt(c, 32)\n\n expect(a).not.toEqual(b)\n })\n\n it('should return a random bigint up to specified byte length', () => {\n const a = randomBigInt(c, 32)\n const b = randomBigInt(c, 64)\n\n expect(bigIntBitLength(a)).toBeLessThanOrEqual(32 * 8)\n expect(bigIntBitLength(b)).toBeLessThanOrEqual(64 * 8)\n })\n})\n\ndescribe('randomBigIntBits', async () => {\n const c = await defaultTestCryptoProvider()\n\n it('should return a random bigint', () => {\n const a = randomBigIntBits(c, 32)\n const b = randomBigIntBits(c, 32)\n\n expect(a).not.toEqual(b)\n })\n\n it('should return a random bigint up to specified bit length', () => {\n const a = randomBigIntBits(c, 32)\n const b = randomBigIntBits(c, 64)\n\n expect(bigIntBitLength(a)).toBeLessThanOrEqual(32)\n expect(bigIntBitLength(b)).toBeLessThanOrEqual(64)\n })\n})\n\ndescribe('randomBigIntInRange', async () => {\n const c = await defaultTestCryptoProvider()\n\n it('should return a random bigint', () => {\n const a = randomBigIntInRange(c, 10000n)\n const b = randomBigIntInRange(c, 10000n)\n\n expect(a).not.toEqual(b)\n })\n\n it('should return a bigint within a given range', () => {\n const a = randomBigIntInRange(c, 200n, 100n)\n\n expect(a).toBeGreaterThanOrEqual(100n)\n expect(a).toBeLessThan(200n)\n })\n})\n\ndescribe('twoMultiplicity', () => {\n it('should return the multiplicity of 2 in the prime factorization of n', () => {\n expect(twoMultiplicity(0n)).toEqual(0n)\n expect(twoMultiplicity(1n)).toEqual(0n)\n expect(twoMultiplicity(2n)).toEqual(1n)\n expect(twoMultiplicity(4n)).toEqual(2n)\n expect(twoMultiplicity(65536n)).toEqual(16n)\n expect(twoMultiplicity(65537n)).toEqual(0n)\n })\n})\n\ndescribe('bigIntGcd', () => {\n it('should return the greatest common divisor of a and b', () => {\n expect(bigIntGcd(123n, 456n)).toEqual(3n)\n })\n\n it('should correctly handle zeros', () => {\n expect(bigIntGcd(0n, 0n)).toEqual(0n)\n expect(bigIntGcd(0n, 1n)).toEqual(1n)\n expect(bigIntGcd(1n, 0n)).toEqual(1n)\n })\n\n it('should correctly handle equal values', () => {\n expect(bigIntGcd(1n, 1n)).toEqual(1n)\n })\n})\n\ndescribe('bigIntModPow', () => {\n it('should correctly calculate modular exponentiation', () => {\n expect(bigIntModPow(2n, 3n, 5n)).toEqual(3n)\n expect(bigIntModPow(2n, 3n, 6n)).toEqual(2n)\n expect(bigIntModPow(2n, 3n, 7n)).toEqual(1n)\n expect(bigIntModPow(2n, 3n, 8n)).toEqual(0n)\n })\n\n it('should correctly handle very large numbers', () => {\n // calculating this with BigInt would either take forever or error with \"Maximum BigInt size exceeded\n expect(bigIntModPow(2n, 100000000000n, 100n)).toEqual(76n)\n })\n})\n\ndescribe('bigIntModInv', () => {\n it('should correctly calculate modular inverse', () => {\n expect(bigIntModInv(2n, 5n)).toEqual(3n)\n expect(bigIntModInv(2n, 7n)).toEqual(4n)\n })\n\n it(\"should error if there's no modular inverse\", () => {\n expect(() => bigIntModInv(2n, 6n)).toThrow(RangeError)\n expect(() => bigIntModInv(2n, 8n)).toThrow(RangeError)\n })\n\n it('should correctly handle very large numbers', () => {\n // calculating this with BigInt would either take forever or error with \"Maximum BigInt size exceeded\n expect(bigIntModInv(123123123123n, 1829n)).toEqual(318n)\n })\n})\n"]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { _randomBytes as randomBytes } from './platform/random.js';
|
|
2
1
|
/**
|
|
3
2
|
* Check if two buffers are equal
|
|
4
3
|
*
|
|
@@ -18,4 +17,11 @@ export declare function cloneBuffer(buf: Uint8Array, start?: number, end?: numbe
|
|
|
18
17
|
* Concatenate multiple buffers into one
|
|
19
18
|
*/
|
|
20
19
|
export declare function concatBuffers(buffers: Uint8Array[]): Uint8Array;
|
|
20
|
+
/**
|
|
21
|
+
* Shortcut for creating a DataView from a Uint8Array
|
|
22
|
+
*/
|
|
21
23
|
export declare function dataViewFromBuffer(buf: Uint8Array): DataView;
|
|
24
|
+
/**
|
|
25
|
+
* Reverse a buffer (or a part of it) into a new buffer
|
|
26
|
+
*/
|
|
27
|
+
export declare function bufferToReversed(buf: Uint8Array, start?: number, end?: number): Uint8Array;
|