fix: update ffi api and readme

This commit is contained in:
rymnc
2024-03-12 14:35:36 +05:30
parent 531cbf54ce
commit 70bfb8ac9a
4 changed files with 23 additions and 21 deletions

View File

@@ -11,7 +11,7 @@ crate-type = ["staticlib"]
[features]
ffi = []
include_rln_ffi = []
default = []
default = ["ffi"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -13,24 +13,25 @@ Uses the [arkworks-rs](https://github.com/arkworks-rs/curves) suite of libraries
Note: this scheme should be used with the fork of [circom-rln](https://github.com/rymnc/circom-rln-erc5564).
```rust
use erc_5564_bn254::{random_keypair, generate_stealth_commitment, generate_stealth_private_key};
use erc_5564_rs::{StealthAddressOnCurve}; // can use bls12_381_impl or bls12_377_impl too
use ark_bn254::Bn254;
fn main() {
let (spending_key, spending_public_key) = random_keypair();
let (viewing_key, viewing_public_key) = random_keypair();
let (spending_key, spending_public_key) = Bn254::random_keypair();
let (viewing_key, viewing_public_key) = Bn254::random_keypair();
// generate ephemeral keypair
let (ephemeral_private_key, ephemeral_public_key) = random_keypair();
let (ephemeral_private_key, ephemeral_public_key) = Bn254::random_keypair();
let (stealth_commitment, view_tag) = generate_stealth_commitment(viewing_public_key, spending_public_key, ephemeral_private_key);
let (stealth_commitment, view_tag) = Bn254::generate_stealth_commitment(viewing_public_key, spending_public_key, ephemeral_private_key);
let stealth_private_key_opt = generate_stealth_private_key(ephemeral_public_key, viewing_key, spending_key, view_tag);
let stealth_private_key_opt = Bn254::generate_stealth_private_key(ephemeral_public_key, viewing_key, spending_key, view_tag);
if stealth_private_key_opt.is_none() {
panic!("View tags did not match");
}
let derived_commitment = derive_public_key(stealth_private_key_opt.unwrap());
let derived_commitment = Bn254::derive_public_key(stealth_private_key_opt.unwrap());
assert_eq!(derived_commitment, stealth_commitment);
}
```
@@ -43,6 +44,10 @@ fn main() {
2. Testing
`cargo test --release`
## FFI Api
The exposed FFI API only supports the bn254 curve at the moment. In the future, it will support all the curve implementations.
## Attribution
- The original circuits for rln are located [here](https://github.com/Rate-Limting-Nullifier/circom-rln), by the PSE group

View File

@@ -2,11 +2,8 @@ use crate::ffi::CErrorCode::{
NoError, SerializationErrorInvalidData, SerializationErrorIoError,
SerializationErrorNotEnoughSpace, SerializationErrorUnexpectedFlags,
};
use crate::stealth_commitments::{
derive_public_key, generate_random_fr, generate_stealth_commitment,
generate_stealth_private_key, random_keypair,
};
use ark_bn254::{Fr, G1Projective};
use crate::stealth_commitments::{StealthAddressOnCurve};
use ark_bn254::{Bn254, Fr, G1Projective};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
use num_traits::Zero;
use std::ops::Add;
@@ -193,7 +190,7 @@ impl TryInto<(G1Projective, u64)> for CStealthCommitment {
#[no_mangle]
pub extern "C" fn ffi_generate_random_fr() -> *mut CReturn<CFr> {
let res = match CFr::try_from(generate_random_fr()) {
let res = match CFr::try_from(Bn254::generate_random_fr()) {
Ok(v) => CReturn {
value: v,
err_code: NoError,
@@ -237,7 +234,7 @@ pub extern "C" fn ffi_derive_public_key(private_key: *mut CFr) -> *mut CReturn<C
}
};
let res = match CG1Projective::try_from(derive_public_key(private_key)) {
let res = match CG1Projective::try_from(Bn254::derive_public_key(&private_key)) {
Ok(v) => CReturn {
value: v,
err_code: NoError,
@@ -262,7 +259,7 @@ pub extern "C" fn drop_ffi_derive_public_key(ptr: *mut CReturn<CG1Projective>) {
#[no_mangle]
pub extern "C" fn ffi_random_keypair() -> *mut CReturn<CKeyPair> {
let (private_key, public_key) = random_keypair();
let (private_key, public_key) = Bn254::random_keypair();
let private_key = match CFr::try_from(private_key) {
Ok(v) => v,
Err(err) => {
@@ -362,7 +359,7 @@ pub extern "C" fn ffi_generate_stealth_commitment(
}))
}
};
let res = match CStealthCommitment::try_from(generate_stealth_commitment(
let res = match CStealthCommitment::try_from(Bn254::generate_stealth_commitment(
viewing_public_key,
spending_public_key,
ephemeral_private_key,
@@ -463,7 +460,7 @@ pub extern "C" fn ffi_generate_stealth_private_key(
}
};
let stealth_private_key_opt =
generate_stealth_private_key(ephemeral_public_key, spending_key, viewing_key, *view_tag);
Bn254::generate_stealth_private_key(ephemeral_public_key, spending_key, viewing_key, *view_tag);
if stealth_private_key_opt.is_none() {
return Box::into_raw(Box::new(CReturn {
value: CFr::zero(),
@@ -497,7 +494,6 @@ pub extern "C" fn drop_ffi_generate_stealth_private_key(ptr: *mut CReturn<CFr>)
mod tests {
use super::*;
use crate::stealth_commitments::derive_public_key;
use ark_ec::CurveGroup;
#[test]
@@ -522,7 +518,7 @@ mod tests {
assert!(public_key.into_affine().is_on_curve());
// Check if the derived key matches the one generated from the original key
assert_eq!(derive_public_key(private_key), public_key);
assert_eq!(Bn254::derive_public_key(&private_key), public_key);
}
#[test]

View File

@@ -2,6 +2,7 @@ mod stealth_commitments;
mod bls12_381_impl;
mod bn254_impl;
mod bls12_377_impl;
#[cfg(feature = "ffi")]
mod ffi;
mod bls12_377_impl;