refactor(RLN): use iterators instead of loops (#33)

* refactor(RLN): use iterators instead of loops
* fix(rln): cargo fmt
* fix(rln): cargo clippy: avoid Vec object creation

Co-authored-by: s1fr0 <28568419+s1fr0@users.noreply.github.com>
This commit is contained in:
Richard Ramos 2022-08-22 07:48:31 -04:00 committed by GitHub
parent 824296e695
commit 2455bea922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 54 deletions

View File

@ -38,8 +38,8 @@ pub const TEST_RESOURCES_FOLDER: &str = "./resources/tree_height_20/";
pub fn ZKEY(resources_folder: &str) -> Result<(ProvingKey<Bn254>, ConstraintMatrices<Fr>)> { pub fn ZKEY(resources_folder: &str) -> Result<(ProvingKey<Bn254>, ConstraintMatrices<Fr>)> {
let zkey_path = format!("{resources_folder}{ZKEY_FILENAME}"); let zkey_path = format!("{resources_folder}{ZKEY_FILENAME}");
if Path::new(&zkey_path).exists() { if Path::new(&zkey_path).exists() {
let mut file = File::open(&zkey_path).unwrap(); let mut file = File::open(&zkey_path)?;
let proving_key_and_matrices = read_zkey(&mut file).unwrap(); let proving_key_and_matrices = read_zkey(&mut file)?;
Ok(proving_key_and_matrices) Ok(proving_key_and_matrices)
} else { } else {
Err(Error::new(ErrorKind::NotFound, "No proving key found!")) Err(Error::new(ErrorKind::NotFound, "No proving key found!"))
@ -57,7 +57,7 @@ pub fn VK(resources_folder: &str) -> Result<VerifyingKey<Bn254>> {
verifying_key = vk_from_json(&vk_path); verifying_key = vk_from_json(&vk_path);
Ok(verifying_key) Ok(verifying_key)
} else if Path::new(&zkey_path).exists() { } else if Path::new(&zkey_path).exists() {
let (proving_key, _matrices) = ZKEY(resources_folder).unwrap(); let (proving_key, _matrices) = ZKEY(resources_folder)?;
verifying_key = proving_key.vk; verifying_key = proving_key.vk;
Ok(verifying_key) Ok(verifying_key)
} else { } else {

View File

@ -207,15 +207,19 @@ pub fn rln_witness_from_json(input_json_str: &str) -> RLNWitnessInput {
let identity_secret = str_to_field(input_json["identity_secret"].to_string(), 10); let identity_secret = str_to_field(input_json["identity_secret"].to_string(), 10);
let mut path_elements: Vec<Field> = vec![]; let path_elements = input_json["path_elements"]
for v in input_json["path_elements"].as_array().unwrap().iter() { .as_array()
path_elements.push(str_to_field(v.to_string(), 10)); .unwrap()
} .iter()
.map(|v| str_to_field(v.to_string(), 10))
.collect();
let mut identity_path_index: Vec<u8> = vec![]; let identity_path_index = input_json["identity_path_index"]
for v in input_json["identity_path_index"].as_array().unwrap().iter() { .as_array()
identity_path_index.push(v.as_u64().unwrap() as u8); .unwrap()
} .iter()
.map(|v| v.as_u64().unwrap() as u8)
.collect();
let x = str_to_field(input_json["x"].to_string(), 10); let x = str_to_field(input_json["x"].to_string(), 10);
@ -430,15 +434,17 @@ pub fn generate_proof(
) -> Result<Proof, ProofError> { ) -> Result<Proof, ProofError> {
// We confert the path indexes to field elements // We confert the path indexes to field elements
// TODO: check if necessary // TODO: check if necessary
let mut path_elements: Vec<BigInt> = Vec::new(); let mut path_elements = Vec::new();
for v in rln_witness.path_elements.iter() { rln_witness
path_elements.push(BigInt::from(*v)); .path_elements
} .iter()
.for_each(|v| path_elements.push(BigInt::from(*v)));
let mut identity_path_index: Vec<BigInt> = Vec::new(); let mut identity_path_index = Vec::new();
for v in rln_witness.identity_path_index.iter() { rln_witness
identity_path_index.push(BigInt::from(*v)); .identity_path_index
} .iter()
.for_each(|v| identity_path_index.push(BigInt::from(*v)));
let inputs = [ let inputs = [
( (

View File

@ -28,36 +28,20 @@ pub fn to_field(el: &Fr) -> Field {
(*el).try_into().unwrap() (*el).try_into().unwrap()
} }
pub fn vec_to_fr(v: &Vec<Field>) -> Vec<Fr> { pub fn vec_to_fr(v: &[Field]) -> Vec<Fr> {
let mut result: Vec<Fr> = vec![]; v.iter().map(|el| to_fr(el)).collect()
for el in v {
result.push(to_fr(el));
}
result
} }
pub fn vec_to_field(v: &Vec<Fr>) -> Vec<Field> { pub fn vec_to_field(v: &[Fr]) -> Vec<Field> {
let mut result: Vec<Field> = vec![]; v.iter().map(|el| to_field(el)).collect()
for el in v {
result.push(to_field(el));
}
result
} }
pub fn vec_fr_to_field(input: &Vec<Fr>) -> Vec<Field> { pub fn vec_fr_to_field(input: &[Fr]) -> Vec<Field> {
let mut res: Vec<Field> = Vec::new(); input.iter().map(|el| to_field(el)).collect()
for el in input {
res.push(to_field(el));
}
res
} }
pub fn vec_field_to_fr(input: &Vec<Field>) -> Vec<Fr> { pub fn vec_field_to_fr(input: &[Field]) -> Vec<Fr> {
let mut res: Vec<Fr> = Vec::new(); input.iter().map(|el| to_fr(el)).collect()
for el in input {
res.push(to_fr(el));
}
res
} }
pub fn str_to_field(input: String, radix: i32) -> Field { pub fn str_to_field(input: String, radix: i32) -> Field {
@ -136,37 +120,35 @@ pub fn field_to_bytes_be(input: &Field) -> Vec<u8> {
fr_to_bytes_be(&to_fr(input)) fr_to_bytes_be(&to_fr(input))
} }
pub fn vec_fr_to_bytes_le(input: &Vec<Fr>) -> Vec<u8> { pub fn vec_fr_to_bytes_le(input: &[Fr]) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::new(); let mut bytes: Vec<u8> = Vec::new();
//We store the vector length //We store the vector length
bytes.extend(input.len().to_le_bytes().to_vec()); bytes.extend(input.len().to_le_bytes().to_vec());
// We store each element // We store each element
for el in input { input.iter().for_each(|el| bytes.extend(fr_to_bytes_le(el)));
bytes.extend(fr_to_bytes_le(el));
}
bytes bytes
} }
pub fn vec_fr_to_bytes_be(input: &Vec<Fr>) -> Vec<u8> { pub fn vec_fr_to_bytes_be(input: &[Fr]) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::new(); let mut bytes: Vec<u8> = Vec::new();
//We store the vector length //We store the vector length
bytes.extend(input.len().to_be_bytes().to_vec()); bytes.extend(input.len().to_be_bytes().to_vec());
// We store each element // We store each element
for el in input { input.iter().for_each(|el| bytes.extend(fr_to_bytes_be(el)));
bytes.extend(fr_to_bytes_be(el));
}
bytes bytes
} }
pub fn vec_field_to_bytes_le(input: &Vec<Field>) -> Vec<u8> { pub fn vec_field_to_bytes_le(input: &[Field]) -> Vec<u8> {
vec_fr_to_bytes_le(&vec_field_to_fr(input)) vec_fr_to_bytes_le(&vec_field_to_fr(input))
} }
pub fn vec_field_to_bytes_be(input: &Vec<Field>) -> Vec<u8> { pub fn vec_field_to_bytes_be(input: &[Field]) -> Vec<u8> {
vec_fr_to_bytes_be(&vec_field_to_fr(input)) vec_fr_to_bytes_be(&vec_field_to_fr(input))
} }
pub fn vec_u8_to_bytes_le(input: &Vec<u8>) -> Vec<u8> { pub fn vec_u8_to_bytes_le(input: &[u8]) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::new(); let mut bytes: Vec<u8> = Vec::new();
//We store the vector length //We store the vector length
bytes.extend(u64::try_from(input.len()).unwrap().to_le_bytes().to_vec()); bytes.extend(u64::try_from(input.len()).unwrap().to_le_bytes().to_vec());