#encryption #homomorphic #fhe

concrete-core

Concrete 是一个完全同态加密 (FHE) 库,实现了 Zama 的 TFHE 变体

11 个版本 (3 个稳定版)

1.0.2 2022年11月30日
1.0.0-gamma2022年7月6日
1.0.0-alpha2022年2月1日
0.1.10 2021年9月30日
0.1.7 2021年3月25日

#1257 in 密码学

Download history 62/week @ 2024-03-11 30/week @ 2024-03-18 27/week @ 2024-03-25 79/week @ 2024-04-01 26/week @ 2024-04-08 23/week @ 2024-04-15 29/week @ 2024-04-22 30/week @ 2024-04-29 22/week @ 2024-05-06 23/week @ 2024-05-13 24/week @ 2024-05-20 20/week @ 2024-05-27 24/week @ 2024-06-03 22/week @ 2024-06-10 49/week @ 2024-06-17 36/week @ 2024-06-24

每月 133 次下载
用于 5 个crate (4 个直接使用)

BSD-3-Clause-Clear

4MB
65K SLoC

Concrete Core

此crate包含用于 concrete 库中同态运算的低级实现 (GitHub 仓库)。

⚠ 警告 ⚠

此crate假定用户熟悉 FHE 的理论。如果您希望使用更简单的 API,该 API 将为您执行检查,则应使用更高级的 concrete crate。

示例

以下是如何使用 concrete-core 进行简单同态运算的小示例

// This examples shows how to multiply a secret value by a public one homomorphically.

// First we import the proper symbols
use concrete_core::prelude::Variance;
use concrete_core::prelude::LweDimension;
use concrete_core::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // DISCLAIMER: the parameters used here are only for test purpose, and cannot be considered secure.
    let lwe_dimension = LweDimension(750);
    let noise = Variance(2_f64.powf(-104.));

    // Here a hard-set encoding is applied on the input (shift by 59 bits) which corresponds here
    // to a precision of 4 bits with an additional bit of padding (won't be used but required for
    // PBS)
    let raw_input = 3_u64 << 59;

    // We will multiply by 4
    let raw_input_cleatext = 4_u64;

    // Unix seeder must be given a secret input.
    // Here we just give it 0, which is totally unsafe.
    const UNSAFE_SECRET: u128 = 0;
    let mut engine = DefaultEngine::new(Box::new(UnixSeeder::new(UNSAFE_SECRET)))?;

    // We create a cleartext from the raw cleartext
    let cleartext: Cleartext64 = engine.create_cleartext_from(&raw_input_cleatext)?;
    let key: LweSecretKey64 = engine.generate_new_lwe_secret_key(lwe_dimension)?;

    // We crate the input plaintext from the raw input
    let input_plaintext = engine.create_plaintext_from(&raw_input)?;
    let input_ciphertext = engine.encrypt_lwe_ciphertext(&key, &input_plaintext, noise)?;

    // Create a container for the output, whose content will be discarded during the operation
    let mut output_ciphertext =
        engine.trivially_encrypt_lwe_ciphertext(lwe_dimension.to_lwe_size(), &input_plaintext)?;

    // Perform the multiplication, overwriting (discarding) the output ciphertext content
    engine.discard_mul_lwe_ciphertext_cleartext(
        &mut output_ciphertext,
        &input_ciphertext,
        &cleartext
    )?;

    // Get the decrypted result as a plaintext and then a raw value
    let decrypted_plaintext = engine.decrypt_lwe_ciphertext(&key, &output_ciphertext)?;
    let raw_decrypted_plaintext = engine.retrieve_plaintext(&decrypted_plaintext)?;

    // Round the output for our 4 bits of precision
    let output = raw_decrypted_plaintext >> 58;
    let carry = output % 2;
    let output = ((output >> 1) + carry) % (1 << 5);

    // Check the high bits have the result we expect
    assert_eq!(output, 12);

    Ok(())
}

许可证

本软件根据 BSD-3-Clause-Clear 许可证分发。如有任何疑问,请联系我们 hello@zama.ai

依赖项