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
Arch輪読: 詳解システムパフォーマンス 第二版 第6章
kota_yata
0
50
RG-Arch 輪講資料: Binary Hacks Rebooted 数値演算など
kota_yata
0
71
結局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
100
パタヘネ輪読: 第五章
kota_yata
0
88
パタヘネ輪読: 第一章
kota_yata
0
340
2023年秋 中村研 WIP発表資料
kota_yata
0
150
Other Decks in Programming
See All in Programming
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
2.7k
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
150
霧の中の代数的エフェクト
funnyycat
1
410
Claude Team Plan導入・ガイド
tk3fftk
0
210
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
130
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
560
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
170
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.6k
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
180
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
150
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
320
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.9k
Building Adaptive Systems
keathley
44
3.1k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
460
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
290
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Designing for Performance
lara
611
70k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
A designer walks into a library…
pauljervisheath
211
24k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
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