Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tfhe-rs(Zama.ai) compatibility with Risczero ZKVM #1857

Open
turbofakesmile opened this issue May 18, 2024 · 1 comment
Open

tfhe-rs(Zama.ai) compatibility with Risczero ZKVM #1857

turbofakesmile opened this issue May 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@turbofakesmile
Copy link

Feature

I wanted to use ZAMA FHE on top of ZKVM. I tried to implement the tfhe-rs FHE (Fully Homomorphic Encryption) library into Risczero ZKVM but didn't succeed. From my understanding, it doesn't work because of the Rust std issue and possibly some troubles in the Hyle.eu verification layer. You can find a sample of Risczero at https://github.com/Hyle-org/collatz-conjecture/.

That what ZAMA team told me:

  • Hello @Yehor some of our code, here the csprng, is not compatible with « exotic » targets, the build file here has a problem identifying the target you are compiling for, you would likely need to patch the csprng to work in the context of the risc0

Motivation

My motivation is to expand possibilities of ZK modular stack and accelerate cryptographic algorithms with Risczero tech stack

Recent code and error example

#![no_main]

risc0_zkvm::guest::entry!(main);
use hyle_contract::{HyleInput, HyleOutput};
use risc0_zkvm::guest::env;
use std::prelude::*;
use tfhe::prelude::*;
use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint32, FheUint8};

#[cfg(all(not(feature = "std"), target_os = "zkvm"))]
pub fn main() {
    let input: HyleInput<u32> = env::read();

    let config = ConfigBuilder::default().build();
    let (client_key, server_keys) = generate_keys(config);

    let clear_b = 5u32;
    let clear_c = 7u8;

    let encrypted_b = FheUint32::try_encrypt(clear_b, &client_key).unwrap();
    let encrypted_c = FheUint8::try_encrypt(clear_c, &client_key).unwrap();

    set_server_key(server_keys);

    let encrypted_res_mul = &encrypted_state * &encrypted_b;
    let encrypted_a_shifted = &encrypted_res_mul >> &encrypted_b;
    let casted_a: FheUint8 = encrypted_a_shifted.cast_into();
    let encrypted_res_min = casted_a.min(&encrypted_c);
    let encrypted_res = &encrypted_res_min & 1_u8;

    let encrypted_vec = [encrypted_res_mul, encrypted_a_shifted];

    for encrypted_val in &encrypted_vec {
        let _clear_val: u32 = encrypted_val.decrypt(&client_key);
    }

    let final_res: u8 = encrypted_res.decrypt(&client_key);

    let encrypted_next_state = FheUint32::try_encrypt(next_state, &client_key)?;
    let decrypted_next_state = encrypted_next_state.decrypt(&client_key);

    env::commit(&HyleOutput {
        block_number: input.block_number,
        block_time: input.block_time,
        caller: input.caller,
        tx_hash: input.tx_hash,
        program_outputs: Some("Any output heehee"),
        initial_state: u32::to_be_bytes(input.initial_state).to_vec(),
        next_state: u32::to_be_bytes(if input.initial_state == 1 {
            match input.program_inputs.unwrap() {
                0 => panic!("Cannot reset to 0 as that would block the contract."),
                a => a,
            }
        } else {
            // Calculate the next number in the collatz conjecture
            let mut n = input.initial_state;
            if n % 2 == 0 {
                n = n / 2;
            } else {
                n = 3 * n + 1;
            }
            n
        })
        .to_vec(),
    });
}

Error

method: Caused by:
method: process didn't exit successfully: /Users/yehorbalytskyi/collatz-conjecture/target/riscv-guest/release/build/concrete-csprng-99bb20818c93500d/build-script-build (exit status: 101)
method: --- stderr
method: thread 'main' panicked at /Users/yehorbalytskyi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/concrete-csprng-0.4.0/build.rs:101:41:
method: CARGO_CFG_TARGET_FAMILY is not set: NotPresent
method: note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
error: failed to run custom build command for methods v0.1.0 (/Users/yehorbalytskyi/collatz-conjecture/methods)

Caused by:
process didn't exit successfully: /Users/yehorbalytskyi/collatz-conjecture/target/release/build/methods-6d11fcc7401469fd/build-script-build (exit status: 101)
--- stdout
Building guest package methods.method
cargo:rerun-if-env-changed=RISC0_SKIP_BUILD
Using rustc: /Users/yehorbalytskyi/.rustup/toolchains/risc0/bin/rustc
cargo:rerun-if-env-changed=RISC0_RUST_SRC
Building guest package: cargo build --target riscv32im-risc0-zkvm-elf
cargo:rerun-if-env-changed=RISC0_BUILD_DEBUG

@turbofakesmile turbofakesmile added the enhancement New feature or request label May 18, 2024
Copy link

linear bot commented May 18, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant