-
Notifications
You must be signed in to change notification settings - Fork 87
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6002529
Import circom-2 test vectors
oskarth 6a7c360
Add failing test under feature flag
oskarth 5d967fc
Add exceptionHandler
oskarth ceb20c9
Add showSharedRWMemory
oskarth 1d110f0
Add getFieldNumLen32 and disable getFrLen
oskarth ae93f04
Add getVersion
oskarth 01f65b8
Add getRawPrime
oskarth 4051bde
Refactor cfg code blocks
oskarth 14c45fd
Add readSharedRWMemory and get prime from WASM mem
oskarth 19faa5c
WIP: Debug R1CSfile header
oskarth 37bb1e4
Add CircomVersion struct to Wasm
oskarth e934fca
XXX: Enum test
oskarth 2b3d94a
Trait version
oskarth 97ce16a
Move traits to Circom, CircomBase, Circom2
oskarth 28ddd22
Simplify Wasm struct and remove version
oskarth dbcd1aa
Feature gate Circom1/Circom2 traits
oskarth 2cae186
Use cfg_if for witness calculation
oskarth b4271e1
Fix visibilty for both test paths
oskarth efa60ea
Remove println
oskarth 2e39422
refactor
oskarth 06f79d7
Make clippy happy with imports, unused variables
oskarth File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
use super::{fnv, CircomBase, SafeMemory, Wasm}; | ||
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")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
use super::Circom2; | ||
|
||
#[cfg(not(feature = "circom-2"))] | ||
use super::Circom; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct WitnessCalculator { | ||
|
@@ -19,6 +24,16 @@ pub struct WitnessCalculator { | |
#[error("{0}")] | ||
struct ExitCode(u32); | ||
|
||
#[cfg(feature = "circom-2")] | ||
fn from_array32(arr: Vec<i32>) -> BigInt { | ||
let mut res = BigInt::zero(); | ||
let radix = BigInt::from(0x100000000u64); | ||
for &val in arr.iter() { | ||
res = res * &radix + BigInt::from(val); | ||
} | ||
res | ||
} | ||
|
||
impl WitnessCalculator { | ||
pub fn new(path: impl AsRef<std::path::Path>) -> Result<Self> { | ||
let store = Store::default(); | ||
|
@@ -38,22 +53,44 @@ impl WitnessCalculator { | |
"logFinishComponent" => runtime::log_component(&store), | ||
"logStartComponent" => runtime::log_component(&store), | ||
"log" => runtime::log_component(&store), | ||
"exceptionHandler" => runtime::exception_handler(&store), | ||
"showSharedRWMemory" => runtime::show_memory(&store), | ||
} | ||
}; | ||
let instance = Wasm::new(Instance::new(&module, &import_object)?); | ||
|
||
let n32 = (instance.get_fr_len()? >> 2) - 2; | ||
|
||
let mut memory = SafeMemory::new(memory, n32 as usize, BigInt::zero()); | ||
let n32; | ||
let prime: BigInt; | ||
let mut safe_memory: SafeMemory; | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "circom-2")] { | ||
//let version = instance.get_version()?; | ||
n32 = instance.get_field_num_len32()?; | ||
safe_memory = SafeMemory::new(memory, n32 as usize, BigInt::zero()); | ||
let _res = instance.get_raw_prime()?; | ||
let mut arr = vec![0; n32 as usize]; | ||
for i in 0..n32 { | ||
let res = instance.read_shared_rw_memory(i)?; | ||
arr[(n32 as usize) - (i as usize) - 1] = res; | ||
} | ||
prime = from_array32(arr); | ||
} else { | ||
// Fallback to Circom 1 behavior | ||
//version = 1; | ||
n32 = (instance.get_fr_len()? >> 2) - 2; | ||
safe_memory = SafeMemory::new(memory, n32 as usize, BigInt::zero()); | ||
let ptr = instance.get_ptr_raw_prime()?; | ||
prime = safe_memory.read_big(ptr as usize, n32 as usize)?; | ||
} | ||
} | ||
|
||
let ptr = instance.get_ptr_raw_prime()?; | ||
let prime = memory.read_big(ptr as usize, n32 as usize)?; | ||
let n64 = ((prime.bits() - 1) / 64 + 1) as i32; | ||
memory.prime = prime; | ||
safe_memory.prime = prime; | ||
|
||
Ok(WitnessCalculator { | ||
instance, | ||
memory, | ||
memory: safe_memory, | ||
n64, | ||
}) | ||
} | ||
|
@@ -162,6 +199,20 @@ mod runtime { | |
Function::new_native(store, func) | ||
} | ||
|
||
// Circom 2.0 | ||
pub fn exception_handler(store: &Store) -> Function { | ||
oskarth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[allow(unused)] | ||
fn func(a: i32) {} | ||
Function::new_native(store, func) | ||
} | ||
|
||
// Circom 2.0 | ||
pub fn show_memory(store: &Store) -> Function { | ||
#[allow(unused)] | ||
fn func() {} | ||
Function::new_native(store, func) | ||
} | ||
|
||
pub fn log_signal(store: &Store) -> Function { | ||
#[allow(unused)] | ||
fn func(a: i32, b: i32) {} | ||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.