-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
156 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
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,33 @@ | ||
use common::{encode_hex_u32, encode_hex_u8, PasswordRequest, Proof}; | ||
use methods::{PASSWORD_ELF, PASSWORD_ID}; | ||
use rand::{thread_rng, RngCore}; | ||
use risc0_zkp::core::sha::Digest; | ||
use risc0_zkvm::host::Prover; | ||
use risc0_zkvm::serde::{from_slice, to_vec}; | ||
|
||
fn main() { | ||
let mut rng = thread_rng(); | ||
let mut salt = [0u8; 32]; | ||
rng.fill_bytes(&mut salt); | ||
|
||
let request = PasswordRequest { | ||
password: "S00perSecr1t!!!".into(), | ||
salt, | ||
}; | ||
|
||
let mut prover = Prover::new(PASSWORD_ELF, PASSWORD_ID).unwrap(); | ||
|
||
// Adding input to the prover makes it readable by the guest | ||
let vec = to_vec(&request).unwrap(); | ||
prover.add_input(&vec).unwrap(); | ||
|
||
let receipt = prover.run().unwrap(); | ||
let password_hash: Digest = from_slice(&receipt.get_journal_vec().unwrap()).unwrap(); | ||
println!("Password hash is: {}", &password_hash); | ||
|
||
let proof = Proof { | ||
journal: encode_hex_u8(receipt.get_journal().unwrap()), | ||
seal: encode_hex_u32(receipt.get_seal().unwrap()), | ||
}; | ||
confy::store_path("./proof.toml", proof).unwrap(); | ||
} |
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,14 @@ | ||
use common::{decode_hex_u32, decode_hex_u8, Proof}; | ||
use methods::PASSWORD_ID; | ||
use risc0_zkvm::host::Receipt; | ||
|
||
fn main() { | ||
let proof: Proof = confy::load_path("./proof.toml").unwrap(); | ||
let journal = decode_hex_u8(&proof.journal).unwrap(); | ||
let seal = decode_hex_u32(&proof.seal).unwrap(); | ||
let receipt = Receipt::new(&journal, &seal).unwrap(); | ||
match receipt.verify(PASSWORD_ID) { | ||
Ok(_) => println!("Verified OK!"), | ||
Err(_) => println!("Verify Failed!"), | ||
}; | ||
} |
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
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
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,91 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
use common::PasswordRequest; | ||
use risc0_zkvm_guest::{env, sha}; | ||
|
||
risc0_zkvm_guest::entry!(main); | ||
|
||
struct PasswordPolicy { | ||
pub min_length: usize, | ||
pub max_length: usize, | ||
pub min_uppercase: usize, | ||
pub min_lowercase: usize, | ||
pub min_numeric: usize, | ||
pub min_special_chars: usize, | ||
} | ||
|
||
impl PasswordPolicy { | ||
pub fn is_valid(&self, pw: &str) -> bool { | ||
let metrics = PasswordMetrics::new(pw); | ||
self.correct_length(pw) | ||
&& (metrics.numeric >= self.min_numeric) | ||
&& (metrics.uppercase >= self.min_uppercase) | ||
&& (metrics.lowercase >= self.min_lowercase) | ||
&& (metrics.special >= self.min_special_chars) | ||
} | ||
|
||
fn correct_length(&self, password: &str) -> bool { | ||
password.len() > (self.min_length - 1) && password.len() < (self.max_length + 1) | ||
} | ||
} | ||
|
||
struct PasswordMetrics { | ||
pub numeric: usize, | ||
pub special: usize, | ||
pub uppercase: usize, | ||
pub lowercase: usize, | ||
} | ||
|
||
impl PasswordMetrics { | ||
pub fn new(password: &str) -> Self { | ||
let mut numeric = 0; | ||
let mut special = 0; | ||
let mut uppercase = 0; | ||
let mut lowercase = 0; | ||
for ch in password.chars() { | ||
if ch.is_ascii_digit() { | ||
numeric += 1; | ||
} | ||
if ch.is_ascii_punctuation() { | ||
special += 1; | ||
} | ||
if ch.is_ascii_uppercase() { | ||
uppercase += 1; | ||
} | ||
if ch.is_ascii_lowercase() { | ||
lowercase += 1; | ||
} | ||
} | ||
PasswordMetrics { | ||
numeric, | ||
special, | ||
uppercase, | ||
lowercase, | ||
} | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let request: PasswordRequest = env::read(); | ||
|
||
let policy = PasswordPolicy { | ||
min_length: 3, | ||
max_length: 64, | ||
min_numeric: 2, | ||
min_uppercase: 2, | ||
min_lowercase: 2, | ||
min_special_chars: 1, | ||
}; | ||
|
||
if !policy.is_valid(&request.password) { | ||
panic!("Password invalid. Please try again."); | ||
} | ||
|
||
let mut salted_password = request.password.as_bytes().to_vec(); | ||
salted_password.extend(request.salt); | ||
let password_hash = sha::digest_u8_slice(&salted_password[..]); | ||
|
||
env::commit(&password_hash); | ||
env::commit(&request.salt); | ||
} |