Skip to content

Commit

Permalink
curve params
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Capybara committed May 27, 2023
1 parent 826b755 commit 75cb565
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use capycrypt::model::shake_functions::{
use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes;
use criterion::{criterion_group, criterion_main, Criterion};
use std::borrow::BorrowMut;
const SELECTED_CURVE: Curves = Curves::E448;
const SELECTED_CURVE: Curves = Curves::E222;

/// Symmetric encrypt and decrypt roundtrip
fn sym_enc(pw: &mut Vec<u8>, mut message: Box<Vec<u8>>) {
Expand Down
4 changes: 2 additions & 2 deletions src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod curve_constants {
pub const R_521: &str = "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD15B6C64746FC85F736B8AF5E7EC53F04FBD8C4569A8F1F4540EA2435F5180D6B";

pub const D_448: i32 = -39081;
pub const N_448: &str = "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDF3288FA7113B6D26BB58DA4085B309CA37163D548DE30A4AAD6113CC";
pub const N_448: &str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDF3288FA7113B6D26BB58DA4085B309CA37163D548DE30A4AAD6113CC";
pub const P_448: &str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
pub const R_448: &str = "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7CCA23E9C44EDB49AED63690216CC2728DC58F552378C292AB5844F3";

Expand Down Expand Up @@ -323,7 +323,7 @@ pub fn curve_n(curve: Curves) -> Integer {

/// Initializes curve modulus 𝑝, a prime defining the finite field 𝔽𝑝.
/// <https://eprint.iacr.org/2013/647.pdf>
fn curve_p(curve: Curves) -> Integer {
pub fn curve_p(curve: Curves) -> Integer {
match curve {
Curves::E222 => Integer::from_str_radix(curve_constants::P_222, 16).unwrap(),
Curves::E382 => Integer::from_str_radix(curve_constants::P_382, 16).unwrap(),
Expand Down
10 changes: 4 additions & 6 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod shake_functions {
use rug::Integer;
use std::borrow::{Borrow, BorrowMut};

const SELECTED_CURVE: Curves = Curves::E521;
const SELECTED_CURVE: Curves = Curves::E448;

/// # SHA3-Keccak
/// ref NIST FIPS 202.
Expand Down Expand Up @@ -209,7 +209,7 @@ pub mod shake_functions {
let k: Integer = (bytes_to_big(get_random_bytes(64)) * 4) % curve_n(SELECTED_CURVE);
let w = pub_key * k.clone();
let z = CurvePoint::generator(SELECTED_CURVE, false) * k;
let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &mut vec![], 1024, "P", d);
let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &mut vec![], 1024, "PK", d);
let ke = &mut ke_ka[..64].to_vec();
let ka = &mut ke_ka[64..].to_vec();

Expand Down Expand Up @@ -247,13 +247,12 @@ pub mod shake_functions {
% z.clone().n;

let w = z * s;
let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &mut vec![], 1024, "P", d);
let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &mut vec![], 1024, "PK", d);
let ke = &mut ke_ka[..64].to_vec();
let ka = &mut ke_ka[64..].to_vec();
let len = message.c.len() * 8;
let m = Box::new(kmac_xof(ke, &mut vec![], (len) as u64, "PKE", d));
xor_bytes(&mut message.c, m.borrow());
dbg!(message.c.clone());
let t_p = kmac_xof(&mut ka.clone(), &mut message.c, 512, "PKA", d);
t_p == message.t
}
Expand Down Expand Up @@ -306,8 +305,7 @@ pub mod shake_functions {
let mut u = CurvePoint::generator(SELECTED_CURVE, false) * sig.z.clone();
let hv = pub_key * (bytes_to_big(sig.h.clone()));
u = u + hv;
let mut ux_bytes = big_to_bytes(u.x);
let h_p = kmac_xof(&mut ux_bytes, message.borrow_mut(), 512, "T", d);
let h_p = kmac_xof(&mut big_to_bytes(u.x), message.borrow_mut(), 512, "T", d);
h_p == sig.h
}
}
7 changes: 2 additions & 5 deletions tests/model_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ pub mod model_tests {
};
use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes;
use std::borrow::BorrowMut;

use capycrypt::curve::Curves;
const SELECTED_CURVE: Curves = Curves::E521;
const SELECTED_CURVE: Curves = Curves::E448;

#[test]
pub fn test_sym_enc() {
Expand All @@ -26,14 +25,12 @@ pub mod model_tests {
//check conversion to and from bytes.
let pw = get_random_bytes(16);
let owner = "test key".to_string();
let mut message = Box::new(get_random_bytes(1).to_owned()); //5mb
dbg!(message.clone());
let mut message = Box::new(get_random_bytes(5242880).to_owned()); //5mb
let key_obj = gen_keypair(&mut pw.clone(), owner, 512);
let x = key_obj.pub_x;
let y = key_obj.pub_y;
let pub_key = CurvePoint::point(SELECTED_CURVE, x, y);
let mut enc = encrypt_with_key(pub_key, &mut message, 512);
dbg!(enc.c.clone());
let res = decrypt_with_key(&mut pw.clone(), enc.borrow_mut(), 512);
assert!(res);
}
Expand Down

0 comments on commit 75cb565

Please sign in to comment.