-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
|
||
pub mod kem; | ||
pub mod kzg; | ||
pub mod vec; | ||
pub mod we; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//! # Vector | ||
//! | ||
//! This module contains a helper struct to use the extractable witness encryption scheme with vectors as polynomials. | ||
|
||
use crate::{ | ||
kem, | ||
kzg::{KZGError, KZG}, | ||
we::{self, Ciphertext}, | ||
}; | ||
use ark_ec::pairing::Pairing; | ||
use ark_poly::{ | ||
domain::{EvaluationDomain, Radix2EvaluationDomain}, | ||
polynomial::univariate::DensePolynomial, | ||
DenseUVPolynomial, | ||
}; | ||
use ark_std::{rand::rngs::SmallRng, rand::SeedableRng, UniformRand}; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct VectorWE<E: Pairing> { | ||
kzg: KZG<E>, | ||
domain_d: Radix2EvaluationDomain<<E as Pairing>::ScalarField>, | ||
} | ||
|
||
impl<E: Pairing> VectorWE<E> { | ||
pub fn new(kzg: KZG<E>) -> Self { | ||
let domain_d = Radix2EvaluationDomain::new(kzg.g1_pow().len()).unwrap(); | ||
|
||
Self { kzg, domain_d } | ||
} | ||
|
||
/// Commit to a vector of values. | ||
/// Returns the commitment and the proofs for each index. | ||
pub fn commit(&self, vec: &[E::ScalarField]) -> Result<(E::G1, Vec<E::G1>), VectorWEError> { | ||
// Pad with a random value. | ||
// This is necessary since the commitment could leak information about the vector. | ||
let mut rng = SmallRng::from_entropy(); | ||
let r = E::ScalarField::rand(&mut rng); | ||
|
||
let mut values = vec.to_vec(); | ||
values.push(r); | ||
|
||
// Transform the vector into a polynomial in coefficient form | ||
let p_coeff = self.domain_d.ifft(&values); | ||
|
||
// Calculate proofs for each index | ||
let proofs = self.kzg.open_fk(&p_coeff)?; | ||
|
||
// Construct the dense polynomial from the coefficients | ||
let p_dense = DensePolynomial::from_coefficients_vec(p_coeff); | ||
|
||
// Commit | ||
let com = self.kzg.commit(&p_dense)?; | ||
|
||
Ok((com, proofs)) | ||
} | ||
|
||
/// Encrypt | ||
pub fn encrypt( | ||
&self, | ||
com: E::G1, | ||
index: usize, | ||
value: E::ScalarField, | ||
message: &[u8], | ||
) -> Result<Ciphertext<E>, VectorWEError> { | ||
// Get n-th root of unity | ||
let omega = self.domain_d.element(index); | ||
|
||
// Encrypt | ||
we::encrypt::<E>(com, omega, value, message, self.kzg.tau_g2()).map_err(VectorWEError::from) | ||
} | ||
|
||
// Decrypt | ||
pub fn decrypt(&self, proof: E::G1, ct: Ciphertext<E>) -> Result<Vec<u8>, VectorWEError> { | ||
we::decrypt::<E>(proof, ct).map_err(VectorWEError::from) | ||
} | ||
} | ||
|
||
#[derive(Error, Debug, PartialEq, Eq)] | ||
pub enum VectorWEError { | ||
#[error("KZG error: {0}")] | ||
KZGError(KZGError), | ||
#[error("KEM error: {0}")] | ||
KEMError(kem::KEMError), | ||
} | ||
|
||
impl From<KZGError> for VectorWEError { | ||
fn from(err: KZGError) -> Self { | ||
VectorWEError::KZGError(err) | ||
} | ||
} | ||
|
||
impl From<kem::KEMError> for VectorWEError { | ||
fn from(err: kem::KEMError) -> Self { | ||
VectorWEError::KEMError(err) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.