Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
BigIntの良いとこ悪いとこ
Search
kota-yata
September 15, 2022
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
BigIntの良いとこ悪いとこ
kota-yata
September 15, 2022
More Decks by kota-yata
See All by kota-yata
リアルな遅延を測る仕様
kota_yata
1
130
Arch輪読: 詳解システムパフォーマンス 第二版 第6章
kota_yata
0
69
RG-Arch 輪講資料: Binary Hacks Rebooted 数値演算など
kota_yata
0
77
結局QUICで通信は速くなるの?
kota_yata
10
8k
RG-Arch輪考資料: QUIC is not Quick Enough over Fast Internet
kota_yata
0
180
RG-Arch輪考資料: Implementation and Performance Evaluation of the QUIC Protocol in Linux Kernel
kota_yata
0
220
2024年秋 中村研 WIP発表資料
kota_yata
0
100
パタヘネ輪読: 第五章
kota_yata
0
91
パタヘネ輪読: 第一章
kota_yata
0
360
Other Decks in Programming
See All in Programming
Dockerfile CMD for Node.js
grazie1999
0
160
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
570
Hono + Inertia + React で LP を構築した話
oukayuka
2
190
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
600
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
710
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
400
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
110
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
280
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
130
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.4k
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
500
AIの中の人になってみる
htkym
0
150
Featured
See All Featured
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
230
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
220
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Code Review Best Practice
trishagee
74
20k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
910
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
RailsConf 2023
tenderlove
30
1.5k
How GitHub (no longer) Works
holman
316
150k
Transcript
BigIntの良いとこ悪いとこ ~ JavaScriptで大きい整数を扱う ~ 2022/07/30 - @kota_yata 1
Kota Yatagai (@kota_yata) 19歳 P2Pネットワークとか暗号技術とか勉強してます 仕事はフロントエンドエンジニア&コンテスト運営 麻雀とチーズナンカレーにハマった 2022/07/30 - @kota_yata
2
(アジアン料理サハラ 狛江店) 2022/07/30 - @kota_yata 3
JavaScriptにおける大きい整数 通常のNumber型に収まらない値 : 以上 (9007兆1992億5474万991) Number.MAX_SAFE_INTEGER // 9007199254740991 Number.MIN_SAFE_INTEGER //
-9007199254740991 9007199254740992 === 9007199254740993 // true 9007199254740992 < 9007199254740993 // false は大きい数ではない 暗号処理においては1024ビットの素数が必要になったりする UUIDやハッシュ値を数値で保存できない 2 − 53 1 253 2022/07/30 - @kota_yata 4
BigInt ES2020で追加されたプリミティブな符号整数型 ECMAScriptでは最大値が定められていない 実装に任されている(多くのブラウザでは1Mビット) アプリケーション単位でユースケースに合わせて最大値を設定することが推奨され ている BigInt同士であれば通常の演算子はほとんど使用可能 記法: const bigint:
bigint = BigInt(1); // もしくは const bigint: bigint = 1n; 2022/07/30 - @kota_yata 5
BigIntの良くないとこ 2022/07/30 - @kota_yata 6
弱点1 : 既存オブジェクト/型との相性の悪さ BigIntとNumberを混合した四則演算はTypeErrorになる 1 + 1n // TypeError: Cannot
mix BigInt and other types, use explicit conversions [0, 1n, 2, 3n].sort((a, b) => a - b); // TypeError: Cannot convert BigInt value to Number value なのに比較演算子は使える 1n < 2 // true 2n > 1 // true 2 > 2 // false 2n > 2 // false Mathオブジェクトにも非対応 JSONのシリアライズにも未対応 2022/07/30 - @kota_yata 7
弱点2 : 激遅 Number型の演算は浮動小数点演算...ハードウェアで実装されている(FPU) BigInt型に対しては任意精度演算を行うためソフトウェアで演算を実装する必要がある 2022/07/30 - @kota_yata 8
弱点3 : 暗号処理には不適切 "Constant-time Operation"がサポートされていない 2022/07/30 - @kota_yata 9
Constant-time Operation ある演算において処理結果を返す時間を定数にすること 処理結果が返ってくるまでの時間を計測し、ヒントとして利用するタイミング攻撃への 対策 例えば... 2022/07/30 - @kota_yata 10
例 : 文字列の線形なマッチング 正しい文字列を abcde とする 文字列 kdisw とマッチングすると、1文字目で違う文字列であることが分かる。 1文字比較した時点で
false を返せる 文字列 abcdef とマッチングすると、6文字目まで違う文字列であることは分からない kdisw に比べて処理の終了が遅くなる 攻撃者はこれより abcdef の方が遥に正しい文字列と近いことが分かってしまう 本来の処理時間に関わらず一定の時間が経ってから結果を返せば攻撃者はヒントを得 られなくなる => これがConstant-time Operation 2022/07/30 - @kota_yata 11
じゃあなんでBigIntの演算でできないの? BigIntは可変長であるため、最長でかかる処理時間を予測できない。 できたとしても全ての値に対してその時間待つのは非効率すぎる 1ビットのマッチングで1Mビットのマッチング分待つのはヤバい BigInt で対応している演算は、実行時間が一定ではないので、タイミング攻撃を受ける 可能性があります。したがって、 JavaScript の BigInt
は暗号処理での使用には向きま せん。 (BigInt - developer.mozilla.org) セキュリティとトレードオフで巨大な整数を扱っているのがBigInt 2022/07/30 - @kota_yata 12
BigIntの良いとこ 2022/07/30 - @kota_yata 13
他言語からの値の受け取りとか 64ビット固定長など、JavaScriptのNumber型よりも大きい値を保存できる言語は多数 (Cのlong longとか) 他言語で書かれたサーバーからAPIで値をもらってくる。これまでだったら文字列として 扱っていたものをBigInt型に代入でき、数値として演算ができるようになる UUIDやハッシュ値も数値として扱えるようになる 実質Uint128Array(かそれ以上)的な使い方ができる 2022/07/30 -
@kota_yata 14
既存サードパーティーライブラリに比べれば格段に速い (BigInt: arbitrary-precision integers in JavaScript - v8.dev) 2022/07/30 -
@kota_yata 15
なんか金融系のライブラリにも嬉しいらしい 金融系の演算は巨大な整数をたくさん扱う(らしい) 2022/07/30 - @kota_yata 16
おまけ:各主要ブラウザにおけるBigInt実装経緯 2022/07/30 - @kota_yata 17
SpiderMonkey(FireFox)が先頭を切る 2017年5月19日 BigIntがまだステージ2だった段階でMozillaがBigIntスレを立てる 2022/07/30 - @kota_yata 18
3ヶ月後にJSC(Safari)が実装開始 2017年8月8日 プロポーザルがステージ3に昇格した段階でスレが立つ 2022/07/30 - @kota_yata 19
9月にV8(Chrome)が実装開始 2017年9月12日 2022/07/30 - @kota_yata 20
フォーク案とか再実装とか色々案が出る JSCではV8からフォークして実装を開始する案、SpiderMonkeyでは割と実装が進んだ頃に なってやっぱ違うライブラリ使った方が良かった説のスレッドが立つが結局独自実装で Shippingまで進むことになる 2022/07/30 - @kota_yata 21
まとめ 2022/07/30 - @kota_yata 22
BigIntの良いとこ 大きい整数が扱える これまでのサードパーティーライブラリに比べると格段に速い BigIntの悪いとこ Number型に比べて制約が多く、実装時に混乱する Number型に比べると遅い(それはそう) 暗号処理には向かない 2022/07/30 - @kota_yata
23
参考文献 2022/07/30 - @kota_yata 24
SpiderMonkey: Threads and Implementations https://bugzilla.mozilla.org/show_bug.cgi?id=1502797 https://bugzilla.mozilla.org/show_bug.cgi?id=1494346 https://bugzilla.mozilla.org/show_bug.cgi?id=1366287 https://github.com/mozilla/standards-positions/issues/65 https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt https://gmplib.org/
V8: Threads, Blog Posts and Implementations https://v8.dev/blog/bigint https://v8.dev/features/bigint https://github.com/v8/v8/blob/master/src/objects/bigint.cc https://bugs.chromium.org/p/v8/issues/detail?id=6791 2022/07/30 - @kota_yata 25
JSC: Threads and Implementations https://bugs.webkit.org/show_bug.cgi?id=175359 https://lists.webkit.org/pipermail/webkit-dev/2017-October/029676.html https://trac.webkit.org/browser/webkit/trunk/Source/JavaScriptCore/runtime/JSBigInt.cpp https://github.com/WebKit/WebKit/commit/954a77a437aee376dcc4c3bd8ec5e112fa9e 03a1 TC39:
Proposals and Specs https://github.com/tc39/proposal-bigint/issues/53 https://tc39.es/proposal-bigint/ https://tc39.es/process-document/ 2022/07/30 - @kota_yata 26
Others https://golb.hplar.ch/2018/09/javascript-bigint.html https://betterprogramming.pub/the-downsides-of-bigints-in-javascript-6350fd807d https://inst.eecs.berkeley.edu//~cs10/labs/cur/programming/algorithms/timing/constant- time.html?topic=berkeley_bjc%2Fareas%2Falgorithm-complexity.topic https://www.tektutorialshub.com/javascript/bigint-vs-number-in-javascript/#bigint-is-an- integer-number-is-a-decimal https://en.wikipedia.org/wiki/Double-precision_floating-point_format#JavaScript 2022/07/30 -
@kota_yata 27