- Created new crate `bevy_vanth` with basic plugin structure for Bevy integration. - Refactored `Id` generation in `vanth` to use `OsRng` and removed redundant `to_u128_pair`/`from_u128_pair` methods. - Moved networking functionality into new `net` module with `Node`, `Packet`, and `Message` types. - Updated `vanth_derive` to use `proc-macro-crate` for reliable crate path resolution. - Added `rand` dependency to replace custom ID generation logic. - Updated `Cargo.toml`/`Cargo.lock` with new dependencies: `bevy_app`, `nix`, `cfg_aliases`, `proc-macro-crate`. - Modified `README.md` with improved project description. - Added commented clippy check in `flake.nix`. - Added `smol`, `async-process`, and `async-trait` dependencies in root and `vanth` crate. - Integrated `vanth` crate into `bevy_vanth` and added serde dependency. - Reorganized test files into module structure for `cli` and `vanth` crates. - Created new modules `compress` and `ecc` in `vanth`. - Implemented `Node` with async `run` method and `Backend` trait for networking in `vanth`. - Renamed `Memory` backend to `InMemoryStore` in `vanth` store module.
50 lines
No EOL
1.8 KiB
Rust
50 lines
No EOL
1.8 KiB
Rust
use serde_json::{from_value, to_value, Value};
|
|
use vanth_transport::crypto::ntru::{PrivateKey, PublicKey};
|
|
|
|
use ntrulp::{
|
|
key::{priv_key::PrivKey as NtruPrivKey, pub_key::PubKey as NtruPubKey},
|
|
poly::{r3::R3, rq::Rq},
|
|
};
|
|
|
|
fn generate_keypair() -> (NtruPrivKey, NtruPubKey) {
|
|
let mut rng = rand::rng();
|
|
let f: Rq = Rq::from(ntrulp::rng::short_random(&mut rng).expect("failed to sample short_random"));
|
|
let mut g: R3;
|
|
|
|
let sk = loop {
|
|
g = R3::from(ntrulp::rng::random_small(&mut rng));
|
|
match NtruPrivKey::compute(&f, &g) {
|
|
Ok(s) => break s,
|
|
Err(_) => continue,
|
|
};
|
|
};
|
|
|
|
let pk = NtruPubKey::from_sk(&sk).expect("failed to derive public key from secret key");
|
|
(sk, pk)
|
|
}
|
|
|
|
#[test]
|
|
fn test_private_key_serde_roundtrip() {
|
|
let (sk, _) = generate_keypair();
|
|
|
|
// Round-trip via serde_json Value without touching internal fields.
|
|
let wrapped: PrivateKey = sk.into();
|
|
let json1: Value = to_value(&wrapped).expect("failed to serialize PrivateKey");
|
|
let reparsed: PrivateKey = from_value(json1.clone()).expect("failed to deserialize PrivateKey");
|
|
let json2: Value = to_value(&reparsed).expect("failed to serialize PrivateKey again");
|
|
|
|
assert_eq!(json2, json1, "PrivateKey JSON did not round-trip losslessly.");
|
|
}
|
|
|
|
#[test]
|
|
fn test_public_key_serde_roundtrip() {
|
|
let (_, pk) = generate_keypair();
|
|
|
|
// Round-trip via serde_json Value without touching internal fields.
|
|
let wrapped: PublicKey = pk.into();
|
|
let json1: Value = to_value(&wrapped).expect("failed to serialize PublicKey");
|
|
let reparsed: PublicKey = from_value(json1.clone()).expect("failed to deserialize PublicKey");
|
|
let json2: Value = to_value(&reparsed).expect("failed to serialize PublicKey again");
|
|
|
|
assert_eq!(json2, json1, "PublicKey JSON did not round-trip losslessly.");
|
|
} |