Skip to content

Commit

Permalink
pt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pdtfh committed Nov 21, 2024
1 parent 7841dd9 commit 2ef0ba3
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 127 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions walletkit-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ crate-type = ["lib", "staticlib", "cdylib"]
name = "walletkit_core"

[dependencies]
hex = "0.4.3"
ruint = { version = "1.12.3", default-features = false, features = ["alloc"] }
semaphore = { git = "https://github.com/worldcoin/semaphore-rs", rev = "accb14b", features = ["depth_30"] }
serde_json = "1.0.133"
thiserror = "2.0.3"
uniffi = { workspace = true, features = ["build"] }
28 changes: 0 additions & 28 deletions walletkit-core/src/field.rs

This file was deleted.

11 changes: 3 additions & 8 deletions walletkit-core/src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ruint::aliases::U256;
use semaphore::protocol::generate_nullifier_hash;

use crate::proof::Context;
use crate::{proof::Context, u256::U256Wrapper};

#[derive(Clone, PartialEq, Eq, Debug, uniffi::Object)]
pub struct Identity(pub semaphore::identity::Identity);
Expand All @@ -25,18 +24,14 @@ impl Identity {
/// More information can be found [here](https://docs.world.org/world-id/concepts#vocabulary)
///
/// [Protocol Reference](https://docs.semaphore.pse.dev/V2/technical-reference/circuits#nullifier-hash).
///
/// # Result
/// Outputs a Field element as a `U256` value
#[must_use]
pub fn generate_nullifier_hash(&self, context: &Context) -> U256 {
generate_nullifier_hash(&self.0, context.external_nullifier)
pub fn generate_nullifier_hash(&self, context: &Context) -> U256Wrapper {
generate_nullifier_hash(&self.0, context.external_nullifier).into()
}
}

#[cfg(test)]
mod tests {
use crate::utils::HexNumber;

use super::*;
#[test]
Expand Down
4 changes: 1 addition & 3 deletions walletkit-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#![deny(clippy::all, clippy::pedantic, clippy::nursery)]

pub mod error;
pub mod field;
pub mod identity;
pub mod proof;
mod utils;
pub use utils::*;
pub mod u256;

uniffi::setup_scaffolding!("walletkit_core");
4 changes: 2 additions & 2 deletions walletkit-core/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub struct Context {

impl Context {
#[must_use]
pub fn new(app_id: &[u8], action: &[u8]) -> Self {
pub fn new(app_id: &[u8], _action: &[u8]) -> Self {
let external_nullifier = hash_to_field(app_id);
// TODO: handle action properly
Self { external_nullifier }
}
}
}
85 changes: 85 additions & 0 deletions walletkit-core/src/u256.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use ruint::aliases::U256;

/// A wrapper around `U256` to represent a field element in the protocol. Wrapper enables FFI interoperability.
///
/// Most inputs and outputs from the zero-knowledge proofs are `U256` values.
/// While using `U256` directly is convenient and recommended when working with the proofs, particularly in Rust,
/// it is not a user-friendly type for interactions or communications in other languages / systems.
///
/// Particularly, when sending proof inputs/outputs as JSON on HTTP requests, the values SHOULD
/// be represented as padded hex strings from Big Endian bytes.
#[allow(clippy::module_name_repetitions)]
#[derive(uniffi::Object, Debug, PartialEq, Eq, Clone)]
pub struct U256Wrapper(pub U256);

#[uniffi::export]
impl U256Wrapper {
#[must_use]
pub fn to_hex_string(&self) -> String {
format!("0x{}", hex::encode(self.0.to_be_bytes::<32>()))
}
}

impl U256Wrapper {
#[must_use]
pub const fn as_inner(&self) -> U256 {
self.0
}
}

impl From<U256Wrapper> for U256 {
fn from(val: U256Wrapper) -> Self {
val.0
}
}

impl From<U256> for U256Wrapper {
fn from(val: U256) -> Self {
Self(val)
}
}

impl std::fmt::Display for U256Wrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_hex_string())
}
}

#[cfg(test)]
mod tests {
use super::*;
use ruint::uint;

#[test]
fn test_to_hex_string_for_u256() {
assert_eq!(
U256Wrapper(U256::from(1)).to_hex_string(),
"0x0000000000000000000000000000000000000000000000000000000000000001"
);
assert_eq!(
U256Wrapper(U256::from(42)).to_hex_string(),
"0x000000000000000000000000000000000000000000000000000000000000002a"
);

assert_eq!(
U256Wrapper(uint!(999999_U256)).to_hex_string(),
"0x00000000000000000000000000000000000000000000000000000000000f423f"
);

assert_eq!(
U256Wrapper(uint!(
80084422859880547211683076133703299733277748156566366325829078699459944778998_U256
))
.to_hex_string(),
"0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"
);

assert_eq!(
U256Wrapper(uint!(
0x036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0_U256
))
.to_hex_string(),
"0x036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0"
);
}
}
84 changes: 0 additions & 84 deletions walletkit-core/src/utils.rs

This file was deleted.

0 comments on commit 2ef0ba3

Please sign in to comment.