From b3dfd161efbaab20451b7f2f02761681dd081bbe Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Thu, 11 Sep 2025 10:26:43 +0200 Subject: [PATCH] make it faster --- .cargo/config | 2 + Cargo.toml | 1 + src/bin/07_weird_assembly_machine.rs | 215 +++++++++++++++++++++++++-- 3 files changed, 204 insertions(+), 14 deletions(-) create mode 100644 .cargo/config diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..b736a9f --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[target.x86_64-unknown-linux-gnu] +rustflags = ["-Ctarget-cpu=native"] \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 0f7c227..ad0933f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ json = "0.12.4" lazy_static = "1.5.0" reqwest = { version = "0.12.23", features = ["blocking"] } rug = "1.28.0" +rand = "0.9.2" tqdm = "0.8.0" [lib] diff --git a/src/bin/07_weird_assembly_machine.rs b/src/bin/07_weird_assembly_machine.rs index 8a7cf2e..7640946 100644 --- a/src/bin/07_weird_assembly_machine.rs +++ b/src/bin/07_weird_assembly_machine.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use lib::compute_async; // https://button.qedaka.de/07_weird_assembly_machine.html?name=Jonah&n=10000000000000070&x=4410539207075887818 @@ -68,13 +69,128 @@ const DATA: &[u64] = &[ 11796026101712826135, 2668163399826034890, 1114227485540495868, 12046048025420064663, ]; -const N: u64 = 10000000047000070; -const X: u64 = 2925478411027973783; +const F_DATA_ODD: &[u64] = &[ + 1983433274171152502, 4306149096757034228, 14343520429273361980, 8958880034284534116, + 8603547194786577806, 2972859592172386223, 13957499527926304704, 18175802355555230033, + 9041554160340718905, 8781875577011631289, 12740759996936730793, 36982762481351889, + 14153151992898335474, 4935862324477075532, 7306264470589679036, 17533588483460941893, + 15871150386325366037, 3478268861108230615, 479142766442036494, 7159105042955251045, + 1624086221287676688, 8919795748898340418, 10671829566847605085, 13008024323290385149, + 14049021129695364125, 6780048778243776205, 2651487030051752391, 10722855545904724329, + 14035725187653591614, 2870088686340434062, 15310909505354296866, 12556381622736022, + 11529835006400851656, 6703871419421901239, 9550987712235670634, 775437932064144618, + 14931070884148319338, 2987230423093748765, 383566693419049119, 17309525382419025795, + 11236287710874215373, 18340814078744364919, 1279161511621817187, 1920989508643120885, + 8190686368463715609, 14160173248617445090, 738224523457857238, 16243257164432346178, + 10511018072084198626, 10935888386526855665, 10951084336179899998, 14735018136779508073, + 15907612288190699771, 10243968957660548523, 482804104250219826, 17583492563727572164, + 7464585852479953859, 485394859215598454, 1593433270596013571, 18000026569263060290, + 14858770339459881083, 4722043718580981069, 7028873583468179429, 16634011040379547181, +]; + +const F_DATA_EVEN: &[u64] = &[ + 4652450378788876572, 1718813027742122072, 663918887826880444, 6670740316248663863, + 6700009143044041388, 16923466181555045374, 11387212886131952048, 13965979168082782709, + 2422236213071595233, 4699138634002865087, 6493539149331166441, 7518901355855126514, + 16145558963603531461, 14786024152105552650, 6994957500730603671, 6969343254888557997, + 13840983121869895238, 13660562997364925760, 17265722274403090344, 16718136092983418592, + 4687774949942350684, 1375392036798645113, 2003687933393780368, 10165815540082234487, + 6089981897628107340, 14541747794483572236, 10203690416684845063, 3215632556062991790, + 5425642782789424861, 13599224699425799085, 12514386124935070361, 8663785549291965158, + 16808180269220250739, 958315213891692337, 17602667895639479950, 1575696692466204467, + 17915906007703271560, 14748722093156428888, 1657002993440401555, 661824732220019475, + 4209888120689731341, 16768316015744524190, 4243337273520801682, 2339349826505601295, + 3824297886017649294, 12702982826142705690, 16612576840497393302, 14651413872459783243, + 1731111914295200492, 7107419468395208292, 13131631592659790069, 7650600002594848965, + 10405567400049055611, 8308615406494459745, 3539740125465153737, 5461340322324117263, + 3781913427618838536, 15971526239448286573, 14866630059533000065, 9057464874518875466, + 12416130366361631938, 16536231451954324990, 16940792417174466635, 5850876492391527467 +]; + +const G_DATA_ODD: &[u64] = &[ + 12410528330544508756, 16866623929471404015, 11911839240743312753, 16311567754696617855, + 6167443972059675738, 15654252885237968221, 15893625052572332938, 4346427344699072411, + 4877879602443786926, 7599063822119831887, 5205882822753754388, 4650709573688509009, + 3439542438319165753, 11367809398768370698, 15697070102606971745, 17660823851141944045, + 790555901408282286, 14373045957383048804, 4172730130324824477, 10580693312540310997, + 10831187000660487642, 15625485673361876786, 6251220815425995920, 11570094412152496266, + 17462270586712613320, 7800984058182535552, 7394492557611433175, 6052520958514178935, + 3384539537094122312, 5250220469713550118, 7249675845174927105, 7117552070856836775, + 1868116097780436993, 1331676174817657411, 9255364723391110707, 10751825807113283157, + 9253933195750624372, 1607833087306003215, 13321802884521950102, 15257351979122208618, + 6044969349874479085, 5675576855821817897, 5999636052071649795, 8099973220884922619, + 15348422104481620954, 5769862232404285767, 11753891690213098393, 1134431722125240231, + 17890554632367806108, 17260857268223912753, 10532322140172121280, 6955449057221054224, + 15997800061778221221, 14631080324596192799, 11760495983535321642, 7751431864180074995, + 17568784491445889504, 4152071707666216212, 16639276319169537728, 10469641446888148209, + 14773299592165001119, 4842175923022528858, 471566220542957038, 2375787302702964486, +]; + +const G_DATA_EVEN: &[u64] = &[ + 15604445254264824171, 2390604585013608559, 11986374754145330532, 11017949026101828516, + 12566345980261789685, 15356854551478336603, 7512099414187555837, 12750275831668325946, + 10214055675311439170, 13443646811679848535, 16700058242611721938, 4423454089307812290, + 9103802734880117173, 2844597800879830746, 13843180097957768889, 8451963189055726564, + 3487238693652813426, 1299546876649551534, 12113986972185246713, 6519448239293388674, + 15121003074094999781, 14734970561277902445, 14188671603580717908, 6990763574012814336, + 10826116378520780499, 12743257710490956107, 10544515943049329704, 7398185314804331323, + 17207979707505274370, 10312488623848443780, 2525681217825377635, 1209032913852334193, + 2466271244689038797, 11480020761807728949, 9382271556984549762, 11547951404763329175, + 6098523488344631739, 13976835011142976617, 5952967173673330791, 10742137247908568911, + 17711990646608869291, 614357866529740908, 9459271394207301277, 13255507779934222511, + 4405075157125241953, 2658215311620786269, 3438348388076752545, 11273337813277990213, + 6461206275074180061, 407462859018947407, 16015082222849827426, 15274784353409233905, + 10868734442868526034, 15301879674491522395, 15304647555608021557, 1004732939015301729, + 14098615156664030434, 11650248670150490593, 15906967279319496936, 531379790791487398, + 11796026101712826135, 2668163399826034890, 1114227485540495868, 12046048025420064663, +]; + +const N: u64 = 10000045930000000; +const X: u64 = 16195396392266746892; + +/* +F = GF(2) +R. = PolynomialRing(F) +S. = R.quotient_ring(x^64 + 1) +T. = PolynomialRing(S) +Q. = T.quotient_ring(X^64 + 1) + +def num_to_poly(num): + out = 0 + i = 0 + while num != 0: + if num % 2 == 1: + out += xi^i + i += 1 + num //= 2 + return out + +def poly_to_num(poly): + out = 0 + for i in range(0, 64): + if poly[i] == 1: + out |= 1 << i + return out + + +data = [....] +f = reduce(lambda a, b: a * Xi + b, [num_to_poly(a) for a in data[0:128]]) +g = reduce(lambda a, b: a * Xi + b, [num_to_poly(a) for a in data[128:256]]) + +[poly_to_num(f.lift()[i]) for i in range(0, 64)].reverse() + + */ fn main() { compute_async(|tx| { let mut n = N; let mut x = X; + + while n % 1_000_000 != 0 { + n += 1; + x = func1(x, n); + } + loop { for _ in 0..1_000_000 { n += 1; @@ -86,6 +202,25 @@ fn main() { }); } +fn func1(x: u64, mut n: u64) -> u64 { + let x = func2(x, F_DATA_EVEN, F_DATA_ODD); + if x <= n { + n += 1; + } + func2(x.wrapping_sub(n), G_DATA_EVEN, G_DATA_ODD) +} + +fn func2(x: u64, data_even: &[u64], data_odd: &[u64]) -> u64 { + let data = if x.count_ones() % 2 == 0 { data_even } else { data_odd }; + + let mut out = 0; + for i in 0..data.len() { + out = func0(out, x) ^ data[i]; + } + out +} + +#[cfg(not(all(target_arch = "x86_64", target_feature = "pclmulqdq")))] fn func0(a: u64, mut b: u64) -> u64 { let mut x: u64 = 0; while b != 0 { @@ -96,25 +231,25 @@ fn func0(a: u64, mut b: u64) -> u64 { x } -fn func1(x: u64, mut n: u64) -> u64 { - let x = func2(x, &DATA[0..128]); - if x <= n { - n += 1; - } - func2(x.wrapping_sub(n), &DATA[128..256]) -} +#[cfg(all(target_arch = "x86_64", target_feature = "pclmulqdq"))] +fn func0(a: u64, b: u64) -> u64 { + use std::arch::x86_64::*; + unsafe { + let a_vec = _mm_set_epi64x(0, a as i64); + let b_vec = _mm_set_epi64x(0, b as i64); + let result = _mm_clmulepi64_si128(a_vec, b_vec, 0x00); -fn func2(x: u64, data: &[u64]) -> u64 { - let mut out = 0; - for i in 0..data.len() { - out = func0(out, x) ^ data[i]; + // Extract both 64-bit halves and XOR them + let low = _mm_extract_epi64(result, 0) as u64; + let high = _mm_extract_epi64(result, 1) as u64; + low ^ high } - out } #[cfg(test)] mod tests { use super::*; + use rand::prelude::*; #[test] fn func0test() { @@ -1119,6 +1254,58 @@ mod tests { assert_eq!(7713452682525289398, func0(3450079054, 2478937125)); assert_eq!(2321762543005257815, func0(3971444723, 1783469149)); } + + #[test] + fn func0assoc() { + let mut rng = rand::rng(); + for _ in 0..10000 { + let a = rng.next_u64(); + let b = rng.next_u64(); + let c = rng.next_u64(); + assert_eq!(func0(func0(a, b), c), func0(a, func0(b, c))); + } + } + + #[test] + fn func0power() { + let mut rng = rand::rng(); + for _ in 0..10000 { + let a = rng.next_u64(); + + let mut out = 1; + for _ in 0..64 { + out = func0(out, a); + } + + if a.count_ones() % 2 == 0 { + assert_eq!(out, 0); + } else { + assert_eq!(out, 1); + } + } + } + + #[test] + fn func0comm() { + let mut rng = rand::rng(); + for _ in 0..10000 { + let a = rng.next_u64(); + let b = rng.next_u64(); + assert_eq!(func0(a, b), func0(b, a)); + } + } + + #[test] + fn func0linear() { + let mut rng = rand::rng(); + for _ in 0..10000 { + let a = rng.next_u64(); + let b = rng.next_u64(); + let c = rng.next_u64(); + assert_eq!(func0(a ^ b, c), func0(a, c) ^ func0(b, c)); + assert_eq!(func0(a, b ^ c), func0(a, b) ^ func0(a, c)); + } + } #[test] fn func1test() {