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

WIP: Initial Circom 2 support #10

Merged
merged 21 commits into from Nov 29, 2021
Merged

WIP: Initial Circom 2 support #10

merged 21 commits into from Nov 29, 2021

Conversation

oskarth
Copy link
Contributor

@oskarth oskarth commented Nov 24, 2021

WIP PR towards #8

Minimal subset of WASM interface has been ported based on https://docs.google.com/document/d/1P0j9zXjJRkhnyPT03VjxYBUySC8l4k41ZQ5LY7uyQnw/edit and https://github.com/oskarth/hello-circom/blob/master/multiplier2_js/witness_calculator.js#L74

Can be tested with cargo test groth16_proof_circom2 --features circom-2. All current tests should pass as normal since the feature flag circom-2 is off by default.

Can either clean up and merge as partial progress with minimal WASM interface, or keep open until R1CS format stuff is sorted.

Current state

19faa5c

WIP: Debug R1CSfile header field_size in header is 1, not 32 as expected

Don't see anything recently changed here: https://github.com/iden3/r1csfile/blob/master/src/r1csfile.js (used by snarkjs)

But this seems new: https://github.com/iden3/circom/blob/0149dc0643c3842635fb758efc9ebc102f170a38/constraint_writers/src/r1cs_writer.rs

I also see this: https://github.com/gakonst/ark-circom/blob/11e6d04f3b88a8adb12cc157024ac4b5f11dd74f/src/circom/r1cs_reader.rs#L59-L62 which may or may not be related (haven't investigated in detail and currently not familiar the R1CS file format).

@oskarth oskarth mentioned this pull request Nov 24, 2021
@oskarth
Copy link
Contributor Author

oskarth commented Nov 24, 2021

The circom-1 and circom-2 code paths / functions can probably be separated more cleanly, but this works for now. Open to ideas for doing this separation better :)

Copy link
Collaborator

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

Nice work! I think it's worth refactoring a bit so that 1.0 vs 2.0 are more nicely separated, but excited to see it working.

Also don't forget to cargo fmt / cargo clippy

src/circom/builder.rs Outdated Show resolved Hide resolved
src/witness/witness_calculator.rs Outdated Show resolved Hide resolved
src/witness/witness_calculator.rs Show resolved Hide resolved
Comment on lines 19 to 32
// Circom 2.0
pub fn get_version(&self) -> Result<i32> {
self.get_i32("getVersion")
}

// Circom 1
pub fn get_fr_len(&self) -> Result<i32> {
self.get_i32("getFrLen")
}

// Circom 2.0
pub fn get_field_num_len32(&self) -> Result<i32> {
self.get_i32("getFieldNumLen32")
}
Copy link
Collaborator

@gakonst gakonst Nov 24, 2021

Choose a reason for hiding this comment

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

You could perhaps create a struct CircomBase (for any shared functions), struct Circom (for 1.0) and struct Circom2 (for 2.0) to better separate 1.0 vs 2.0 functions, and instantiate the struct Wasm with one or the other based on the feature?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried a few different things here, with enums/generics etc. The current trait version is the one that that seemed somewhat sensible and I managed to get to compile 😅 Let me know what you think.

@oskarth
Copy link
Contributor Author

oskarth commented Nov 27, 2021

@gakonst Updated PR based on review, please take a look.

Any ideas on the R1CSfile header field_size error?

@oskarth oskarth requested a review from gakonst November 27, 2021 13:43
use color_eyre::Result;
use num_bigint::BigInt;
use num_traits::Zero;
use std::cell::Cell;
use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, RuntimeError, Store};

use super::{fnv, SafeMemory, Wasm};
#[cfg(feature = "circom-2")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't very pretty, but it makes Clippy happy. Is this idiomatic or is there a better way of doing this?

fn get_ptr_raw_prime(&self) -> Result<i32>;
}

pub trait Circom2 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

More fns are needed here I think but I suggest it can be merged as experimental WIP support, since it Circom 2 isn't fully functioning yet and it is hidden under a feature flag.

@gakonst gakonst merged commit 64e0ee9 into arkworks-rs:master Nov 29, 2021
@gakonst
Copy link
Collaborator

gakonst commented Nov 29, 2021

Nice work, merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants