Merge pull request #17 from Sunscreen-tech/rweber/bench

Add test for secret key size
This commit is contained in:
rickwebiii
2022-01-06 19:41:34 -08:00
committed by GitHub
2 changed files with 56 additions and 20 deletions

View File

@@ -4,7 +4,7 @@
When using a plain modulus large enough for batching, generating relin keys fails at `N=1024,2048`.
## Noise budget impact at minimum plain modulus to support batching of a single operation
### Noise budget impact at minimum plain modulus to support batching of a single operation
| n | Add | Mul+relin |
|-------|------|-----------|
@@ -15,9 +15,16 @@ When using a plain modulus large enough for batching, generating relin keys fail
| 16384 | ~0 | ~29 |
| 32768 | ~0 | ~30 |
## Noise budget at minimum plain modulus to support batching
### Noise budget at minimum plain modulus to support batching
| n | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 |
|------|------|------|------|------|-------|-------|
| bits | N/A | N/A | 49 | 149 | 365 | 800 |
### Size of secret key
Actual values may vary slightly with RNG.
| n | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 |
|-------|-------|--------|--------|---------|----------|----------|
| bytes | ~4282 | ~15496 | ~70230 | ~270701 | ~1028500 | ~3950099 |

View File

@@ -251,27 +251,15 @@ impl SecretKey {
pub fn get_handle(&self) -> *mut c_void {
self.handle
}
}
impl Drop for SecretKey {
fn drop(&mut self) {
convert_seal_error(unsafe { bindgen::SecretKey_Destroy(self.handle) })
.expect("Fatal error in PublicKey::drop")
}
}
impl Serialize for SecretKey {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
/**
* Copies the secret key into a byte array.
*/
pub fn as_bytes(&self) -> Result<Vec<u8>> {
let mut num_bytes: i64 = 0;
convert_seal_error(unsafe {
bindgen::SecretKey_SaveSize(self.handle, CompressionType::ZStd as u8, &mut num_bytes)
})
.map_err(|e| {
S::Error::custom(format!("Failed to get secret key serialized size: {}", e))
})?;
let mut data: Vec<u8> = Vec::with_capacity(num_bytes as usize);
@@ -287,11 +275,28 @@ impl Serialize for SecretKey {
CompressionType::ZStd as u8,
&mut bytes_written,
)
})
.map_err(|e| S::Error::custom(format!("Failed to get secret key bytes: {}", e)))?;
})?;
unsafe { data.set_len(bytes_written as usize) };
Ok(data)
}
}
impl Drop for SecretKey {
fn drop(&mut self) {
convert_seal_error(unsafe { bindgen::SecretKey_Destroy(self.handle) })
.expect("Fatal error in PublicKey::drop")
}
}
impl Serialize for SecretKey {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let data = self.as_bytes().map_err(|e| S::Error::custom(format!("Failed to get secret key bytes: {}", e)))?;
serializer.serialize_bytes(&data)
}
}
@@ -541,4 +546,28 @@ mod tests {
serde_json::to_string(&secret_key).unwrap()
);
}
#[test]
fn serialized_secret_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(&params, false, SecurityLevel::default()).unwrap();
let gen = KeyGenerator::new(&context).unwrap();
let secret = gen.secret_key();
println!("\tSecret key size poly_degree={} bytes={}", d, secret.as_bytes().unwrap().len());
}
}
}