Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 195961df1b | |||
| c276a5a45b | |||
| 3556025849 | |||
| cb876e7427 | |||
| 5e37cfe883 | |||
| b2e43654c1 | |||
| d54dfc15ec | |||
| f8563d118c | |||
| 3dbf23547a | |||
| 521d25689a | |||
| a499dd2629 | |||
| 6dbaffdd28 | |||
| 0416261f09 | |||
| 6ed1c9b6ae | |||
| 12f3120b29 | |||
| 04ac13c45d | |||
| 83dacf81c5 | |||
| 6babd63c0a |
+1
-3
@@ -6,10 +6,8 @@ edition = "2024"
|
||||
[dependencies]
|
||||
anyhow = "1.0.99"
|
||||
json = "0.12.4"
|
||||
lazy_static = "1.5.0"
|
||||
reqwest = { version = "0.12.23", features = ["blocking"] }
|
||||
rug = "1.28.0"
|
||||
rand = "0.9.2"
|
||||
rug = { version = "1.28.0", features = ["integer"], default-features = false }
|
||||
tqdm = "0.8.0"
|
||||
sha2 = "0.10.9"
|
||||
percent-encoding = "2.3.2"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# button
|
||||
|
||||
This repository contains code for solving the challenges from [button.qedaka.de](button.qedaka.de). There are eight
|
||||
challenges in total:
|
||||
- [01_welcome](./js/01_welcome.js)
|
||||
- [02_these_numbers_are_big](./js/02_these_numbers_are_big.js)
|
||||
- [03_are_you_still_doing_this_by_hand](./js/03_are_you_still_doing_this_by_hand.js)
|
||||
- [04_broken_proof_of_work](./js/04_broken_proof_of_work.js)
|
||||
- [05_what_the_bf](./js/05_what_the_bf.js)
|
||||
- [06_automation_is_not_enough](./js/06_automation_is_not_enough.js)
|
||||
- [07_weird_assembly_machine](./js/07_weird_assembly_machine.js)
|
||||
- [08_revisit_everything](./js/08_revisit_everything.js)
|
||||
|
||||
Challenges one through four are trivial to solve. For challenges five through eight there is one binary each which
|
||||
demonstrates an approach to a solution.
|
||||
|
||||
One additional binary for solving the whole puzzle from start to finish is provided.
|
||||
|
||||
@@ -14,6 +14,7 @@ fn main() {
|
||||
println!("x = {x}");
|
||||
}
|
||||
|
||||
/// Tries to find a cycle in [`f_n`] using Brent's algorithm.
|
||||
fn main_cycle_detection() {
|
||||
compute_async(|tx| {
|
||||
// brent's algorithm
|
||||
@@ -69,20 +70,19 @@ fn main_cycle_detection() {
|
||||
}
|
||||
|
||||
|
||||
const A: u128 = 909_090_909_091u128; // (10 * b + 1) / 11
|
||||
const B: u128 = 1_000_000_000_000u128; // 10^12
|
||||
const C: u128 = 999_999_999_989u128; // b - 11
|
||||
const D: u128 = 999_999_999_999u128; // b - 1
|
||||
/// The deobfuscated function `f`
|
||||
fn f(mut x: u128) -> u128 {
|
||||
const A: u128 = 909_090_909_091u128; // (10 * b + 1) / 11
|
||||
const B: u128 = 1_000_000_000_000u128; // 10^12
|
||||
const C: u128 = 999_999_999_989u128; // b - 11
|
||||
const D: u128 = 999_999_999_999u128; // b - 1
|
||||
|
||||
x = x * A % B;
|
||||
x = (x * C + D) / B;
|
||||
x
|
||||
}
|
||||
|
||||
|
||||
const MU: u128 = 0;
|
||||
const LAM: u128 = 249_999_999_997;
|
||||
/// Computes `f(f(...f(X0)...))` where `f` is applied `n` times.
|
||||
/// Computes `f(f(...f(X0)...))` (where [`f`] is applied `n` times) using data produces by [`main_cycle_detection`]
|
||||
fn f_n(n: u128) -> u128 {
|
||||
let n = n % LAM;
|
||||
|
||||
@@ -101,6 +101,13 @@ fn f_n(n: u128) -> u128 {
|
||||
value
|
||||
}
|
||||
|
||||
/// The offset of the cycle of [`f_n`] as returned by [`main_cycle_detection`].
|
||||
const MU: u128 = 0;
|
||||
|
||||
/// The length of the cycle of [`f_n`] as returned by [`main_cycle_detection`].
|
||||
const LAM: u128 = 249_999_999_997;
|
||||
|
||||
/// In- and output values of [`f_n`] as returned by [`main_cycle_detection`].
|
||||
const F_DATA: &[(u128, u128)] = &[
|
||||
(11538000000, 540610237213),
|
||||
(11539000000, 525319061659),
|
||||
|
||||
@@ -3,38 +3,34 @@
|
||||
use anyhow::Result;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use lazy_static::lazy_static;
|
||||
use rug::ops::MulFrom;
|
||||
use rug::{Assign, Integer};
|
||||
use std::ops::Add;
|
||||
use std::str::FromStr;
|
||||
use std::sync::LazyLock;
|
||||
use lib::submit;
|
||||
|
||||
lazy_static!(
|
||||
static ref N0: Integer = Integer::from(1001997000002u128);
|
||||
static ref X0: Integer = Integer::from_str("13361120425250501347832030920224855036595311511513374827901659942687569213067904382419070310529480239935839308518100143939024253857202176158254361885679515473530816156355117821922648901555956036125537445852483998567339002752976575910942962150").unwrap();
|
||||
static N0: LazyLock<Integer> = LazyLock::new(|| Integer::from(1001997000002u128));
|
||||
static X0: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("13361120425250501347832030920224855036595311511513374827901659942687569213067904382419070310529480239935839308518100143939024253857202176158254361885679515473530816156355117821922648901555956036125537445852483998567339002752976575910942962150").unwrap());
|
||||
|
||||
static ref N1: Integer = Integer::from(1774734677598263u128);
|
||||
static ref X1: Integer = Integer::from_str("11593323295292067533341930289979269834079920106030434522240627836294015987043679078861672344892723053626369715841527508395668434915559610809835295347647318767117730544084796074700752732601302352244011354650441946234192592199510139121367920997").unwrap();
|
||||
static N1: LazyLock<Integer> = LazyLock::new(|| Integer::from(1774734677598263u128));
|
||||
static X1: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("11593323295292067533341930289979269834079920106030434522240627836294015987043679078861672344892723053626369715841527508395668434915559610809835295347647318767117730544084796074700752732601302352244011354650441946234192592199510139121367920997").unwrap());
|
||||
|
||||
static ref N2: Integer = Integer::from(10000000000000000u128);
|
||||
static ref X2: Integer = Integer::from_str("12451812012967875768280645960359102621677777894853024330321329610110355503343897740935595925071894025774585740051400274576363979250507927352038651542641174781860822343081975931661658111525566916416897092390763978119448659635732847002032508677").unwrap();
|
||||
static M: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("14004392365098131090160062970945115111185775413941111064876648140973294115502980816410773368597517292734034227298996122159833675150497554142801209096513652073059992938078366061434391648276904643753267405058183481162693381822800709938988762923").unwrap());
|
||||
static E: LazyLock<Integer> = LazyLock::new(|| Integer::from(65537));
|
||||
|
||||
static ref M: Integer = Integer::from_str("14004392365098131090160062970945115111185775413941111064876648140973294115502980816410773368597517292734034227298996122159833675150497554142801209096513652073059992938078366061434391648276904643753267405058183481162693381822800709938988762923").unwrap();
|
||||
static ref E: Integer = Integer::from(65537);
|
||||
|
||||
static ref A: Integer = Integer::from_str("1466928606874115117499939299261").unwrap();
|
||||
static ref B: Integer = Integer::from_str("49119078231137394008451554322").unwrap();
|
||||
);
|
||||
static A: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("1466928606874115117499939299261").unwrap());
|
||||
static B: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("49119078231137394008451554322").unwrap());
|
||||
|
||||
fn main() {
|
||||
let f = faulhaber_equation_from_file("res/06_faulhaber_coefficient_65537.log").unwrap();
|
||||
let f = faulhaber_formula_from_file("res/06_faulhaber_coefficient_65537.log").unwrap();
|
||||
|
||||
let n: Integer = Integer::from(10000000000000000u128);
|
||||
let x = (X1.clone() + f(n.clone()) - f(N1.clone())).modulo(&M);
|
||||
println!("{:?}", x);
|
||||
println!("{:?}", submit(n, x).unwrap());
|
||||
|
||||
// Use the submission server as an oracle to find the `n` at which `x` needs to be doubles.
|
||||
// let (mut low, mut high) = (
|
||||
// Integer::from(1774734677598262u128),
|
||||
// Integer::from(1774734677598263u128),
|
||||
@@ -55,19 +51,17 @@ fn main() {
|
||||
// }
|
||||
}
|
||||
|
||||
fn step(mut n: Integer, mut x: Integer, mut temp1: Integer, mut temp2: Integer) -> (Integer, Integer, Integer, Integer) {
|
||||
/// Computes one step of the challenge.
|
||||
fn step(mut n: Integer, mut x: Integer) -> (Integer, Integer) {
|
||||
n += 1;
|
||||
|
||||
let b: &Integer = &B;
|
||||
|
||||
// a = pow(n, e, m)
|
||||
temp1.assign(&n);
|
||||
let a = temp1.pow_mod(&E, &M).unwrap();
|
||||
let a = n.clone().pow_mod(&E, &M).unwrap();
|
||||
|
||||
// if (a % A == b) {
|
||||
temp2.assign(&a);
|
||||
let c = temp2.modulo(&A);
|
||||
if c.eq(b) {
|
||||
if a.clone().modulo(&A).eq(b) {
|
||||
// x += x
|
||||
x *= 2;
|
||||
}
|
||||
@@ -76,19 +70,18 @@ fn step(mut n: Integer, mut x: Integer, mut temp1: Integer, mut temp2: Integer)
|
||||
// x %= m
|
||||
x = x.add(&a).modulo(&M);
|
||||
|
||||
(n, x, a, c)
|
||||
(n, x)
|
||||
}
|
||||
|
||||
|
||||
fn faulhaber_equation(n: usize) -> impl Fn(Integer) -> Integer {
|
||||
fn faulhaber_formula(n: usize) -> impl Fn(Integer) -> Integer {
|
||||
let coefficients = faulhaber_triangle(n);
|
||||
for (i, coeff) in coefficients.iter().enumerate() {
|
||||
println!("{i} = {coeff}");
|
||||
}
|
||||
faulhaber_equation_from_coefficients(coefficients)
|
||||
faulhaber_formula_from_coefficients(coefficients)
|
||||
}
|
||||
|
||||
fn faulhaber_equation_from_file(path: &str) -> Result<impl Fn(Integer) -> Integer> {
|
||||
fn faulhaber_formula_from_file(path: &str) -> Result<impl Fn(Integer) -> Integer> {
|
||||
let mut coefficients = Vec::new();
|
||||
|
||||
let file = File::open(path)?;
|
||||
@@ -103,10 +96,10 @@ fn faulhaber_equation_from_file(path: &str) -> Result<impl Fn(Integer) -> Intege
|
||||
sum.modulo_mut(&M);
|
||||
println!("{:?}", sum);
|
||||
|
||||
Ok(faulhaber_equation_from_coefficients(coefficients))
|
||||
Ok(faulhaber_formula_from_coefficients(coefficients))
|
||||
}
|
||||
|
||||
fn faulhaber_equation_from_coefficients(coefficients: Vec<Integer>) -> impl Fn(Integer) -> Integer {
|
||||
fn faulhaber_formula_from_coefficients(coefficients: Vec<Integer>) -> impl Fn(Integer) -> Integer {
|
||||
move |x| {
|
||||
let mut result = Integer::from(0);
|
||||
let mut temp = Integer::from(0);
|
||||
@@ -127,6 +120,7 @@ fn faulhaber_equation_from_coefficients(coefficients: Vec<Integer>) -> impl Fn(I
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the coefficients of the Faulhaber's formula.
|
||||
fn faulhaber_triangle(n: usize) -> Vec<Integer> {
|
||||
let neg1 = Integer::from(-1);
|
||||
let mut inv = Vec::with_capacity(n + 10);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use lib::submit;
|
||||
use lib::challenges::c7_weird_assembly_machine::f_n;
|
||||
use lib::challenges::c7_weird_assembly_machine::h_n;
|
||||
|
||||
const N: u64 = 979607657800000055;
|
||||
const X: u64 = 10962444957429324784;
|
||||
|
||||
fn main() {
|
||||
let (n, x) = f_n(N, X, 1_000_000_000_000_000_000u64 - N);
|
||||
let (n, x) = h_n(N, X, 1_000_000_000_000_000_000u64 - N);
|
||||
println!("n = {n}");
|
||||
println!("x = {x}");
|
||||
submit(n, x).unwrap();
|
||||
|
||||
@@ -1,66 +1,64 @@
|
||||
#![allow(dead_code)]
|
||||
// You can change the color of your name without changing your name.
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::LazyLock;
|
||||
use lib::submit_with_name;
|
||||
use rug::ops::{DivRounding, Pow};
|
||||
use rug::ops::DivRounding;
|
||||
use rug::Integer;
|
||||
use std::str::FromStr;
|
||||
|
||||
lazy_static!(
|
||||
static ref M: Vec<Integer> = vec![
|
||||
Integer::from_str("1434008120931012805118743381511953470053626771934922366720474361817092344508461315782084314493561729943207476618986862630642339876124303277669871276872966561372584096756184146218943438497504108129008248947186295903702650936563195517405177267").unwrap(),
|
||||
Integer::from_str("2279983661166952569412337967792106681853682761140508555222469060830820310854240357968620360297575902950280777825697828457195462861233847516393870602678279643129330544204145040751369969379909048248974150190225328726111083312867874886821021103").unwrap(),
|
||||
Integer::from_str("2403618920217682814630057320815015451780244245370783386150600183266752801851444036459202928614238339117073052233115617730895326577436837775969446809467396763885076993639257102975158794586432834588823770619549927566204909745661463923650207527").unwrap(),
|
||||
Integer::from_str("2211546667714470043261801539278553165605603776798179390251801872528197206642136833266100994696226928008915728096531663182964211466279822828133246114741263620898853542511659005977098742505685248155022513107926722707345462354618444663133511693").unwrap(),
|
||||
Integer::from_str("5010303295979029720953954773805463848657367496896667256925387074631207128976445011411567186128665343367664557600927865746013809617850756247038364977849593298370638415538050617717579812040063809050466079467593876757904967054587247239775981173").unwrap(),
|
||||
Integer::from_str("3643715306191323974849750185026376664803002052031009917919146298629124228139669830885418351414368670190933608901908463160313491829979902265661425377942098542786579385118688686530379428281500432090209857512031899193610942009997666480141622931").unwrap(),
|
||||
Integer::from_str("4241834355731759088612098900889150480486830356437779625635656124158632786788891567665015370346587717023382467402755844902551269466840016945251052520143748217810459016980596787260934741544681783243577436529017346762263051435128563504848147047").unwrap(),
|
||||
Integer::from_str("2523900322512898153092283527555686191954886966926984043710614987380057039954266050243558368595618000310491183390298731322739555708825633335455707335046252155477145429505231996976303780687577721717937956727462248609588253109048826912219512233").unwrap(),
|
||||
Integer::from_str("1171824891807552748899024459763353345756465570610286429518292854539955442689370561445213645478177496112486904608544314008088766463602198222556565269084818799872887643847609538261074418353385510616209919715913997563223016102524218731609073989").unwrap(),
|
||||
Integer::from_str("46143420546617745197213974992864349706110841054938173439888506485894731816049443533063388738883948066474775508317079165731544093393852672679526902040341664165103617856112836137307481490757786503309809660177211850194917302104601047445901953").unwrap(),
|
||||
Integer::from_str("752940078968697502678967384415617976772053933529596207932495368245135279916303315035371282640886295210962243111798224672106391175631921911566406669776741217206089188001565916873143077167066022880702036063882903129906642676129495022972131209").unwrap(),
|
||||
Integer::from_str("1520329335648026567766367275046307578542257991128853001791455552493665632232325588405913221176897049790060536801309884790312144713538875231774340342847186215385917144874277668809985499482994078141272859278475075285977463657736734963652987373").unwrap(),
|
||||
Integer::from_str("50017625495288818782435924507013834110031999034643307338382821405698791610778877531830614693405608386655072058895222505909833754916862533905752631274852366748935792253442598350951461591550736346237955863906449458952663228800927140320157667").unwrap(),
|
||||
Integer::from_str("38401610206931167080264498597398870802860254093725730772991011582569894725813373580324866968569176388369611982707097825344506655231925447764442769351345178318652542550409150557352343943618674608946011463309394821673565650165744273097849949").unwrap(),
|
||||
Integer::from_str("184608244646942021323366304782439538668746375277959348413634650687235587234614717772753935753287722857766192563391263109670280253185426161046298354787439672044497169888140233752555779424149969713422475505388807903963252621863734032956280519").unwrap(),
|
||||
Integer::from_str("351257593287654452522276916291373563742090233637383104641821340906564637280571336214002417682603763101501053115051814027840290224795605063148356403544846161606884919930269677277921998672949839936317112237896436966630449023178621753921263019").unwrap(),
|
||||
Integer::from_str("17776213033029898042736097216007742758258673720676143715791913350512342764629944960235747926785035898455679245982654344139524841812891998487061291841045102881105147645119091822393445581339563032411679270141022474438218196237858872902098301").unwrap(),
|
||||
Integer::from_str("1119968749281206669187118962426182039471518541839452651714133200569385292395928211155932154512551823867768143506831179697150725335729082336884390018097450391780455129434304717766108240883676474188676687895688014005390129134060293445952714131").unwrap(),
|
||||
Integer::from_str("2380359028703143565637146811469828781458088476878403649995644924026273963767396041207843943179863650694243979825010501084343724584937371440937331372027478732522369607122724766608383356158192256503987385487068417019456480384653228271517184073").unwrap(),
|
||||
Integer::from_str("936391425922485612330507260988001138454208806035318063315033628148710609664547188763477146808759892616317103092432173093713978381604382096301822456981993396860893433866194478210978043268587964153688806562028136539787248761208718402921714823").unwrap(),
|
||||
Integer::from_str("2391922140661526942794958081614866503484666510987906109575239558478076826463675970540222364377429236018134610321177838404492145979186686367103845387306040811399772932134010203596245485657848849575365830819533003431899529331962049972105422829").unwrap(),
|
||||
Integer::from_str("964374520562152761892099701871246383149820707812540579294812519029773619714236033304961754196611532652288982150226191973613367922123899620952613834335606606286016170579235167587934724407287026718339208674781371124584567946455593866472142439").unwrap(),
|
||||
Integer::from_str("661248789387918195968343456268415015655227429901485551376733649662534559008900836450733880455467249525009566343824739697592753526891306540092946639656510220832776564628364080959055214446202095345118415029995831071367289505624996230686042743").unwrap(),
|
||||
Integer::from_str("1495554819407996415862156847203243369888937825574835272994697401723776375381219545680004526750874917248129621701075619516487125846492717688083196840105055779039883420765261872964729328338142922514236283019345885773272705802789375709371531891").unwrap(),
|
||||
Integer::from_str("2050623645029219438127819922659780851370739111321093329697282080391534071710675160727304612357988694773734599338417354100409517400415116108045288729878347023871908849359856487326222636295170968226843328973460491743192554642495605580203827647").unwrap(),
|
||||
Integer::from_str("5300307462410213577667480837938258777512352583051978520467068441893175668391866168747583454129771867041290645051627565118578602725370687799533406736037648231124221590788082333656018605599589489800640114935804161182962684573894987008279375881").unwrap(),
|
||||
Integer::from_str("3704327655413180273494896105414341120264540967426618385392149993686601012341448146813701061680592497964083943234912731844161335477856719778925685202496984827468326099066276955751929212099287128630393090960611662156345752418189491108549779761").unwrap(),
|
||||
Integer::from_str("4293873523905537531247339274016468845613818921758414861106911431522285730431627111776083755372250902094439575944006556178967448926801694819806680902651676479412361255593771594300163105918014188734865473316383051363586442734877422424944476487").unwrap(),
|
||||
Integer::from_str("1589457356017416267968030208391225107392251829332800185708809994594071680629191862623870630995580949827174233852065931316179886989972734453310743029959684374385051032958116409820646011612627461794497048612013500205304829106740521813279065839").unwrap(),
|
||||
Integer::from_str("677060499756811014115027316973737658993937681267671378751091515074444191639222306675859985253758266662687011882335084637681850124628916282787941181661248429412152192230660554583653075199720096784977912362093898785710927643429800554309996621").unwrap(),
|
||||
Integer::from_str("4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616569935833357648393898915677206732580748330214938398411668157941515847730087263301030154072458063812554524260405671629754999").unwrap(),
|
||||
Integer::from_str("1602074580348123931489241137005511885150980182400049992026659585131187985620536794154659820191316264063547338262596211651955802983450000900446591221580949545163434870046188079786417621515405352023176920247457687361358148356208096808525338397").unwrap(),
|
||||
Integer::from_str("2561993755606565068035375148802286812072711717323441363005149722023895797027723216764951149251447180880317275019544062589806987036712157902709291674934493635207098007981128987753281766285445399631018190456476459604899786219806829862034503919").unwrap(),
|
||||
Integer::from_str("1457054801380350736800076081677201400665406203824020727471775641174383980331187058211832905604880205627938232136048301389594977658775329490376467311636742994730009572046684919498881836580821362806591224089188354188782107744327550013385898679").unwrap(),
|
||||
Integer::from_str("63236406531205434317594389945524553066650347945711034187542626293222184231786980314347573971609802315464270916666042219689326463061892323637662557342398379414478296594450374661462435994098294952191568682470529143295022566680404002417824407").unwrap(),
|
||||
Integer::from_str("197584207289801721640320350702781512605131263686515431131918340568997783498811538104924263177220361290500812540231048642224996620094592927292942263102162109623392197130698355962984228640696217934383302653006588137949636000986800801778335657").unwrap(),
|
||||
Integer::from_str("107646974355805618130192750841008263946998235997036512644246089806483971253986128403392278299190598483359268422291624377225409658571257936659998943160556099228016404911568061972541462863973742808216138653302666989981023076627131009019648093").unwrap(),
|
||||
Integer::from_str("1339338638103691041488101095975795624888087363910833767941545495164307897693922886889431835980515628174298063795556722365483994582432076986194090322659335982083818451837412988740007497627907154682332316972997765904572884621671832534078952307").unwrap(),
|
||||
Integer::from_str("4434008847683596023521080910694307462609680232648893208331172489192536036999251664058651161016448705529504803145115341661351913895515662510503514243928030586139092055076376059786525834604160882940981043706619590945099008097035756330871176929").unwrap(),
|
||||
Integer::from_str("2861034394438211841574954026003099819138101239247590122220350015475642835378420417008259980687528330087930738069620032709655292858712710050922359392413494086067180601383341612168193292772028605966992134213209023470256124404157207490432001519").unwrap(),
|
||||
Integer::from_str("2192496740130688243467014868961425982524426805724142867736885035473886629020046845749143690992453345556925544037271383038390660805714030629830845497904446979103911346917405936472233104694103049940390437540539269358666128436739707024829933171").unwrap(),
|
||||
Integer::from_str("4729254672091433598483706976591610165407510022326154887172965802144851945337220063633854456109688411742113721031379206359782182729020553179365019549633309936476652959201812418790254033614677768930428905990768941807243900642422321011756634063").unwrap(),
|
||||
];
|
||||
// You can change the color of your name without changing your name.
|
||||
static M: LazyLock<Vec<Integer>> = LazyLock::new(|| vec![
|
||||
Integer::from_str("1434008120931012805118743381511953470053626771934922366720474361817092344508461315782084314493561729943207476618986862630642339876124303277669871276872966561372584096756184146218943438497504108129008248947186295903702650936563195517405177267").unwrap(),
|
||||
Integer::from_str("2279983661166952569412337967792106681853682761140508555222469060830820310854240357968620360297575902950280777825697828457195462861233847516393870602678279643129330544204145040751369969379909048248974150190225328726111083312867874886821021103").unwrap(),
|
||||
Integer::from_str("2403618920217682814630057320815015451780244245370783386150600183266752801851444036459202928614238339117073052233115617730895326577436837775969446809467396763885076993639257102975158794586432834588823770619549927566204909745661463923650207527").unwrap(),
|
||||
Integer::from_str("2211546667714470043261801539278553165605603776798179390251801872528197206642136833266100994696226928008915728096531663182964211466279822828133246114741263620898853542511659005977098742505685248155022513107926722707345462354618444663133511693").unwrap(),
|
||||
Integer::from_str("5010303295979029720953954773805463848657367496896667256925387074631207128976445011411567186128665343367664557600927865746013809617850756247038364977849593298370638415538050617717579812040063809050466079467593876757904967054587247239775981173").unwrap(),
|
||||
Integer::from_str("3643715306191323974849750185026376664803002052031009917919146298629124228139669830885418351414368670190933608901908463160313491829979902265661425377942098542786579385118688686530379428281500432090209857512031899193610942009997666480141622931").unwrap(),
|
||||
Integer::from_str("4241834355731759088612098900889150480486830356437779625635656124158632786788891567665015370346587717023382467402755844902551269466840016945251052520143748217810459016980596787260934741544681783243577436529017346762263051435128563504848147047").unwrap(),
|
||||
Integer::from_str("2523900322512898153092283527555686191954886966926984043710614987380057039954266050243558368595618000310491183390298731322739555708825633335455707335046252155477145429505231996976303780687577721717937956727462248609588253109048826912219512233").unwrap(),
|
||||
Integer::from_str("1171824891807552748899024459763353345756465570610286429518292854539955442689370561445213645478177496112486904608544314008088766463602198222556565269084818799872887643847609538261074418353385510616209919715913997563223016102524218731609073989").unwrap(),
|
||||
Integer::from_str("46143420546617745197213974992864349706110841054938173439888506485894731816049443533063388738883948066474775508317079165731544093393852672679526902040341664165103617856112836137307481490757786503309809660177211850194917302104601047445901953").unwrap(),
|
||||
Integer::from_str("752940078968697502678967384415617976772053933529596207932495368245135279916303315035371282640886295210962243111798224672106391175631921911566406669776741217206089188001565916873143077167066022880702036063882903129906642676129495022972131209").unwrap(),
|
||||
Integer::from_str("1520329335648026567766367275046307578542257991128853001791455552493665632232325588405913221176897049790060536801309884790312144713538875231774340342847186215385917144874277668809985499482994078141272859278475075285977463657736734963652987373").unwrap(),
|
||||
Integer::from_str("50017625495288818782435924507013834110031999034643307338382821405698791610778877531830614693405608386655072058895222505909833754916862533905752631274852366748935792253442598350951461591550736346237955863906449458952663228800927140320157667").unwrap(),
|
||||
Integer::from_str("38401610206931167080264498597398870802860254093725730772991011582569894725813373580324866968569176388369611982707097825344506655231925447764442769351345178318652542550409150557352343943618674608946011463309394821673565650165744273097849949").unwrap(),
|
||||
Integer::from_str("184608244646942021323366304782439538668746375277959348413634650687235587234614717772753935753287722857766192563391263109670280253185426161046298354787439672044497169888140233752555779424149969713422475505388807903963252621863734032956280519").unwrap(),
|
||||
Integer::from_str("351257593287654452522276916291373563742090233637383104641821340906564637280571336214002417682603763101501053115051814027840290224795605063148356403544846161606884919930269677277921998672949839936317112237896436966630449023178621753921263019").unwrap(),
|
||||
Integer::from_str("17776213033029898042736097216007742758258673720676143715791913350512342764629944960235747926785035898455679245982654344139524841812891998487061291841045102881105147645119091822393445581339563032411679270141022474438218196237858872902098301").unwrap(),
|
||||
Integer::from_str("1119968749281206669187118962426182039471518541839452651714133200569385292395928211155932154512551823867768143506831179697150725335729082336884390018097450391780455129434304717766108240883676474188676687895688014005390129134060293445952714131").unwrap(),
|
||||
Integer::from_str("2380359028703143565637146811469828781458088476878403649995644924026273963767396041207843943179863650694243979825010501084343724584937371440937331372027478732522369607122724766608383356158192256503987385487068417019456480384653228271517184073").unwrap(),
|
||||
Integer::from_str("936391425922485612330507260988001138454208806035318063315033628148710609664547188763477146808759892616317103092432173093713978381604382096301822456981993396860893433866194478210978043268587964153688806562028136539787248761208718402921714823").unwrap(),
|
||||
Integer::from_str("2391922140661526942794958081614866503484666510987906109575239558478076826463675970540222364377429236018134610321177838404492145979186686367103845387306040811399772932134010203596245485657848849575365830819533003431899529331962049972105422829").unwrap(),
|
||||
Integer::from_str("964374520562152761892099701871246383149820707812540579294812519029773619714236033304961754196611532652288982150226191973613367922123899620952613834335606606286016170579235167587934724407287026718339208674781371124584567946455593866472142439").unwrap(),
|
||||
Integer::from_str("661248789387918195968343456268415015655227429901485551376733649662534559008900836450733880455467249525009566343824739697592753526891306540092946639656510220832776564628364080959055214446202095345118415029995831071367289505624996230686042743").unwrap(),
|
||||
Integer::from_str("1495554819407996415862156847203243369888937825574835272994697401723776375381219545680004526750874917248129621701075619516487125846492717688083196840105055779039883420765261872964729328338142922514236283019345885773272705802789375709371531891").unwrap(),
|
||||
Integer::from_str("2050623645029219438127819922659780851370739111321093329697282080391534071710675160727304612357988694773734599338417354100409517400415116108045288729878347023871908849359856487326222636295170968226843328973460491743192554642495605580203827647").unwrap(),
|
||||
Integer::from_str("5300307462410213577667480837938258777512352583051978520467068441893175668391866168747583454129771867041290645051627565118578602725370687799533406736037648231124221590788082333656018605599589489800640114935804161182962684573894987008279375881").unwrap(),
|
||||
Integer::from_str("3704327655413180273494896105414341120264540967426618385392149993686601012341448146813701061680592497964083943234912731844161335477856719778925685202496984827468326099066276955751929212099287128630393090960611662156345752418189491108549779761").unwrap(),
|
||||
Integer::from_str("4293873523905537531247339274016468845613818921758414861106911431522285730431627111776083755372250902094439575944006556178967448926801694819806680902651676479412361255593771594300163105918014188734865473316383051363586442734877422424944476487").unwrap(),
|
||||
Integer::from_str("1589457356017416267968030208391225107392251829332800185708809994594071680629191862623870630995580949827174233852065931316179886989972734453310743029959684374385051032958116409820646011612627461794497048612013500205304829106740521813279065839").unwrap(),
|
||||
Integer::from_str("677060499756811014115027316973737658993937681267671378751091515074444191639222306675859985253758266662687011882335084637681850124628916282787941181661248429412152192230660554583653075199720096784977912362093898785710927643429800554309996621").unwrap(),
|
||||
Integer::from_str("4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616569935833357648393898915677206732580748330214938398411668157941515847730087263301030154072458063812554524260405671629754999").unwrap(),
|
||||
Integer::from_str("1602074580348123931489241137005511885150980182400049992026659585131187985620536794154659820191316264063547338262596211651955802983450000900446591221580949545163434870046188079786417621515405352023176920247457687361358148356208096808525338397").unwrap(),
|
||||
Integer::from_str("2561993755606565068035375148802286812072711717323441363005149722023895797027723216764951149251447180880317275019544062589806987036712157902709291674934493635207098007981128987753281766285445399631018190456476459604899786219806829862034503919").unwrap(),
|
||||
Integer::from_str("1457054801380350736800076081677201400665406203824020727471775641174383980331187058211832905604880205627938232136048301389594977658775329490376467311636742994730009572046684919498881836580821362806591224089188354188782107744327550013385898679").unwrap(),
|
||||
Integer::from_str("63236406531205434317594389945524553066650347945711034187542626293222184231786980314347573971609802315464270916666042219689326463061892323637662557342398379414478296594450374661462435994098294952191568682470529143295022566680404002417824407").unwrap(),
|
||||
Integer::from_str("197584207289801721640320350702781512605131263686515431131918340568997783498811538104924263177220361290500812540231048642224996620094592927292942263102162109623392197130698355962984228640696217934383302653006588137949636000986800801778335657").unwrap(),
|
||||
Integer::from_str("107646974355805618130192750841008263946998235997036512644246089806483971253986128403392278299190598483359268422291624377225409658571257936659998943160556099228016404911568061972541462863973742808216138653302666989981023076627131009019648093").unwrap(),
|
||||
Integer::from_str("1339338638103691041488101095975795624888087363910833767941545495164307897693922886889431835980515628174298063795556722365483994582432076986194090322659335982083818451837412988740007497627907154682332316972997765904572884621671832534078952307").unwrap(),
|
||||
Integer::from_str("4434008847683596023521080910694307462609680232648893208331172489192536036999251664058651161016448705529504803145115341661351913895515662510503514243928030586139092055076376059786525834604160882940981043706619590945099008097035756330871176929").unwrap(),
|
||||
Integer::from_str("2861034394438211841574954026003099819138101239247590122220350015475642835378420417008259980687528330087930738069620032709655292858712710050922359392413494086067180601383341612168193292772028605966992134213209023470256124404157207490432001519").unwrap(),
|
||||
Integer::from_str("2192496740130688243467014868961425982524426805724142867736885035473886629020046845749143690992453345556925544037271383038390660805714030629830845497904446979103911346917405936472233104694103049940390437540539269358666128436739707024829933171").unwrap(),
|
||||
Integer::from_str("4729254672091433598483706976591610165407510022326154887172965802144851945337220063633854456109688411742113721031379206359782182729020553179365019549633309936476652959201812418790254033614677768930428905990768941807243900642422321011756634063").unwrap(),
|
||||
]);
|
||||
|
||||
static ref E: Integer = Integer::from(65537);
|
||||
static ref E_100: Integer = Integer::from(65537).pow(100);
|
||||
static ref E_1000: Integer = Integer::from(65537).pow(1000);
|
||||
static E: LazyLock<Integer> = LazyLock::new(|| Integer::from(65537));
|
||||
|
||||
static ref P: Integer = Integer::from_str("2134638905903015345595412931439525422327695207079839833349799642610037970639957457078422305821013930668706217301787851463").unwrap();
|
||||
static ref Q: Integer = Integer::from_str("2134638905903015345595412931439525422327695207079839833349799642610037970639957457078422305821013930668706217301787851473").unwrap();
|
||||
static ref PHI: Integer = Integer::from_str("4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616565666555545842363207724851343853529903674824524238732001458342230627654145983386115997227846421784693186847971068054052064").unwrap();
|
||||
);
|
||||
/// the first prime factor of [`M[30]`](M)
|
||||
static P: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("2134638905903015345595412931439525422327695207079839833349799642610037970639957457078422305821013930668706217301787851463").unwrap());
|
||||
/// the second prime factor of [`M[30]`](M)
|
||||
static Q: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("2134638905903015345595412931439525422327695207079839833349799642610037970639957457078422305821013930668706217301787851473").unwrap());
|
||||
/// [`φ(M[30])`](M) where `φ` is Euler's totient function
|
||||
static PHI: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616565666555545842363207724851343853529903674824524238732001458342230627654145983386115997227846421784693186847971068054052064").unwrap());
|
||||
|
||||
const LIMIT: u128 = 100_000_000_000_000_000_000;
|
||||
const NAME: &str = " Jonah";
|
||||
@@ -68,8 +66,8 @@ const N: u128 = 1000000000000000000;
|
||||
const X: &str = "3595876711108608978";
|
||||
|
||||
fn main() {
|
||||
assert_eq!(M[30].clone(), P.clone() * Q.clone());
|
||||
assert_eq!(PHI.clone(), (P.clone() - 1) * (Q.clone() - 1));
|
||||
debug_assert_eq!(M[30].clone(), P.clone() * Q.clone());
|
||||
debug_assert_eq!(PHI.clone(), (P.clone() - 1) * (Q.clone() - 1));
|
||||
|
||||
let truth = Integer::from(42);
|
||||
let k = Integer::from(LIMIT - N) * 100;
|
||||
|
||||
+1
-1
@@ -9,8 +9,8 @@ fn main() -> ExitCode {
|
||||
eprintln!("Usage: main NAME");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
|
||||
let mut name = args[1].clone();
|
||||
|
||||
loop {
|
||||
println!("\nAttempting to solve for \"{}\"", &name);
|
||||
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
use std::sync::LazyLock;
|
||||
use std::str::FromStr;
|
||||
use lazy_static::lazy_static;
|
||||
use rug::Integer;
|
||||
use rug::integer::Order;
|
||||
use sha2::{Sha256, Digest};
|
||||
|
||||
lazy_static!(
|
||||
static ref M: Integer = Integer::from_str("14004392365098131090160062970945115111185775413941111064876648140973294115502980816410773368597517292734034227298996122159833675150497554142801209096513652073059992938078366061434391648276904643753267405058183481162693381822800709938988762923").unwrap();
|
||||
static ref E: Integer = Integer::from(65537);
|
||||
);
|
||||
static M: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("14004392365098131090160062970945115111185775413941111064876648140973294115502980816410773368597517292734034227298996122159833675150497554142801209096513652073059992938078366061434391648276904643753267405058183481162693381822800709938988762923").unwrap());
|
||||
static E: LazyLock<Integer> = LazyLock::new(|| Integer::from(65537));
|
||||
|
||||
pub fn solve(name: &str) -> (u64, Integer) {
|
||||
let mut x = sha256(name);
|
||||
|
||||
@@ -1,24 +1,44 @@
|
||||
use lazy_static::lazy_static;
|
||||
use rug::Integer;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{stdout, Write};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::sync::LazyLock;
|
||||
use tqdm::tqdm;
|
||||
|
||||
/// the base of the discrete logarithm
|
||||
const G: u64 = 42;
|
||||
/// the stride value of the Baby-Step-Giant-Step algorithm, controlling the space-time-tradeoff
|
||||
const S: u64 = 5341666;
|
||||
/// the modul of the discrete logarithm
|
||||
const P: u64 = 12345679943u64;
|
||||
|
||||
lazy_static!(
|
||||
static ref BABY_STEPS: Vec<u64> = std::fs::read("res/03_baby_steps.bin").unwrap()
|
||||
/// the precomputed baby steps
|
||||
static BABY_STEPS: LazyLock<Vec<u64>> = LazyLock::new(|| {
|
||||
let path = "res/03_baby_steps.bin";
|
||||
if !std::fs::exists(path).unwrap() {
|
||||
precompute_baby_steps(G, P, S, &mut File::create(path).unwrap());
|
||||
}
|
||||
|
||||
std::fs::read(path).unwrap()
|
||||
.chunks_exact(8)
|
||||
.map(|chunk| u64::from_be_bytes(chunk.try_into().unwrap()))
|
||||
.collect();
|
||||
.collect()
|
||||
});
|
||||
|
||||
static ref GIANT_STEPS: HashMap<u64, usize> = std::fs::read("res/03_giant_steps.bin").unwrap()
|
||||
/// the precomputed giant steps
|
||||
static GIANT_STEPS: LazyLock<HashMap<u64, usize>> = LazyLock::new(|| {
|
||||
let path = "res/03_giant_steps.bin";
|
||||
if !std::fs::exists(path).unwrap() {
|
||||
precompute_giant_steps(G, P, S, &mut File::create(path).unwrap());
|
||||
}
|
||||
|
||||
std::fs::read(path).unwrap()
|
||||
.chunks_exact(8)
|
||||
.map(|chunk| u64::from_be_bytes(chunk.try_into().unwrap()))
|
||||
.enumerate()
|
||||
.map(|(idx, step)| (step, idx))
|
||||
.collect();
|
||||
);
|
||||
.collect()
|
||||
});
|
||||
|
||||
pub fn solve(n: u64, x: Integer) -> (u64, Integer) {
|
||||
let x = x.modulo(&Integer::from(P - 1)).to_u64().unwrap();
|
||||
@@ -33,6 +53,8 @@ pub fn solve(n: u64, x: Integer) -> (u64, Integer) {
|
||||
(n, Integer::from(x))
|
||||
}
|
||||
|
||||
/// Quickly computes the discrete logarithm of `b` to the base [`G`] modulo [`M`] using the Baby-Step-Giant-Step
|
||||
/// algorithm with precomputed steps
|
||||
fn log42_mod_p(b: u64) -> Option<u64> {
|
||||
if b % P == 1 {
|
||||
return Some(0);
|
||||
@@ -51,36 +73,33 @@ fn log42_mod_p(b: u64) -> Option<u64> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn precompute() {
|
||||
let g = 42u64;
|
||||
let m = P;
|
||||
let s = 5341666;
|
||||
precompute_giant_steps(g, m, s);
|
||||
precompute_baby_steps(g, m, s);
|
||||
}
|
||||
|
||||
fn precompute_giant_steps(g: u64, m: u64, s: u64) {
|
||||
/// Precomputes the giant steps for use in the Baby-Step-Giant-Step algorithm for a fixed base `g` and modul `m` using a
|
||||
/// stride of `s`.
|
||||
fn precompute_giant_steps(g: u64, m: u64, s: u64, out: &mut impl Write) {
|
||||
let g = g as u128;
|
||||
let m = m as u128;
|
||||
let mut a = 1u128;
|
||||
for _ in tqdm(0..s) {
|
||||
stdout().write_all(&(a as u64).to_be_bytes()).unwrap();
|
||||
out.write_all(&(a as u64).to_be_bytes()).unwrap();
|
||||
a *= g;
|
||||
a %= m;
|
||||
}
|
||||
}
|
||||
|
||||
fn precompute_baby_steps(g: u64, m: u64, s: u64) {
|
||||
/// Precomputes the baby steps for use in the Baby-Step-Giant-Step algorithm for a fixed base `g` and modul `m` using a
|
||||
/// stride of `s`.
|
||||
fn precompute_baby_steps(g: u64, m: u64, s: u64, out: &mut impl Write) {
|
||||
let gs = pow(g, s, m) as u128;
|
||||
let m= m as u128;
|
||||
let mut a = gs;
|
||||
for _ in tqdm(1..=(m as u64 / s + 1)) {
|
||||
stdout().write_all(&(a as u64).to_be_bytes()).unwrap();
|
||||
out.write_all(&(a as u64).to_be_bytes()).unwrap();
|
||||
a *= gs;
|
||||
a %= m;
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the discrete logarithm of `b` to the base `g` modulo `m` using the Baby-Step-Giant-Step algorithm.
|
||||
fn log(g: u64, b: u64, m: u64) -> Option<u64> {
|
||||
let g: u128 = g as u128;
|
||||
let b: u128 = b as u128;
|
||||
@@ -114,6 +133,7 @@ fn log(g: u64, b: u64, m: u64) -> Option<u64> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Computes the `k`-th power of `a` modulo `m`.
|
||||
fn pow(a: u64, mut k: u64, m: u64) -> u64 {
|
||||
let mut a: u128 = a as u128;
|
||||
let m: u128 = m as u128;
|
||||
|
||||
@@ -21,12 +21,12 @@ pub fn solve(n: u64, x: Integer) -> (u64, Integer) {
|
||||
(LIMIT, x * b % &M)
|
||||
}
|
||||
|
||||
|
||||
const A: u128 = 909_090_909_091u128; // (10 * b + 1) / 11
|
||||
const B: u128 = 1_000_000_000_000u128; // 10^12
|
||||
const C: u128 = 999_999_999_989u128; // b - 11
|
||||
const D: u128 = 999_999_999_999u128; // b - 1
|
||||
fn f(mut x: u128) -> u128 {
|
||||
const A: u128 = 909_090_909_091u128; // (10 * b + 1) / 11
|
||||
const B: u128 = 1_000_000_000_000u128; // 10^12
|
||||
const C: u128 = 999_999_999_989u128; // b - 11
|
||||
const D: u128 = 999_999_999_999u128; // b - 1
|
||||
|
||||
x = x * A % B;
|
||||
x = (x * C + D) / B;
|
||||
x
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
use lazy_static::lazy_static;
|
||||
use rug::Integer;
|
||||
use std::str::FromStr;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
const START: u64 = 1_000_000_000_000u64;
|
||||
const LIMIT: u64 = 10_000_000_000_000_000u64;
|
||||
|
||||
lazy_static!(
|
||||
static ref M: Integer = Integer::from_str("14004392365098131090160062970945115111185775413941111064876648140973294115502980816410773368597517292734034227298996122159833675150497554142801209096513652073059992938078366061434391648276904643753267405058183481162693381822800709938988762923").unwrap();
|
||||
static ref A: Integer = Integer::from_str("10729297455904899337681752672816753703351288544833760635567859176397566160330812285369370751389224534974913042757043771146367160829669925123791471756026119030734890865062863499420767799283504416995775836275660636668989077836006690621539265605").unwrap();
|
||||
static ref B: Integer = Integer::from_str("4997609466256208183077203585670710326160976219126614124062259398288517298185253986607627790890962248558793881836308854443463332741665631247256917127102588793451033551034614994254514161235462726178612824897626185944163885786520174491419225456").unwrap();
|
||||
);
|
||||
static M: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("14004392365098131090160062970945115111185775413941111064876648140973294115502980816410773368597517292734034227298996122159833675150497554142801209096513652073059992938078366061434391648276904643753267405058183481162693381822800709938988762923").unwrap());
|
||||
static A: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("10729297455904899337681752672816753703351288544833760635567859176397566160330812285369370751389224534974913042757043771146367160829669925123791471756026119030734890865062863499420767799283504416995775836275660636668989077836006690621539265605").unwrap());
|
||||
static B: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("4997609466256208183077203585670710326160976219126614124062259398288517298185253986607627790890962248558793881836308854443463332741665631247256917127102588793451033551034614994254514161235462726178612824897626185944163885786520174491419225456").unwrap());
|
||||
|
||||
pub fn solve(n: u64, x: Integer) -> (u64, Integer) {
|
||||
assert_eq!(n, START);
|
||||
let m: &Integer = &M;
|
||||
let a: &Integer = &A;
|
||||
let b: &Integer = &B;
|
||||
|
||||
let mut x = x;
|
||||
x = (x + a) % m;
|
||||
x = x * 2 % m;
|
||||
x = (x + b) % m;
|
||||
x = (x + &*A) % &*M;
|
||||
x = x * 2 % &*M;
|
||||
x = (x + &*B) % &*M;
|
||||
|
||||
(LIMIT, x)
|
||||
}
|
||||
@@ -1,32 +1,33 @@
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::str::FromStr;
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::LazyLock;
|
||||
use rug::Integer;
|
||||
|
||||
lazy_static!(
|
||||
static ref DATA: Vec<u64> = {
|
||||
let file = File::open("./res/07_weird_assembly_machine.data");
|
||||
let lines = BufReader::new(file.unwrap()).lines();
|
||||
static DATA: LazyLock<Vec<u64>> = LazyLock::new(|| {
|
||||
let file = File::open("./res/07_weird_assembly_machine.data");
|
||||
let lines = BufReader::new(file.unwrap()).lines();
|
||||
|
||||
let mut data = Vec::new();
|
||||
for line in lines {
|
||||
data.push(u64::from_str(&line.unwrap()).unwrap());
|
||||
}
|
||||
data
|
||||
};
|
||||
);
|
||||
let mut data = Vec::new();
|
||||
for line in lines {
|
||||
data.push(u64::from_str(&line.unwrap()).unwrap());
|
||||
}
|
||||
data
|
||||
});
|
||||
|
||||
const LIMIT: u64 = 1_000_000_000_000_000_000u64;
|
||||
|
||||
pub fn solve(n: u64, x: Integer) -> (u64, Integer) {
|
||||
let x = x.to_u64_wrapping();
|
||||
let (n, x) = f_n(n, x, LIMIT - n);
|
||||
let (n, x) = h_n(n, x, LIMIT - n);
|
||||
(n, Integer::from(x))
|
||||
}
|
||||
|
||||
fn h(x: u64, n: u64) -> u64 {
|
||||
g(sub(f(x), n))
|
||||
}
|
||||
|
||||
pub fn f_n(mut n: u64, mut x: u64, k: u64) -> (u64, u64) {
|
||||
pub fn h_n(mut n: u64, mut x: u64, k: u64) -> (u64, u64) {
|
||||
n += 1;
|
||||
x = sub(f(x), n);
|
||||
|
||||
@@ -45,6 +46,28 @@ pub fn f_n(mut n: u64, mut x: u64, k: u64) -> (u64, u64) {
|
||||
(n, x)
|
||||
}
|
||||
|
||||
fn f(x: u64) -> u64 {
|
||||
evaluate(x, &DATA[..128])
|
||||
}
|
||||
|
||||
fn g(x: u64) -> u64 {
|
||||
evaluate(x, &DATA[128..])
|
||||
}
|
||||
|
||||
/// Quickly computes [`f`](f)([`g`](g)(x)).
|
||||
fn fg(x: u64) -> u64 {
|
||||
!x.rotate_left(17)
|
||||
}
|
||||
|
||||
/// Computes [`fg`](fg)(...[`fg`](fg)(x)...) where [`fg`] is applied `n` times.
|
||||
fn fg_n(mut x: u64, n: u64) -> u64 {
|
||||
for _ in 0..n%64 {
|
||||
x = fg(x);
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
/// Computes the sum from `i = 1` to `k` over `fg^(k-i)(n + i)`.
|
||||
fn fg_sum(n: u64, k: u64) -> u64 {
|
||||
let mut sum = 0u64;
|
||||
for i in 1..=k {
|
||||
@@ -53,6 +76,7 @@ fn fg_sum(n: u64, k: u64) -> u64 {
|
||||
sum
|
||||
}
|
||||
|
||||
/// Quickly computes `fg_sum` when `k` is divisible by `192`.
|
||||
fn fast_fg_sum(n: u64, k: u64) -> u64 {
|
||||
debug_assert!(k % 192 == 0);
|
||||
let mut result = fg_sum(n, 0);
|
||||
@@ -60,12 +84,13 @@ fn fast_fg_sum(n: u64, k: u64) -> u64 {
|
||||
result
|
||||
}
|
||||
|
||||
fn f(x: u64) -> u64 {
|
||||
evaluate(x, &DATA[..128], &DATA[..128])
|
||||
}
|
||||
|
||||
fn g(x: u64) -> u64 {
|
||||
evaluate(x, &DATA[128..], &DATA[128..])
|
||||
/// Evaluates a polynomial over `GF(2)[X] / (X^64 + 1)`
|
||||
fn evaluate(x: u64, data: &[u64]) -> u64 {
|
||||
let mut out = 0;
|
||||
for i in 0..data.len() {
|
||||
out = xor_mul(out, x) ^ data[i];
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn add(a: u64, b: u64) -> u64 {
|
||||
@@ -80,27 +105,6 @@ fn sub(x: u64, mut n: u64) -> u64 {
|
||||
x.wrapping_sub(n)
|
||||
}
|
||||
|
||||
fn fg(x: u64) -> u64 {
|
||||
!x.rotate_left(17)
|
||||
}
|
||||
|
||||
fn fg_n(mut x: u64, n: u64) -> u64 {
|
||||
for _ in 0..n%64 {
|
||||
x = fg(x);
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
fn evaluate(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 = xor_mul(out, x) ^ data[i];
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(not(all(target_arch = "x86_64", target_feature = "pclmulqdq")))]
|
||||
fn xor_mul(a: u64, mut b: u64) -> u64 {
|
||||
let mut x: u64 = 0;
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
use rug::ops::DivRounding;
|
||||
use rug::Integer;
|
||||
use std::str::FromStr;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
const PHI: &str = "4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616565666555545842363207724851343853529903674824524238732001458342230627654145983386115997227846421784693186847971068054052064";
|
||||
const M: &str = "4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616569935833357648393898915677206732580748330214938398411668157941515847730087263301030154072458063812554524260405671629754999";
|
||||
const E: u64 = 65537;
|
||||
static PHI: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616565666555545842363207724851343853529903674824524238732001458342230627654145983386115997227846421784693186847971068054052064").unwrap());
|
||||
static M: LazyLock<Integer> = LazyLock::new(|| Integer::from_str("4556683258594822402855414380362620295231673046713752880933967889771646856051745784462468117575177892006513392693685616569935833357648393898915677206732580748330214938398411668157941515847730087263301030154072458063812554524260405671629754999").unwrap());
|
||||
static E: LazyLock<Integer> = LazyLock::new(|| Integer::from(65537));
|
||||
static TRUTH: LazyLock<Integer> = LazyLock::new(|| Integer::from(42));
|
||||
const LIMIT: u128 = 100_000_000_000_000_000_000;
|
||||
|
||||
pub fn solve(n: u64, x: Integer) -> Result<(u128, Integer), u32> {
|
||||
let truth = Integer::from(42);
|
||||
let phi = Integer::from_str(PHI).unwrap();
|
||||
let m = Integer::from_str(M).unwrap();
|
||||
|
||||
let k = Integer::from(LIMIT - n as u128) * 100;
|
||||
let e = Integer::from(E).pow_mod(&k, &phi).unwrap();
|
||||
let w: u32 = x.clone().modulo(&truth).try_into().unwrap();
|
||||
let e = E.clone().pow_mod(&k, &PHI).unwrap();
|
||||
let w: u32 = x.clone().modulo(&TRUTH).try_into().unwrap();
|
||||
if w != 30 {
|
||||
return Err(w);
|
||||
}
|
||||
|
||||
let mut y = x.clone().div_floor(&truth);
|
||||
y = y.pow_mod(&e, &m).unwrap();
|
||||
let mut y = x.clone().div_floor(&*TRUTH);
|
||||
y = y.pow_mod(&e, &M).unwrap();
|
||||
|
||||
let x: Integer = y * 42 + w;
|
||||
Ok((LIMIT, x))
|
||||
|
||||
Reference in New Issue
Block a user