Skip to content

Commit

Permalink
feat: factordb attack
Browse files Browse the repository at this point in the history
  • Loading branch information
skyf0l committed Nov 9, 2024
1 parent dcc3fb6 commit c14c090
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 4 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:

env:
RUST_BACKTRACE: 1
NO_FACTORDB: 1

jobs:
fmt:
Expand Down Expand Up @@ -192,8 +193,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 30

needs:
[fmt, clippy, docs, tests, ctf-challenges, coverage, build, docker-build]
needs: [fmt, clippy, docs, tests, ctf-challenges, coverage, build, docker-build]
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')

steps:
Expand All @@ -211,8 +211,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 120

needs:
[fmt, clippy, docs, tests, ctf-challenges, coverage, build, docker-build]
needs: [fmt, clippy, docs, tests, ctf-challenges, coverage, build, docker-build]
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')

env:
Expand Down
177 changes: 177 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ indicatif = "0.17"
itertools = "0.13"
discrete-logarithm = "1.0"
base64 = "0.22"
factordb = { version = "0.3.0", features = ["blocking"] }

[dependencies.rug]
version = "1.26"
Expand Down
49 changes: 49 additions & 0 deletions src/attack/factorization/factordb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::str::FromStr;

use factordb::FactorDbBlockingClient;
use indicatif::ProgressBar;
use rug::Integer;

use crate::{key::PrivateKey, Attack, AttackSpeed, Error, Parameters, Solution};

/// Factordb attack
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FactorDbAttack;

impl Attack for FactorDbAttack {
fn name(&self) -> &'static str {
"factordb"
}

fn speed(&self) -> AttackSpeed {
AttackSpeed::Fast
}

fn run(&self, params: &Parameters, _pb: Option<&ProgressBar>) -> Result<Solution, Error> {
// If `NO_FACTORDB` is set, ignore the factordb attack
// This is useful for testing
if std::env::var("NO_FACTORDB").is_ok() {
return Err(Error::NotFound);
}

let e = &params.e;
let n = params.n.as_ref().ok_or(Error::MissingParameters)?;

let factors = tokio::task::block_in_place(|| FactorDbBlockingClient::new().get(n))
.map_err(|_| Error::NotFound)?
.into_factors_flattened()
.iter()
.map(|f| Integer::from_str(&f.to_string()))
.collect::<Result<Vec<Integer>, _>>()
.map_err(|_| Error::NotFound)?;

if factors.len() < 2 {
return Err(Error::NotFound);
}

Ok(Solution::new_pk(
self.name(),
PrivateKey::from_factors(factors, e.clone())?,
))
}
}
3 changes: 3 additions & 0 deletions src/attack/factorization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use lazy_static::lazy_static;
mod brent;
mod cunningham_chain;
mod ecm;
mod factordb;
mod fermat;
mod hart;
mod known_factors;
Expand All @@ -23,6 +24,7 @@ mod twin_prime;
pub use self::ecm::EcmAttack;
pub use brent::BrentAttack;
pub use cunningham_chain::CunninghamChainAttack;
pub use factordb::FactorDbAttack;
pub use fermat::FermatAttack;
pub use hart::HartAttack;
pub use known_factors::KnownFactorsAttack;
Expand All @@ -45,6 +47,7 @@ lazy_static! {
Arc::new(BrentAttack),
Arc::new(CunninghamChainAttack),
Arc::new(EcmAttack),
Arc::new(FactorDbAttack),
Arc::new(FermatAttack),
Arc::new(HartAttack),
Arc::new(KnownFactorsAttack),
Expand Down

0 comments on commit c14c090

Please sign in to comment.