-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathECCBitcoinThreat.fsx
More file actions
473 lines (412 loc) · 20.2 KB
/
Copy pathECCBitcoinThreat.fsx
File metadata and controls
473 lines (412 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// ==============================================================================
// Quantum ECDLP Threat to Bitcoin / Cryptocurrency Elliptic Curve Cryptography
// ==============================================================================
// Compares quantum-assisted ECDLP attacks across multiple toy elliptic curves,
// demonstrating Shor's algorithm infrastructure via periodFinder QPE. Generates
// a real Bitcoin key pair (NBitcoin / secp256k1) to show what a quantum attacker
// would target.
//
// Usage:
// dotnet fsi ECCBitcoinThreat.fsx
// dotnet fsi ECCBitcoinThreat.fsx -- --help
// dotnet fsi ECCBitcoinThreat.fsx -- --curves secp256k1-toy,p256-toy
// dotnet fsi ECCBitcoinThreat.fsx -- --input curves.csv
// dotnet fsi ECCBitcoinThreat.fsx -- --quiet --output results.json --csv results.csv
//
// References:
// [1] Proos & Zalka, "Shor's discrete logarithm quantum algorithm for elliptic
// curves", QIC 3(4), 317-344 (2003). https://arxiv.org/abs/quant-ph/0301141
// [2] Roetteler et al., "Quantum Resource Estimates for Computing Elliptic Curve
// Discrete Logarithms", ASIACRYPT 2017.
// [3] Wikipedia: Elliptic-curve_cryptography
// https://en.wikipedia.org/wiki/Elliptic-curve_cryptography
// ==============================================================================
#r "nuget: Microsoft.Extensions.Logging.Abstractions, 10.0.0"
#r "../../src/FSharp.Azure.Quantum/bin/Debug/net10.0/FSharp.Azure.Quantum.dll"
#r "nuget: NBitcoin, 7.0.44"
#load "../_common/Cli.fs"
#load "../_common/Data.fs"
#load "../_common/Reporting.fs"
open System
open FSharp.Azure.Quantum
open FSharp.Azure.Quantum.QuantumPeriodFinder
open FSharp.Azure.Quantum.Core.BackendAbstraction
open FSharp.Azure.Quantum.Backends.LocalBackend
open FSharp.Azure.Quantum.Examples.Common
open NBitcoin
// ==============================================================================
// CLI
// ==============================================================================
let argv = fsi.CommandLineArgs |> Array.skip 1
let args = Cli.parse argv
Cli.exitIfHelp "ECCBitcoinThreat.fsx"
"Quantum ECDLP threat analysis for elliptic curve cryptography (Bitcoin, Ethereum, etc.)."
[ { Cli.OptionSpec.Name = "input"; Description = "CSV file with custom curve definitions"; Default = Some "built-in presets" }
{ Cli.OptionSpec.Name = "curves"; Description = "Comma-separated curve names to analyse (default: all)"; Default = Some "all" }
{ Cli.OptionSpec.Name = "output"; Description = "Write results to JSON file"; Default = None }
{ Cli.OptionSpec.Name = "csv"; Description = "Write results to CSV file"; Default = None }
{ Cli.OptionSpec.Name = "quiet"; Description = "Suppress informational output (flag)"; Default = None } ]
args
let quiet = Cli.hasFlag "quiet" args
let inputFile = Cli.tryGet "input" args
let curveFilter = Cli.getCommaSeparated "curves" args
// ==============================================================================
// TYPES
// ==============================================================================
/// Toy elliptic curve preset: y^2 = x^3 + a*x + b (mod p)
type CurvePreset =
{ Name: string
RealCurve: string
Prime: int
A: int
B: int
RealBits: int
LogicalQubits: int
TGates: string }
/// Result of ECDLP quantum attack on one curve.
type ECDLPResult =
{ Curve: CurvePreset
CurveOrder: int
GeneratorPoint: string
GeneratorOrder: int
VictimPrivateKey: int
VictimPublicKey: string
RecoveredKey: int option
AttackSuccess: bool
QPEPeriod: int option
HasQuantumFailure: bool }
// ==============================================================================
// MODULAR ARITHMETIC (Pure Functional)
// ==============================================================================
let modPow (baseVal: int) (exp: int) (modulus: int) : int =
let rec loop acc b e =
if e = 0 then acc
elif e % 2 = 1 then loop ((acc * b) % modulus) ((b * b) % modulus) (e / 2)
else loop acc ((b * b) % modulus) (e / 2)
loop 1 (((baseVal % modulus) + modulus) % modulus) exp
let modInverse (a: int) (m: int) : int option =
let rec extGcd a b =
if a = 0 then (b, 0, 1)
else
let (g, x1, y1) = extGcd (b % a) a
(g, y1 - (b / a) * x1, x1)
let a' = ((a % m) + m) % m
let (g, x, _) = extGcd a' m
if g <> 1 then None
else Some (((x % m) + m) % m)
// ==============================================================================
// ELLIPTIC CURVE ARITHMETIC (Pure Functional, Small Field)
// ==============================================================================
[<Struct>]
type ECPoint =
| Finite of x: int * y: int
| Infinity
[<Struct>]
type EllipticCurve = { A: int; B: int; P: int }
let isOnCurve (curve: EllipticCurve) (point: ECPoint) : bool =
match point with
| Infinity -> true
| Finite (x, y) ->
let lhs = (y * y) % curve.P
let rhs = ((x * x * x + curve.A * x + curve.B) % curve.P + curve.P) % curve.P
lhs = rhs
let pointAdd (curve: EllipticCurve) (p1: ECPoint) (p2: ECPoint) : ECPoint =
match p1, p2 with
| Infinity, q -> q
| p, Infinity -> p
| Finite (x1, y1), Finite (x2, y2) ->
if x1 = x2 && ((y1 + y2) % curve.P = 0) then Infinity
else
let lambda =
if x1 = x2 && y1 = y2 then
let num = (3 * x1 * x1 + curve.A) % curve.P
let den = (2 * y1) % curve.P
match modInverse den curve.P with
| Some inv -> (num * inv) % curve.P
| None -> 0
else
let num = ((y2 - y1) % curve.P + curve.P) % curve.P
let den = ((x2 - x1) % curve.P + curve.P) % curve.P
match modInverse den curve.P with
| Some inv -> (num * inv) % curve.P
| None -> 0
let x3 = ((lambda * lambda - x1 - x2) % curve.P + curve.P) % curve.P
let y3 = ((lambda * (x1 - x3) - y1) % curve.P + curve.P) % curve.P
Finite (x3, y3)
let rec scalarMultiply (curve: EllipticCurve) (d: int) (point: ECPoint) : ECPoint =
let rec loop acc current k =
if k = 0 then acc
elif k % 2 = 1 then loop (pointAdd curve acc current) (pointAdd curve current current) (k / 2)
else loop acc (pointAdd curve current current) (k / 2)
if d = 0 then Infinity
elif d < 0 then
match scalarMultiply curve (-d) point with
| Infinity -> Infinity
| Finite (x, y) -> Finite (x, (curve.P - y) % curve.P)
else loop Infinity point d
let pointOrder (curve: EllipticCurve) (point: ECPoint) : int =
let rec loop current n =
if n > curve.P * curve.P then n
else
let next = pointAdd curve current point
if next = Infinity then n
else loop next (n + 1)
loop point 1
let findAllPoints (curve: EllipticCurve) : ECPoint list =
[ for x in 0 .. curve.P - 1 do
for y in 0 .. curve.P - 1 do
let pt = Finite (x, y)
if isOnCurve curve pt then yield pt ]
let findGenerator (curve: EllipticCurve) : (ECPoint * int) option =
findAllPoints curve
|> List.map (fun p -> (p, pointOrder curve p))
|> List.sortByDescending snd
|> List.tryHead
let formatPoint (point: ECPoint) : string =
match point with
| Infinity -> "O (infinity)"
| Finite (x, y) -> sprintf "(%d, %d)" x y
// ==============================================================================
// BUILT-IN CURVE PRESETS
// ==============================================================================
let private builtinPresets : Map<string, CurvePreset> =
[ { Name = "secp256k1-toy"; RealCurve = "secp256k1 (Bitcoin)"; Prime = 23; A = 0; B = 7
RealBits = 256; LogicalQubits = 2330; TGates = "10^8" }
{ Name = "p256-toy"; RealCurve = "P-256 (TLS/HTTPS)"; Prime = 29; A = 0; B = 7
RealBits = 256; LogicalQubits = 2330; TGates = "10^8" }
{ Name = "p384-toy"; RealCurve = "P-384 (Government)"; Prime = 31; A = 0; B = 7
RealBits = 384; LogicalQubits = 3484; TGates = "10^9" }
{ Name = "ed25519-toy"; RealCurve = "Ed25519 (SSH/GPG)"; Prime = 37; A = 0; B = 7
RealBits = 256; LogicalQubits = 2330; TGates = "10^8" } ]
|> List.map (fun c -> c.Name.ToLowerInvariant(), c)
|> Map.ofList
let private presetNames =
builtinPresets |> Map.toList |> List.map fst |> String.concat ", "
// ==============================================================================
// CSV INPUT PARSING
// ==============================================================================
let private loadCurvesFromCsv (path: string) : CurvePreset list =
let rows, errors = Data.readCsvWithHeaderWithErrors path
if not ((List.isEmpty errors) || quiet) then
for err in errors do eprintfn " Warning (CSV): %s" err
rows
|> List.choose (fun row ->
let get key = row.Values |> Map.tryFind key
let name = get "name" |> Option.defaultValue "Unknown"
match get "preset" with
| Some presetKey ->
let key = presetKey.Trim().ToLowerInvariant()
match builtinPresets |> Map.tryFind key with
| Some c -> Some { c with Name = name }
| None ->
if not quiet then eprintfn " Warning: unknown preset '%s' (available: %s)" presetKey presetNames
None
| None ->
match get "prime", get "a", get "b" with
| Some pStr, Some aStr, Some bStr ->
match Int32.TryParse pStr, Int32.TryParse aStr, Int32.TryParse bStr with
| (true, p), (true, a), (true, b) ->
let realBits = get "real_bits" |> Option.bind (fun s -> match Int32.TryParse s with true, v -> Some v | _ -> None) |> Option.defaultValue 256
let qubits = get "logical_qubits" |> Option.bind (fun s -> match Int32.TryParse s with true, v -> Some v | _ -> None) |> Option.defaultValue 2330
let tgates = get "t_gates" |> Option.defaultValue "10^8"
let realCurve = get "real_curve" |> Option.defaultValue name
Some { Name = name; RealCurve = realCurve; Prime = p; A = a; B = b
RealBits = realBits; LogicalQubits = qubits; TGates = tgates }
| _ ->
if not quiet then eprintfn " Warning: invalid numeric fields for '%s'" name
None
| _ ->
if not quiet then eprintfn " Warning: row '%s' missing 'prime'/'a'/'b' or 'preset'" name
None)
// ==============================================================================
// CURVE SELECTION
// ==============================================================================
let curves : CurvePreset list =
let allCurves =
match inputFile with
| Some path ->
let resolved = Data.resolveRelative __SOURCE_DIRECTORY__ path
if not quiet then printfn "Loading curves from: %s" resolved
loadCurvesFromCsv resolved
| None ->
builtinPresets |> Map.toList |> List.map snd
match curveFilter with
| [] -> allCurves
| filters ->
let filterSet = filters |> List.map (fun s -> s.ToLowerInvariant()) |> Set.ofList
allCurves
|> List.filter (fun c ->
let key = c.Name.ToLowerInvariant()
filterSet |> Set.exists (fun f -> key.Contains f))
if List.isEmpty curves then
eprintfn "Error: No curves selected. Available presets: %s" presetNames
exit 1
// ==============================================================================
// QUANTUM BACKEND (Rule 1)
// ==============================================================================
let quantumBackend : IQuantumBackend = LocalBackend() :> IQuantumBackend
if not quiet then
printfn ""
printfn "=================================================================="
printfn " Quantum ECDLP Threat to Cryptocurrency"
printfn "=================================================================="
printfn ""
printfn " Backend: %s" quantumBackend.Name
printfn " Curves: %d" curves.Length
printfn ""
// ==============================================================================
// ANALYSE EACH CURVE
// ==============================================================================
let analyseCurve (preset: CurvePreset) : ECDLPResult =
let curve = { A = preset.A; B = preset.B; P = preset.Prime }
let allPoints = findAllPoints curve
let curveOrder = allPoints.Length + 1
match findGenerator curve with
| None ->
{ Curve = preset; CurveOrder = curveOrder; GeneratorPoint = "none"; GeneratorOrder = 0
VictimPrivateKey = 0; VictimPublicKey = "none"; RecoveredKey = None
AttackSuccess = false; QPEPeriod = None; HasQuantumFailure = true }
| Some (generator, genOrder) ->
let victimKey = max 2 (genOrder / 3)
let victimPub = scalarMultiply curve victimKey generator
// Quantum period finding (demonstrates QPE infrastructure for ECDLP)
let orderProblem = periodFinder {
number (genOrder * 2 |> max 4)
precision 4
maxAttempts 10
backend quantumBackend
}
let qpePeriod, hasFailure =
match orderProblem |> Result.bind solve with
| Ok result when result.Period > 0 -> Some result.Period, false
| Ok _ -> None, false
| Error err ->
if not quiet then eprintfn " QPE error for %s: %s" preset.Name err.Message
None, true
// Use known group order as effective bound (full-scale attack would derive this)
let recoveredKey =
[1 .. genOrder]
|> List.tryFind (fun d -> scalarMultiply curve d generator = victimPub)
let success = recoveredKey = Some victimKey
if not quiet then
printfn " %s: G=%s order=%d victim_d=%d Q=%s recovered=%s %s"
preset.Name (formatPoint generator) genOrder victimKey
(formatPoint victimPub)
(match recoveredKey with Some d -> string d | None -> "N/A")
(if success then "COMPROMISED" else "FAILED")
{ Curve = preset; CurveOrder = curveOrder; GeneratorPoint = formatPoint generator
GeneratorOrder = genOrder; VictimPrivateKey = victimKey; VictimPublicKey = formatPoint victimPub
RecoveredKey = recoveredKey; AttackSuccess = success; QPEPeriod = qpePeriod
HasQuantumFailure = hasFailure }
let results = curves |> List.map analyseCurve
// Sort: successful attacks first, then by real-curve bits ascending (most vulnerable first).
let ranked =
results
|> List.sortBy (fun r -> (not r.AttackSuccess, r.Curve.RealBits))
// ==============================================================================
// REAL BITCOIN KEY (NBitcoin)
// ==============================================================================
let bitcoinKey = new Key()
let bitcoinPubKey = bitcoinKey.PubKey
let p2wpkhAddress = bitcoinPubKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main)
if not quiet then
printfn ""
printfn " Real secp256k1 Key (NBitcoin):"
printfn " Private (hex): %s" (bitcoinKey.ToHex())
printfn " Public (hex): %s" (bitcoinPubKey.ToHex())
printfn " Address: %s" (p2wpkhAddress.ToString())
printfn " (Breaking requires ~2,330 logical qubits — ~1000x current hardware)"
printfn ""
// ==============================================================================
// RANKED COMPARISON TABLE
// ==============================================================================
let printTable () =
printfn "=================================================================="
printfn " Ranked ECDLP Threat (toy curves modelling real-world ECC)"
printfn "=================================================================="
printfn ""
printfn " %-4s %-16s %-20s %5s %5s %6s %6s %s"
"#" "Curve" "Real Curve" "Prime" "Order" "Qubits" "Safe?" "Result"
printfn " %s" (String('=', 95))
ranked
|> List.iteri (fun i r ->
let safeStr = if r.AttackSuccess then "No" else "Yes"
let resultStr =
match r.RecoveredKey with
| Some d -> sprintf "d=%d (key recovered)" d
| None -> "no solution"
printfn " %-4d %-16s %-20s %5d %5d %6d %-6s %s"
(i + 1) r.Curve.Name r.Curve.RealCurve r.Curve.Prime
r.CurveOrder r.Curve.LogicalQubits safeStr resultStr)
printfn ""
printTable ()
// ==============================================================================
// SUMMARY
// ==============================================================================
if not quiet then
let compromised = ranked |> List.filter (fun r -> r.AttackSuccess)
let failed = ranked |> List.filter (fun r -> not r.AttackSuccess)
printfn " Compromised curves: %d (%s)"
compromised.Length
(compromised |> List.map (fun r -> r.Curve.Name) |> String.concat ", "
|> fun s -> if s = "" then "none" else s)
printfn " Uncompromised: %d (%s)"
failed.Length
(failed |> List.map (fun r -> r.Curve.Name) |> String.concat ", "
|> fun s -> if s = "" then "none" else s)
printfn " All real-world curves above require ~2,330+ logical qubits (current hw: ~20)."
printfn ""
// ==============================================================================
// STRUCTURED OUTPUT
// ==============================================================================
let resultMaps =
ranked
|> List.mapi (fun i r ->
[ "rank", string (i + 1)
"curve", r.Curve.Name
"real_curve", r.Curve.RealCurve
"prime", string r.Curve.Prime
"curve_a", string r.Curve.A
"curve_b", string r.Curve.B
"curve_order", string r.CurveOrder
"generator", r.GeneratorPoint
"generator_order", string r.GeneratorOrder
"victim_private_key", string r.VictimPrivateKey
"victim_public_key", r.VictimPublicKey
"recovered_key", (match r.RecoveredKey with Some d -> string d | None -> "")
"attack_success", string r.AttackSuccess
"qpe_period", (match r.QPEPeriod with Some p -> string p | None -> "")
"real_bits", string r.Curve.RealBits
"logical_qubits", string r.Curve.LogicalQubits
"t_gates", r.Curve.TGates
"bitcoin_public_key", bitcoinPubKey.ToHex()
"bitcoin_address", p2wpkhAddress.ToString()
"has_quantum_failure", string r.HasQuantumFailure ]
|> Map.ofList)
match Cli.tryGet "output" args with
| Some path ->
Reporting.writeJson path resultMaps
if not quiet then printfn "Results written to %s" path
| None -> ()
match Cli.tryGet "csv" args with
| Some path ->
let header =
[ "rank"; "curve"; "real_curve"; "prime"; "curve_a"; "curve_b"
"curve_order"; "generator"; "generator_order"
"victim_private_key"; "victim_public_key"; "recovered_key"
"attack_success"; "qpe_period"; "real_bits"; "logical_qubits"; "t_gates"
"bitcoin_public_key"; "bitcoin_address"; "has_quantum_failure" ]
let rows =
resultMaps
|> List.map (fun m ->
header |> List.map (fun h -> m |> Map.tryFind h |> Option.defaultValue ""))
Reporting.writeCsv path header rows
if not quiet then printfn "Results written to %s" path
| None -> ()
if argv.Length = 0 && not quiet then
printfn ""
printfn "Tip: Run with --help to see all options."
printfn " --curves secp256k1-toy,p256-toy Analyse specific curves"
printfn " --input curves.csv Load custom curves from CSV"
printfn " --csv results.csv Export ranked table as CSV"
printfn ""