feat: add functionality to encrypt a u64 vector#853
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughVector-based BFV encryption is extended across the stack: new functions ( Changes
Sequence DiagramsequenceDiagram
participant Client
participant SDK as TypeScript SDK
participant WASM
participant BFVHelper as BFV Helper (Rust)
rect rgb(200, 220, 255)
Note over Client,BFVHelper: Vector Encryption Flow
Client->>SDK: encryptVector(data, publicKey)
SDK->>WASM: bfv_encrypt_vector(data, publicKey, ...)
WASM->>BFVHelper: bfv_encrypt_v64(data, publicKey, ...)
BFVHelper->>BFVHelper: Deserialize pubkey
BFVHelper->>BFVHelper: Encode plaintext
BFVHelper->>BFVHelper: Encrypt vector
BFVHelper-->>WASM: Vec<u8> (ciphertext)
WASM-->>SDK: Uint8Array
SDK-->>Client: Uint8Array
end
rect rgb(220, 255, 220)
Note over Client,BFVHelper: Verifiable Encryption Flow
Client->>SDK: encryptVectorAndGenProof(data, publicKey, circuit)
SDK->>WASM: bfv_verifiable_encrypt_vector(data, publicKey, ...)
WASM->>BFVHelper: bfv_verifiable_encrypt_v64(data, publicKey, ...)
BFVHelper->>BFVHelper: Encrypt extended
BFVHelper->>BFVHelper: Compute validation vectors
BFVHelper->>BFVHelper: Generate circuit inputs (JSON)
BFVHelper-->>WASM: VerifiableEncryptionResult
WASM-->>SDK: Vec<JsValue>
SDK->>SDK: Parse circuit inputs
SDK->>SDK: generateProof(circuit, inputs)
SDK-->>Client: VerifiableEncryptionResult
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Changes follow a consistent pattern of extending existing single-value encryption functions to vector variants across three layers (Rust, WASM, TypeScript). While the spread is moderate, the additions are mostly parallel implementations with homogeneous logic density and straightforward error handling. Tests validate core functionality adequately. Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
8bc14e5 to
9d86f47
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 2
♻️ Duplicate comments (1)
packages/enclave-sdk/src/types.ts (1)
316-330: Tighten typing and fix doc wording
- It returns public inputs, not a proof.
- Avoid
Object; useRecord<string, unknown>(or a concrete schema).Apply:
-/** - * The result of encrypting a value and generating a proof - */ +/** + * Result of encrypting a vector and producing circuit public inputs + */ export interface EncryptedValueAndPublicInputs { @@ - publicInputs: Object; + publicInputs: Record<string, unknown>; }Optional: consider harmonizing field names across types (e.g., use
ciphertextinstead of mixingencryptedVector/encryptedVote) to reduce confusion.
🧹 Nitpick comments (6)
examples/CRISP/sdk/package.json (1)
41-41: Avoid publishing with workspace: dependency*"workspace:*" is not publishable to npm. This example has a publish script; prevent accidental publish or pin a real version.
Suggested patch to mark the example package as private:
{ "name": "@enclave/crisp-sdk", "version": "0.0.1", "type": "module", + "private": true, "files": [ "dist" ],packages/enclave-sdk/tests/sdk.test.ts (1)
53-60: Fix typos and avoid brittle size assertion
- Spelling: "vector" and "environment".
- Don’t pin ciphertext size; assert non‑empty instead.
Apply:
-it("should encrypt a vecor of numbers without crashing in a node environent", async () => { +it("should encrypt a vector of numbers without crashing in a node environment", async () => { @@ - expect(value.length).to.equal(27_674); + expect(value.length).to.be.greaterThan(0); @@ -it("should encrypt a vector and generate a proof without crashing in a node environent", async () => { +it("should encrypt a vector and generate a proof without crashing in a node environment", async () => {Also applies to: 62-72
crates/wasm/src/lib.rs (2)
42-70: Docstring nit: not “default params”These functions take caller‑provided params. Suggest updating wording to avoid confusion.
-/// A function to encrypt a Vec<u64> value using BFV and default params. +/// Encrypt a Vec<u64> using BFV with provided parameters. @@ -/// * `moduli` - Modulus for BFV parameters +/// * `moduli` - Modulus for BFV parameters (single modulus)
127-142: Docstring nit: clarify params and returnsSame as above; also clarify the two return elements’ meaning.
-/// A function to encrypt a Vec<u64> value using BFV and default params and -/// generate circuit inputs for Greco +/// Encrypt a Vec<u64> using BFV (with provided parameters) and +/// generate circuit inputs for Greco. @@ -/// Returns a `Result<Vec<JsValue>, JsValue>` containing the encrypted data, circuit inputs and any errors. +/// Returns `[encrypted_ciphertext_bytes, circuit_inputs_json]` as `Vec<JsValue>`.crates/bfv-helpers/src/client.rs (2)
58-98: Docstring corrections and minor API polish
- “Vector of moduli” -> single‑element array
[u64; 1].- Error list includes input‑validation vectors, which don’t apply here.
-/// * `moduli` - Vector of moduli for BFV parameters +/// * `moduli` - Single‑element array of moduli for BFV parameters @@ -/// - Input validation vector computation fails +/// - (n/a for non‑verifiable variant)Optional: accept
impl AsRef<[u64]>instead ofVec<u64>to avoid unnecessary allocations.
191-233: Renamevote→dataand fix docsUse neutral naming and correct docs to reflect
Vec<u64>and actualResulttype.Apply:
-pub fn bfv_verifiable_encrypt_v64( - vote: Vec<u64>, +pub fn bfv_verifiable_encrypt_v64( + data: Vec<u64>, @@ - let plaintext = Plaintext::try_encode(&vote, Encoding::poly(), ¶ms) + let plaintext = Plaintext::try_encode(&data, Encoding::poly(), ¶ms) .map_err(|e| anyhow!("Error encoding plaintext: {}", e))?;Doc fixes:
-/// Verifiably encrypt a u64 using BFV homomorphic encryption and generate circuit inputs +/// Verifiably encrypt a Vec<u64> using BFV and generate circuit inputs @@ -/// * `data` - The value to encrypt (u64) +/// * `data` - The values to encrypt (Vec<u64>) @@ -/// * `Result<VerifiableEncryptionResult, String>` - Contains encrypted u64 and circuit inputs for ZKP +/// * `Result<VerifiableEncryptionResult>` - Contains encrypted data and circuit inputs for ZKPOptional: consider renaming
encrypted_voteinVerifiableEncryptionResulttociphertextfor consistency across layers.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (6)
crates/bfv-helpers/src/client.rs(4 hunks)crates/wasm/src/lib.rs(3 hunks)examples/CRISP/sdk/package.json(1 hunks)packages/enclave-sdk/src/enclave-sdk.ts(3 hunks)packages/enclave-sdk/src/types.ts(1 hunks)packages/enclave-sdk/tests/sdk.test.ts(1 hunks)
🧰 Additional context used
🪛 Biome (2.1.2)
packages/enclave-sdk/src/enclave-sdk.ts
[error] 219-225: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
The declaration is defined in this switch clause:
Safe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 227-227: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
The declaration is defined in this switch clause:
Safe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 229-229: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
The declaration is defined in this switch clause:
Safe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 253-259: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
The declaration is defined in this switch clause:
Safe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 261-261: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
The declaration is defined in this switch clause:
Safe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
🔇 Additional comments (3)
crates/bfv-helpers/src/client.rs (1)
262-288: LGTM: vector encrypt/verifiable testsHappy path decryption checks look good and cover both variants.
Also applies to: 312-338
packages/enclave-sdk/src/enclave-sdk.ts (2)
37-37: LGTM!The new imports for vector encryption functionality are correctly structured and necessary for the new methods.
Also applies to: 41-41, 43-43
143-166: LGTM!The
encryptVectormethod correctly follows the established pattern fromencryptNumber, properly initializes WASM, and passes the protocol parameters to the underlying BFV encryption function.
b14916b to
e1ad867
Compare
Add vector encryption function in bfv_helpers, wasm and enclave sdk. related to #687
Summary by CodeRabbit
Release Notes