chore(rln): further refactoring of interface (#261)

This commit is contained in:
Aaryamann Challani 2024-06-18 11:56:23 +05:30 committed by GitHub
parent d8f813bc2e
commit 5540ddc993
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 65 deletions

1
Cargo.lock generated
View File

@ -2284,6 +2284,7 @@ dependencies = [
"cfg-if", "cfg-if",
"color-eyre", "color-eyre",
"criterion 0.4.0", "criterion 0.4.0",
"lazy_static 1.4.0",
"num-bigint", "num-bigint",
"num-traits", "num-traits",
"once_cell", "once_cell",

View File

@ -48,6 +48,7 @@ num-bigint = { version = "=0.4.3", default-features = false, features = [
] } ] }
num-traits = "=0.2.15" num-traits = "=0.2.15"
once_cell = "=1.17.1" once_cell = "=1.17.1"
lazy_static = "=1.4.0"
rand = "=0.8.5" rand = "=0.8.5"
rand_chacha = "=0.3.1" rand_chacha = "=0.3.1"
tiny-keccak = { version = "=2.0.2", features = ["keccak"] } tiny-keccak = { version = "=2.0.2", features = ["keccak"] }

View File

@ -10,50 +10,49 @@ use ark_serialize::CanonicalDeserialize;
use cfg_if::cfg_if; use cfg_if::cfg_if;
use color_eyre::{Report, Result}; use color_eyre::{Report, Result};
cfg_if! { #[cfg(not(target_arch = "wasm32"))]
if #[cfg(not(target_arch = "wasm32"))] { use {
use ark_circom::{WitnessCalculator}; ark_circom::WitnessCalculator,
use once_cell::sync::{Lazy}; lazy_static::lazy_static,
use std::sync::Mutex; std::sync::{Arc, Mutex},
use wasmer::{Module, Store}; wasmer::{Module, Store},
use std::sync::Arc; };
}
}
cfg_if! { #[cfg(feature = "arkzkey")]
if #[cfg(feature = "arkzkey")] { use ark_zkey::read_arkzkey_from_bytes;
use ark_zkey::read_arkzkey_from_bytes;
const ARKZKEY_BYTES: &[u8] = include_bytes!("tree_height_20/rln_final.arkzkey"); #[cfg(not(feature = "arkzkey"))]
} else { use {ark_circom::read_zkey, std::io::Cursor};
use std::io::Cursor;
use ark_circom::read_zkey; #[cfg(feature = "arkzkey")]
} const ARKZKEY_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln_final.arkzkey");
}
pub const ZKEY_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln_final.zkey"); pub const ZKEY_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln_final.zkey");
pub const VK_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/verification_key.arkvkey"); pub const VK_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/verification_key.arkvkey");
const WASM_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln.wasm"); const WASM_BYTES: &[u8] = include_bytes!("../resources/tree_height_20/rln.wasm");
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
static ZKEY: Lazy<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> = Lazy::new(|| { lazy_static! {
cfg_if! { #[cfg(not(target_arch = "wasm32"))]
if #[cfg(feature = "arkzkey")] { static ref ZKEY: (ProvingKey<Curve>, ConstraintMatrices<Fr>) = {
read_arkzkey_from_bytes(ARKZKEY_BYTES).expect("Failed to read arkzkey") cfg_if! {
} else { if #[cfg(feature = "arkzkey")] {
let mut reader = Cursor::new(ZKEY_BYTES); read_arkzkey_from_bytes(ARKZKEY_BYTES).expect("Failed to read arkzkey")
read_zkey(&mut reader).expect("Failed to read zkey") } else {
let mut reader = Cursor::new(ZKEY_BYTES);
read_zkey(&mut reader).expect("Failed to read zkey")
}
} }
} };
});
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
static VK: Lazy<VerifyingKey<Curve>> = static ref VK: VerifyingKey<Curve> = vk_from_ark_serialized(VK_BYTES).expect("Failed to read vk");
Lazy::new(|| vk_from_ark_serialized(VK_BYTES).expect("Failed to read vk"));
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
static WITNESS_CALCULATOR: Lazy<Arc<Mutex<WitnessCalculator>>> = Lazy::new(|| { static ref WITNESS_CALCULATOR: Arc<Mutex<WitnessCalculator>> = {
circom_from_raw(WASM_BYTES.to_vec()).expect("Failed to create witness calculator") circom_from_raw(WASM_BYTES).expect("Failed to create witness calculator")
}); };
}
pub const TEST_TREE_HEIGHT: usize = 20; pub const TEST_TREE_HEIGHT: usize = 20;
@ -69,21 +68,22 @@ pub type G2Affine = ArkG2Affine;
pub type G2Projective = ArkG2Projective; pub type G2Projective = ArkG2Projective;
// Loads the proving key using a bytes vector // Loads the proving key using a bytes vector
pub fn zkey_from_raw(zkey_data: &Vec<u8>) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> { pub fn zkey_from_raw(zkey_data: &[u8]) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> {
if !zkey_data.is_empty() { if zkey_data.is_empty() {
let proving_key_and_matrices = match () { return Err(Report::msg("No proving key found!"));
#[cfg(feature = "arkzkey")]
() => read_arkzkey_from_bytes(zkey_data.as_slice())?,
#[cfg(not(feature = "arkzkey"))]
() => {
let mut c = Cursor::new(zkey_data);
read_zkey(&mut c)?
}
};
Ok(proving_key_and_matrices)
} else {
Err(Report::msg("No proving key found!"))
} }
let proving_key_and_matrices = match () {
#[cfg(feature = "arkzkey")]
() => read_arkzkey_from_bytes(zkey_data)?,
#[cfg(not(feature = "arkzkey"))]
() => {
let mut reader = Cursor::new(zkey_data);
read_zkey(&mut reader)?
}
};
Ok(proving_key_and_matrices)
} }
// Loads the proving key // Loads the proving key
@ -93,19 +93,17 @@ pub fn zkey_from_folder() -> &'static (ProvingKey<Curve>, ConstraintMatrices<Fr>
} }
// Loads the verification key from a bytes vector // Loads the verification key from a bytes vector
pub fn vk_from_raw(vk_data: &[u8], zkey_data: &Vec<u8>) -> Result<VerifyingKey<Curve>> { pub fn vk_from_raw(vk_data: &[u8], zkey_data: &[u8]) -> Result<VerifyingKey<Curve>> {
let verifying_key: VerifyingKey<Curve>;
if !vk_data.is_empty() { if !vk_data.is_empty() {
verifying_key = vk_from_ark_serialized(vk_data)?; return vk_from_ark_serialized(vk_data);
Ok(verifying_key)
} else if !zkey_data.is_empty() {
let (proving_key, _matrices) = zkey_from_raw(zkey_data)?;
verifying_key = proving_key.vk;
Ok(verifying_key)
} else {
Err(Report::msg("No proving/verification key found!"))
} }
if !zkey_data.is_empty() {
let (proving_key, _matrices) = zkey_from_raw(zkey_data)?;
return Ok(proving_key.vk);
}
Err(Report::msg("No proving/verification key found!"))
} }
// Loads the verification key // Loads the verification key
@ -116,12 +114,10 @@ pub fn vk_from_folder() -> &'static VerifyingKey<Curve> {
// Initializes the witness calculator using a bytes vector // Initializes the witness calculator using a bytes vector
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub fn circom_from_raw(wasm_buffer: Vec<u8>) -> Result<Arc<Mutex<WitnessCalculator>>> { pub fn circom_from_raw(wasm_buffer: &[u8]) -> Result<Arc<Mutex<WitnessCalculator>>> {
let store = Store::default(); let module = Module::new(&Store::default(), wasm_buffer)?;
let module = Module::new(&store, wasm_buffer)?;
let result = WitnessCalculator::from_module(module)?; let result = WitnessCalculator::from_module(module)?;
let wrapped = Mutex::new(result); Ok(Arc::new(Mutex::new(result)))
Ok(Arc::new(wrapped))
} }
// Initializes the witness calculator // Initializes the witness calculator

View File

@ -153,7 +153,7 @@ impl RLN {
mut tree_config_input: R, mut tree_config_input: R,
) -> Result<RLN> { ) -> Result<RLN> {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
let witness_calculator = circom_from_raw(circom_vec)?; let witness_calculator = circom_from_raw(&circom_vec)?;
let proving_key = zkey_from_raw(&zkey_vec)?; let proving_key = zkey_from_raw(&zkey_vec)?;
let verification_key = vk_from_raw(&vk_vec, &zkey_vec)?; let verification_key = vk_from_raw(&vk_vec, &zkey_vec)?;