Skip to content

Commit

Permalink
feat(keccak): remove circom1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
tchataigner committed Jan 12, 2024
1 parent 994f824 commit 637b97b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 237 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ getrandom = { version = "0.2.10", features = ["js"] }
pasta_curves = { version = "0.5.1" }

[features]
default = ["circom-2"]
circom-2 = []
default = []
llvm = ["dep:wasmer-compiler-llvm"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

This repository provides necessary middleware to take generated output of the Circom compiler (R1CS constraints and generated witnesses) and use them with Bellperson. It is based off the work of [Nova-Scotia](https://github.com/nalinbhardwaj/Nova-Scotia) and Arkworks' [Circom-Compat](https://github.com/arkworks-rs/circom-compat). Please see **Credits** at the bottom for proper credits towards the various works used here.

> ⚠️ Note: `circom-scotia` only provide support for Circom 2.*
## How?

To use it yourself, install version 2.1.6 or greater of [Circom](https://docs.circom.io). Refer to the [Circom documentation](https://docs.circom.io/getting-started/installation/#installing-dependencies) for more information.
Expand Down
119 changes: 52 additions & 67 deletions src/witness/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use wasmer::{AsStoreMut, Function, Instance, Value};
pub struct Wasm(Instance);

/// Base trait for interacting with Circom WASM instances.
pub trait CircomBase {
pub trait Circom {
fn init(&self, store: &mut impl AsStoreMut, sanity_check: bool) -> Result<()>;
fn func(&self, name: &str) -> &Function;
fn get_ptr_witness_buffer(&self, store: &mut impl AsStoreMut) -> Result<u32>;
Expand All @@ -38,19 +38,10 @@ pub trait CircomBase {
p_val: u32,
) -> Result<()>;
fn get_u32(&self, store: &mut impl AsStoreMut, name: &str) -> Result<u32>;
// Only exists natively in Circom2, hardcoded for Circom
fn get_version(&self, store: &mut impl AsStoreMut) -> Result<u32>;
}

/// Extended trait for working with Circom-specific features.
pub trait Circom {
fn get_fr_len(&self, store: &mut impl AsStoreMut) -> Result<u32>;
fn get_ptr_raw_prime(&self, store: &mut impl AsStoreMut) -> Result<u32>;
}

/// Extended trait for Circom version 2 specific functionalities.
#[cfg(feature = "circom-2")]
pub trait Circom2 {
fn get_field_num_len32(&self, store: &mut impl AsStoreMut) -> Result<u32>;
fn get_raw_prime(&self, store: &mut impl AsStoreMut) -> Result<()>;
fn read_shared_rw_memory(&self, store: &mut impl AsStoreMut, i: u32) -> Result<u32>;
Expand All @@ -67,63 +58,6 @@ pub trait Circom2 {
}

impl Circom for Wasm {
fn get_fr_len(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getFrLen")
}

fn get_ptr_raw_prime(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getPRawPrime")
}
}

#[cfg(feature = "circom-2")]
impl Circom2 for Wasm {
fn get_field_num_len32(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getFieldNumLen32")
}

fn get_raw_prime(&self, store: &mut impl AsStoreMut) -> Result<()> {
let func = self.func("getRawPrime");
func.call(store, &[])?;
Ok(())
}

fn read_shared_rw_memory(&self, store: &mut impl AsStoreMut, i: u32) -> Result<u32> {
let func = self.func("readSharedRWMemory");
let result = func.call(store, &[i.into()])?;
Ok(result[0].unwrap_i32() as u32)
}

fn write_shared_rw_memory(&self, store: &mut impl AsStoreMut, i: u32, v: u32) -> Result<()> {
let func = self.func("writeSharedRWMemory");
func.call(store, &[i.into(), v.into()])?;
Ok(())
}

fn set_input_signal(
&self,
store: &mut impl AsStoreMut,
hmsb: u32,
hlsb: u32,
pos: u32,
) -> Result<()> {
let func = self.func("setInputSignal");
func.call(store, &[hmsb.into(), hlsb.into(), pos.into()])?;
Ok(())
}

fn get_witness(&self, store: &mut impl AsStoreMut, i: u32) -> Result<()> {
let func = self.func("getWitness");
func.call(store, &[i.into()])?;
Ok(())
}

fn get_witness_size(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getWitnessSize")
}
}

impl CircomBase for Wasm {
fn init(&self, store: &mut impl AsStoreMut, sanity_check: bool) -> Result<()> {
let func = self.func("init");
func.call(store, &[Value::I32(i32::from(sanity_check))])?;
Expand Down Expand Up @@ -204,6 +138,57 @@ impl CircomBase for Wasm {
.get_function(name)
.unwrap_or_else(|_| panic!("function {} not found", name))
}
fn get_fr_len(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getFrLen")
}

fn get_ptr_raw_prime(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getPRawPrime")
}

fn get_field_num_len32(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getFieldNumLen32")
}

fn get_raw_prime(&self, store: &mut impl AsStoreMut) -> Result<()> {
let func = self.func("getRawPrime");
func.call(store, &[])?;
Ok(())
}

fn read_shared_rw_memory(&self, store: &mut impl AsStoreMut, i: u32) -> Result<u32> {
let func = self.func("readSharedRWMemory");
let result = func.call(store, &[i.into()])?;
Ok(result[0].unwrap_i32() as u32)
}

fn write_shared_rw_memory(&self, store: &mut impl AsStoreMut, i: u32, v: u32) -> Result<()> {
let func = self.func("writeSharedRWMemory");
func.call(store, &[i.into(), v.into()])?;
Ok(())
}

fn set_input_signal(
&self,
store: &mut impl AsStoreMut,
hmsb: u32,
hlsb: u32,
pos: u32,
) -> Result<()> {
let func = self.func("setInputSignal");
func.call(store, &[hmsb.into(), hlsb.into(), pos.into()])?;
Ok(())
}

fn get_witness(&self, store: &mut impl AsStoreMut, i: u32) -> Result<()> {
let func = self.func("getWitness");
func.call(store, &[i.into()])?;
Ok(())
}

fn get_witness_size(&self, store: &mut impl AsStoreMut) -> Result<u32> {
self.get_u32(store, "getWitnessSize")
}
}

impl Wasm {
Expand Down
7 changes: 1 addition & 6 deletions src/witness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ pub(super) use memory::SafeMemory;
mod circom;
mod error;

pub(super) use circom::{CircomBase, Wasm};

#[cfg(feature = "circom-2")]
pub(super) use circom::Circom2;

pub(super) use circom::Circom;
pub(super) use circom::{Circom, Wasm};

use fnv::FnvHasher;
use std::hash::Hasher;
Expand Down
Loading

0 comments on commit 637b97b

Please sign in to comment.