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
140
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
Arch輪読: 詳解システムパフォーマンス 第二版 第6章
kota_yata
0
40
RG-Arch 輪講資料: Binary Hacks Rebooted 数値演算など
kota_yata
0
68
結局QUICで通信は速くなるの?
kota_yata
10
8k
RG-Arch輪考資料: QUIC is not Quick Enough over Fast Internet
kota_yata
0
170
RG-Arch輪考資料: Implementation and Performance Evaluation of the QUIC Protocol in Linux Kernel
kota_yata
0
200
2024年秋 中村研 WIP発表資料
kota_yata
0
97
パタヘネ輪読: 第五章
kota_yata
0
78
パタヘネ輪読: 第一章
kota_yata
0
320
2023年秋 中村研 WIP発表資料
kota_yata
0
150
Other Decks in Programming
See All in Programming
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
350k
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1k
さぁV100、メモリをお食べ・・・
nilpe
0
160
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
260
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
130
スマートグラスで並列バイブコーディング
hyshu
0
260
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
280
JavaDoc 再入門
nagise
1
420
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.3k
Featured
See All Featured
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
190
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Become a Pro
speakerdeck
PRO
31
6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
590
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
240
Paper Plane
katiecoart
PRO
1
52k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Tell your own story through comics
letsgokoyo
1
970
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
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