Skip to content

Commit 85d71a5

Browse files
authored
feat: expose custom witness ffi (#255)
1 parent 7790954 commit 85d71a5

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

rln/src/ffi.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,21 @@ pub extern "C" fn generate_rln_proof(
361361
call_with_output_arg!(ctx, generate_rln_proof, output_buffer, input_buffer)
362362
}
363363

364+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
365+
#[no_mangle]
366+
pub extern "C" fn generate_rln_proof_with_witness(
367+
ctx: *mut RLN,
368+
input_buffer: *const Buffer,
369+
output_buffer: *mut Buffer,
370+
) -> bool {
371+
call_with_output_arg!(
372+
ctx,
373+
generate_rln_proof_with_witness,
374+
output_buffer,
375+
input_buffer
376+
)
377+
}
378+
364379
#[allow(clippy::not_unsafe_ptr_arg_deref)]
365380
#[no_mangle]
366381
pub extern "C" fn verify_rln_proof(

rln/src/public.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use ark_relations::r1cs::ConstraintMatrices;
1111
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, Write};
1212
use cfg_if::cfg_if;
1313
use color_eyre::{Report, Result};
14-
use num_bigint::BigInt;
1514
use std::io::Cursor;
1615
use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
1716

@@ -26,6 +25,7 @@ cfg_if! {
2625
use std::str::FromStr;
2726
} else {
2827
use std::marker::*;
28+
use num_bigint::BigInt;
2929
}
3030
}
3131

@@ -819,8 +819,7 @@ impl RLN<'_> {
819819
// Generate RLN Proof using a witness calculated from outside zerokit
820820
//
821821
// output_data is [ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32>]
822-
// we skip it from documentation for now
823-
#[doc(hidden)]
822+
#[cfg(target_arch = "wasm32")]
824823
pub fn generate_rln_proof_with_witness<W: Write>(
825824
&mut self,
826825
calculated_witness: Vec<BigInt>,
@@ -839,6 +838,30 @@ impl RLN<'_> {
839838
Ok(())
840839
}
841840

841+
// Generate RLN Proof using a witness calculated from outside zerokit
842+
//
843+
// output_data is [ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32>]
844+
// we skip it from documentation for now
845+
#[cfg(not(target_arch = "wasm32"))]
846+
pub fn generate_rln_proof_with_witness<R: Read, W: Write>(
847+
&mut self,
848+
mut input_data: R,
849+
mut output_data: W,
850+
) -> Result<()> {
851+
let mut witness_byte: Vec<u8> = Vec::new();
852+
input_data.read_to_end(&mut witness_byte)?;
853+
let (rln_witness, _) = deserialize_witness(&witness_byte)?;
854+
let proof_values = proof_values_from_witness(&rln_witness)?;
855+
856+
let proof = generate_proof(self.witness_calculator, &self.proving_key, &rln_witness)?;
857+
858+
// Note: we export a serialization of ark-groth16::Proof not semaphore::Proof
859+
// This proof is compressed, i.e. 128 bytes long
860+
proof.serialize_compressed(&mut output_data)?;
861+
output_data.write_all(&serialize_proof_values(&proof_values))?;
862+
Ok(())
863+
}
864+
842865
/// Verifies a zkSNARK RLN proof against the provided proof values and the state of the internal Merkle tree.
843866
///
844867
/// Input values are:

rln/src/public_api_tests.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,10 @@ fn test_rln_with_witness() {
665665
.collect();
666666

667667
// Generating the proof
668+
let mut input_buffer = Cursor::new(serialized_witness);
668669
let mut output_buffer = Cursor::new(Vec::<u8>::new());
669-
rln.generate_rln_proof_with_witness(
670-
calculated_witness_vec,
671-
serialized_witness,
672-
&mut output_buffer,
673-
)
674-
.unwrap();
670+
rln.generate_rln_proof_with_witness(&mut input_buffer, &mut output_buffer)
671+
.unwrap();
675672

676673
// output_data is [ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> ]
677674
let mut proof_data = output_buffer.into_inner();

0 commit comments

Comments
 (0)