Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pdtfh committed Nov 20, 2024
1 parent 2c339f2 commit a956a60
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ semaphore-depth-config = { path = "crates/semaphore-depth-config" }
semaphore-depth-macros = { path = "crates/semaphore-depth-macros" }

# 3rd Party
alloy-core = { version = "0.8.12", default-features = false, features = ["sol-types"] }
bincode = "1.3.3"
bytemuck = "1.18"
color-eyre = "0.6"
criterion = { version = "0.5", features = ["async_tokio", "html_reports"] }
derive-where = "1"
ethabi = "18.0.0"
hex = "0.4.0"
hex-literal = "0.4"
itertools = "0.13"
Expand Down Expand Up @@ -80,7 +80,8 @@ quote = "1.0.26"

# Ark
ark-bn254 = { version = "=0.4.0" }
ark-circom = { git = "https://github.com/arkworks-rs/circom-compat.git", rev = "7f80002", features = [
# TODO: Move to arkworks-rs/circom-compat once https://github.com/arkworks-rs/circom-compat/pull/80 is merged
ark-circom = { git = "https://github.com/paolodamico/circom-compat.git", rev = "49729d3", features = [
"circom-2",
] }
ark-ec = { version = "0.4.2", default-features = false, features = [
Expand Down Expand Up @@ -130,10 +131,10 @@ semaphore-depth-config.workspace = true
semaphore-depth-macros.workspace = true

# 3rd Party
alloy-core.workspace = true
bincode.workspace = true
bytemuck.workspace = true
color-eyre.workspace = true
ethabi.workspace = true
hex.workspace = true
hex-literal.workspace = true
itertools.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::{
str,
str::FromStr,
};
use ethabi::ethereum_types::U256;
use num_bigint::{BigInt, Sign};
use ruint::aliases::U256;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Container for 256-bit hash values.
Expand All @@ -24,18 +24,18 @@ impl Hash {
}
}

/// Conversion from Ether U256
/// Conversion from U256
impl From<&Hash> for U256 {
fn from(hash: &Hash) -> Self {
Self::from_big_endian(hash.as_bytes_be())
Self::from_be_bytes(*hash.as_bytes_be())
}
}

/// Conversion to Ether U256
/// Conversion to U256
impl From<U256> for Hash {
fn from(u256: U256) -> Self {
let mut bytes = [0_u8; 32];
u256.to_big_endian(&mut bytes);
bytes.copy_from_slice(&u256.to_be_bytes::<32>());
Self::from_bytes_be(bytes)
}
}
Expand Down
50 changes: 25 additions & 25 deletions src/packed_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use std::{
};

use crate::protocol::Proof;
use ethabi::{decode, encode, ethereum_types::U256, ParamType, Token};
use alloy_core::sol_types::{
sol_data::{FixedArray, Uint},
SolType, SolValue,
};
use ruint::aliases::U256;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::util::{bytes_from_hex, bytes_to_hex, deserialize_bytes, serialize_bytes};
Expand All @@ -16,18 +21,18 @@ pub struct PackedProof(pub [u8; 256]);

impl From<Proof> for PackedProof {
fn from(proof: Proof) -> Self {
let tokens = Token::FixedArray(vec![
Token::Uint(proof.0 .0),
Token::Uint(proof.0 .1),
Token::Uint(proof.1 .0[0]),
Token::Uint(proof.1 .0[1]),
Token::Uint(proof.1 .1[0]),
Token::Uint(proof.1 .1[1]),
Token::Uint(proof.2 .0),
Token::Uint(proof.2 .1),
]);

let bytes = encode(&[tokens]);
let flat_proof = [
proof.0 .0,
proof.0 .1,
proof.1 .0[0],
proof.1 .0[1],
proof.1 .1[0],
proof.1 .1[1],
proof.2 .0,
proof.2 .1,
];

let bytes = flat_proof.abi_encode();
let mut encoded = [0u8; 256];
encoded.copy_from_slice(&bytes[..256]);
Self(encoded)
Expand All @@ -36,18 +41,13 @@ impl From<Proof> for PackedProof {

impl From<PackedProof> for Proof {
fn from(proof: PackedProof) -> Self {
let decoded = decode(&vec![ParamType::Uint(256); 8], &proof.0).unwrap();
let decoded_uint_array = decoded
.into_iter()
.map(|x| x.into_uint().unwrap())
.collect::<Vec<U256>>();

let a = (decoded_uint_array[0], decoded_uint_array[1]);
let b = (
[decoded_uint_array[2], decoded_uint_array[3]],
[decoded_uint_array[4], decoded_uint_array[5]],
);
let c = (decoded_uint_array[6], decoded_uint_array[7]);
type PackedProofSolType = FixedArray<Uint<256>, 8>;

let decoded = PackedProofSolType::abi_decode(&proof.0, true).unwrap();

let a = (decoded[0], decoded[1]);
let b = ([decoded[2], decoded[3]], [decoded[4], decoded[5]]);
let c = (decoded[6], decoded[7]);
Self(a, b, c)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use ark_groth16::{prepare_verifying_key, Groth16, Proof as ArkProof};
use ark_relations::r1cs::SynthesisError;
use ark_std::UniformRand;
use color_eyre::Result;
use ethabi::ethereum_types::U256;
use once_cell::sync::Lazy;
use poseidon::Poseidon;
use rand::{thread_rng, Rng};
use ruint::aliases::U256;
use semaphore_depth_config::{get_depth_index, get_supported_depth_count};
use semaphore_depth_macros::array_for_depths;
use serde::{Deserialize, Serialize};
Expand Down

0 comments on commit a956a60

Please sign in to comment.