Skip to content

Instantly share code, notes, and snippets.

@faytey
Created May 16, 2024 19:05
Show Gist options
  • Save faytey/fc874e543547e3efca9fdbe9f87909b4 to your computer and use it in GitHub Desktop.
Save faytey/fc874e543547e3efca9fdbe9f87909b4 to your computer and use it in GitHub Desktop.
commitment scheme
use std::{error::Error, hash::{DefaultHasher, Hash, Hasher}};
trait Auction {
fn commit(a: u8, b: u8) -> Result<Vec<u8>, Box<dyn Error>>;
fn open(input: u8, commitment: Vec<u8>) -> Result<bool, Box<dyn Error>>;
}
struct Auctioning {}
impl Auctioning {
fn to_hash(value: u8) -> Vec<u8> {
let mut hashing = DefaultHasher::new();
value.hash(&mut hashing);
let hashed = hashing.finish();
hashed.to_ne_bytes().to_vec()
}
}
impl Auction for Auctioning {
fn commit(a: u8, b: u8) -> Result<Vec<u8>, Box<dyn Error>> {
let value = a^b;
let commitment = Auctioning::to_hash(value);
println!("Commitment Successful");
Ok(commitment)
}
fn open(input: u8, commitment: Vec<u8>) -> Result<bool, Box<dyn Error>> {
let check_commit = Auctioning::to_hash(input);
if check_commit == commitment {
println!("Commit Verification Valid");
Ok(true)
} else {
println!("Commit Verification Invalid");
Ok(false)
}
}
}
fn main() {
let v1 = 3u8;
let v2 = 5u8;
let v3 = v1 ^ v2;
let commit = Auctioning::commit(v1, v2);
let _ = Auctioning::open(v3, commit.expect("msg"));
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment