Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cdb4e77
Showing
8 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
/target | ||
**/*.rs.bk |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "defcon26-pow" | ||
version = "0.1.0" | ||
description = "Same proof, less work" | ||
authors = ["kpcyrd <git@rxv.cc>"] | ||
license = "MIT" | ||
repository = "https://github.com/kpcyrd/defcon26-pow" | ||
readme = "README.md" | ||
|
||
[features] | ||
nightly = [] | ||
|
||
[dependencies] | ||
num_cpus = "1.8.0" | ||
sha2 = "0.7.1" | ||
byteorder = "1" | ||
num-bigint = "0.1" | ||
num-traits = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 kpcyrd | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# defcon26-pow | ||
|
||
Same proof, less work. | ||
|
||
## Tests | ||
|
||
``` | ||
$ cargo test | ||
``` | ||
|
||
## Benchmarks | ||
|
||
``` | ||
$ cargo +nightly bench --features=nightly | ||
running 5 tests | ||
test tests::test_check_pow_invalid ... ignored | ||
test tests::test_check_pow_valid ... ignored | ||
test tests::test_pow_hash ... ignored | ||
test tests::bench_pow_hash ... bench: 411 ns/iter (+/- 92) | ||
test tests::bench_pow_valid ... bench: 1,459 ns/iter (+/- 223) | ||
test result: ok. 0 passed; 0 failed; 3 ignored; 2 measured; 0 filtered out | ||
$ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This folder contains a copy of the original implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python | ||
from __future__ import print_function | ||
import sys | ||
import struct | ||
import hashlib | ||
|
||
# inspired by C3CTF's POW | ||
|
||
def pow_hash(challenge, solution): | ||
return hashlib.sha256(challenge.encode('ascii') + struct.pack('<Q', solution)).hexdigest() | ||
|
||
def check_pow(challenge, n, solution): | ||
h = pow_hash(challenge, solution) | ||
return (int(h, 16) % (2**n)) == 0 | ||
|
||
def solve_pow(challenge, n): | ||
candidate = 0 | ||
while True: | ||
if check_pow(challenge, n, candidate): | ||
return candidate | ||
candidate += 1 | ||
|
||
if __name__ == '__main__': | ||
challenge = sys.argv[1] | ||
n = int(sys.argv[2]) | ||
|
||
print('Solving challenge: "{}", n: {}'.format(challenge, n)) | ||
|
||
solution = solve_pow(challenge, n) | ||
print('Solution: {} -> {}'.format(solution, pow_hash(challenge, solution))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#![cfg_attr(feature = "nightly", feature(test))] | ||
#[cfg(feature = "nightly")] | ||
extern crate test; | ||
|
||
extern crate num_cpus; | ||
extern crate sha2; | ||
extern crate byteorder; | ||
extern crate num_bigint; | ||
extern crate num_traits; | ||
|
||
use std::env; | ||
use std::io::prelude::*; | ||
use byteorder::{LittleEndian, WriteBytesExt}; | ||
use sha2::{Sha256, Digest}; | ||
use num_bigint::BigUint; | ||
use num_traits::Zero; | ||
|
||
|
||
#[inline] | ||
fn pow_hash(challenge: &str, solution: u64) -> Vec<u8> { | ||
let mut wtr = vec![]; | ||
wtr.write(challenge.as_bytes()).unwrap(); | ||
wtr.write_u64::<LittleEndian>(solution).unwrap(); | ||
Vec::from(Sha256::digest(&wtr).as_slice()) | ||
} | ||
|
||
#[inline] | ||
fn check_pow(challenge: &str, n: u8, solution: u64) -> bool { | ||
let h = pow_hash(challenge, solution); | ||
let num = BigUint::from_bytes_be(&h); | ||
|
||
let op = 2u64.pow(n as u32); | ||
|
||
(num % op).is_zero() | ||
} | ||
|
||
fn solve_pow(challenge: &str, n: u8) -> u64 { | ||
let mut candidate = 0; | ||
|
||
loop { | ||
if check_pow(challenge, n, candidate) { | ||
break; | ||
} | ||
candidate += 1; | ||
} | ||
|
||
candidate | ||
} | ||
|
||
fn main() { | ||
let mut args = env::args().skip(1); | ||
let challenge = args.next().expect("challenge missing"); | ||
let n = args.next().expect("n missing"); | ||
let n = n.parse::<u8>().expect("n is not u8"); | ||
println!("Solving challenge: {:?}, n: {:?}", challenge, n); | ||
|
||
let solution = solve_pow(&challenge, n); | ||
|
||
println!("Solution: {:?} -> {:?}", solution, pow_hash(&challenge, solution)); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[cfg(feature = "nightly")] | ||
use test::Bencher; | ||
|
||
#[test] | ||
fn test_check_pow_valid() { | ||
let valid = check_pow("e2ZgIzlOpe", 26, 52644528); | ||
assert!(valid); | ||
} | ||
|
||
#[test] | ||
fn test_check_pow_invalid() { | ||
let valid = check_pow("e2ZgIzlOpe", 26, 1); | ||
assert!(!valid); | ||
} | ||
|
||
#[test] | ||
fn test_pow_hash() { | ||
let hash = pow_hash("e2ZgIzlOpe", 52644528); | ||
|
||
let hash = hash.into_iter() | ||
.map(|b| format!("{:02x}", b)) | ||
.collect::<String>(); | ||
|
||
assert_eq!("a51496f8ce009bab48108eaaa085b749b39c8707ae622e8d446a5c9228000000", hash); | ||
} | ||
|
||
#[bench] | ||
#[cfg(feature = "nightly")] | ||
fn bench_pow_hash(b: &mut Bencher) { | ||
b.iter(|| pow_hash("e2ZgIzlOpe", 52644528)); | ||
} | ||
|
||
#[bench] | ||
#[cfg(feature = "nightly")] | ||
fn bench_pow_valid(b: &mut Bencher) { | ||
b.iter(|| check_pow("e2ZgIzlOpe", 26, 52644528)); | ||
} | ||
} |