Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parcnet-pod): refactor and add verification #28

Merged
merged 6 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,101 changes: 771 additions & 330 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions chat/src/chat/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl ViewInputHandler for TextInput {
fn text_for_range(
&mut self,
range_utf16: Range<usize>,
_adjusted_range: &mut Option<Range<usize>>,
_cx: &mut ViewContext<Self>,
) -> Option<String> {
let range = self.range_from_utf16(&range_utf16);
Expand Down
11 changes: 9 additions & 2 deletions parcnet-pod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@ name = "parcnet_pod"
path = "src/lib.rs"

[dependencies]
ark-bn254 = "0.4.0"
ark-ff = "0.4.0"
ark-std = "0.4.0"
babyjubjub-ark = { git = "https://github.com/ax0/babyjubjub-ark" }
base64 = "0.22.1"
ff = { package = "ff_ce", version = "0.11", features = ["derive"] }
hex = "0.4.3"
indexmap = { version = "2.5.0", features = ["serde"] }
indexmap = { version = "2.5.0", features = ["rayon", "serde"] }
lazy_static = "1.5.0"
num-bigint = { version = "0.4.6", features = ["serde"] }
num-traits = "0.2.19"
poseidon-ark = { git = "https://github.com/arnaucube/poseidon-ark", version = "0.0.1" }
poseidon-rs = "0.0.10"
rand = "0.8.5"
rayon = "1.10.0"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["preserve_order"] }
serde_with = { version = "3.11.0", features = ["hex"] }
sha2 = "0.10.8"
thiserror = "1.0.64"
time = {version = "0.3.36", features = ["macros", "serde"]}
url = "2.5.2"
urlencoding = "2.1.3"
uuid = { version = "1.10.0", features = ["v4", "serde"] }
Expand Down
6 changes: 3 additions & 3 deletions parcnet-pod/benches/pod_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ark_bn254::Fr as Fq;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use num_bigint::BigInt;
use parcnet_pod::pod::pod_impl::{create_pod, PodValue};
use parcnet_pod::pod::{create_pod, value::PodValue};

fn benchmark_create_pod(c: &mut Criterion) {
let private_key = vec![0u8; 32];
Expand Down Expand Up @@ -68,7 +68,7 @@ fn benchmark_create_pod(c: &mut Criterion) {
("element".to_string(), PodValue::String("holy".to_string())),
(
"cryptoHash".to_string(),
PodValue::Cryptographic(BigInt::from(1234567890)),
PodValue::Cryptographic(Fq::from(1234567890)),
),
]),
)
Expand Down
195 changes: 0 additions & 195 deletions parcnet-pod/src/crypto/eddsa.rs

This file was deleted.

42 changes: 25 additions & 17 deletions parcnet-pod/src/crypto/lean_imt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::crypto::poseidon_hash::hash_bigints;
use num_bigint::BigInt;
use babyjubjub_ark::Fq;
use poseidon_ark::Poseidon;

pub fn lean_poseidon_imt(inputs: &[Fq]) -> Result<Fq, &'static str> {
let poseidon = Poseidon::new();

pub fn lean_poseidon_imt(inputs: &[BigInt]) -> Result<BigInt, &'static str> {
if inputs.is_empty() {
return Err("At least one input is required");
}
Expand All @@ -12,55 +14,61 @@ pub fn lean_poseidon_imt(inputs: &[BigInt]) -> Result<BigInt, &'static str> {
let mut new_items = Vec::new();
for chunk in items.chunks(2) {
if chunk.len() == 2 {
let hash = hash_bigints(&[chunk[0].clone(), chunk[1].clone()])?;
let hash = poseidon
.hash(vec![chunk[0], chunk[1]])
.map_err(|_| "Error hashing")?;
new_items.push(hash);
} else {
new_items.push(chunk[0].clone());
new_items.push(chunk[0]);
}
}
items = new_items;
}

Ok(items[0].clone())
Ok(items[0])
}

#[cfg(test)]
mod tests {
use super::*;
use num_bigint::BigInt;
use babyjubjub_ark::Fq;
use std::str::FromStr;

type Error = Box<dyn std::error::Error>;

#[test]
fn test_lean_imt() {
let input_strs = ["1", "2", "3", "4", "5"];
let inputs: Vec<BigInt> = input_strs
.iter()
.map(|s| BigInt::from_str(s).unwrap())
.collect();
fn test_lean_imt() -> Result<(), Error> {
let inputs = ["1", "2", "3", "4", "5"]
.into_iter()
.map(Fq::from_str)
.collect::<Result<Vec<_>, ()>>()
.map_err(|_| "Error converting strings to fields.")?;

let result = lean_poseidon_imt(&inputs);
assert!(result.is_ok());
let hash = result.unwrap();
assert_eq!(
hash,
BigInt::from_str(
Fq::from_str(
"11512324111804726054755717642058292259866309947044530224809882918003853859592"
)
.expect("can't parse")
);

Ok(())
}

#[test]
fn test_lean_imt_single_input() {
let inputs = vec![BigInt::from(42)];
let inputs = vec![Fq::from(42)];
let result = lean_poseidon_imt(&inputs);
assert!(result.is_ok());
assert_eq!(result.unwrap(), BigInt::from(42));
assert_eq!(result.unwrap(), Fq::from(42));
}

#[test]
fn test_lean_imt_empty_input() {
let inputs: Vec<BigInt> = vec![];
let inputs: Vec<Fq> = vec![];
let result = lean_poseidon_imt(&inputs);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "At least one input is required");
Expand Down
3 changes: 0 additions & 3 deletions parcnet-pod/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
pub mod blake512;
pub mod eddsa;
pub mod lean_imt;
pub mod poseidon_hash;
Loading
Loading