mirror of https://github.com/vacp2p/zerokit.git
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:
parent
824296e695
commit
2455bea922
|
@ -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>)> {
|
||||
let zkey_path = format!("{resources_folder}{ZKEY_FILENAME}");
|
||||
if Path::new(&zkey_path).exists() {
|
||||
let mut file = File::open(&zkey_path).unwrap();
|
||||
let proving_key_and_matrices = read_zkey(&mut file).unwrap();
|
||||
let mut file = File::open(&zkey_path)?;
|
||||
let proving_key_and_matrices = read_zkey(&mut file)?;
|
||||
Ok(proving_key_and_matrices)
|
||||
} else {
|
||||
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);
|
||||
Ok(verifying_key)
|
||||
} 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;
|
||||
Ok(verifying_key)
|
||||
} else {
|
||||
|
|
|
@ -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 mut path_elements: Vec<Field> = vec![];
|
||||
for v in input_json["path_elements"].as_array().unwrap().iter() {
|
||||
path_elements.push(str_to_field(v.to_string(), 10));
|
||||
}
|
||||
let path_elements = input_json["path_elements"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|v| str_to_field(v.to_string(), 10))
|
||||
.collect();
|
||||
|
||||
let mut identity_path_index: Vec<u8> = vec![];
|
||||
for v in input_json["identity_path_index"].as_array().unwrap().iter() {
|
||||
identity_path_index.push(v.as_u64().unwrap() as u8);
|
||||
}
|
||||
let identity_path_index = input_json["identity_path_index"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|v| v.as_u64().unwrap() as u8)
|
||||
.collect();
|
||||
|
||||
let x = str_to_field(input_json["x"].to_string(), 10);
|
||||
|
||||
|
@ -430,15 +434,17 @@ pub fn generate_proof(
|
|||
) -> Result<Proof, ProofError> {
|
||||
// We confert the path indexes to field elements
|
||||
// TODO: check if necessary
|
||||
let mut path_elements: Vec<BigInt> = Vec::new();
|
||||
for v in rln_witness.path_elements.iter() {
|
||||
path_elements.push(BigInt::from(*v));
|
||||
}
|
||||
let mut path_elements = Vec::new();
|
||||
rln_witness
|
||||
.path_elements
|
||||
.iter()
|
||||
.for_each(|v| path_elements.push(BigInt::from(*v)));
|
||||
|
||||
let mut identity_path_index: Vec<BigInt> = Vec::new();
|
||||
for v in rln_witness.identity_path_index.iter() {
|
||||
identity_path_index.push(BigInt::from(*v));
|
||||
}
|
||||
let mut identity_path_index = Vec::new();
|
||||
rln_witness
|
||||
.identity_path_index
|
||||
.iter()
|
||||
.for_each(|v| identity_path_index.push(BigInt::from(*v)));
|
||||
|
||||
let inputs = [
|
||||
(
|
||||
|
|
|
@ -28,36 +28,20 @@ pub fn to_field(el: &Fr) -> Field {
|
|||
(*el).try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn vec_to_fr(v: &Vec<Field>) -> Vec<Fr> {
|
||||
let mut result: Vec<Fr> = vec![];
|
||||
for el in v {
|
||||
result.push(to_fr(el));
|
||||
}
|
||||
result
|
||||
pub fn vec_to_fr(v: &[Field]) -> Vec<Fr> {
|
||||
v.iter().map(|el| to_fr(el)).collect()
|
||||
}
|
||||
|
||||
pub fn vec_to_field(v: &Vec<Fr>) -> Vec<Field> {
|
||||
let mut result: Vec<Field> = vec![];
|
||||
for el in v {
|
||||
result.push(to_field(el));
|
||||
}
|
||||
result
|
||||
pub fn vec_to_field(v: &[Fr]) -> Vec<Field> {
|
||||
v.iter().map(|el| to_field(el)).collect()
|
||||
}
|
||||
|
||||
pub fn vec_fr_to_field(input: &Vec<Fr>) -> Vec<Field> {
|
||||
let mut res: Vec<Field> = Vec::new();
|
||||
for el in input {
|
||||
res.push(to_field(el));
|
||||
}
|
||||
res
|
||||
pub fn vec_fr_to_field(input: &[Fr]) -> Vec<Field> {
|
||||
input.iter().map(|el| to_field(el)).collect()
|
||||
}
|
||||
|
||||
pub fn vec_field_to_fr(input: &Vec<Field>) -> Vec<Fr> {
|
||||
let mut res: Vec<Fr> = Vec::new();
|
||||
for el in input {
|
||||
res.push(to_fr(el));
|
||||
}
|
||||
res
|
||||
pub fn vec_field_to_fr(input: &[Field]) -> Vec<Fr> {
|
||||
input.iter().map(|el| to_fr(el)).collect()
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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();
|
||||
//We store the vector length
|
||||
bytes.extend(input.len().to_le_bytes().to_vec());
|
||||
// We store each element
|
||||
for el in input {
|
||||
bytes.extend(fr_to_bytes_le(el));
|
||||
}
|
||||
input.iter().for_each(|el| bytes.extend(fr_to_bytes_le(el)));
|
||||
|
||||
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();
|
||||
//We store the vector length
|
||||
bytes.extend(input.len().to_be_bytes().to_vec());
|
||||
// We store each element
|
||||
for el in input {
|
||||
bytes.extend(fr_to_bytes_be(el));
|
||||
}
|
||||
input.iter().for_each(|el| bytes.extend(fr_to_bytes_be(el)));
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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();
|
||||
//We store the vector length
|
||||
bytes.extend(u64::try_from(input.len()).unwrap().to_le_bytes().to_vec());
|
||||
|
|
Loading…
Reference in New Issue