diff --git a/Tables_of_things.md b/Tables_of_things.md index 03f07ffd7..4ae66e7fe 100644 --- a/Tables_of_things.md +++ b/Tables_of_things.md @@ -31,7 +31,7 @@ Actual values may vary slightly with RNG. ### Size of public key -Actual values may vary slightly with RNG. +Actual values may vary slightly with RNG. Compact keys are half as large. | n | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | |-------|-------|--------|---------|---------|----------|----------| diff --git a/seal/src/key_generator.rs b/seal/src/key_generator.rs index 93f8a75c2..e3c2697ea 100644 --- a/seal/src/key_generator.rs +++ b/seal/src/key_generator.rs @@ -264,6 +264,15 @@ impl Clone for PublicKey { */ pub struct CompactPublicKey(PublicKey); +impl CompactPublicKey { + /** + * Returns the key as a byte array. + */ + pub fn as_bytes(&self) -> Result> { + self.0.as_bytes() + } +} + /** * Class to store a secret key. */ @@ -624,4 +633,28 @@ mod tests { println!("\tPublic key size poly_degree={} bytes={}", d, public.as_bytes().unwrap().len()); } } + + #[test] + fn compact_public_key_size() { + let degree = [1024, 2048, 4096, 8192, 16384, 32768]; + + for d in degree { + let params = BfvEncryptionParametersBuilder::new() + .set_poly_modulus_degree(d) + .set_coefficient_modulus( + CoefficientModulus::bfv_default(d, SecurityLevel::default()).unwrap(), + ) + .set_plain_modulus_u64(1_000_000) + .build() + .unwrap(); + + let context = Context::new(¶ms, false, SecurityLevel::default()).unwrap(); + + let gen = KeyGenerator::new(&context).unwrap(); + + let public = gen.create_compact_public_key(); + + println!("\tCompact public key size poly_degree={} bytes={}", d, public.as_bytes().unwrap().len()); + } + } }