Skip to content

Commit

Permalink
fix latex, add bench
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdaybird committed Dec 30, 2024
1 parent f6235fa commit 2078b6f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/dsa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Here is where **Digital Signatures** come into the picture.
Digital signature schemes consists of three algorithms **Gen**, **Sign**, **Verify**, such that:

1. The key generation algorithm, $Gen$ which takes in the security parameter $1^n$ and outputs public key, $pk$ and private key, $sk$.
2. The signing algorithm $Sign$ takes as input the keys and a message and outputs a signature.
3. The verification algorithm $Verify$, takes as input the public key, a message, and a signature.
2. The signing algorithm **Sign** takes as input the keys and a message and outputs a signature.
3. The verification algorithm **Verify**, takes as input the public key, a message, and a signature.
It outputs bit 1 if the signature is valid for the given message and public key, otherwise 0.

**How is a digital signature scheme used?**
Expand All @@ -30,8 +30,8 @@ You can read more on this here: [Public key infrastructure](https://en.wikipedia

![](./keygen.gif)

Now when Alex sends a message(document, contract, etc.), $m$, for Bobby to sign, they compute the signature, $s$ as, $s\leftarrow Sign(sk,m)$ and sents $s$ to Alex or any other party who wants to take a look.
Now, any party who wants to see if Bobby signed the document or not, applies the verification algorithm using the public key as $Verify(pk,m,s)$. Thus Alex or any other party can be sure of the authenicity of
Now when Alex sends a message(document, contract, etc.), $m$, for Bobby to sign, they compute the signature, $s$ as, $s\leftarrow$**Sign(sk,m)** and sents $s$ to Alex or any other party who wants to take a look.
Now, any party who wants to see if Bobby signed the document or not, applies the verification algorithm using the public key as **Verify(pk,m,s)**. Thus Alex or any other party can be sure of the authenicity of
the signature as well as the integrity of the message.

![](./sign_and_verify.gif)
Expand Down
86 changes: 86 additions & 0 deletions src/dsa/eddsa/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
extern crate test;

use std::num::ParseIntError;

use hex_literal::hex;
use pretty_assertions::assert_eq;
use rand::Rng;
use rstest::*;
use test::Bencher;

use super::*;

Expand Down Expand Up @@ -87,3 +91,85 @@ fn test_large() {
assert!(Ed25519::verify(public_key, &msg, signature));
}
}

#[bench]
fn bench_keygen(b: &mut Bencher) {
let mut rng = rand::thread_rng();
let sk_v: Vec<_> = (0..32).map(|_| rng.gen_range(0..=255)).collect();
let mut sk_b = [0u8; 32];
sk_b.copy_from_slice(&sk_v);

b.iter(|| {
let sk = test::black_box(sk_b);
Ed25519::keygen(sk)
});
}

macro_rules! bench_sign {
($($test_name:ident, $n:literal)+) => {
$(#[bench]
fn $test_name(b: &mut Bencher) {
let mut rng = rand::thread_rng();
let sk_v: Vec<_> = (0..32).map(|_| rng.gen_range(0..=255)).collect();
let mut sk_b = [0u8; 32];
sk_b.copy_from_slice(&sk_v);
let (_, pk_b) = Ed25519::keygen(sk_b);

let msg_v: Vec<_> = (0..$n).map(|_| rng.gen_range(0..=255)).collect();
let mut msg_b = [0u8; $n];
msg_b.copy_from_slice(&msg_v);

b.iter(|| {
let sk = test::black_box(sk_b);
let pk = test::black_box(pk_b);
let msg = test::black_box(msg_b);

Ed25519::sign(sk, pk, &msg)
});
}
)+
};
}

bench_sign![
bench_sign_100, 100
bench_sign_1000, 1000
bench_sign_10000, 10000
bench_sign_100000, 100000
];

macro_rules! bench_verify {
($($name:ident, $n:literal)+) => {
$(
#[bench]
fn $name(b: &mut Bencher) {
let mut rng = rand::thread_rng();
let sk_v: Vec<_> = (0..32).map(|_| rng.gen_range(0..=255)).collect();
let mut sk_b = [0u8; 32];
sk_b.copy_from_slice(&sk_v);
let (_, pk_b) = Ed25519::keygen(sk_b);

let msg_v: Vec<_> = (0..$n).map(|_| rng.gen_range(0..=255)).collect();
let mut msg_b = [0u8; $n];
msg_b.copy_from_slice(&msg_v);

let sig_b = Ed25519::sign(sk_b, pk_b, &msg_b);

b.iter(|| {
let pk = test::black_box(pk_b);
let msg = test::black_box(msg_b);
let sign = test::black_box(sig_b);

Ed25519::verify(pk, &msg, sign)
});
}
)+
};
}

bench_verify![
bench_verify_100, 100
bench_verify_1000, 1000
bench_verify_10000, 10000
bench_verify_100000, 100000
];
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![feature(const_option)]
#![feature(generic_const_exprs)]
#![feature(specialization)]
#![feature(test)]
#![warn(missing_docs)]

pub mod algebra;
Expand Down

0 comments on commit 2078b6f

Please sign in to comment.