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

feat(RLN): add new_with_params #36

Merged
merged 2 commits into from
Aug 31, 2022
Merged

Conversation

richard-ramos
Copy link
Member

Allows passing the wasm, zkey and verification key data as buffers, instead of using a path to a folder.
This is useful when you want to have this data embedded in a program / library, to not deal with paths to the resource folders

Required by:

@s1fr0 s1fr0 added the track:zerokit Zerokit track (Applied ZK/Explorations) label Aug 29, 2022
@s1fr0 s1fr0 mentioned this pull request Aug 29, 2022
10 tasks
Allows passing the wasm, zkey and verification key data as buffers, instead of using a path to a folder
@richard-ramos richard-ramos changed the base branch from master to release-v0.1 August 29, 2022 18:34
Copy link
Contributor

@s1fr0 s1fr0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you! Left some comments about adding a test and renaming old procs.

#[allow(non_snake_case)]
pub fn ZKEY(zkey_data: &Vec<u8>) -> Result<(ProvingKey<Curve>, ConstraintMatrices<Fr>)> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can call this something like zkey_from_raw to differentiate it from zkey_from_folder and same for VK, CIRCOM? At that point we might remove all capitalizations in proc names and the #[allow(non_snake_case)] too.

@@ -47,6 +47,23 @@ pub extern "C" fn new(tree_height: usize, input_buffer: *const Buffer, ctx: *mut
true
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle]
pub extern "C" fn new_with_params(
Copy link
Contributor

@s1fr0 s1fr0 Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be great if we can add a test, e.g. load params with File and create a new RLN object and maybe compare with an RLN object created with new (doing it directly would probably require implementation of Eq/PartialEq for RLN, but you can compare e.g. tree roots and you can try to create/verify a proof for a random witness

zerokit/rln/src/ffi.rs

Lines 557 to 585 in 9471c90

let rln_witness = random_rln_witness(tree_height);
let proof_values = proof_values_from_witness(&rln_witness);
// We prepare id_commitment and we set the leaf at provided index
let rln_witness_ser = serialize_witness(&rln_witness);
let input_buffer = &Buffer::from(rln_witness_ser.as_ref());
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
let now = Instant::now();
let success = prove(rln_pointer, input_buffer, output_buffer.as_mut_ptr());
prove_time += now.elapsed().as_nanos();
assert!(success, "prove call failed");
let output_buffer = unsafe { output_buffer.assume_init() };
// We read the returned proof and we append proof values for verify
let serialized_proof = <&[u8]>::from(&output_buffer).to_vec();
let serialized_proof_values = serialize_proof_values(&proof_values);
let mut verify_data = Vec::<u8>::new();
verify_data.extend(&serialized_proof);
verify_data.extend(&serialized_proof_values);
// We prepare input proof values and we call verify
let input_buffer = &Buffer::from(verify_data.as_ref());
let mut proof_is_valid: bool = false;
let proof_is_valid_ptr = &mut proof_is_valid as *mut bool;
let now = Instant::now();
let success = verify(rln_pointer, input_buffer, proof_is_valid_ptr);
verify_time += now.elapsed().as_nanos();
assert!(success, "verify call failed");
assert_eq!(proof_is_valid, true);

FYI (not for this PR) you can actually check if they generate the same proofs by passing the same inputs and same r,s randomness here

zerokit/rln/src/protocol.rs

Lines 443 to 444 in 9471c90

r,
s,

@richard-ramos richard-ramos merged commit 7acbde2 into release-v0.1 Aug 31, 2022
s1fr0 pushed a commit that referenced this pull request Sep 14, 2022
Allows passing the wasm, zkey and verification key data as buffers, instead of using a path to a folder
oskarth pushed a commit that referenced this pull request Sep 15, 2022
* refactor(rln): removing unused crates/dependencies

* cargo fmt

* refactor(rln): removed more dependencies; curve/fields as parameters

* refactor(rln): use poseidon-rs hash instead of semaphore-rs poseidon

* chore(rln): remove deps

* refactor(rln): use exclusively arkworks Fr

* refactor(rln): integrate poseidon-rs implementation to work with arkworks arithmetic

* fix(rln): remove previous poseidon-rs wrapper

* feat(rln): add features to select MT; remove prints if not in debug mode

* fix(rln): collect test parameters in a vector

* feat(RLN): add `new_with_params` (#36)

Allows passing the wasm, zkey and verification key data as buffers, instead of using a path to a folder

* chore(rln): simplify read wasm

* fix(rln): remove unused dependencies

* cargo fmt

* fix(rln): update dependencies, fix commit

* refactor(rln): restore ark-circom original dep

Co-authored-by: Richard Ramos <info@richardramos.me>
@richard-ramos richard-ramos deleted the feat/new_params branch October 3, 2022 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
track:zerokit Zerokit track (Applied ZK/Explorations)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants