-
Notifications
You must be signed in to change notification settings - Fork 31
fix(docs): correct stale API references in support and building guides #1813
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
Open
Toby1009
wants to merge
5
commits into
theinterfold:main
Choose a base branch
from
Toby1009:docs/correct-stale-api-references
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
049f09c
fix(docs): correct stale API references in the support and building g…
Toby1009 7aa9e4f
fix(docs): make the policy section usable and keep the template uncom…
Toby1009 0e1e411
fix(docs): say the walkthrough needs a project before Step 2
Toby1009 1d5cf41
fix(docs): name the fields matches_commitment compares
Toby1009 811c9cf
Merge branch 'main' into docs/correct-stale-api-references
Toby1009 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,142 @@ | ||
| # FHE Compute Manager | ||
|
|
||
| This project provides a flexible and efficient framework for managing Secure Programs (SP) of the | ||
| [Interfold Protocol](https://theinterfold.com). It supports both sequential and parallel processing, | ||
| with the ability to integrate various compute providers. | ||
| This project provides a framework for managing Secure Programs (SP) of the | ||
| [Interfold Protocol](https://theinterfold.com), with the ability to integrate various compute | ||
| providers. | ||
|
|
||
| ## Features | ||
|
|
||
| - Support for both sequential and parallel FHE computations | ||
| - Flexible integration of different compute providers | ||
| - Merkle tree generation for input verification | ||
| - Ciphertext hashing for output verification | ||
| - Per-program input policies that decide the leaf layout and which inputs the computation sees | ||
|
|
||
| ## Installation | ||
|
|
||
| To use this library, add it to your `Cargo.toml`: | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| e3-compute-provider = { git = "https://github.com/gnosisguild/interfold.git", path = "crates/compute-provider"} | ||
| e3-compute-provider = { git = "https://github.com/theinterfold/interfold.git" } | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| To use the library, follow these steps: | ||
|
|
||
| 1. Create an instance of the `ComputeManager` with your desired configuration. | ||
| 2. Call the `start` method to begin the computation process. | ||
| 3. The method will return the computed ciphertext and the corresponding proof. | ||
| 1. Create an instance of the `ComputeManager` with your compute provider and inputs. | ||
| 2. Call the `start` method with your E3 program's `InputPolicy`. | ||
| 3. The method returns the provider output together with the computed ciphertext bytes. | ||
|
|
||
| ```rust | ||
| use anyhow::Result; | ||
| use e3_compute_provider::{ComputeInput, ComputeManager, ComputeProvider, ComputeResult, FHEInputs}; | ||
| use voting_core::fhe_processor; | ||
|
|
||
| // Define your Risc0Provider struct and implement the ComputeProvider trait | ||
| pub fn run_compute(params: FHEInputs) -> Result<(Risc0Output, Vec<u8>)> { | ||
| let risc0_provider = Risc0Provider; | ||
| let mut provider = ComputeManager::new(risc0_provider, params, fhe_processor, false, None); | ||
| let output = provider.start(); | ||
| Ok(output) | ||
| use e3_compute_provider::{ComputeError, ComputeManager, ComputeProvider, FHEInputs, InputPolicy}; | ||
| use my_program::fhe_processor; | ||
|
|
||
| pub fn run_compute<P>(params: FHEInputs, provider: P) -> Result<(P::Output, Vec<u8>), ComputeError> | ||
| where | ||
| P: ComputeProvider + Send + Sync, | ||
| { | ||
| let mut manager = ComputeManager::new(provider, params, fhe_processor); | ||
| manager.start(InputPolicy::default()) | ||
| } | ||
| ``` | ||
|
|
||
| ## Risc0 Example | ||
| `fhe_processor` is your own function. It must match the exported `FHEProcessor` alias, | ||
| `fn(&FHEInputs) -> Vec<u8>`. | ||
|
|
||
| Here's a more detailed example of how to use the Compute Manager with Risc0: | ||
| ## Input policies | ||
|
|
||
| `InputPolicy` carries the two answers that differ between E3 programs: | ||
|
|
||
| - `leaf` builds a tree leaf. It must equal what the E3 program builds on chain for the same input. | ||
| - `select` chooses which inputs the computation runs over, by index. | ||
|
|
||
| Both are plain function pointers, so a policy is a value rather than a trait implementation: | ||
|
|
||
| ```rust | ||
| pub type LeafFn = fn(&PublishedInput) -> Result<String, ComputeError>; | ||
| pub type SelectFn = fn(&[PublishedInput]) -> Vec<usize>; | ||
| ``` | ||
|
|
||
| A leaf is returned as hex, already reduced into the BN254 scalar field. `leaf_from_digest` does that | ||
| reduction, so a program hashing its own fields does not restate the modulus: | ||
|
|
||
| ```rust | ||
| use e3_compute_provider::{ComputeInput, ComputeManager, ComputeProvider, ComputeResult, FHEInputs}; | ||
| use methods::VOTING_ELF; | ||
| use risc0_ethereum_contracts::groth16; | ||
| use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, VerifierContext}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| pub struct Risc0Provider; | ||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct Risc0Output { | ||
| pub result: ComputeResult, | ||
| pub seal: Vec<u8>, | ||
| use e3_compute_provider::policy::{all_inputs, leaf_from_digest, InputPolicy, PublishedInput}; | ||
| use e3_compute_provider::ComputeError; | ||
| use sha2::{Digest, Sha256}; | ||
|
|
||
| fn my_leaf(input: &PublishedInput) -> Result<String, ComputeError> { | ||
| let digest = Sha256::digest([input.ciphertext, input.metadata].concat()); | ||
| Ok(leaf_from_digest(&digest)) | ||
| } | ||
|
|
||
| impl ComputeProvider for Risc0Provider { | ||
| type Output = Risc0Output; | ||
| fn prove(&self, input: &ComputeInput) -> Self::Output { | ||
| // Implementation details | ||
| pub fn policy() -> InputPolicy { | ||
| InputPolicy { | ||
| leaf: my_leaf, | ||
| select: all_inputs, | ||
| } | ||
| } | ||
| pub fn run_compute(params: FHEInputs) -> Result<(Risc0Output, Vec<u8>)> { | ||
| let risc0_provider = Risc0Provider; | ||
| let mut provider = ComputeManager::new(risc0_provider, params, fhe_processor, false, None); | ||
| let output: (Risc0Output, Vec<u8>) = provider.start(); | ||
| Ok(output) | ||
| ``` | ||
|
|
||
| `PublishedInput` carries the input's `index`, its `ciphertext` bytes, the `commitment` the program | ||
| stored when it stores one, whatever `metadata` it published, and `recomputed`, the commitment | ||
| derived from the bytes. `matches_commitment()` compares `commitment` against `recomputed`. | ||
|
|
||
| `InputPolicy::default()` is the behaviour every E3 program had before policies existed. The leaf is | ||
| the ciphertext's own SAFE commitment, and every input is computed over. A program whose contract | ||
| inserts something else, or that treats a second input from one participant as a replacement, | ||
| supplies its own. | ||
|
|
||
| A policy cannot supply a root or drop an input from the tree. Every published ciphertext gets a leaf | ||
| built from its own bytes, whatever `select` then decides to compute over. | ||
|
|
||
| When your E3 program publishes a commitment or other data alongside each ciphertext, build the | ||
| manager with `with_published` so the policy can read it: | ||
|
|
||
| ```rust | ||
| let mut manager = ComputeManager::with_published(provider, params, published, fhe_processor); | ||
| ``` | ||
|
|
||
| ## Implementing a provider | ||
|
|
||
| `ComputeProvider` has one method and one associated type. Everything else is yours to choose: | ||
|
|
||
| ```rust | ||
| use e3_compute_provider::{ComputeInput, ComputeProvider, InputPolicy}; | ||
|
|
||
| pub struct MyProvider; | ||
|
|
||
| pub struct MyOutput { | ||
| pub proof: Vec<u8>, | ||
| } | ||
|
|
||
| impl ComputeProvider for MyProvider { | ||
| type Output = MyOutput; | ||
|
|
||
| fn prove(&self, input: &ComputeInput, policy: InputPolicy) -> Self::Output { | ||
| // Prove that `input` produced its committed result under `policy`, however your | ||
| // backend does that, and return whatever the caller needs. | ||
| MyOutput { proof: Vec::new() } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This example demonstrates how to create a Risc0Provider, use it with the ComputeManager, and measure | ||
| the execution time of the computation. | ||
| `prove` receives the policy rather than choosing one. A prover that picked its own would select a | ||
| different input set from the one `start` returned the ciphertext for. | ||
|
|
||
| The repository's RISC Zero and Boundless providers live in `e3-support-host`. That crate is in a | ||
| separate workspace, so the dependency above does not pull it in. Inside an Interfold checkout, its | ||
| `run_risc0_compute` and `run_compute` entry points wrap the two backends, and | ||
| `crates/support/host/src/lib.rs` is the reference implementation to read. | ||
|
|
||
| ## Configuration | ||
|
|
||
| The `ComputeManager::new()` function takes several parameters: | ||
| `ComputeManager::new()` takes three parameters: | ||
|
|
||
| - `provider`: An instance of your compute provider (e.g., `Risc0Provider`) | ||
| - `provider`: An instance of your compute provider (e.g., `MyProvider`) | ||
| - `fhe_inputs`: The FHE inputs for the computation | ||
| - `fhe_processor`: A function to process the FHE inputs | ||
| - `use_parallel`: A boolean indicating whether to use parallel processing | ||
| - `batch_size`: An optional batch size for parallel processing, must be a power of 2 | ||
|
|
||
| `ComputeManager::with_published()` takes the same three, plus `published`: one `PublishedData` entry | ||
| per ciphertext, in the same order as `fhe_inputs.ciphertexts`. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use
on-chainas a compound adjective.Line 51 says “builds on chain.” Change it to “builds on-chain.”
Suggested wording
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~51-~51: Use a hyphen to join words.
Context: ...must equal what the E3 program builds on chain for the same input. -
selectcho...(QB_NEW_EN_HYPHEN)
🤖 Prompt for AI Agents
Source: Linters/SAST tools