1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

add compile config option in zokrates_js

This commit is contained in:
dark64 2020-07-07 16:32:15 +02:00
parent 1e0919f085
commit 74de3c7c8d
5 changed files with 35 additions and 46 deletions

View file

@ -140,7 +140,7 @@ impl fmt::Display for CompileErrorInner {
}
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct CompileConfig {
is_release: bool,
}

16
zokrates_js/Cargo.lock generated
View file

@ -273,7 +273,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50c052fa6d4c2f12305ec364bfb8ef884836f3f61ea015b202372ff996d1ac4b"
dependencies = [
"num-bigint 0.2.6",
"num-bigint",
"num-integer",
"num-traits 0.2.12",
"proc-macro2 1.0.18",
@ -481,16 +481,6 @@ dependencies = [
"num-traits 0.2.12",
]
[[package]]
name = "num-bigint"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1"
dependencies = [
"num-integer",
"num-traits 0.2.12",
]
[[package]]
name = "num-bigint"
version = "0.2.6"
@ -1095,7 +1085,7 @@ dependencies = [
"hex",
"lazy_static",
"num",
"num-bigint 0.1.44",
"num-bigint",
"pairing_ce",
"rand",
"reduce",
@ -1126,7 +1116,7 @@ dependencies = [
"bellman_ce",
"bincode 0.8.0",
"lazy_static",
"num-bigint 0.2.6",
"num-bigint",
"num-integer",
"num-traits 0.2.12",
"serde",

View file

@ -4,6 +4,10 @@ declare module 'zokrates-js' {
export type G2Affine = [G1Affine, G1Affine];
export type ProvingKey = Uint8Array;
export interface CompileConfig {
is_release: boolean
}
export interface VerificationKey {
alpha: G1Affine,
beta: G2Affine,
@ -49,7 +53,7 @@ declare module 'zokrates-js' {
export type ResolveCallback = (location: string, path: string) => ResolverResult;
export interface ZoKratesProvider {
compile(source: string, location: string, callback: ResolveCallback): CompilationArtifacts;
compile(source: string, location: string, callback: ResolveCallback, config?: CompileConfig): CompilationArtifacts;
setup(program: Uint8Array): SetupKeypair;
computeWitness(artifacts: CompilationArtifacts, args: any[]): ComputationResult;
exportSolidityVerifier(verifyingKey: VerificationKey, abi: SolidityAbi): string;

View file

@ -3,47 +3,31 @@ import stdlib from './stdlib.json';
const initialize = async () => {
const EXTENSION_ZOK = '.zok';
const RESERVED_PATHS = [
'ecc/',
'signature/',
'hashes/',
'utils/'
];
// load web assembly module
const zokrates = await import('./pkg/index.js');
const resolveModule = (currentLocation, importLocation, callback) => {
if (isReserved(currentLocation) || isReserved(importLocation)) {
return resolveFromStandardLibrary(currentLocation, importLocation);
}
return callback(currentLocation, importLocation);
}
const isReserved = (path) => RESERVED_PATHS.some(p => path.startsWith(p));
const resolveFromStandardLibrary = (currentLocation, importLocation) => {
let key = appendExtension(getAbsolutePath(currentLocation, importLocation), EXTENSION_ZOK);
const resolveFromStdlib = (currentLocation, importLocation) => {
let key = appendExtension(getAbsolutePath(currentLocation, importLocation), '.zok');
let source = stdlib[key];
return source ? { source, location: key } : null;
}
return {
compile: (source, location, callback) => {
let result = zokrates.compile(source, location, (currentLocation, importLocation) =>
resolveModule(currentLocation, importLocation, callback)
);
compile: (source, location, callback, config) => {
let importCallback = (currentLocation, importLocation) => {
return resolveFromStdlib(currentLocation, importLocation) || callback(currentLocation, importLocation);
};
const { program, abi } = zokrates.compile(source, location, importCallback, config);
return {
program: Array.from(result.program),
abi: result.abi
program: Array.from(program),
abi
}
},
setup: (program) => {
let result = zokrates.setup(program);
const { vk, pk } = zokrates.setup(program);
return {
vk: result.vk,
pk: Array.from(result.pk)
vk,
pk: Array.from(pk)
};
},
computeWitness: (artifacts, args) => {

View file

@ -10,8 +10,8 @@ use zokrates_core::compile::{
};
use zokrates_core::imports::Error;
use zokrates_core::ir;
use zokrates_core::proof_system::{ProofSystem, SolidityAbi};
use zokrates_core::proof_system::bellman::groth16::G16;
use zokrates_core::proof_system::{ProofSystem, SolidityAbi};
use zokrates_core::typed_absy::abi::Abi;
use zokrates_core::typed_absy::types::Signature;
use zokrates_field::Bn128Field;
@ -77,7 +77,7 @@ impl<'a> Resolver<Error> for JsResolver<'a> {
)
.map_err(|_| {
Error::new(format!(
"Error thrown in callback: could not resolve {}",
"Error thrown in JS callback: could not resolve {}",
import_location.display()
))
})?;
@ -99,15 +99,26 @@ pub fn compile(
source: JsValue,
location: JsValue,
resolve: &js_sys::Function,
config: JsValue,
) -> Result<JsValue, JsValue> {
let fmt_error = |e: &CompileError| format!("{}:{}", e.file().display(), e.value());
let resolver = JsResolver::new(resolve);
let config: CompileConfig = {
if config.is_object() {
config
.into_serde()
.map_err(|e| JsValue::from_str(&format!("Invalid config format: {}", e)))?
} else {
CompileConfig::default()
}
};
let artifacts: CompilationArtifacts<Bn128Field> = core_compile(
source.as_string().unwrap(),
PathBuf::from(location.as_string().unwrap()),
Some(&resolver),
&CompileConfig::default().with_is_release(true),
&config,
)
.map_err(|ce| {
JsValue::from_str(&format!(