Skip to content

Commit f6a1e46

Browse files
committed
feat: Use actual huge numbers, and optimize:
Update dev profile to optimize level 3 Initialize HashMap with known capacity For modular inversion, calculate g^(p-2) ahead of time Loop-invariant code motion for g^B in lookup
1 parent 41fe3fe commit f6a1e46

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

w5-mitm_dlog/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ version = "0.1.0"
44
authors = ["mikong <[email protected]>"]
55
edition = "2018"
66

7+
[profile.dev]
8+
opt-level = 3
9+
710
[dependencies]
811
num-bigint = "0.2"

w5-mitm_dlog/src/main.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ use num_bigint::BigUint;
77
type BigMap = HashMap<BigUint, u32>;
88

99
fn build_table(h: &BigUint, g: &BigUint, p: &BigUint, b: u32) -> BigMap {
10-
let mut table = HashMap::new();
10+
let mut table = HashMap::with_capacity(b as usize);
1111

1212
let two = vec![2];
1313
let two = BigUint::new(two);
1414
let p_minus_2 = p - &two;
15+
let g_inverse = g.modpow(&p_minus_2, p);
1516

1617
for x1 in 0..b {
1718
let bytes = x1.to_le_bytes();
1819
let big_x1 = BigUint::from_bytes_le(&bytes);
19-
let g_x1 = g.modpow(&big_x1, p);
20-
let g_x1_inverse = g_x1.modpow(&p_minus_2, p);
20+
let g_x1_inverse = g_inverse.modpow(&big_x1, p);
2121
let left = h * &g_x1_inverse % p;
2222

2323
table.insert(left, x1);
@@ -28,10 +28,9 @@ fn build_table(h: &BigUint, g: &BigUint, p: &BigUint, b: u32) -> BigMap {
2828

2929
fn lookup_x0_x1(table: &BigMap, g: &BigUint, p: &BigUint, b: u32) -> Option<(u32, u32)> {
3030
let big_b = BigUint::from_bytes_le(&b.to_le_bytes());
31+
let g_b = g.modpow(&big_b, p);
3132

3233
for x0 in 0..b {
33-
let g_b = g.modpow(&big_b, p);
34-
3534
let bytes = x0.to_le_bytes();
3635
let big_x0 = BigUint::from_bytes_le(&bytes);
3736
let right = g_b.modpow(&big_x0, p);
@@ -50,13 +49,10 @@ fn find_x(x0: u32, x1: u32, b: u32) -> u64 {
5049
fn main() {
5150
println!("Meet-in-the-Middle Attack (MITM)");
5251

53-
let h = vec![491];
54-
let h = BigUint::new(h);
55-
let g = vec![2];
56-
let g = BigUint::new(g);
57-
let p = vec![499];
58-
let p = BigUint::new(p);
59-
let b = 2u32.pow(3);
52+
let h = BigUint::parse_bytes(b"3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333", 10).unwrap();
53+
let g = BigUint::parse_bytes(b"11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568", 10).unwrap();
54+
let p = BigUint::parse_bytes(b"13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171", 10).unwrap();
55+
let b = 2u32.pow(20);
6056

6157
let table = build_table(&h, &g, &p, b);
6258
match lookup_x0_x1(&table, &g, &p, b) {

0 commit comments

Comments
 (0)