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

Move to ruint & alloy-core for packed proof #99

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +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"
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
hex = "0.4.0"
hex-literal = "0.4"
itertools = "0.13"
Expand Down Expand Up @@ -81,7 +80,8 @@ quote = "1.0.26"

# Ark
ark-bn254 = { version = "=0.4.0" }
ark-circom = { git = "https://github.com/Dzejkop/circom-compat.git", rev = "3b19f79", 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 All @@ -98,9 +98,7 @@ ark-std = { version = "0.4.0", default-features = false, features = [
] }
ark-serialize = { version = "0.4.2", features = ["derive"] }

# Necessary because there's an issue with dependencies in the workspace
# this has something to do with `net` feature not being enabled in `mio`.
tokio = "=1.38"
tokio = "1.0"

[features]
default = []
Expand Down Expand Up @@ -133,11 +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
ethers-core.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 ethers_core::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
11 changes: 8 additions & 3 deletions src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn derive_field(seed_hex: &[u8; 64], suffix: &[u8]) -> Field {
Field::try_from_be_slice(hasher.finalize().as_ref()).unwrap() % MODULUS
}

fn seed_hex(seed: &[u8]) -> [u8; 64] {
pub fn seed_hex(seed: &[u8]) -> [u8; 64] {
let mut hasher = Sha256::new();
hasher.update(seed);
let bytes: [u8; 32] = hasher.finalize().into();
Expand All @@ -45,9 +45,14 @@ impl Identity {
let mut secret_hex = seed_hex(secret);
secret.zeroize();

Self::from_hashed_secret(&mut secret_hex, trapdoor_seed)
}

#[must_use]
pub fn from_hashed_secret(secret_hex: &mut [u8; 64], trapdoor_seed: Option<&[u8]>) -> Self {
let identity = Self {
trapdoor: derive_field(&secret_hex, trapdoor_seed.unwrap_or(b"identity_trapdoor")),
nullifier: derive_field(&secret_hex, b"identity_nullifier"),
trapdoor: derive_field(secret_hex, trapdoor_seed.unwrap_or(b"identity_trapdoor")),
nullifier: derive_field(secret_hex, b"identity_nullifier"),
};
secret_hex.zeroize();
identity
Expand Down
51 changes: 25 additions & 26 deletions src/packed_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use std::{
};

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

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

use crate::util::{bytes_from_hex, bytes_to_hex, deserialize_bytes, serialize_bytes};
Expand All @@ -17,18 +20,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 @@ -37,18 +40,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 Expand Up @@ -88,6 +86,7 @@ impl<'de> Deserialize<'de> for PackedProof {
#[cfg(test)]
pub mod test {
use super::*;
use ruint::aliases::U256;

#[test]
fn test_serializing_proof_into_packed_proof() {
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 ethers_core::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
7 changes: 0 additions & 7 deletions supply-chain/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ url = "https://raw.githubusercontent.com/mozilla/supply-chain/main/audits.toml"
[policy.ark-groth16]
audit-as-crates-io = true

[policy.ethers-core]
audit-as-crates-io = true

[policy.semaphore]
audit-as-crates-io = false

Expand Down Expand Up @@ -348,10 +345,6 @@ criteria = "safe-to-deploy"
version = "0.14.1"
criteria = "safe-to-deploy"

[[exemptions.ethers-core]]
version = "2.0.14@git:5394d899adca736a602e316e6f0c06fdb5aa64b9"
criteria = "safe-to-deploy"

[[exemptions.eyre]]
version = "0.6.12"
criteria = "safe-to-deploy"
Expand Down