Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ license = "MIT"
backend-avx = ["dep:poulpy-cpu-avx"]

[dependencies]
poulpy-core = { git = "https://github.com/phantomzone-org/poulpy", branch = "main" }
poulpy-schemes = { git = "https://github.com/phantomzone-org/poulpy", branch = "main" }
poulpy-hal = { git = "https://github.com/phantomzone-org/poulpy", branch = "main" }
poulpy-cpu-ref = { git = "https://github.com/phantomzone-org/poulpy", branch = "main" }
poulpy-cpu-avx = { git = "https://github.com/phantomzone-org/poulpy", branch = "main", optional = true }
poulpy-core = { git = "https://github.com/cedoor/poulpy.git", branch = "feat/serialization-key-types" }
poulpy-schemes = { git = "https://github.com/cedoor/poulpy.git", branch = "feat/serialization-key-types" }
poulpy-hal = { git = "https://github.com/cedoor/poulpy.git", branch = "feat/serialization-key-types" }
poulpy-cpu-ref = { git = "https://github.com/cedoor/poulpy.git", branch = "feat/serialization-key-types" }
poulpy-cpu-avx = { git = "https://github.com/cedoor/poulpy.git", branch = "feat/serialization-key-types", optional = true }
Comment thread
cedoor marked this conversation as resolved.
Outdated
getrandom = "0.3"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The public API is identical regardless of which backend is selected.
- [x] Add at least one runnable example in examples/: [#7](https://github.com/cedoor/squid/issues/7)
- [ ] Add tests for all existing ops: [#4](https://github.com/cedoor/squid/issues/4)
- [ ] Add rustdoc comments to all public items: [#6](https://github.com/cedoor/squid/issues/6)
- [ ] Faster tests via fixtures or deterministic keygen: [#19](https://github.com/cedoor/squid/issues/19)
- [x] Faster tests via fixtures or deterministic keygen: [#19](https://github.com/cedoor/squid/issues/19)

### Milestone 2 — Full bin_fhe Coverage: [#2](https://github.com/cedoor/squid/milestone/2)

Expand All @@ -85,7 +85,7 @@ The public API is identical regardless of which backend is selected.
- [ ] Sub-word operations: [#10](https://github.com/cedoor/squid/issues/10)
- [ ] Identity / noise refresh: [#11](https://github.com/cedoor/squid/issues/11)
- [ ] NTT backend: [#12](https://github.com/cedoor/squid/issues/12)
- [ ] Key serialization: [#13](https://github.com/cedoor/squid/issues/13)
- [x] Key serialization: [#13](https://github.com/cedoor/squid/issues/13)

### Milestone 3 — Developer Experience & Optimization: [#3](https://github.com/cedoor/squid/milestone/3)

Expand Down
102 changes: 102 additions & 0 deletions examples/serialize_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//! Serialize [`squid::SecretKey`] and [`squid::EvaluationKey`] to files (standard form).
//!
//! Uses [`Params::test`] and a single [`Context::keygen`] run. Output file names are fixed:
//! `params_test_secret_key.bin` and `params_test_evaluation_key.bin`.
//!
//! ```sh
//! cargo run --example serialize_keys -- --output-dir ./out
//! ```
//!
//! The output directory is created if missing. Useful for inspecting blob sizes or feeding
//! keys to out-of-tree tooling — integration tests keygen in-process instead.

use std::path::PathBuf;

use squid::{Context, Params};

const SECRET_KEY_FILE: &str = "params_test_secret_key.bin";
const EVALUATION_KEY_FILE: &str = "params_test_evaluation_key.bin";

struct Args {
output_dir: PathBuf,
}

fn print_usage() {
eprintln!(
"\
Usage: serialize_keys --output-dir <DIR>

Write standard-form key blobs from one OS-random keygen using Params::test().

Files written (fixed names):
{SECRET_KEY_FILE}
{EVALUATION_KEY_FILE}

Options:
-o, --output-dir <DIR> Directory to write the two key files into
-h, --help Show this help
"
);
}

fn parse_args() -> Result<Option<Args>, String> {
let mut args = std::env::args().skip(1);
let mut output_dir: Option<PathBuf> = None;

while let Some(arg) = args.next() {
match arg.as_str() {
"-h" | "--help" => return Ok(None),
"-o" | "--output-dir" => {
let path = args
.next()
.ok_or_else(|| "--output-dir requires a directory".to_string())?;
output_dir = Some(PathBuf::from(path));
}
other => return Err(format!("unknown argument: {other}")),
}
}

Ok(Some(Args {
output_dir: output_dir
.ok_or_else(|| "missing --output-dir <DIR> (or -o <DIR>)".to_string())?,
}))
}

fn main() -> std::io::Result<()> {
let args = match parse_args() {
Ok(None) => {
print_usage();
return Ok(());
}
Ok(Some(a)) => a,
Err(e) => {
eprintln!("Error: {e}");
print_usage();
std::process::exit(1);
}
};

std::fs::create_dir_all(&args.output_dir)?;

let secret_key = args.output_dir.join(SECRET_KEY_FILE);
let evaluation_key = args.output_dir.join(EVALUATION_KEY_FILE);

let params = Params::test();
let mut ctx = Context::new(params);
let (sk, ek) = ctx.keygen();

let sk_blob = ctx.serialize_secret_key(&sk).expect("serialize secret key");
let ek_blob = ctx
.serialize_evaluation_key(&ek)
.expect("serialize evaluation key");

std::fs::write(&secret_key, sk_blob)?;
std::fs::write(&evaluation_key, ek_blob)?;
Comment thread
cedoor marked this conversation as resolved.
Outdated

eprintln!(
"Wrote {} and {}.",
secret_key.display(),
evaluation_key.display()
);
Ok(())
}
Loading
Loading