complete refactor
This commit is contained in:
parent
7c35389829
commit
ed04267a9a
100 changed files with 1146 additions and 2178 deletions
122
Cargo.lock
generated
122
Cargo.lock
generated
|
@ -1065,17 +1065,6 @@ dependencies = [
|
|||
"instant",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ff_ce"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83c0a6a2c53b0bd6d8cae976816dc8a171e0554c03f304e3dac110473e0219fd"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"hex 0.4.3",
|
||||
"rand 0.4.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ff_ce"
|
||||
version = "0.10.3"
|
||||
|
@ -1671,7 +1660,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "bfc00d65b1d29e0a067a967fcea83d8db261c149f76a557ba73f0304f01cdfde"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"ff_ce 0.10.3",
|
||||
"ff_ce",
|
||||
"rand 0.4.6",
|
||||
]
|
||||
|
||||
|
@ -2827,7 +2816,7 @@ dependencies = [
|
|||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
"zokrates_core",
|
||||
"zokrates_ast",
|
||||
"zokrates_field",
|
||||
]
|
||||
|
||||
|
@ -2835,24 +2824,62 @@ dependencies = [
|
|||
name = "zokrates_ark"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ark-bls12-377",
|
||||
"ark-bn254",
|
||||
"ark-bw6-761",
|
||||
"ark-crypto-primitives",
|
||||
"ark-ec",
|
||||
"ark-ff",
|
||||
"ark-gm17",
|
||||
"ark-groth16",
|
||||
"ark-marlin",
|
||||
"ark-poly",
|
||||
"ark-poly-commit",
|
||||
"ark-relations",
|
||||
"ark-serialize",
|
||||
"digest 0.9.0",
|
||||
"hex 0.4.3",
|
||||
"rand 0.8.5",
|
||||
"sha3 0.9.1",
|
||||
"zokrates_ast",
|
||||
"zokrates_field",
|
||||
"zokrates_interpreter",
|
||||
"zokrates_proof_systems",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_ast"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ark-bls12-377",
|
||||
"cfg-if 0.1.10",
|
||||
"csv",
|
||||
"num-bigint 0.2.6",
|
||||
"pairing_ce",
|
||||
"serde",
|
||||
"serde_cbor",
|
||||
"serde_json",
|
||||
"zokrates_embed",
|
||||
"zokrates_field",
|
||||
"zokrates_pest_ast",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_bellman"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bellman_ce",
|
||||
"getrandom",
|
||||
"hex 0.4.3",
|
||||
"pairing_ce",
|
||||
"phase2",
|
||||
"rand 0.4.6",
|
||||
"zokrates_ast",
|
||||
"zokrates_field",
|
||||
"zokrates_interpreter",
|
||||
"zokrates_proof_systems",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_cli"
|
||||
version = "0.7.13"
|
||||
|
@ -2881,10 +2908,16 @@ dependencies = [
|
|||
"tempdir",
|
||||
"typed-arena",
|
||||
"zokrates_abi",
|
||||
"zokrates_ark",
|
||||
"zokrates_ast",
|
||||
"zokrates_bellman",
|
||||
"zokrates_common",
|
||||
"zokrates_core",
|
||||
"zokrates_field",
|
||||
"zokrates_fs_resolver",
|
||||
"zokrates_interpreter",
|
||||
"zokrates_libsnark",
|
||||
"zokrates_proof_systems",
|
||||
"zokrates_solidity_test",
|
||||
]
|
||||
|
||||
|
@ -2896,46 +2929,17 @@ version = "0.1.0"
|
|||
name = "zokrates_core"
|
||||
version = "0.6.12"
|
||||
dependencies = [
|
||||
"ark-bls12-377",
|
||||
"ark-bn254",
|
||||
"ark-bw6-761",
|
||||
"ark-crypto-primitives",
|
||||
"ark-ec",
|
||||
"ark-ff",
|
||||
"ark-gm17",
|
||||
"ark-groth16",
|
||||
"ark-marlin",
|
||||
"ark-poly",
|
||||
"ark-poly-commit",
|
||||
"ark-relations",
|
||||
"ark-serialize",
|
||||
"bellman_ce",
|
||||
"cc",
|
||||
"cfg-if 0.1.10",
|
||||
"cmake",
|
||||
"csv",
|
||||
"digest 0.9.0",
|
||||
"ethabi",
|
||||
"ff_ce 0.9.0",
|
||||
"getrandom",
|
||||
"hex 0.4.3",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"num 0.1.42",
|
||||
"num-bigint 0.2.6",
|
||||
"pairing_ce",
|
||||
"phase2",
|
||||
"pretty_assertions",
|
||||
"primitive-types",
|
||||
"rand 0.4.6",
|
||||
"rand 0.8.5",
|
||||
"reduce",
|
||||
"regex 0.2.11",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha3 0.9.1",
|
||||
"typed-arena",
|
||||
"wasm-bindgen-test",
|
||||
"zokrates_ast",
|
||||
"zokrates_common",
|
||||
"zokrates_embed",
|
||||
|
@ -3007,12 +3011,25 @@ dependencies = [
|
|||
name = "zokrates_interpreter"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ark-bls12-377",
|
||||
"num 0.1.42",
|
||||
"num-bigint 0.2.6",
|
||||
"pairing_ce",
|
||||
"serde",
|
||||
"zokrates_ast",
|
||||
"zokrates_embed",
|
||||
"zokrates_field",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_libsnark"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cmake",
|
||||
"zokrates_proof_systems",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_parser"
|
||||
version = "0.2.6"
|
||||
|
@ -3034,6 +3051,21 @@ dependencies = [
|
|||
"zokrates_parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_proof_systems"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10",
|
||||
"ethabi",
|
||||
"hex 0.4.3",
|
||||
"primitive-types",
|
||||
"rand 0.4.6",
|
||||
"regex 0.2.11",
|
||||
"serde",
|
||||
"zokrates_ast",
|
||||
"zokrates_field",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_solidity_test"
|
||||
version = "0.1.0"
|
||||
|
@ -3065,10 +3097,16 @@ dependencies = [
|
|||
"serde_derive",
|
||||
"serde_json",
|
||||
"typed-arena",
|
||||
"wasm-bindgen-test",
|
||||
"zokrates_abi",
|
||||
"zokrates_ark",
|
||||
"zokrates_ast",
|
||||
"zokrates_common",
|
||||
"zokrates_core",
|
||||
"zokrates_field",
|
||||
"zokrates_fs_resolver",
|
||||
"zokrates_interpreter",
|
||||
"zokrates_proof_systems",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -15,6 +15,8 @@ members = [
|
|||
"zokrates_ast",
|
||||
"zokrates_interpreter",
|
||||
"zokrates_embed",
|
||||
"zokrates_bellman",
|
||||
"zokrates_proof_systems",
|
||||
]
|
||||
|
||||
exclude = ["zokrates_js"]
|
||||
exclude = ["zokrates_js", "zokrates_libsnark"]
|
|
@ -6,7 +6,7 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
zokrates_field = { version = "0.5", path = "../zokrates_field", default-features = false }
|
||||
zokrates_core = { version = "0.6", path = "../zokrates_core", default-features = false }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast", default-features = false }
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
|
|
|
@ -15,7 +15,7 @@ impl<T: Field> Encode<T> for Inputs<T> {
|
|||
}
|
||||
|
||||
use std::fmt;
|
||||
use zokrates_core::typed_absy::types::{ConcreteType, UBitwidth};
|
||||
use zokrates_ast::typed::types::{ConcreteType, UBitwidth};
|
||||
|
||||
use zokrates_field::Field;
|
||||
|
||||
|
@ -380,9 +380,7 @@ pub fn parse_strict_json<T: Field>(
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use zokrates_core::typed_absy::types::{
|
||||
ConcreteStructMember, ConcreteStructType, ConcreteType,
|
||||
};
|
||||
use zokrates_ast::typed::types::{ConcreteStructMember, ConcreteStructType, ConcreteType};
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -8,3 +8,27 @@ edition = "2021"
|
|||
[dependencies]
|
||||
zokrates_field = { version = "0.5", path = "../zokrates_field", default-features = false }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast", default-features = false }
|
||||
zokrates_proof_systems = { version = "0.1", path = "../zokrates_proof_systems", default-features = false }
|
||||
|
||||
ark-ff = { version = "^0.3.0", default-features = false }
|
||||
ark-ec = { version = "^0.3.0", default-features = false }
|
||||
ark-bn254 = { version = "^0.3.0", features = ["curve"], default-features = false }
|
||||
ark-bls12-377 = { version = "^0.3.0", features = ["curve"], default-features = false }
|
||||
ark-bw6-761 = { version = "^0.3.0", default-features = false }
|
||||
ark-gm17 = { version = "^0.3.0", default-features = false }
|
||||
ark-groth16 = { version = "^0.3.0", default-features = false }
|
||||
ark-serialize = { version = "^0.3.0", default-features = false }
|
||||
ark-relations = { version = "^0.3.0", default-features = false }
|
||||
ark-marlin = { git = "https://github.com/arkworks-rs/marlin", rev = "63cfd82", default-features = false }
|
||||
ark-poly = { version = "^0.3.0", default-features = false }
|
||||
ark-poly-commit = { version = "^0.3.0", default-features = false }
|
||||
ark-crypto-primitives = { version = "^0.3.0", default-features = false }
|
||||
sha3 = { version = "0.9" }
|
||||
digest = { version = "0.9" }
|
||||
rand_0_8 = { version = "0.8", package = "rand" }
|
||||
hex = "0.4.2"
|
||||
|
||||
[dev-dependencies]
|
||||
zokrates_interpreter = { version = "0.1", path = "../zokrates_interpreter" }
|
||||
|
||||
|
||||
|
|
|
@ -4,19 +4,18 @@ use ark_gm17::{
|
|||
VerifyingKey, GM17 as ArkGM17,
|
||||
};
|
||||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
|
||||
use zokrates_field::{ArkFieldExtensions, Bw6_761Field, Field};
|
||||
use zokrates_field::{ArkFieldExtensions, Field};
|
||||
|
||||
use crate::proof_system::ark::Computation;
|
||||
use crate::proof_system::ark::{parse_fr, parse_g1, parse_g2, parse_g2_fq};
|
||||
use crate::proof_system::ark::{serialization, Ark};
|
||||
use crate::proof_system::gm17::{ProofPoints, VerificationKey, GM17};
|
||||
use crate::proof_system::{Backend, NonUniversalBackend, Proof, SetupKeypair};
|
||||
use crate::proof_system::{NotBw6_761Field, Scheme};
|
||||
use ark_bw6_761::BW6_761;
|
||||
use crate::Computation;
|
||||
use crate::{parse_fr, parse_g1, parse_g2};
|
||||
use crate::{serialization, Ark};
|
||||
use rand_0_8::{rngs::StdRng, SeedableRng};
|
||||
use zokrates_ast::ir::{ProgIterator, Statement, Witness};
|
||||
use zokrates_proof_systems::gm17::{ProofPoints, VerificationKey, GM17};
|
||||
use zokrates_proof_systems::Scheme;
|
||||
use zokrates_proof_systems::{Backend, NonUniversalBackend, Proof, SetupKeypair};
|
||||
|
||||
impl<T: Field + ArkFieldExtensions + NotBw6_761Field> NonUniversalBackend<T, GM17> for Ark {
|
||||
impl<T: Field + ArkFieldExtensions> NonUniversalBackend<T, GM17> for Ark {
|
||||
fn setup<I: IntoIterator<Item = Statement<T>>>(
|
||||
program: ProgIterator<T, I>,
|
||||
) -> SetupKeypair<<GM17 as Scheme<T>>::VerificationKey> {
|
||||
|
@ -41,7 +40,7 @@ impl<T: Field + ArkFieldExtensions + NotBw6_761Field> NonUniversalBackend<T, GM1
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions + NotBw6_761Field> Backend<T, GM17> for Ark {
|
||||
impl<T: Field + ArkFieldExtensions> Backend<T, GM17> for Ark {
|
||||
fn generate_proof<I: IntoIterator<Item = Statement<T>>>(
|
||||
program: ProgIterator<T, I>,
|
||||
witness: Witness<T>,
|
||||
|
@ -109,107 +108,108 @@ impl<T: Field + ArkFieldExtensions + NotBw6_761Field> Backend<T, GM17> for Ark {
|
|||
}
|
||||
}
|
||||
|
||||
impl NonUniversalBackend<Bw6_761Field, GM17> for Ark {
|
||||
fn setup<I: IntoIterator<Item = Statement<Bw6_761Field>>>(
|
||||
program: ProgIterator<Bw6_761Field, I>,
|
||||
) -> SetupKeypair<<GM17 as Scheme<Bw6_761Field>>::VerificationKey> {
|
||||
let computation = Computation::without_witness(program);
|
||||
// impl NonUniversalBackend<Bw6_761Field, GM17> for Ark {
|
||||
// fn setup<I: IntoIterator<Item = Statement<Bw6_761Field>>>(
|
||||
// program: ProgIterator<Bw6_761Field, I>,
|
||||
// ) -> SetupKeypair<<GM17 as Scheme<Bw6_761Field>>::VerificationKey> {
|
||||
// let computation = Computation::without_witness(program);
|
||||
|
||||
let rng = &mut StdRng::from_entropy();
|
||||
let (pk, vk) = ArkGM17::<BW6_761>::circuit_specific_setup(computation, rng).unwrap();
|
||||
// let rng = &mut StdRng::from_entropy();
|
||||
// let (pk, vk) = ArkGM17::<BW6_761>::circuit_specific_setup(computation, rng).unwrap();
|
||||
|
||||
let mut pk_vec: Vec<u8> = Vec::new();
|
||||
pk.serialize_uncompressed(&mut pk_vec).unwrap();
|
||||
// let mut pk_vec: Vec<u8> = Vec::new();
|
||||
// pk.serialize_uncompressed(&mut pk_vec).unwrap();
|
||||
|
||||
let vk = VerificationKey {
|
||||
h: parse_g2_fq::<Bw6_761Field>(&vk.h_g2),
|
||||
g_alpha: parse_g1::<Bw6_761Field>(&vk.g_alpha_g1),
|
||||
h_beta: parse_g2_fq::<Bw6_761Field>(&vk.h_beta_g2),
|
||||
g_gamma: parse_g1::<Bw6_761Field>(&vk.g_gamma_g1),
|
||||
h_gamma: parse_g2_fq::<Bw6_761Field>(&vk.h_gamma_g2),
|
||||
query: vk.query.iter().map(parse_g1::<Bw6_761Field>).collect(),
|
||||
};
|
||||
// let vk = VerificationKey {
|
||||
// h: parse_g2_fq::<Bw6_761Field>(&vk.h_g2),
|
||||
// g_alpha: parse_g1::<Bw6_761Field>(&vk.g_alpha_g1),
|
||||
// h_beta: parse_g2_fq::<Bw6_761Field>(&vk.h_beta_g2),
|
||||
// g_gamma: parse_g1::<Bw6_761Field>(&vk.g_gamma_g1),
|
||||
// h_gamma: parse_g2_fq::<Bw6_761Field>(&vk.h_gamma_g2),
|
||||
// query: vk.query.iter().map(parse_g1::<Bw6_761Field>).collect(),
|
||||
// };
|
||||
|
||||
SetupKeypair::new(vk, pk_vec)
|
||||
}
|
||||
}
|
||||
// SetupKeypair::new(vk, pk_vec)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Backend<Bw6_761Field, GM17> for Ark {
|
||||
fn generate_proof<I: IntoIterator<Item = Statement<Bw6_761Field>>>(
|
||||
program: ProgIterator<Bw6_761Field, I>,
|
||||
witness: Witness<Bw6_761Field>,
|
||||
proving_key: Vec<u8>,
|
||||
) -> Proof<Bw6_761Field, GM17> {
|
||||
let computation = Computation::with_witness(program, witness);
|
||||
// impl Backend<Bw6_761Field, GM17> for Ark {
|
||||
// fn generate_proof<I: IntoIterator<Item = Statement<Bw6_761Field>>>(
|
||||
// program: ProgIterator<Bw6_761Field, I>,
|
||||
// witness: Witness<Bw6_761Field>,
|
||||
// proving_key: Vec<u8>,
|
||||
// ) -> Proof<Bw6_761Field, GM17> {
|
||||
// let computation = Computation::with_witness(program, witness);
|
||||
|
||||
let inputs = computation
|
||||
.public_inputs_values()
|
||||
.iter()
|
||||
.map(parse_fr::<Bw6_761Field>)
|
||||
.collect::<Vec<_>>();
|
||||
// let inputs = computation
|
||||
// .public_inputs_values()
|
||||
// .iter()
|
||||
// .map(parse_fr::<Bw6_761Field>)
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
let pk =
|
||||
ProvingKey::<<Bw6_761Field as ArkFieldExtensions>::ArkEngine>::deserialize_uncompressed(
|
||||
&mut proving_key.as_slice(),
|
||||
)
|
||||
.unwrap();
|
||||
// let pk =
|
||||
// ProvingKey::<<Bw6_761Field as ArkFieldExtensions>::ArkEngine>::deserialize_uncompressed(
|
||||
// &mut proving_key.as_slice(),
|
||||
// )
|
||||
// .unwrap();
|
||||
|
||||
let rng = &mut StdRng::from_entropy();
|
||||
let proof = ArkGM17::<BW6_761>::prove(&pk, computation, rng).unwrap();
|
||||
// let rng = &mut StdRng::from_entropy();
|
||||
// let proof = ArkGM17::<BW6_761>::prove(&pk, computation, rng).unwrap();
|
||||
|
||||
let proof_points = ProofPoints {
|
||||
a: parse_g1::<Bw6_761Field>(&proof.a),
|
||||
b: parse_g2_fq::<Bw6_761Field>(&proof.b),
|
||||
c: parse_g1::<Bw6_761Field>(&proof.c),
|
||||
};
|
||||
// let proof_points = ProofPoints {
|
||||
// a: parse_g1::<Bw6_761Field>(&proof.a),
|
||||
// b: parse_g2_fq::<Bw6_761Field>(&proof.b),
|
||||
// c: parse_g1::<Bw6_761Field>(&proof.c),
|
||||
// };
|
||||
|
||||
Proof::new(proof_points, inputs)
|
||||
}
|
||||
// Proof::new(proof_points, inputs)
|
||||
// }
|
||||
|
||||
fn verify(
|
||||
vk: <GM17 as Scheme<Bw6_761Field>>::VerificationKey,
|
||||
proof: Proof<Bw6_761Field, GM17>,
|
||||
) -> bool {
|
||||
let vk = VerifyingKey {
|
||||
h_g2: serialization::to_g2_fq::<Bw6_761Field>(vk.h),
|
||||
g_alpha_g1: serialization::to_g1::<Bw6_761Field>(vk.g_alpha),
|
||||
h_beta_g2: serialization::to_g2_fq::<Bw6_761Field>(vk.h_beta),
|
||||
g_gamma_g1: serialization::to_g1::<Bw6_761Field>(vk.g_gamma),
|
||||
h_gamma_g2: serialization::to_g2_fq::<Bw6_761Field>(vk.h_gamma),
|
||||
query: vk
|
||||
.query
|
||||
.into_iter()
|
||||
.map(serialization::to_g1::<Bw6_761Field>)
|
||||
.collect(),
|
||||
};
|
||||
// fn verify(
|
||||
// vk: <GM17 as Scheme<Bw6_761Field>>::VerificationKey,
|
||||
// proof: Proof<Bw6_761Field, GM17>,
|
||||
// ) -> bool {
|
||||
// let vk = VerifyingKey {
|
||||
// h_g2: serialization::to_g2_fq::<Bw6_761Field>(vk.h),
|
||||
// g_alpha_g1: serialization::to_g1::<Bw6_761Field>(vk.g_alpha),
|
||||
// h_beta_g2: serialization::to_g2_fq::<Bw6_761Field>(vk.h_beta),
|
||||
// g_gamma_g1: serialization::to_g1::<Bw6_761Field>(vk.g_gamma),
|
||||
// h_gamma_g2: serialization::to_g2_fq::<Bw6_761Field>(vk.h_gamma),
|
||||
// query: vk
|
||||
// .query
|
||||
// .into_iter()
|
||||
// .map(serialization::to_g1::<Bw6_761Field>)
|
||||
// .collect(),
|
||||
// };
|
||||
|
||||
let ark_proof = ArkProof {
|
||||
a: serialization::to_g1::<Bw6_761Field>(proof.proof.a),
|
||||
b: serialization::to_g2_fq::<Bw6_761Field>(proof.proof.b),
|
||||
c: serialization::to_g1::<Bw6_761Field>(proof.proof.c),
|
||||
};
|
||||
// let ark_proof = ArkProof {
|
||||
// a: serialization::to_g1::<Bw6_761Field>(proof.proof.a),
|
||||
// b: serialization::to_g2_fq::<Bw6_761Field>(proof.proof.b),
|
||||
// c: serialization::to_g1::<Bw6_761Field>(proof.proof.c),
|
||||
// };
|
||||
|
||||
let pvk: PreparedVerifyingKey<<Bw6_761Field as ArkFieldExtensions>::ArkEngine> =
|
||||
prepare_verifying_key(&vk);
|
||||
// let pvk: PreparedVerifyingKey<<Bw6_761Field as ArkFieldExtensions>::ArkEngine> =
|
||||
// prepare_verifying_key(&vk);
|
||||
|
||||
let public_inputs: Vec<_> = proof
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|s| {
|
||||
Bw6_761Field::try_from_str(s.trim_start_matches("0x"), 16)
|
||||
.unwrap()
|
||||
.into_ark()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
// let public_inputs: Vec<_> = proof
|
||||
// .inputs
|
||||
// .iter()
|
||||
// .map(|s| {
|
||||
// Bw6_761Field::try_from_str(s.trim_start_matches("0x"), 16)
|
||||
// .unwrap()
|
||||
// .into_ark()
|
||||
// })
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
verify_proof(&pvk, &ark_proof, &public_inputs).unwrap()
|
||||
}
|
||||
}
|
||||
// verify_proof(&pvk, &ark_proof, &public_inputs).unwrap()
|
||||
// }
|
||||
// }
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::flat_absy::{FlatParameter, FlatVariable};
|
||||
use zokrates_ast::ir::{Interpreter, Prog, Statement};
|
||||
use zokrates_ast::flat::{Parameter, Variable};
|
||||
use zokrates_ast::ir::{Prog, Statement};
|
||||
use zokrates_interpreter::Interpreter;
|
||||
|
||||
use super::*;
|
||||
use zokrates_field::{Bls12_377Field, Bw6_761Field};
|
||||
|
@ -217,12 +217,9 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_bls12_377_field() {
|
||||
let program: Prog<Bls12_377Field> = Prog {
|
||||
arguments: vec![FlatParameter::public(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::public(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![Statement::constraint(
|
||||
FlatVariable::new(0),
|
||||
FlatVariable::public(0),
|
||||
)],
|
||||
statements: vec![Statement::constraint(Variable::new(0), Variable::public(0))],
|
||||
};
|
||||
|
||||
let keypair = <Ark as NonUniversalBackend<Bls12_377Field, GM17>>::setup(program.clone());
|
||||
|
@ -242,12 +239,9 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_bw6_761_field() {
|
||||
let program: Prog<Bw6_761Field> = Prog {
|
||||
arguments: vec![FlatParameter::public(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::public(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![Statement::constraint(
|
||||
FlatVariable::new(0),
|
||||
FlatVariable::public(0),
|
||||
)],
|
||||
statements: vec![Statement::constraint(Variable::new(0), Variable::public(0))],
|
||||
};
|
||||
|
||||
let keypair = <Ark as NonUniversalBackend<Bw6_761Field, GM17>>::setup(program.clone());
|
||||
|
|
|
@ -1,25 +1,24 @@
|
|||
use crate::proof_system::{Backend, NonUniversalBackend, NotBw6_761Field, Proof, SetupKeypair};
|
||||
use ark_crypto_primitives::SNARK;
|
||||
use ark_groth16::{
|
||||
prepare_verifying_key, verify_proof, Groth16, PreparedVerifyingKey, Proof as ArkProof,
|
||||
ProvingKey, VerifyingKey,
|
||||
};
|
||||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
|
||||
use zokrates_field::ArkFieldExtensions;
|
||||
use zokrates_field::Field;
|
||||
use zokrates_field::{ArkFieldExtensions, Bw6_761Field};
|
||||
use zokrates_proof_systems::{Backend, NonUniversalBackend, Proof, SetupKeypair};
|
||||
|
||||
use crate::proof_system::ark::Computation;
|
||||
use crate::proof_system::ark::{parse_fr, serialization, Ark};
|
||||
use crate::proof_system::ark::{parse_g1, parse_g2};
|
||||
use crate::proof_system::groth16::{ProofPoints, VerificationKey, G16};
|
||||
use crate::proof_system::Scheme;
|
||||
use ark_bw6_761::BW6_761;
|
||||
use crate::Computation;
|
||||
use crate::{parse_fr, serialization, Ark};
|
||||
use crate::{parse_g1, parse_g2};
|
||||
use rand_0_8::{rngs::StdRng, SeedableRng};
|
||||
use zokrates_ast::ir::{ProgIterator, Statement, Witness};
|
||||
use zokrates_proof_systems::groth16::{ProofPoints, VerificationKey, G16};
|
||||
use zokrates_proof_systems::Scheme;
|
||||
|
||||
const G16_WARNING: &str = "WARNING: You are using the G16 scheme which is subject to malleability. See zokrates.github.io/toolbox/proving_schemes.html#g16-malleability for implications.";
|
||||
|
||||
impl<T: Field + ArkFieldExtensions + NotBw6_761Field> Backend<T, G16> for Ark {
|
||||
impl<T: Field + ArkFieldExtensions> Backend<T, G16> for Ark {
|
||||
fn generate_proof<I: IntoIterator<Item = Statement<T>>>(
|
||||
program: ProgIterator<T, I>,
|
||||
witness: Witness<T>,
|
||||
|
@ -86,7 +85,7 @@ impl<T: Field + ArkFieldExtensions + NotBw6_761Field> Backend<T, G16> for Ark {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions + NotBw6_761Field> NonUniversalBackend<T, G16> for Ark {
|
||||
impl<T: Field + ArkFieldExtensions> NonUniversalBackend<T, G16> for Ark {
|
||||
fn setup<I: IntoIterator<Item = Statement<T>>>(
|
||||
program: ProgIterator<T, I>,
|
||||
) -> SetupKeypair<<G16 as Scheme<T>>::VerificationKey> {
|
||||
|
@ -112,108 +111,11 @@ impl<T: Field + ArkFieldExtensions + NotBw6_761Field> NonUniversalBackend<T, G16
|
|||
}
|
||||
}
|
||||
|
||||
impl Backend<Bw6_761Field, G16> for Ark {
|
||||
fn generate_proof<I: IntoIterator<Item = Statement<Bw6_761Field>>>(
|
||||
program: ProgIterator<Bw6_761Field, I>,
|
||||
witness: Witness<Bw6_761Field>,
|
||||
proving_key: Vec<u8>,
|
||||
) -> Proof<Bw6_761Field, G16> {
|
||||
println!("{}", G16_WARNING);
|
||||
|
||||
let computation = Computation::with_witness(program, witness);
|
||||
|
||||
let inputs = computation
|
||||
.public_inputs_values()
|
||||
.iter()
|
||||
.map(parse_fr::<Bw6_761Field>)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let pk =
|
||||
ProvingKey::<BW6_761>::deserialize_uncompressed(&mut proving_key.as_slice()).unwrap();
|
||||
|
||||
let rng = &mut StdRng::from_entropy();
|
||||
let proof = Groth16::<BW6_761>::prove(&pk, computation, rng).unwrap();
|
||||
|
||||
let proof_points = ProofPoints {
|
||||
a: parse_g1::<Bw6_761Field>(&proof.a),
|
||||
b: parse_g2::<Bw6_761Field>(&proof.b),
|
||||
c: parse_g1::<Bw6_761Field>(&proof.c),
|
||||
};
|
||||
|
||||
Proof::new(proof_points, inputs)
|
||||
}
|
||||
|
||||
fn verify(
|
||||
vk: <G16 as Scheme<Bw6_761Field>>::VerificationKey,
|
||||
proof: Proof<Bw6_761Field, G16>,
|
||||
) -> bool {
|
||||
let vk = VerifyingKey {
|
||||
alpha_g1: serialization::to_g1::<Bw6_761Field>(vk.alpha),
|
||||
beta_g2: serialization::to_g2::<Bw6_761Field>(vk.beta),
|
||||
gamma_g2: serialization::to_g2::<Bw6_761Field>(vk.gamma),
|
||||
delta_g2: serialization::to_g2::<Bw6_761Field>(vk.delta),
|
||||
gamma_abc_g1: vk
|
||||
.gamma_abc
|
||||
.into_iter()
|
||||
.map(serialization::to_g1::<Bw6_761Field>)
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let pvk: PreparedVerifyingKey<BW6_761> = prepare_verifying_key(&vk);
|
||||
let ark_proof = ArkProof {
|
||||
a: serialization::to_g1::<Bw6_761Field>(proof.proof.a),
|
||||
b: serialization::to_g2::<Bw6_761Field>(proof.proof.b),
|
||||
c: serialization::to_g1::<Bw6_761Field>(proof.proof.c),
|
||||
};
|
||||
|
||||
let public_inputs: Vec<_> = proof
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|s| {
|
||||
Bw6_761Field::try_from_str(s.trim_start_matches("0x"), 16)
|
||||
.unwrap()
|
||||
.into_ark()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
verify_proof(&pvk, &ark_proof, &public_inputs).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl NonUniversalBackend<Bw6_761Field, G16> for Ark {
|
||||
fn setup<I: IntoIterator<Item = Statement<Bw6_761Field>>>(
|
||||
program: ProgIterator<Bw6_761Field, I>,
|
||||
) -> SetupKeypair<<G16 as Scheme<Bw6_761Field>>::VerificationKey> {
|
||||
println!("{}", G16_WARNING);
|
||||
|
||||
let computation = Computation::without_witness(program);
|
||||
|
||||
let rng = &mut StdRng::from_entropy();
|
||||
let (pk, vk) = Groth16::<BW6_761>::circuit_specific_setup(computation, rng).unwrap();
|
||||
|
||||
let mut pk_vec: Vec<u8> = Vec::new();
|
||||
pk.serialize_uncompressed(&mut pk_vec).unwrap();
|
||||
|
||||
let vk = VerificationKey {
|
||||
alpha: parse_g1::<Bw6_761Field>(&vk.alpha_g1),
|
||||
beta: parse_g2::<Bw6_761Field>(&vk.beta_g2),
|
||||
gamma: parse_g2::<Bw6_761Field>(&vk.gamma_g2),
|
||||
delta: parse_g2::<Bw6_761Field>(&vk.delta_g2),
|
||||
gamma_abc: vk
|
||||
.gamma_abc_g1
|
||||
.iter()
|
||||
.map(parse_g1::<Bw6_761Field>)
|
||||
.collect(),
|
||||
};
|
||||
|
||||
SetupKeypair::new(vk, pk_vec)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::flat_absy::{FlatParameter, FlatVariable};
|
||||
use zokrates_ast::ir::{Interpreter, Prog, Statement};
|
||||
use zokrates_ast::flat::{Parameter, Variable};
|
||||
use zokrates_ast::ir::{Prog, Statement};
|
||||
use zokrates_interpreter::Interpreter;
|
||||
|
||||
use super::*;
|
||||
use zokrates_field::{Bls12_377Field, Bw6_761Field};
|
||||
|
@ -221,12 +123,9 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_bls12_377_field() {
|
||||
let program: Prog<Bls12_377Field> = Prog {
|
||||
arguments: vec![FlatParameter::public(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::public(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![Statement::constraint(
|
||||
FlatVariable::new(0),
|
||||
FlatVariable::public(0),
|
||||
)],
|
||||
statements: vec![Statement::constraint(Variable::new(0), Variable::public(0))],
|
||||
};
|
||||
|
||||
let keypair = <Ark as NonUniversalBackend<Bls12_377Field, G16>>::setup(program.clone());
|
||||
|
@ -249,12 +148,9 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_bw6_761_field() {
|
||||
let program: Prog<Bw6_761Field> = Prog {
|
||||
arguments: vec![FlatParameter::public(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::public(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![Statement::constraint(
|
||||
FlatVariable::new(0),
|
||||
FlatVariable::public(0),
|
||||
)],
|
||||
statements: vec![Statement::constraint(Variable::new(0), Variable::public(0))],
|
||||
};
|
||||
|
||||
let keypair = <Ark as NonUniversalBackend<Bw6_761Field, G16>>::setup(program.clone());
|
||||
|
|
|
@ -2,16 +2,22 @@ pub mod gm17;
|
|||
pub mod groth16;
|
||||
pub mod marlin;
|
||||
|
||||
use crate::flat_absy::FlatVariable;
|
||||
use ark_ec::PairingEngine;
|
||||
use ark_relations::r1cs::{
|
||||
ConstraintSynthesizer, ConstraintSystem, ConstraintSystemRef, LinearCombination,
|
||||
SynthesisError, Variable,
|
||||
SynthesisError, Variable as ArkVariable,
|
||||
};
|
||||
use std::collections::BTreeMap;
|
||||
use zokrates_ast::common::Variable;
|
||||
use zokrates_ast::ir::{CanonicalLinComb, ProgIterator, Statement, Witness};
|
||||
use zokrates_field::{ArkFieldExtensions, Field};
|
||||
|
||||
use zokrates_field::{Bls12_377Field, Bls12_381Field, Bn128Field};
|
||||
pub trait NotBw6_761Field {}
|
||||
impl NotBw6_761Field for Bls12_377Field {}
|
||||
impl NotBw6_761Field for Bls12_381Field {}
|
||||
impl NotBw6_761Field for Bn128Field {}
|
||||
|
||||
pub use self::parse::*;
|
||||
|
||||
pub struct Ark;
|
||||
|
@ -41,7 +47,7 @@ impl<T, I: IntoIterator<Item = Statement<T>>> Computation<T, I> {
|
|||
fn ark_combination<T: Field + ArkFieldExtensions>(
|
||||
l: CanonicalLinComb<T>,
|
||||
cs: &mut ConstraintSystem<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>,
|
||||
symbols: &mut BTreeMap<FlatVariable, Variable>,
|
||||
symbols: &mut BTreeMap<Variable, ArkVariable>,
|
||||
witness: &mut Witness<T>,
|
||||
) -> LinearCombination<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr> {
|
||||
l.0.into_iter()
|
||||
|
@ -72,23 +78,35 @@ fn ark_combination<T: Field + ArkFieldExtensions>(
|
|||
.fold(LinearCombination::zero(), |acc, e| acc + e)
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>> ProgIterator<T, I> {
|
||||
pub fn generate_constraints(
|
||||
impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>> Computation<T, I> {
|
||||
pub fn public_inputs_values(&self) -> Vec<<T::ArkEngine as PairingEngine>::Fr> {
|
||||
self.program
|
||||
.public_inputs(self.witness.as_ref().unwrap())
|
||||
.iter()
|
||||
.map(|v| v.clone().into_ark())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>>
|
||||
ConstraintSynthesizer<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>
|
||||
for Computation<T, I>
|
||||
{
|
||||
fn generate_constraints(
|
||||
self,
|
||||
cs: ConstraintSystemRef<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>,
|
||||
witness: Option<Witness<T>>,
|
||||
) -> Result<(), SynthesisError> {
|
||||
// mapping from IR variables
|
||||
let mut symbols = BTreeMap::new();
|
||||
|
||||
let mut witness = witness.unwrap_or_else(Witness::empty);
|
||||
let mut witness = self.witness.unwrap_or_else(Witness::empty);
|
||||
|
||||
assert!(symbols.insert(FlatVariable::one(), ConstraintSystem::<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>::one()).is_none());
|
||||
assert!(symbols.insert(Variable::one(), ConstraintSystem::<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>::one()).is_none());
|
||||
|
||||
match cs {
|
||||
ConstraintSystemRef::CS(rc) => {
|
||||
let mut cs = rc.borrow_mut();
|
||||
symbols.extend(self.arguments.iter().enumerate().map(|(_, p)| {
|
||||
symbols.extend(self.program.arguments.iter().enumerate().map(|(_, p)| {
|
||||
let wire = match p.private {
|
||||
true => cs.new_witness_variable(|| {
|
||||
Ok(witness
|
||||
|
@ -109,7 +127,7 @@ impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>> ProgIt
|
|||
(p.id, wire)
|
||||
}));
|
||||
|
||||
for statement in self.statements {
|
||||
for statement in self.program.statements {
|
||||
if let Statement::Constraint(quad, lin, _) = statement {
|
||||
let a = ark_combination(
|
||||
quad.left.clone().into_canonical(),
|
||||
|
@ -141,32 +159,10 @@ impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>> ProgIt
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>> Computation<T, I> {
|
||||
pub fn public_inputs_values(&self) -> Vec<<T::ArkEngine as PairingEngine>::Fr> {
|
||||
self.program
|
||||
.public_inputs(self.witness.as_ref().unwrap())
|
||||
.iter()
|
||||
.map(|v| v.clone().into_ark())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions, I: IntoIterator<Item = Statement<T>>>
|
||||
ConstraintSynthesizer<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>
|
||||
for Computation<T, I>
|
||||
{
|
||||
fn generate_constraints(
|
||||
self,
|
||||
cs: ConstraintSystemRef<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>,
|
||||
) -> Result<(), SynthesisError> {
|
||||
self.program.generate_constraints(cs, self.witness)
|
||||
}
|
||||
}
|
||||
|
||||
mod parse {
|
||||
use super::*;
|
||||
use crate::proof_system::{Fr, G1Affine, G2Affine, G2AffineFq};
|
||||
use ark_ff::ToBytes;
|
||||
use zokrates_proof_systems::{Fr, G1Affine, G2Affine, G2AffineFq, G2AffineFq2};
|
||||
|
||||
pub fn parse_g1<T: Field + ArkFieldExtensions>(
|
||||
e: &<T::ArkEngine as PairingEngine>::G1Affine,
|
||||
|
@ -207,7 +203,7 @@ mod parse {
|
|||
elements.push(e);
|
||||
}
|
||||
|
||||
G2Affine(
|
||||
G2Affine::Fq2(G2AffineFq2(
|
||||
(
|
||||
format!("0x{}", hex::encode(&elements[0])),
|
||||
format!("0x{}", hex::encode(&elements[1])),
|
||||
|
@ -216,12 +212,12 @@ mod parse {
|
|||
format!("0x{}", hex::encode(&elements[2])),
|
||||
format!("0x{}", hex::encode(&elements[3])),
|
||||
),
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
pub fn parse_g2_fq<T: ArkFieldExtensions>(
|
||||
e: &<T::ArkEngine as PairingEngine>::G2Affine,
|
||||
) -> G2AffineFq {
|
||||
) -> G2Affine {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
e.write(&mut bytes).unwrap();
|
||||
|
||||
|
@ -234,10 +230,10 @@ mod parse {
|
|||
x.reverse();
|
||||
y.reverse();
|
||||
|
||||
G2AffineFq(
|
||||
G2Affine::Fq(G2AffineFq(
|
||||
format!("0x{}", hex::encode(&x)),
|
||||
format!("0x{}", hex::encode(&y)),
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
pub fn parse_fr<T: ArkFieldExtensions>(e: &<T::ArkEngine as PairingEngine>::Fr) -> Fr {
|
||||
|
@ -250,10 +246,10 @@ mod parse {
|
|||
}
|
||||
|
||||
pub mod serialization {
|
||||
use crate::proof_system::{G1Affine, G2Affine, G2AffineFq};
|
||||
use ark_ec::PairingEngine;
|
||||
use ark_ff::FromBytes;
|
||||
use zokrates_field::ArkFieldExtensions;
|
||||
use zokrates_proof_systems::{G1Affine, G2Affine};
|
||||
|
||||
#[inline]
|
||||
fn decode_hex(value: String) -> Vec<u8> {
|
||||
|
@ -273,22 +269,21 @@ pub mod serialization {
|
|||
|
||||
pub fn to_g2<T: ArkFieldExtensions>(g2: G2Affine) -> <T::ArkEngine as PairingEngine>::G2Affine {
|
||||
let mut bytes = vec![];
|
||||
bytes.append(&mut decode_hex((g2.0).0));
|
||||
bytes.append(&mut decode_hex((g2.0).1));
|
||||
bytes.append(&mut decode_hex((g2.1).0));
|
||||
bytes.append(&mut decode_hex((g2.1).1));
|
||||
bytes.push(0u8); // infinity flag
|
||||
|
||||
<T::ArkEngine as PairingEngine>::G2Affine::read(&*bytes).unwrap()
|
||||
}
|
||||
|
||||
pub fn to_g2_fq<T: ArkFieldExtensions>(
|
||||
g2: G2AffineFq,
|
||||
) -> <T::ArkEngine as PairingEngine>::G2Affine {
|
||||
let mut bytes = vec![];
|
||||
bytes.append(&mut decode_hex(g2.0));
|
||||
bytes.append(&mut decode_hex(g2.1));
|
||||
bytes.push(0u8); // infinity flag
|
||||
match g2 {
|
||||
G2Affine::Fq(g2) => {
|
||||
bytes.append(&mut decode_hex(g2.0));
|
||||
bytes.append(&mut decode_hex(g2.1));
|
||||
bytes.push(0u8); // infinity flag
|
||||
}
|
||||
G2Affine::Fq2(g2) => {
|
||||
bytes.append(&mut decode_hex((g2.0).0));
|
||||
bytes.append(&mut decode_hex((g2.0).1));
|
||||
bytes.append(&mut decode_hex((g2.1).0));
|
||||
bytes.append(&mut decode_hex((g2.1).1));
|
||||
bytes.push(0u8); // infinity flag
|
||||
}
|
||||
};
|
||||
|
||||
<T::ArkEngine as PairingEngine>::G2Affine::read(&*bytes).unwrap()
|
||||
}
|
||||
|
|
|
@ -23,13 +23,13 @@ use std::marker::PhantomData;
|
|||
|
||||
use zokrates_field::{ArkFieldExtensions, Field};
|
||||
|
||||
use crate::proof_system::ark::Ark;
|
||||
use crate::proof_system::ark::Computation;
|
||||
use crate::proof_system::ark::{parse_fr, parse_g1, parse_g2, serialization};
|
||||
use crate::proof_system::marlin::{self, KZGVerifierKey, ProofPoints, VerificationKey};
|
||||
use crate::proof_system::Scheme;
|
||||
use crate::proof_system::{Backend, Proof, SetupKeypair, UniversalBackend};
|
||||
use crate::Ark;
|
||||
use crate::Computation;
|
||||
use crate::{parse_fr, parse_g1, parse_g2, serialization};
|
||||
use zokrates_ast::ir::{ProgIterator, Statement, Witness};
|
||||
use zokrates_proof_systems::marlin::{self, KZGVerifierKey, ProofPoints, VerificationKey};
|
||||
use zokrates_proof_systems::Scheme;
|
||||
use zokrates_proof_systems::{Backend, Proof, SetupKeypair, UniversalBackend};
|
||||
|
||||
const MINIMUM_CONSTRAINT_COUNT: usize = 2;
|
||||
|
||||
|
@ -386,27 +386,28 @@ impl<T: Field + ArkFieldExtensions> Backend<T, marlin::Marlin> for Ark {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::flat_absy::{FlatParameter, FlatVariable};
|
||||
use zokrates_ast::ir::{Interpreter, Prog, QuadComb, Statement};
|
||||
use zokrates_ast::flat::{Parameter, Variable};
|
||||
use zokrates_ast::ir::{Prog, QuadComb, Statement};
|
||||
use zokrates_interpreter::Interpreter;
|
||||
|
||||
use super::*;
|
||||
use crate::proof_system::scheme::Marlin;
|
||||
use zokrates_field::{Bls12_377Field, Bw6_761Field};
|
||||
use zokrates_proof_systems::Marlin;
|
||||
|
||||
#[test]
|
||||
fn verify_bls12_377_field() {
|
||||
let program: Prog<Bls12_377Field> = Prog {
|
||||
arguments: vec![FlatParameter::private(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::private(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![
|
||||
Statement::constraint(
|
||||
QuadComb::from_linear_combinations(
|
||||
FlatVariable::new(0).into(),
|
||||
FlatVariable::new(0).into(),
|
||||
Variable::new(0).into(),
|
||||
Variable::new(0).into(),
|
||||
),
|
||||
FlatVariable::new(1),
|
||||
Variable::new(1),
|
||||
),
|
||||
Statement::constraint(FlatVariable::new(1), FlatVariable::public(0)),
|
||||
Statement::constraint(Variable::new(1), Variable::public(0)),
|
||||
],
|
||||
};
|
||||
|
||||
|
@ -429,17 +430,17 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_bw6_761_field() {
|
||||
let program: Prog<Bw6_761Field> = Prog {
|
||||
arguments: vec![FlatParameter::private(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::private(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![
|
||||
Statement::constraint(
|
||||
QuadComb::from_linear_combinations(
|
||||
FlatVariable::new(0).into(),
|
||||
FlatVariable::new(0).into(),
|
||||
Variable::new(0).into(),
|
||||
Variable::new(0).into(),
|
||||
),
|
||||
FlatVariable::new(1),
|
||||
Variable::new(1),
|
||||
),
|
||||
Statement::constraint(FlatVariable::new(1), FlatVariable::public(0)),
|
||||
Statement::constraint(Variable::new(1), Variable::public(0)),
|
||||
],
|
||||
};
|
||||
|
||||
|
|
|
@ -3,7 +3,10 @@ name = "zokrates_ast"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
default = ["bellman", "ark"]
|
||||
bellman = ["zokrates_field/bellman", "pairing_ce"]
|
||||
ark = ["ark-bls12-377"]
|
||||
|
||||
[dependencies]
|
||||
zokrates_pest_ast = { version = "0.2.0", path = "../zokrates_pest_ast" }
|
||||
|
@ -14,6 +17,9 @@ csv = "1"
|
|||
serde_cbor = "0.11.2"
|
||||
num-bigint = { version = "0.2", default-features = false }
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
zokrates_embed = { version = "0.1.0", path = "../zokrates_embed" }
|
||||
pairing_ce = { version = "^0.21", optional = true }
|
||||
ark-bls12-377 = { version = "^0.3.0", features = ["curve"], default-features = false, optional = true }
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -344,7 +344,6 @@ fn flat_expression_from_vec<T: Field>(v: &[(usize, T)]) -> FlatExpression<T> {
|
|||
#[cfg(feature = "bellman")]
|
||||
pub fn sha256_round<T: Field>(
|
||||
) -> FlatFunctionIterator<T, impl IntoIterator<Item = FlatStatement<T>>> {
|
||||
use zokrates_ast::ir::RuntimeError;
|
||||
use zokrates_field::Bn128Field;
|
||||
assert_eq!(T::id(), Bn128Field::id());
|
||||
|
||||
|
@ -449,7 +448,6 @@ pub fn sha256_round<T: Field>(
|
|||
pub fn snark_verify_bls12_377<T: Field>(
|
||||
n: usize,
|
||||
) -> FlatFunctionIterator<T, impl IntoIterator<Item = FlatStatement<T>>> {
|
||||
use zokrates_ast::ir::RuntimeError;
|
||||
use zokrates_field::Bw6_761Field;
|
||||
assert_eq!(T::id(), Bw6_761Field::id());
|
||||
|
||||
|
@ -695,72 +693,73 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "bellman")]
|
||||
#[cfg(test)]
|
||||
mod sha256 {
|
||||
use super::*;
|
||||
use zokrates_ast::ir::Interpreter;
|
||||
// MOVE TO CORE
|
||||
// #[cfg(feature = "bellman")]
|
||||
// #[cfg(test)]
|
||||
// mod sha256 {
|
||||
// use super::*;
|
||||
// use crate::ir::Interpreter;
|
||||
|
||||
#[test]
|
||||
fn generate_sha256_constraints() {
|
||||
let compiled = sha256_round::<Bn128Field>();
|
||||
// #[test]
|
||||
// fn generate_sha256_constraints() {
|
||||
// let compiled = sha256_round::<Bn128Field>();
|
||||
|
||||
let compiled = compiled.collect();
|
||||
// let compiled = compiled.collect();
|
||||
|
||||
// function should have 768 inputs
|
||||
assert_eq!(compiled.arguments.len(), 768);
|
||||
// // function should have 768 inputs
|
||||
// assert_eq!(compiled.arguments.len(), 768);
|
||||
|
||||
// function should return 256 values
|
||||
assert_eq!(compiled.return_count, 256,);
|
||||
// // function should return 256 values
|
||||
// assert_eq!(compiled.return_count, 256,);
|
||||
|
||||
// directive should take 768 inputs and return n_var outputs
|
||||
let directive = compiled
|
||||
.statements
|
||||
.iter()
|
||||
.filter_map(|s| match s {
|
||||
FlatStatement::Directive(d) => Some(d.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.next()
|
||||
.unwrap();
|
||||
assert_eq!(directive.inputs.len(), 768);
|
||||
assert_eq!(directive.outputs.len(), 26935);
|
||||
// function input should be offset by variable_count
|
||||
assert_eq!(
|
||||
compiled.arguments[0].id,
|
||||
Variable::new(directive.outputs.len() + 1)
|
||||
);
|
||||
// // directive should take 768 inputs and return n_var outputs
|
||||
// let directive = compiled
|
||||
// .statements
|
||||
// .iter()
|
||||
// .filter_map(|s| match s {
|
||||
// FlatStatement::Directive(d) => Some(d.clone()),
|
||||
// _ => None,
|
||||
// })
|
||||
// .next()
|
||||
// .unwrap();
|
||||
// assert_eq!(directive.inputs.len(), 768);
|
||||
// assert_eq!(directive.outputs.len(), 26935);
|
||||
// // function input should be offset by variable_count
|
||||
// assert_eq!(
|
||||
// compiled.arguments[0].id,
|
||||
// Variable::new(directive.outputs.len() + 1)
|
||||
// );
|
||||
|
||||
// bellman variable #0: index 0 should equal 1
|
||||
assert_eq!(
|
||||
compiled.statements[1],
|
||||
FlatStatement::Condition(
|
||||
Variable::new(0).into(),
|
||||
FlatExpression::Number(Bn128Field::from(1)),
|
||||
RuntimeError::BellmanOneBinding
|
||||
)
|
||||
);
|
||||
// // bellman variable #0: index 0 should equal 1
|
||||
// assert_eq!(
|
||||
// compiled.statements[1],
|
||||
// FlatStatement::Condition(
|
||||
// Variable::new(0).into(),
|
||||
// FlatExpression::Number(Bn128Field::from(1)),
|
||||
// RuntimeError::BellmanOneBinding
|
||||
// )
|
||||
// );
|
||||
|
||||
// bellman input #0: index 1 should equal zokrates input #0: index v_count
|
||||
assert_eq!(
|
||||
compiled.statements[2],
|
||||
FlatStatement::Condition(
|
||||
Variable::new(1).into(),
|
||||
Variable::new(26936).into(),
|
||||
RuntimeError::BellmanInputBinding
|
||||
)
|
||||
);
|
||||
// // bellman input #0: index 1 should equal zokrates input #0: index v_count
|
||||
// assert_eq!(
|
||||
// compiled.statements[2],
|
||||
// FlatStatement::Condition(
|
||||
// Variable::new(1).into(),
|
||||
// Variable::new(26936).into(),
|
||||
// RuntimeError::BellmanInputBinding
|
||||
// )
|
||||
// );
|
||||
|
||||
let input: Vec<_> = (0..512)
|
||||
.map(|_| 0)
|
||||
.chain((0..256).map(|_| 1))
|
||||
.map(Bn128Field::from)
|
||||
.collect();
|
||||
// let input: Vec<_> = (0..512)
|
||||
// .map(|_| 0)
|
||||
// .chain((0..256).map(|_| 1))
|
||||
// .map(Bn128Field::from)
|
||||
// .collect();
|
||||
|
||||
let ir = zokrates_ast::ir::from_flat::from_flat(compiled);
|
||||
// let ir = zokrates_ast::ir::from_flat::from_flat(compiled);
|
||||
|
||||
let interpreter = Interpreter::default();
|
||||
interpreter.execute(ir, &input).unwrap();
|
||||
}
|
||||
}
|
||||
// let interpreter = Interpreter::default();
|
||||
// interpreter.execute(ir, &input).unwrap();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -32,6 +32,15 @@ pub enum RuntimeError {
|
|||
SelectRangeCheck,
|
||||
}
|
||||
|
||||
impl From<crate::zir::RuntimeError> for RuntimeError {
|
||||
fn from(error: crate::zir::RuntimeError) -> Self {
|
||||
match error {
|
||||
crate::zir::RuntimeError::SourceAssertion(s) => RuntimeError::SourceAssertion(s),
|
||||
crate::zir::RuntimeError::SelectRangeCheck => RuntimeError::SelectRangeCheck,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RuntimeError {
|
||||
pub fn is_malicious(&self) -> bool {
|
||||
use RuntimeError::*;
|
||||
|
|
|
@ -50,22 +50,37 @@ pub fn fold_statement<T: Field, F: Folder<T>>(
|
|||
f: &mut F,
|
||||
s: FlatStatement<T>,
|
||||
) -> Vec<FlatStatement<T>> {
|
||||
// match s {
|
||||
// Statement::Constraint(quad, lin, message) => vec![Statement::Constraint(
|
||||
// f.fold_quadratic_combination(quad),
|
||||
// f.fold_linear_combination(lin),
|
||||
// message,
|
||||
// )],
|
||||
// Statement::Directive(dir) => vec![Statement::Directive(f.fold_directive(dir))],
|
||||
// }
|
||||
unimplemented!()
|
||||
match s {
|
||||
FlatStatement::Condition(left, right, error) => vec![FlatStatement::Condition(
|
||||
f.fold_expression(left),
|
||||
f.fold_expression(right),
|
||||
error,
|
||||
)],
|
||||
FlatStatement::Definition(v, e) => vec![FlatStatement::Definition(
|
||||
f.fold_variable(v),
|
||||
f.fold_expression(e),
|
||||
)],
|
||||
FlatStatement::Directive(d) => vec![FlatStatement::Directive(f.fold_directive(d))],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fold_expression<T: Field, F: Folder<T>>(
|
||||
f: &mut F,
|
||||
e: FlatExpression<T>,
|
||||
) -> FlatExpression<T> {
|
||||
unimplemented!()
|
||||
match e {
|
||||
FlatExpression::Number(n) => FlatExpression::Number(n),
|
||||
FlatExpression::Identifier(id) => FlatExpression::Identifier(f.fold_variable(id)),
|
||||
FlatExpression::Add(box left, box right) => {
|
||||
FlatExpression::Add(box f.fold_expression(left), box f.fold_expression(right))
|
||||
}
|
||||
FlatExpression::Sub(box left, box right) => {
|
||||
FlatExpression::Sub(box f.fold_expression(left), box f.fold_expression(right))
|
||||
}
|
||||
FlatExpression::Mult(box left, box right) => {
|
||||
FlatExpression::Mult(box f.fold_expression(left), box f.fold_expression(right))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fold_directive<T: Field, F: Folder<T>>(f: &mut F, ds: FlatDirective<T>) -> FlatDirective<T> {
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use crate::ir::folder::Folder;
|
||||
use crate::ir::Directive;
|
||||
use crate::ir::Parameter;
|
||||
use crate::ir::ProgIterator;
|
||||
use crate::ir::Statement;
|
||||
use crate::ir::Variable;
|
||||
use std::collections::HashSet;
|
||||
use zokrates_ast::ir::folder::Folder;
|
||||
use zokrates_ast::ir::Directive;
|
||||
use zokrates_ast::ir::Parameter;
|
||||
use zokrates_ast::ir::ProgIterator;
|
||||
use zokrates_ast::ir::Statement;
|
||||
use zokrates_ast::ir::Variable;
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[derive(Debug)]
|
|
@ -1,7 +1,5 @@
|
|||
use crate::flat_absy::{
|
||||
FlatDirective, FlatExpression, FlatProgIterator, FlatStatement, FlatVariable,
|
||||
};
|
||||
use zokrates_ast::ir::{Directive, LinComb, ProgIterator, QuadComb, Statement};
|
||||
use crate::flat::{FlatDirective, FlatExpression, FlatProgIterator, FlatStatement, Variable};
|
||||
use crate::ir::{Directive, LinComb, ProgIterator, QuadComb, Statement};
|
||||
use zokrates_field::Field;
|
||||
|
||||
impl<T: Field> QuadComb<T> {
|
||||
|
@ -33,7 +31,7 @@ impl<T: Field> From<FlatExpression<T>> for LinComb<T> {
|
|||
fn from(flat_expression: FlatExpression<T>) -> LinComb<T> {
|
||||
match flat_expression {
|
||||
FlatExpression::Number(ref n) if *n == T::from(0) => LinComb::zero(),
|
||||
FlatExpression::Number(n) => LinComb::summand(n, FlatVariable::one()),
|
||||
FlatExpression::Number(n) => LinComb::summand(n, Variable::one()),
|
||||
FlatExpression::Identifier(id) => LinComb::from(id),
|
||||
FlatExpression::Add(box e1, box e2) => LinComb::from(e1) + LinComb::from(e2),
|
||||
FlatExpression::Sub(box e1, box e2) => LinComb::from(e1) - LinComb::from(e2),
|
||||
|
@ -48,7 +46,7 @@ impl<T: Field> From<FlatExpression<T>> for LinComb<T> {
|
|||
FlatExpression::Mult(
|
||||
box FlatExpression::Number(n1),
|
||||
box FlatExpression::Number(n2),
|
||||
) => LinComb::summand(n1 * n2, FlatVariable::one()),
|
||||
) => LinComb::summand(n1 * n2, Variable::one()),
|
||||
e => unreachable!("{}", e),
|
||||
}
|
||||
}
|
||||
|
@ -109,7 +107,7 @@ mod tests {
|
|||
fn one() {
|
||||
// 1
|
||||
let one = FlatExpression::Number(Bn128Field::from(1));
|
||||
let expected: LinComb<Bn128Field> = FlatVariable::one().into();
|
||||
let expected: LinComb<Bn128Field> = Variable::one().into();
|
||||
assert_eq!(LinComb::from(one), expected);
|
||||
}
|
||||
|
||||
|
@ -117,7 +115,7 @@ mod tests {
|
|||
fn forty_two() {
|
||||
// 42
|
||||
let one = FlatExpression::Number(Bn128Field::from(42));
|
||||
let expected: LinComb<Bn128Field> = LinComb::summand(42, FlatVariable::one());
|
||||
let expected: LinComb<Bn128Field> = LinComb::summand(42, Variable::one());
|
||||
assert_eq!(LinComb::from(one), expected);
|
||||
}
|
||||
|
||||
|
@ -125,11 +123,11 @@ mod tests {
|
|||
fn add() {
|
||||
// x + y
|
||||
let add = FlatExpression::Add(
|
||||
box FlatExpression::Identifier(FlatVariable::new(42)),
|
||||
box FlatExpression::Identifier(FlatVariable::new(21)),
|
||||
box FlatExpression::Identifier(Variable::new(42)),
|
||||
box FlatExpression::Identifier(Variable::new(21)),
|
||||
);
|
||||
let expected: LinComb<Bn128Field> =
|
||||
LinComb::summand(1, FlatVariable::new(42)) + LinComb::summand(1, FlatVariable::new(21));
|
||||
LinComb::summand(1, Variable::new(42)) + LinComb::summand(1, Variable::new(21));
|
||||
assert_eq!(LinComb::from(add), expected);
|
||||
}
|
||||
|
||||
|
@ -139,15 +137,15 @@ mod tests {
|
|||
let add = FlatExpression::Add(
|
||||
box FlatExpression::Mult(
|
||||
box FlatExpression::Number(Bn128Field::from(42)),
|
||||
box FlatExpression::Identifier(FlatVariable::new(42)),
|
||||
box FlatExpression::Identifier(Variable::new(42)),
|
||||
),
|
||||
box FlatExpression::Mult(
|
||||
box FlatExpression::Number(Bn128Field::from(21)),
|
||||
box FlatExpression::Identifier(FlatVariable::new(21)),
|
||||
box FlatExpression::Identifier(Variable::new(21)),
|
||||
),
|
||||
);
|
||||
let expected: LinComb<Bn128Field> = LinComb::summand(42, FlatVariable::new(42))
|
||||
+ LinComb::summand(21, FlatVariable::new(21));
|
||||
let expected: LinComb<Bn128Field> =
|
||||
LinComb::summand(42, Variable::new(42)) + LinComb::summand(21, Variable::new(21));
|
||||
assert_eq!(LinComb::from(add), expected);
|
||||
}
|
||||
|
||||
|
@ -156,16 +154,16 @@ mod tests {
|
|||
// x*42 + y*21
|
||||
let add = FlatExpression::Add(
|
||||
box FlatExpression::Mult(
|
||||
box FlatExpression::Identifier(FlatVariable::new(42)),
|
||||
box FlatExpression::Identifier(Variable::new(42)),
|
||||
box FlatExpression::Number(Bn128Field::from(42)),
|
||||
),
|
||||
box FlatExpression::Mult(
|
||||
box FlatExpression::Identifier(FlatVariable::new(21)),
|
||||
box FlatExpression::Identifier(Variable::new(21)),
|
||||
box FlatExpression::Number(Bn128Field::from(21)),
|
||||
),
|
||||
);
|
||||
let expected: LinComb<Bn128Field> = LinComb::summand(42, FlatVariable::new(42))
|
||||
+ LinComb::summand(21, FlatVariable::new(21));
|
||||
let expected: LinComb<Bn128Field> =
|
||||
LinComb::summand(42, Variable::new(42)) + LinComb::summand(21, Variable::new(21));
|
||||
assert_eq!(LinComb::from(add), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,10 @@ use std::fmt;
|
|||
use std::hash::Hash;
|
||||
use zokrates_field::Field;
|
||||
|
||||
mod check;
|
||||
mod expression;
|
||||
pub mod folder;
|
||||
//pub mod from_flat;
|
||||
pub mod from_flat;
|
||||
mod serialize;
|
||||
pub mod smtlib2;
|
||||
pub mod visitor;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use crate::ir::check::UnconstrainedVariableDetector;
|
||||
|
||||
use super::{ProgIterator, Statement};
|
||||
use serde_cbor::{self, StreamDeserializer};
|
||||
use std::io::{Read, Write};
|
||||
|
@ -57,7 +59,7 @@ impl<T: Field, I: IntoIterator<Item = Statement<T>>> ProgIterator<T, I> {
|
|||
/// serialize a program iterator, returning the number of constraints serialized
|
||||
/// Note that we only return constraints, not other statements such as directives
|
||||
pub fn serialize<W: Write>(self, mut w: W) -> Result<usize, DynamicError> {
|
||||
//use super::folder::Folder;
|
||||
use super::folder::Folder;
|
||||
|
||||
w.write_all(ZOKRATES_MAGIC)?;
|
||||
w.write_all(ZOKRATES_VERSION_2)?;
|
||||
|
@ -66,7 +68,7 @@ impl<T: Field, I: IntoIterator<Item = Statement<T>>> ProgIterator<T, I> {
|
|||
serde_cbor::to_writer(&mut w, &self.arguments)?;
|
||||
serde_cbor::to_writer(&mut w, &self.return_count)?;
|
||||
|
||||
//let mut unconstrained_variable_detector = UnconstrainedVariableDetector::new(&self);
|
||||
let mut unconstrained_variable_detector = UnconstrainedVariableDetector::new(&self);
|
||||
|
||||
let statements = self.statements.into_iter();
|
||||
|
||||
|
@ -75,17 +77,16 @@ impl<T: Field, I: IntoIterator<Item = Statement<T>>> ProgIterator<T, I> {
|
|||
if matches!(s, Statement::Constraint(..)) {
|
||||
count += 1;
|
||||
}
|
||||
//let s = unconstrained_variable_detector.fold_statement(s);
|
||||
//for s in s {
|
||||
serde_cbor::to_writer(&mut w, &s)?;
|
||||
//}
|
||||
let s = unconstrained_variable_detector.fold_statement(s);
|
||||
for s in s {
|
||||
serde_cbor::to_writer(&mut w, &s)?;
|
||||
}
|
||||
}
|
||||
|
||||
// unconstrained_variable_detector
|
||||
// .finalize()
|
||||
// .map(|_| count)
|
||||
// .map_err(|count| format!("Error: Found {} unconstrained variable(s)", count).into())
|
||||
Ok(count)
|
||||
unconstrained_variable_detector
|
||||
.finalize()
|
||||
.map(|_| count)
|
||||
.map_err(|count| format!("Error: Found {} unconstrained variable(s)", count).into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -85,12 +85,6 @@ pub struct TypedProgram<'ast, T> {
|
|||
pub main: OwnedTypedModuleId,
|
||||
}
|
||||
|
||||
impl<'ast, T> TypedProgram<'ast, T> {
|
||||
pub fn main_function(&self) -> TypedFunction<'ast, T> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T: Field> TypedProgram<'ast, T> {
|
||||
pub fn abi(&self) -> Abi {
|
||||
let main = &self.modules[&self.main]
|
||||
|
|
|
@ -8,7 +8,6 @@ pub struct GParameter<'ast, S> {
|
|||
pub private: bool,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl<'ast, S> From<GVariable<'ast, S>> for GParameter<'ast, S> {
|
||||
fn from(v: GVariable<'ast, S>) -> Self {
|
||||
GParameter {
|
||||
|
|
|
@ -54,11 +54,6 @@ impl<'ast, S: Clone> GVariable<'ast, S> {
|
|||
Self::with_id_and_type(id, GType::uint(bitwidth))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn field_array<I: Into<Identifier<'ast>>>(id: I, size: S) -> Self {
|
||||
Self::array(id, GType::FieldElement, size)
|
||||
}
|
||||
|
||||
pub fn array<I: Into<Identifier<'ast>>, U: Into<S>>(id: I, ty: GType<S>, size: U) -> Self {
|
||||
Self::with_id_and_type(id, GType::array((ty, size.into())))
|
||||
}
|
||||
|
|
|
@ -52,7 +52,6 @@ pub trait NodeValue: fmt::Display + fmt::Debug + Sized + PartialEq {
|
|||
Node::new(start, end, self)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn mock(self) -> Node<Self> {
|
||||
Node::new(Position::mock(), Position::mock(), self)
|
||||
}
|
||||
|
|
|
@ -100,6 +100,12 @@ impl fmt::Display for RuntimeError {
|
|||
}
|
||||
}
|
||||
|
||||
impl RuntimeError {
|
||||
pub fn mock() -> Self {
|
||||
RuntimeError::SourceAssertion(String::default())
|
||||
}
|
||||
}
|
||||
|
||||
/// A statement in a `ZirFunction`
|
||||
#[derive(Clone, PartialEq, Hash, Eq, Debug)]
|
||||
pub enum ZirStatement<'ast, T> {
|
||||
|
|
|
@ -4,12 +4,14 @@ use crate::zir::BooleanExpression;
|
|||
use zokrates_field::Field;
|
||||
|
||||
impl<'ast, T: Field> UExpression<'ast, T> {
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn add(self, other: Self) -> UExpression<'ast, T> {
|
||||
let bitwidth = self.bitwidth;
|
||||
assert_eq!(bitwidth, other.bitwidth);
|
||||
UExpressionInner::Add(box self, box other).annotate(bitwidth)
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn sub(self, other: Self) -> UExpression<'ast, T> {
|
||||
let bitwidth = self.bitwidth;
|
||||
assert_eq!(bitwidth, other.bitwidth);
|
||||
|
@ -27,12 +29,14 @@ impl<'ast, T: Field> UExpression<'ast, T> {
|
|||
UExpressionInner::Mult(box self, box other).annotate(bitwidth)
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn div(self, other: Self) -> UExpression<'ast, T> {
|
||||
let bitwidth = self.bitwidth;
|
||||
assert_eq!(bitwidth, other.bitwidth);
|
||||
UExpressionInner::Div(box self, box other).annotate(bitwidth)
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn rem(self, other: Self) -> UExpression<'ast, T> {
|
||||
let bitwidth = self.bitwidth;
|
||||
assert_eq!(bitwidth, other.bitwidth);
|
||||
|
@ -45,6 +49,7 @@ impl<'ast, T: Field> UExpression<'ast, T> {
|
|||
UExpressionInner::Xor(box self, box other).annotate(bitwidth)
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn not(self) -> UExpression<'ast, T> {
|
||||
let bitwidth = self.bitwidth;
|
||||
UExpressionInner::Not(box self).annotate(bitwidth)
|
||||
|
|
28
zokrates_bellman/Cargo.toml
Normal file
28
zokrates_bellman/Cargo.toml
Normal file
|
@ -0,0 +1,28 @@
|
|||
[package]
|
||||
name = "zokrates_bellman"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
wasm = ["bellman/nolog", "bellman/wasm"]
|
||||
multicore = ["bellman/multicore", "phase2/multicore"]
|
||||
|
||||
[dependencies]
|
||||
zokrates_field = { version = "0.5", path = "../zokrates_field", default-features = false }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast", default-features = false }
|
||||
zokrates_proof_systems = { version = "0.1", path = "../zokrates_proof_systems", default-features = false }
|
||||
|
||||
bellman = { package = "bellman_ce", version = "^0.3", default-features = false }
|
||||
pairing = { package = "pairing_ce", version = "^0.21" }
|
||||
phase2 = { git = "https://github.com/Zokrates/phase2", default-features = false }
|
||||
rand_0_4 = { version = "0.4", package = "rand" }#
|
||||
getrandom = { version = "0.2", features = ["js", "wasm-bindgen"] }
|
||||
hex = "0.4.2"
|
||||
|
||||
[dev-dependencies]
|
||||
zokrates_interpreter = { version = "0.1", path = "../zokrates_interpreter" }
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -4,19 +4,19 @@ use bellman::groth16::{
|
|||
};
|
||||
use pairing::{ff::to_hex, CurveAffine, Engine};
|
||||
|
||||
use crate::proof_system::{Backend, MpcBackend, NonUniversalBackend, Proof, SetupKeypair};
|
||||
use zokrates_field::BellmanFieldExtensions;
|
||||
use zokrates_field::Field;
|
||||
use zokrates_proof_systems::{Backend, MpcBackend, NonUniversalBackend, Proof, SetupKeypair};
|
||||
|
||||
use crate::proof_system::bellman::Bellman;
|
||||
use crate::proof_system::bellman::Computation;
|
||||
use crate::proof_system::bellman::{parse_g1, parse_g2};
|
||||
use crate::proof_system::groth16::{ProofPoints, VerificationKey, G16};
|
||||
use crate::proof_system::Scheme;
|
||||
use crate::Bellman;
|
||||
use crate::Computation;
|
||||
use crate::{parse_g1, parse_g2};
|
||||
use phase2::MPCParameters;
|
||||
use rand_0_4::Rng;
|
||||
use std::io::{Read, Write};
|
||||
use zokrates_ast::ir::{ProgIterator, Statement, Witness};
|
||||
use zokrates_proof_systems::groth16::{ProofPoints, VerificationKey, G16};
|
||||
use zokrates_proof_systems::Scheme;
|
||||
|
||||
const G16_WARNING: &str = "WARNING: You are using the G16 scheme which is subject to malleability. See zokrates.github.io/toolbox/proving_schemes.html#g16-malleability for implications.";
|
||||
|
||||
|
@ -157,8 +157,8 @@ impl<T: Field + BellmanFieldExtensions> MpcBackend<T, G16> for Bellman {
|
|||
|
||||
pub mod serialization {
|
||||
use super::*;
|
||||
use crate::proof_system::{G1Affine, G2Affine};
|
||||
use pairing::from_hex;
|
||||
use zokrates_proof_systems::{G1Affine, G2Affine};
|
||||
|
||||
pub fn parameters_to_verification_key<T: Field + BellmanFieldExtensions>(
|
||||
parameters: &Parameters<T::BellmanEngine>,
|
||||
|
@ -188,9 +188,14 @@ pub mod serialization {
|
|||
pub fn to_g2<T: BellmanFieldExtensions>(
|
||||
g2: G2Affine,
|
||||
) -> <T::BellmanEngine as Engine>::G2Affine {
|
||||
let x = T::new_fq2(&(g2.0).0, &(g2.0).1);
|
||||
let y = T::new_fq2(&(g2.1).0, &(g2.1).1);
|
||||
<T::BellmanEngine as Engine>::G2Affine::from_xy_unchecked(x, y)
|
||||
match g2 {
|
||||
G2Affine::Fq2(g2) => {
|
||||
let x = T::new_fq2(&(g2.0).0, &(g2.0).1);
|
||||
let y = T::new_fq2(&(g2.1).0, &(g2.1).1);
|
||||
<T::BellmanEngine as Engine>::G2Affine::from_xy_unchecked(x, y)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,7 +205,7 @@ mod tests {
|
|||
use zokrates_interpreter::Interpreter;
|
||||
|
||||
use super::*;
|
||||
use crate::flat_absy::{Parameter, Variable};
|
||||
use zokrates_ast::common::{Parameter, Variable};
|
||||
use zokrates_ast::ir::{Prog, Statement};
|
||||
|
||||
#[test]
|
|
@ -83,8 +83,10 @@ fn bellman_combination<T: BellmanFieldExtensions, CS: ConstraintSystem<T::Bellma
|
|||
.fold(LinearCombination::zero(), |acc, e| acc + e)
|
||||
}
|
||||
|
||||
impl<T: BellmanFieldExtensions + Field, I: IntoIterator<Item = Statement<T>>> Computation<T, I> {
|
||||
pub fn synthesize<CS: ConstraintSystem<T::BellmanEngine>>(
|
||||
impl<T: BellmanFieldExtensions + Field, I: IntoIterator<Item = Statement<T>>>
|
||||
Circuit<T::BellmanEngine> for Computation<T, I>
|
||||
{
|
||||
fn synthesize<CS: ConstraintSystem<T::BellmanEngine>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
) -> Result<(), SynthesisError> {
|
||||
|
@ -95,7 +97,7 @@ impl<T: BellmanFieldExtensions + Field, I: IntoIterator<Item = Statement<T>>> Co
|
|||
|
||||
assert!(symbols.insert(Variable::one(), CS::one()).is_none());
|
||||
|
||||
symbols.extend(self.arguments.iter().enumerate().map(|(index, p)| {
|
||||
symbols.extend(self.program.arguments.iter().enumerate().map(|(index, p)| {
|
||||
let wire = match p.private {
|
||||
true => cs.alloc(
|
||||
|| format!("PRIVATE_INPUT_{}", index),
|
||||
|
@ -122,7 +124,7 @@ impl<T: BellmanFieldExtensions + Field, I: IntoIterator<Item = Statement<T>>> Co
|
|||
(p.id, wire)
|
||||
}));
|
||||
|
||||
for statement in self.statements {
|
||||
for statement in self.program.statements {
|
||||
if let Statement::Constraint(quad, lin, _) = statement {
|
||||
let a = &bellman_combination(
|
||||
quad.left.into_canonical(),
|
||||
|
@ -193,21 +195,10 @@ impl<T: BellmanFieldExtensions + Field, I: IntoIterator<Item = Statement<T>>> Co
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: BellmanFieldExtensions + Field, I: IntoIterator<Item = Statement<T>>>
|
||||
Circuit<T::BellmanEngine> for Computation<T, I>
|
||||
{
|
||||
fn synthesize<CS: ConstraintSystem<T::BellmanEngine>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
) -> Result<(), SynthesisError> {
|
||||
self.program.synthesize(cs, self.witness)
|
||||
}
|
||||
}
|
||||
|
||||
mod parse {
|
||||
use super::*;
|
||||
use crate::proof_system::{G1Affine, G2Affine};
|
||||
use pairing_ce::CurveAffine;
|
||||
use pairing::CurveAffine;
|
||||
use zokrates_proof_systems::{G1Affine, G2Affine, G2AffineFq2};
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
let mut hex = hex::encode(bytes);
|
||||
|
@ -240,7 +231,7 @@ mod parse {
|
|||
let y1 = to_hex(iter.next().unwrap());
|
||||
let y0 = to_hex(iter.next().unwrap());
|
||||
|
||||
G2Affine((x0, x1), (y0, y1))
|
||||
G2Affine::Fq2(G2AffineFq2((x0, x1), (y0, y1)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,7 +244,7 @@ mod tests {
|
|||
|
||||
mod prove {
|
||||
use super::*;
|
||||
use crate::flat_absy::Parameter;
|
||||
use zokrates_ast::flat::Parameter;
|
||||
use zokrates_ast::ir::Prog;
|
||||
|
||||
#[test]
|
|
@ -7,9 +7,9 @@ edition = "2018"
|
|||
|
||||
[features]
|
||||
default = ["bellman", "ark"]
|
||||
libsnark = ["zokrates_core/libsnark", "zokrates_common/libsnark"]
|
||||
bellman = ["zokrates_core/bellman", "zokrates_common/bellman"]
|
||||
ark = ["zokrates_core/ark", "zokrates_common/ark"]
|
||||
libsnark = ["zokrates_libsnark"]
|
||||
bellman = ["zokrates_bellman", "zokrates_core/bellman"]
|
||||
ark = ["zokrates_ark", "zokrates_core/ark"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
|
@ -21,6 +21,8 @@ regex = "0.2"
|
|||
zokrates_field = { version = "0.5", path = "../zokrates_field", default-features = false }
|
||||
zokrates_abi = { version = "0.1", path = "../zokrates_abi" }
|
||||
zokrates_core = { version = "0.6", path = "../zokrates_core", default-features = false }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast", default-features = false }
|
||||
zokrates_interpreter = { version = "0.1", path = "../zokrates_interpreter", default-features = false }
|
||||
typed-arena = "1.4.1"
|
||||
zokrates_fs_resolver = { version = "0.5", path = "../zokrates_fs_resolver"}
|
||||
zokrates_common = { version = "0.1", path = "../zokrates_common" }
|
||||
|
@ -35,6 +37,12 @@ hex = "0.3.1"
|
|||
blake2 = "0.8.1"
|
||||
sha2 = "0.10.0"
|
||||
|
||||
# Backends
|
||||
zokrates_proof_systems = { version = "0.1", path = "../zokrates_proof_systems", default-features = false }
|
||||
zokrates_libsnark = { version = "0.1", path = "../zokrates_libsnark", default-features = false, optional = true }
|
||||
zokrates_ark = { version = "0.1", path = "../zokrates_ark", default-features = false, optional = true }
|
||||
zokrates_bellman = { version = "0.1", path = "../zokrates_bellman", default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
glob = "0.2.11"
|
||||
assert_cli = "0.5"
|
||||
|
|
|
@ -122,7 +122,6 @@ mod tests {
|
|||
use std::string::String;
|
||||
use typed_arena::Arena;
|
||||
use zokrates_core::compile::{compile, CompilationArtifacts, CompileConfig};
|
||||
use zokrates_core::ir;
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_fs_resolver::FileSystemResolver;
|
||||
|
||||
|
@ -217,7 +216,7 @@ mod tests {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let interpreter = ir::Interpreter::default();
|
||||
let interpreter = zokrates_interpreter::Interpreter::default();
|
||||
|
||||
let _ = interpreter
|
||||
.execute(artifacts.prog(), &[Bn128Field::from(0)])
|
||||
|
@ -257,7 +256,7 @@ mod tests {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let interpreter = ir::Interpreter::default();
|
||||
let interpreter = zokrates_interpreter::Interpreter::default();
|
||||
|
||||
let res = interpreter.execute(artifacts.prog(), &[Bn128Field::from(0)]);
|
||||
|
||||
|
|
|
@ -5,10 +5,11 @@ use std::fs::File;
|
|||
use std::io::{stdin, BufReader, BufWriter, Read};
|
||||
use std::path::Path;
|
||||
use zokrates_abi::Encode;
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
use zokrates_core::typed_absy::abi::Abi;
|
||||
use zokrates_core::typed_absy::types::{ConcreteSignature, ConcreteType};
|
||||
use zokrates_ast::ir::{self, ProgEnum};
|
||||
use zokrates_ast::typed::{
|
||||
abi::Abi,
|
||||
types::{ConcreteSignature, ConcreteType},
|
||||
};
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
|
@ -156,7 +157,7 @@ fn cli_compute<T: Field, I: Iterator<Item = ir::Statement<T>>>(
|
|||
}
|
||||
.map_err(|e| format!("Could not parse argument: {}", e))?;
|
||||
|
||||
let interpreter = ir::Interpreter::default();
|
||||
let interpreter = zokrates_interpreter::Interpreter::default();
|
||||
|
||||
let witness = interpreter
|
||||
.execute(ir_prog, &arguments.encode())
|
||||
|
|
|
@ -6,8 +6,8 @@ use std::io::{BufReader, BufWriter, Write};
|
|||
use std::path::Path;
|
||||
use zokrates_common::constants;
|
||||
use zokrates_common::helpers::{CurveParameter, SchemeParameter};
|
||||
use zokrates_core::proof_system::*;
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_proof_systems::*;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("export-verifier")
|
||||
|
|
|
@ -4,19 +4,18 @@ use std::convert::TryFrom;
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, Read, Write};
|
||||
use std::path::Path;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_ark::Ark;
|
||||
use zokrates_ast::ir::{self, ProgEnum};
|
||||
#[cfg(feature = "bellman")]
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_common::constants;
|
||||
use zokrates_common::helpers::*;
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_core::proof_system::ark::Ark;
|
||||
#[cfg(feature = "bellman")]
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
#[cfg(feature = "libsnark")]
|
||||
use zokrates_core::proof_system::libsnark::Libsnark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_core::proof_system::*;
|
||||
use zokrates_field::Field;
|
||||
#[cfg(feature = "libsnark")]
|
||||
use zokrates_libsnark::Libsnark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_proof_systems::*;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("generate-proof")
|
||||
|
|
|
@ -3,9 +3,7 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, Write};
|
||||
use std::path::Path;
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::smtlib2::SMTLib2Display;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
use zokrates_ast::ir::{self, smtlib2::SMTLib2Display, ProgEnum};
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
|
|
|
@ -3,8 +3,7 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, BufWriter, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
use zokrates_ast::ir::{self, ProgEnum};
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
|
|
|
@ -3,10 +3,10 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, BufWriter};
|
||||
use std::path::Path;
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_common::constants::{BLS12_381, BN128};
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
use zokrates_core::proof_system::{MpcBackend, MpcScheme, G16};
|
||||
use zokrates_field::{BellmanFieldExtensions, Bls12_381Field, Bn128Field, Field};
|
||||
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("beacon")
|
||||
|
|
|
@ -3,10 +3,10 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, BufWriter};
|
||||
use std::path::Path;
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_common::constants::{BLS12_381, BN128};
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
use zokrates_core::proof_system::{MpcBackend, MpcScheme, G16};
|
||||
use zokrates_field::{BellmanFieldExtensions, Bls12_381Field, Bn128Field, Field};
|
||||
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("contribute")
|
||||
|
|
|
@ -3,10 +3,10 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, Write};
|
||||
use std::path::Path;
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_common::constants::{BLS12_381, BN128};
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
use zokrates_core::proof_system::{MpcBackend, MpcScheme, G16};
|
||||
use zokrates_field::{BellmanFieldExtensions, Bls12_381Field, Bn128Field, Field};
|
||||
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("export")
|
||||
|
|
|
@ -3,11 +3,10 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, BufWriter};
|
||||
use std::path::Path;
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
use zokrates_core::proof_system::{MpcBackend, MpcScheme, G16};
|
||||
use zokrates_ast::ir::{self, ProgEnum};
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_field::{BellmanFieldExtensions, Field};
|
||||
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("init")
|
||||
|
|
|
@ -3,11 +3,10 @@ use clap::{App, Arg, ArgMatches, SubCommand};
|
|||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::Path;
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
use zokrates_core::proof_system::{MpcBackend, MpcScheme, G16};
|
||||
use zokrates_ast::ir::{self, ProgEnum};
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_field::{BellmanFieldExtensions, Field};
|
||||
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("verify")
|
||||
|
|
|
@ -5,10 +5,10 @@ use std::fs::File;
|
|||
use std::path::Path;
|
||||
use zokrates_common::constants as common_constants;
|
||||
use zokrates_common::helpers::{CurveParameter, SchemeParameter};
|
||||
use zokrates_core::proof_system::{
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_proof_systems::{
|
||||
Marlin, Proof, SolidityCompatibleField, SolidityCompatibleScheme, G16, GM17, PGHR13,
|
||||
};
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("print-proof")
|
||||
|
|
|
@ -4,19 +4,18 @@ use std::convert::TryFrom;
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, Write};
|
||||
use std::path::Path;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_ark::Ark;
|
||||
use zokrates_ast::ir::{self, ProgEnum};
|
||||
#[cfg(feature = "bellman")]
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_common::constants;
|
||||
use zokrates_common::helpers::*;
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::ir::ProgEnum;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_core::proof_system::ark::Ark;
|
||||
#[cfg(feature = "bellman")]
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
#[cfg(feature = "libsnark")]
|
||||
use zokrates_core::proof_system::libsnark::Libsnark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_core::proof_system::*;
|
||||
use zokrates_field::Field;
|
||||
#[cfg(feature = "libsnark")]
|
||||
use zokrates_libsnark::Libsnark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_proof_systems::*;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("setup")
|
||||
|
|
|
@ -4,13 +4,13 @@ use std::convert::TryFrom;
|
|||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_ark::Ark;
|
||||
use zokrates_common::constants;
|
||||
use zokrates_common::helpers::*;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_core::proof_system::ark::Ark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_core::proof_system::*;
|
||||
use zokrates_field::{Bls12_377Field, Bls12_381Field, Bn128Field, Bw6_761Field, Field};
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_proof_systems::*;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("universal-setup")
|
||||
|
|
|
@ -4,17 +4,17 @@ use std::convert::TryFrom;
|
|||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::Path;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_ark::Ark;
|
||||
#[cfg(feature = "bellman")]
|
||||
use zokrates_bellman::Bellman;
|
||||
use zokrates_common::constants;
|
||||
use zokrates_common::helpers::*;
|
||||
#[cfg(feature = "ark")]
|
||||
use zokrates_core::proof_system::ark::Ark;
|
||||
#[cfg(feature = "bellman")]
|
||||
use zokrates_core::proof_system::bellman::Bellman;
|
||||
#[cfg(feature = "libsnark")]
|
||||
use zokrates_core::proof_system::libsnark::Libsnark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_core::proof_system::*;
|
||||
use zokrates_field::{Bls12_377Field, Bls12_381Field, Bn128Field, Bw6_761Field, Field};
|
||||
#[cfg(feature = "libsnark")]
|
||||
use zokrates_libsnark::Libsnark;
|
||||
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
|
||||
use zokrates_proof_systems::*;
|
||||
|
||||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("verify")
|
||||
|
|
|
@ -19,12 +19,12 @@ mod integration {
|
|||
use std::path::Path;
|
||||
use tempdir::TempDir;
|
||||
use zokrates_abi::{parse_strict, Encode};
|
||||
use zokrates_core::proof_system::{
|
||||
use zokrates_ast::typed::abi::Abi;
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_proof_systems::{
|
||||
to_token::ToToken, Marlin, Proof, SolidityCompatibleScheme, G16, GM17, PGHR13,
|
||||
SOLIDITY_G2_ADDITION_LIB,
|
||||
};
|
||||
use zokrates_core::typed_absy::abi::Abi;
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
macro_rules! map(
|
||||
{
|
||||
|
|
|
@ -5,15 +5,12 @@ edition = "2018"
|
|||
authors = ["Jacob Eberhardt <jacob.eberhardt@tu-berlin.de>", "Dennis Kuhnert <mail@kyroy.com>"]
|
||||
repository = "https://github.com/Zokrates/ZoKrates"
|
||||
readme = "README.md"
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
default = ["bellman", "ark"]
|
||||
libsnark = ["cc", "cmake"]
|
||||
bellman = ["bellman_ce", "pairing_ce", "ff_ce", "zokrates_field/bellman"]
|
||||
wasm = ["bellman_ce/nolog", "bellman_ce/wasm"]
|
||||
multicore = ["bellman_ce/multicore", "phase2/multicore"]
|
||||
ark = ["ark-ff", "ark-ec", "ark-bls12-377", "ark-bw6-761", "ark-gm17", "ark-groth16", "ark-crypto-primitives", "ark-serialize", "ark-relations", "ark-marlin", "ark-poly", "ark-poly-commit", "sha3", "digest"]
|
||||
default = ["ark", "bellman"]
|
||||
ark = ["zokrates_ast/ark"]
|
||||
bellman = ["zokrates_ast/bellman"]
|
||||
libsnark = []
|
||||
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
|
@ -26,49 +23,14 @@ reduce = "0.1.1"
|
|||
# serialization and deserialization
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
hex = "0.4.2"
|
||||
regex = "0.2"
|
||||
zokrates_field = { version = "0.5.0", path = "../zokrates_field", default-features = false }
|
||||
zokrates_pest_ast = { version = "0.2.0", path = "../zokrates_pest_ast" }
|
||||
zokrates_common = { path = "../zokrates_common" }
|
||||
zokrates_common = { version = "0.1", path = "../zokrates_common" }
|
||||
zokrates_embed = { version = "0.1.0", path = "../zokrates_embed" }
|
||||
zokrates_interpreter = { version = "0.1", path = "../zokrates_interpreter" }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast" }
|
||||
getrandom = { version = "0.2", features = ["js", "wasm-bindgen"] }
|
||||
rand_0_4 = { version = "0.4", package = "rand" }
|
||||
rand_0_8 = { version = "0.8", package = "rand" }
|
||||
csv = "1"
|
||||
phase2 = { git = "https://github.com/Zokrates/phase2", default-features = false }
|
||||
|
||||
# bellman
|
||||
bellman_ce = { version = "^0.3", default-features = false, optional = true }
|
||||
pairing_ce = { version = "^0.21", optional = true }
|
||||
ff_ce = { version = "^0.9", optional = true }
|
||||
|
||||
# ark
|
||||
ark-ff = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-ec = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-bn254 = { version = "^0.3.0", features = ["curve"], default-features = false, optional = true }
|
||||
ark-bls12-377 = { version = "^0.3.0", features = ["curve"], default-features = false, optional = true }
|
||||
ark-bw6-761 = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-gm17 = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-groth16 = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-serialize = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-relations = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-marlin = { git = "https://github.com/arkworks-rs/marlin", rev = "63cfd82", default-features = false, optional = true }
|
||||
ark-poly = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-poly-commit = { version = "^0.3.0", default-features = false, optional = true }
|
||||
ark-crypto-primitives = { version = "^0.3.0", default-features = false, optional = true }
|
||||
sha3 = { version = "0.9", optional = true }
|
||||
digest = { version = "0.9", optional = true }
|
||||
ethabi = "17.0.0"
|
||||
primitive-types = { version = "0.11", features = ["rlp"] }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "^0.3.0"
|
||||
pretty_assertions = "0.6.1"
|
||||
zokrates_fs_resolver = { version = "0.5", path = "../zokrates_fs_resolver"}
|
||||
|
||||
[build-dependencies]
|
||||
cc = { version = "1.0", features = ["parallel"], optional = true }
|
||||
cmake = { version = "=0.1.45", optional = true }
|
||||
|
|
|
@ -3,14 +3,12 @@
|
|||
//! @file compile.rs
|
||||
//! @author Thibaut Schaeffer <thibaut@schaeff.fr>
|
||||
//! @date 2018
|
||||
use crate::absy::{Module, OwnedModuleId, Program};
|
||||
use crate::flatten::FlattenerIterator;
|
||||
use crate::flatten::from_function_and_config;
|
||||
use crate::imports::{self, Importer};
|
||||
use crate::macros;
|
||||
use crate::optimizer::optimize;
|
||||
use crate::semantics::{self, Checker};
|
||||
use crate::static_analysis;
|
||||
use crate::typed_absy::abi::Abi;
|
||||
use crate::zir::ZirProgram;
|
||||
use crate::static_analysis::{self, analyse};
|
||||
use macros::process_macros;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
@ -18,7 +16,10 @@ use std::fmt;
|
|||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use typed_arena::Arena;
|
||||
use zokrates_ast::ir;
|
||||
use zokrates_ast::ir::{self, from_flat::from_flat};
|
||||
use zokrates_ast::typed::abi::Abi;
|
||||
use zokrates_ast::untyped::{Module, OwnedModuleId, Program};
|
||||
use zokrates_ast::zir::ZirProgram;
|
||||
use zokrates_common::Resolver;
|
||||
use zokrates_field::Field;
|
||||
use zokrates_pest_ast as pest;
|
||||
|
@ -195,21 +196,20 @@ pub fn compile<'ast, T: Field, E: Into<imports::Error>>(
|
|||
arena: &'ast Arena<String>,
|
||||
) -> Result<CompilationArtifacts<T, impl IntoIterator<Item = ir::Statement<T>> + 'ast>, CompileErrors>
|
||||
{
|
||||
let (typed_ast, abi): (crate::zir::ZirProgram<'_, T>, _) =
|
||||
let (typed_ast, abi): (zokrates_ast::zir::ZirProgram<'_, T>, _) =
|
||||
check_with_arena(source, location, resolver, &config, arena)?;
|
||||
|
||||
// flatten input program
|
||||
log::debug!("Flatten");
|
||||
let program_flattened = FlattenerIterator::from_function_and_config(typed_ast.main, config);
|
||||
let program_flattened = from_function_and_config(typed_ast.main, config);
|
||||
|
||||
// convert to ir
|
||||
log::debug!("Convert to IR");
|
||||
//let ir_prog = ir::from_flat::from_flat(program_flattened);
|
||||
let ir_prog = unimplemented!();
|
||||
let ir_prog = from_flat(program_flattened);
|
||||
|
||||
// optimize
|
||||
log::debug!("Optimise IR");
|
||||
let optimized_ir_prog = ir_prog.optimize();
|
||||
let optimized_ir_prog = optimize(ir_prog);
|
||||
|
||||
Ok(CompilationArtifacts {
|
||||
prog: optimized_ir_prog,
|
||||
|
@ -254,8 +254,7 @@ fn check_with_arena<'ast, T: Field, E: Into<imports::Error>>(
|
|||
log::debug!("Run static analysis");
|
||||
|
||||
// analyse (unroll and constant propagation)
|
||||
typed_ast
|
||||
.analyse(config)
|
||||
analyse(typed_ast, config)
|
||||
.map_err(|e| CompileErrors(vec![CompileErrorInner::from(e).in_file(&main_module)]))
|
||||
}
|
||||
|
||||
|
@ -361,8 +360,8 @@ mod test {
|
|||
|
||||
mod abi {
|
||||
use super::*;
|
||||
use crate::typed_absy::abi::*;
|
||||
use crate::typed_absy::types::*;
|
||||
use zokrates_ast::typed::abi::*;
|
||||
use zokrates_ast::typed::types::*;
|
||||
|
||||
#[test]
|
||||
fn use_struct_declaration_types() {
|
||||
|
|
|
@ -12,13 +12,6 @@ use zokrates_ast::zir::{ShouldReduce, UMetadata, ZirExpressionList};
|
|||
use zokrates_interpreter::Interpreter;
|
||||
|
||||
use crate::compile::CompileConfig;
|
||||
use crate::flat_absy::*;
|
||||
use crate::zir::types::{Type, UBitwidth};
|
||||
use crate::zir::{
|
||||
BooleanExpression, FieldElementExpression, Identifier, IfElse, Parameter as ZirParameter,
|
||||
UExpression, UExpressionInner, Variable as ZirVariable, ZirExpression, ZirFunction,
|
||||
ZirStatement,
|
||||
};
|
||||
use std::collections::{
|
||||
hash_map::{Entry, HashMap},
|
||||
VecDeque,
|
||||
|
@ -27,7 +20,14 @@ use std::convert::TryFrom;
|
|||
use zokrates_ast::common::embed::*;
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::common::{RuntimeError, Variable};
|
||||
use zokrates_ast::flat::*;
|
||||
use zokrates_ast::ir::Solver;
|
||||
use zokrates_ast::zir::types::{Type, UBitwidth};
|
||||
use zokrates_ast::zir::{
|
||||
BooleanExpression, FieldElementExpression, Identifier, IfElse, Parameter as ZirParameter,
|
||||
UExpression, UExpressionInner, Variable as ZirVariable, ZirExpression, ZirFunction,
|
||||
ZirStatement,
|
||||
};
|
||||
use zokrates_field::Field;
|
||||
|
||||
type FlatStatements<T> = VecDeque<FlatStatement<T>>;
|
||||
|
@ -36,10 +36,10 @@ type FlatStatements<T> = VecDeque<FlatStatement<T>>;
|
|||
///
|
||||
/// # Arguments
|
||||
/// * `funct` - `ZirFunction` that will be flattened
|
||||
pub fn from_function_and_config<'ast, T: Field>(
|
||||
funct: ZirFunction<'ast, T>,
|
||||
pub fn from_function_and_config<T: Field>(
|
||||
funct: ZirFunction<T>,
|
||||
config: CompileConfig,
|
||||
) -> FlattenerIterator<'ast, T> {
|
||||
) -> FlattenerIterator<T> {
|
||||
let mut flattener = Flattener::new(config);
|
||||
let mut statements_flattened = FlatStatements::new();
|
||||
// push parameters
|
||||
|
@ -208,15 +208,6 @@ impl<T: Field> FlatUExpression<T> {
|
|||
}
|
||||
}
|
||||
|
||||
// impl From<crate::zir::RuntimeError> for RuntimeError {
|
||||
// fn from(error: crate::zir::RuntimeError) -> Self {
|
||||
// match error {
|
||||
// crate::zir::RuntimeError::SourceAssertion(s) => RuntimeError::SourceAssertion(s),
|
||||
// crate::zir::RuntimeError::SelectRangeCheck => RuntimeError::SelectRangeCheck,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'ast, T: Field> Flattener<'ast, T> {
|
||||
/// Returns a `Flattener` with fresh `layout`.
|
||||
fn new(config: CompileConfig) -> Flattener<'ast, T> {
|
||||
|
@ -2710,13 +2701,13 @@ impl<'ast, T: Field> Flattener<'ast, T> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::zir;
|
||||
use crate::zir::types::Signature;
|
||||
use crate::zir::types::Type;
|
||||
use zokrates_ast::zir;
|
||||
use zokrates_ast::zir::types::Signature;
|
||||
use zokrates_ast::zir::types::Type;
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
fn flatten_function<T: Field>(f: ZirFunction<T>) -> FlatProg<T> {
|
||||
FlattenerIterator::from_function_and_config(f, CompileConfig::default()).collect()
|
||||
from_function_and_config(f, CompileConfig::default()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2734,11 +2725,11 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::boolean("x".into()),
|
||||
zir::Variable::boolean("x".into()),
|
||||
BooleanExpression::Value(true).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::boolean("y".into()),
|
||||
zir::Variable::boolean("y".into()),
|
||||
BooleanExpression::Value(true).into(),
|
||||
),
|
||||
ZirStatement::Assertion(
|
||||
|
@ -2798,11 +2789,11 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("x"),
|
||||
zir::Variable::field_element("x"),
|
||||
FieldElementExpression::Number(Bn128Field::from(1)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("y"),
|
||||
zir::Variable::field_element("y"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Assertion(
|
||||
|
@ -2872,7 +2863,7 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::uint("x".into(), 32),
|
||||
zir::Variable::uint("x".into(), 32),
|
||||
ZirExpression::Uint(
|
||||
UExpressionInner::Value(42)
|
||||
.annotate(32)
|
||||
|
@ -2936,11 +2927,11 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("x"),
|
||||
zir::Variable::field_element("x"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("y"),
|
||||
zir::Variable::field_element("y"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Assertion(
|
||||
|
@ -3004,15 +2995,15 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("x"),
|
||||
zir::Variable::field_element("x"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("y"),
|
||||
zir::Variable::field_element("y"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("z"),
|
||||
zir::Variable::field_element("z"),
|
||||
FieldElementExpression::Number(Bn128Field::from(4)).into(),
|
||||
),
|
||||
ZirStatement::Assertion(
|
||||
|
@ -3083,15 +3074,15 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("x"),
|
||||
zir::Variable::field_element("x"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("y"),
|
||||
zir::Variable::field_element("y"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("z"),
|
||||
zir::Variable::field_element("z"),
|
||||
FieldElementExpression::Number(Bn128Field::from(4)).into(),
|
||||
),
|
||||
ZirStatement::Assertion(
|
||||
|
@ -3164,19 +3155,19 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("x"),
|
||||
zir::Variable::field_element("x"),
|
||||
FieldElementExpression::Number(Bn128Field::from(4)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("y"),
|
||||
zir::Variable::field_element("y"),
|
||||
FieldElementExpression::Number(Bn128Field::from(4)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("z"),
|
||||
zir::Variable::field_element("z"),
|
||||
FieldElementExpression::Number(Bn128Field::from(8)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("t"),
|
||||
zir::Variable::field_element("t"),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
),
|
||||
ZirStatement::Assertion(
|
||||
|
@ -3257,11 +3248,11 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("a"),
|
||||
zir::Variable::field_element("a"),
|
||||
FieldElementExpression::Number(Bn128Field::from(7)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("b"),
|
||||
zir::Variable::field_element("b"),
|
||||
FieldElementExpression::Pow(
|
||||
box FieldElementExpression::Identifier("a".into()),
|
||||
box 0u32.into(),
|
||||
|
@ -3316,11 +3307,11 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("a"),
|
||||
zir::Variable::field_element("a"),
|
||||
FieldElementExpression::Number(Bn128Field::from(7)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("b"),
|
||||
zir::Variable::field_element("b"),
|
||||
FieldElementExpression::Pow(
|
||||
box FieldElementExpression::Identifier("a".into()),
|
||||
box 1u32.into(),
|
||||
|
@ -3395,11 +3386,11 @@ mod tests {
|
|||
arguments: vec![],
|
||||
statements: vec![
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("a"),
|
||||
zir::Variable::field_element("a"),
|
||||
FieldElementExpression::Number(Bn128Field::from(7)).into(),
|
||||
),
|
||||
ZirStatement::Definition(
|
||||
Variable::field_element("b"),
|
||||
zir::Variable::field_element("b"),
|
||||
FieldElementExpression::Pow(
|
||||
box FieldElementExpression::Identifier("a".into()),
|
||||
box 13u32.into(),
|
||||
|
@ -3542,12 +3533,12 @@ mod tests {
|
|||
let mut statements_flattened = FlatStatements::new();
|
||||
|
||||
let definition = ZirStatement::Definition(
|
||||
Variable::field_element("b"),
|
||||
zir::Variable::field_element("b"),
|
||||
FieldElementExpression::Number(Bn128Field::from(42)).into(),
|
||||
);
|
||||
|
||||
let statement = ZirStatement::Definition(
|
||||
Variable::field_element("a"),
|
||||
zir::Variable::field_element("a"),
|
||||
FieldElementExpression::Div(
|
||||
box FieldElementExpression::Div(
|
||||
box FieldElementExpression::Number(Bn128Field::from(5)),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::flat_absy::*;
|
||||
use zokrates_ast::flat::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub fn flat_expression_from_bits<T: Field>(v: Vec<FlatExpression<T>>) -> FlatExpression<T> {
|
||||
|
|
|
@ -4,17 +4,17 @@
|
|||
//! @author Thibaut Schaeffer <thibaut@schaeff.fr>
|
||||
//! @date 2018
|
||||
|
||||
use crate::absy::*;
|
||||
use crate::compile::parse_module;
|
||||
use crate::compile::{CompileErrorInner, CompileErrors};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use zokrates_ast::untyped::*;
|
||||
|
||||
use crate::absy::types::UnresolvedType;
|
||||
use typed_arena::Arena;
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::untyped::types::UnresolvedType;
|
||||
use zokrates_common::Resolver;
|
||||
use zokrates_field::Field;
|
||||
|
||||
|
|
|
@ -1,23 +1,9 @@
|
|||
#![feature(box_patterns, box_syntax)]
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "bellman")] {
|
||||
extern crate bellman_ce as bellman;
|
||||
extern crate ff_ce as ff;
|
||||
extern crate pairing_ce as pairing;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod compile;
|
||||
mod flatten;
|
||||
pub mod imports;
|
||||
mod macros;
|
||||
mod optimizer;
|
||||
mod semantics;
|
||||
mod static_analysis;
|
||||
use zokrates_ast::zir;
|
||||
|
||||
pub use zokrates_ast::untyped as absy;
|
||||
pub mod compile;
|
||||
pub use zokrates_ast::flat as flat_absy;
|
||||
pub mod proof_system;
|
||||
pub use zokrates_ast::typed as typed_absy;
|
||||
|
|
|
@ -53,7 +53,7 @@ impl<T: Field> Folder<T> for DuplicateOptimizer {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::flat_absy::Variable;
|
||||
use zokrates_ast::flat::Variable;
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -36,8 +36,8 @@
|
|||
// - `q == k * v if v isn't in i`: insert `v` into `i` and return `c_0`
|
||||
// - otherwise return `c_0`
|
||||
|
||||
use crate::flat_absy::Variable;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use zokrates_ast::flat::Variable;
|
||||
use zokrates_ast::ir::folder::Folder;
|
||||
use zokrates_ast::ir::LinComb;
|
||||
use zokrates_ast::ir::*;
|
||||
|
@ -198,7 +198,7 @@ impl<T: Field> Folder<T> for RedefinitionOptimizer<T> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::flat_absy::Parameter;
|
||||
use zokrates_ast::flat::Parameter;
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -4,26 +4,26 @@
|
|||
//! @author Thibaut Schaeffer <thibaut@schaeff.fr>
|
||||
//! @date 2017
|
||||
|
||||
use crate::absy::Identifier;
|
||||
use crate::absy::*;
|
||||
use crate::typed_absy::types::{GGenericsAssignment, GenericsAssignment};
|
||||
use crate::typed_absy::*;
|
||||
use crate::typed_absy::{DeclarationParameter, DeclarationVariable, Variable};
|
||||
use num_bigint::BigUint;
|
||||
use std::collections::{btree_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::fmt;
|
||||
use std::path::PathBuf;
|
||||
use zokrates_ast::typed::types::{GGenericsAssignment, GenericsAssignment};
|
||||
use zokrates_ast::typed::*;
|
||||
use zokrates_ast::typed::{DeclarationParameter, DeclarationVariable, Variable};
|
||||
use zokrates_ast::untyped::Identifier;
|
||||
use zokrates_ast::untyped::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
use crate::absy::types::{UnresolvedSignature, UnresolvedType, UserTypeId};
|
||||
use zokrates_ast::untyped::types::{UnresolvedSignature, UnresolvedType, UserTypeId};
|
||||
|
||||
use crate::typed_absy::types::{
|
||||
use std::hash::{Hash, Hasher};
|
||||
use zokrates_ast::typed::types::{
|
||||
check_type, specialize_declaration_type, ArrayType, DeclarationArrayType, DeclarationConstant,
|
||||
DeclarationFunctionKey, DeclarationSignature, DeclarationStructMember, DeclarationStructType,
|
||||
DeclarationTupleType, DeclarationType, GenericIdentifier, StructLocation, StructMember,
|
||||
TupleType,
|
||||
};
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct ErrorInner {
|
||||
|
@ -902,7 +902,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
self.insert_into_scope(Variable::with_id_and_type(CoreIdentifier::Constant(CanonicalConstantIdentifier::new(
|
||||
declaration.id,
|
||||
module_id.into(),
|
||||
)), crate::typed_absy::types::try_from_g_type(ty.clone()).unwrap()));
|
||||
)), zokrates_ast::typed::types::try_from_g_type(ty.clone()).unwrap()));
|
||||
|
||||
state
|
||||
.constants
|
||||
|
@ -1137,7 +1137,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
|
||||
let ty = specialize_declaration_type(decl_v.clone()._type, &generics).unwrap();
|
||||
|
||||
match self.insert_into_scope(crate::typed_absy::variable::Variable {
|
||||
match self.insert_into_scope(zokrates_ast::typed::variable::Variable {
|
||||
id: decl_v.clone().id,
|
||||
_type: ty,
|
||||
}) {
|
||||
|
@ -1638,7 +1638,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
|
||||
fn check_variable(
|
||||
&mut self,
|
||||
v: crate::absy::VariableNode<'ast>,
|
||||
v: zokrates_ast::untyped::VariableNode<'ast>,
|
||||
module_id: &ModuleId,
|
||||
types: &TypeMap<'ast, T>,
|
||||
) -> Result<Variable<'ast, T>, Vec<ErrorInner>> {
|
||||
|
@ -1651,7 +1651,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
|
||||
fn check_for_loop(
|
||||
&mut self,
|
||||
var: crate::absy::VariableNode<'ast>,
|
||||
var: zokrates_ast::untyped::VariableNode<'ast>,
|
||||
range: (ExpressionNode<'ast>, ExpressionNode<'ast>),
|
||||
statements: Vec<StatementNode<'ast>>,
|
||||
pos: (Position, Position),
|
||||
|
@ -2475,11 +2475,11 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
})?;
|
||||
|
||||
let kind = match kind {
|
||||
crate::absy::ConditionalKind::IfElse => {
|
||||
crate::typed_absy::ConditionalKind::IfElse
|
||||
zokrates_ast::untyped::ConditionalKind::IfElse => {
|
||||
zokrates_ast::typed::ConditionalKind::IfElse
|
||||
}
|
||||
crate::absy::ConditionalKind::Ternary => {
|
||||
crate::typed_absy::ConditionalKind::Ternary
|
||||
zokrates_ast::untyped::ConditionalKind::Ternary => {
|
||||
zokrates_ast::typed::ConditionalKind::Ternary
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -3627,9 +3627,9 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::absy;
|
||||
use crate::typed_absy;
|
||||
use lazy_static::lazy_static;
|
||||
use zokrates_ast::typed;
|
||||
use zokrates_ast::untyped;
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
lazy_static! {
|
||||
|
@ -3752,8 +3752,8 @@ mod tests {
|
|||
)
|
||||
.mock()];
|
||||
|
||||
let arguments = vec![absy::Parameter {
|
||||
id: absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
let arguments = vec![untyped::Parameter {
|
||||
id: untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
private: true,
|
||||
}
|
||||
.mock()];
|
||||
|
@ -3955,8 +3955,8 @@ mod tests {
|
|||
|
||||
let mut f0 = function0();
|
||||
|
||||
f0.value.arguments = vec![absy::Parameter::private(
|
||||
absy::Variable::new(
|
||||
f0.value.arguments = vec![untyped::Parameter::private(
|
||||
untyped::Variable::new(
|
||||
"a",
|
||||
UnresolvedType::array(
|
||||
UnresolvedType::FieldElement.mock(),
|
||||
|
@ -3976,8 +3976,8 @@ mod tests {
|
|||
.mock()]);
|
||||
|
||||
let mut f1 = function0();
|
||||
f1.value.arguments = vec![absy::Parameter::private(
|
||||
absy::Variable::new(
|
||||
f1.value.arguments = vec![untyped::Parameter::private(
|
||||
untyped::Variable::new(
|
||||
"a",
|
||||
UnresolvedType::array(
|
||||
UnresolvedType::FieldElement.mock(),
|
||||
|
@ -4064,8 +4064,8 @@ mod tests {
|
|||
|
||||
let mut foo = function0();
|
||||
|
||||
foo.value.arguments = vec![absy::Parameter::private(
|
||||
absy::Variable::new(
|
||||
foo.value.arguments = vec![untyped::Parameter::private(
|
||||
untyped::Variable::new(
|
||||
"a",
|
||||
UnresolvedType::array(
|
||||
UnresolvedType::FieldElement.mock(),
|
||||
|
@ -4474,7 +4474,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
checker.check_statement(statement, &*MODULE_ID, &TypeMap::new()),
|
||||
Ok(TypedStatement::Definition(
|
||||
TypedAssignee::Identifier(typed_absy::Variable::field_element("a")),
|
||||
TypedAssignee::Identifier(typed::Variable::field_element("a")),
|
||||
FieldElementExpression::Identifier("b".into()).into()
|
||||
))
|
||||
);
|
||||
|
@ -4491,7 +4491,7 @@ mod tests {
|
|||
let foo_args = vec![];
|
||||
let foo_statements = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::Definition(
|
||||
|
@ -4576,7 +4576,7 @@ mod tests {
|
|||
let foo_args = vec![];
|
||||
let foo_statements = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::Definition(
|
||||
|
@ -4603,7 +4603,7 @@ mod tests {
|
|||
let bar_args = vec![];
|
||||
let bar_statements = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::Definition(
|
||||
|
@ -4679,7 +4679,7 @@ mod tests {
|
|||
// should fail
|
||||
let foo_statements: Vec<StatementNode> = vec![
|
||||
Statement::For(
|
||||
absy::Variable::new("i", UnresolvedType::Uint(32).mock()).mock(),
|
||||
untyped::Variable::new("i", UnresolvedType::Uint(32).mock()).mock(),
|
||||
Expression::IntConstant(0usize.into()).mock(),
|
||||
Expression::IntConstant(10usize.into()).mock(),
|
||||
vec![],
|
||||
|
@ -4726,7 +4726,7 @@ mod tests {
|
|||
|
||||
let for_statements = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::Uint(32).mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::Uint(32).mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::Definition(
|
||||
|
@ -4738,7 +4738,7 @@ mod tests {
|
|||
|
||||
let foo_statements = vec![
|
||||
Statement::For(
|
||||
absy::Variable::new("i", UnresolvedType::Uint(32).mock()).mock(),
|
||||
untyped::Variable::new("i", UnresolvedType::Uint(32).mock()).mock(),
|
||||
Expression::IntConstant(0usize.into()).mock(),
|
||||
Expression::IntConstant(10usize.into()).mock(),
|
||||
for_statements,
|
||||
|
@ -4754,9 +4754,9 @@ mod tests {
|
|||
];
|
||||
|
||||
let for_statements_checked = vec![
|
||||
TypedStatement::Declaration(typed_absy::Variable::uint("a", UBitwidth::B32)),
|
||||
TypedStatement::Declaration(typed::Variable::uint("a", UBitwidth::B32)),
|
||||
TypedStatement::Definition(
|
||||
TypedAssignee::Identifier(typed_absy::Variable::uint("a", UBitwidth::B32)),
|
||||
TypedAssignee::Identifier(typed::Variable::uint("a", UBitwidth::B32)),
|
||||
UExpressionInner::Identifier("i".into())
|
||||
.annotate(UBitwidth::B32)
|
||||
.into(),
|
||||
|
@ -4765,7 +4765,7 @@ mod tests {
|
|||
|
||||
let foo_statements_checked = vec![
|
||||
TypedStatement::For(
|
||||
typed_absy::Variable::uint("i", UBitwidth::B32),
|
||||
typed::Variable::uint("i", UBitwidth::B32),
|
||||
0u32.into(),
|
||||
10u32.into(),
|
||||
for_statements_checked,
|
||||
|
@ -4805,7 +4805,7 @@ mod tests {
|
|||
// should fail
|
||||
let bar_statements: Vec<StatementNode> = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::MultipleDefinition(
|
||||
|
@ -4928,7 +4928,7 @@ mod tests {
|
|||
// should fail
|
||||
let bar_statements: Vec<StatementNode> = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::MultipleDefinition(
|
||||
|
@ -4990,8 +4990,8 @@ mod tests {
|
|||
.mock()];
|
||||
|
||||
let foo = Function {
|
||||
arguments: vec![crate::absy::Parameter {
|
||||
id: absy::Variable::new("x", UnresolvedType::FieldElement.mock()).mock(),
|
||||
arguments: vec![zokrates_ast::untyped::Parameter {
|
||||
id: untyped::Variable::new("x", UnresolvedType::FieldElement.mock()).mock(),
|
||||
private: false,
|
||||
}
|
||||
.mock()],
|
||||
|
@ -5007,11 +5007,11 @@ mod tests {
|
|||
|
||||
let main_statements: Vec<StatementNode> = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("b", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("b", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::MultipleDefinition(
|
||||
|
@ -5201,7 +5201,7 @@ mod tests {
|
|||
|
||||
let main_statements: Vec<StatementNode> = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new(
|
||||
untyped::Variable::new(
|
||||
"a",
|
||||
UnresolvedType::array(
|
||||
UnresolvedType::FieldElement.mock(),
|
||||
|
@ -5214,7 +5214,7 @@ mod tests {
|
|||
.mock(),
|
||||
Statement::Definition(
|
||||
Assignee::Identifier("a").mock(),
|
||||
Expression::InlineArray(vec![absy::SpreadOrExpression::Expression(
|
||||
Expression::InlineArray(vec![untyped::SpreadOrExpression::Expression(
|
||||
Expression::IntConstant(0usize.into()).mock(),
|
||||
)])
|
||||
.mock(),
|
||||
|
@ -5224,7 +5224,7 @@ mod tests {
|
|||
vec![Assignee::Select(
|
||||
box Assignee::Identifier("a").mock(),
|
||||
box RangeOrExpression::Expression(
|
||||
absy::Expression::IntConstant(0usize.into()).mock(),
|
||||
untyped::Expression::IntConstant(0usize.into()).mock(),
|
||||
),
|
||||
)
|
||||
.mock()],
|
||||
|
@ -5373,11 +5373,11 @@ mod tests {
|
|||
// should pass
|
||||
let bar_statements: Vec<StatementNode> = vec![
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("b", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("b", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
Statement::MultipleDefinition(
|
||||
|
@ -5403,12 +5403,12 @@ mod tests {
|
|||
];
|
||||
|
||||
let bar_statements_checked: Vec<TypedStatement<Bn128Field>> = vec![
|
||||
TypedStatement::Declaration(typed_absy::Variable::field_element("a")),
|
||||
TypedStatement::Declaration(typed_absy::Variable::field_element("b")),
|
||||
TypedStatement::Declaration(typed::Variable::field_element("a")),
|
||||
TypedStatement::Declaration(typed::Variable::field_element("b")),
|
||||
TypedStatement::MultipleDefinition(
|
||||
vec![
|
||||
typed_absy::Variable::field_element("a").into(),
|
||||
typed_absy::Variable::field_element("b").into(),
|
||||
typed::Variable::field_element("a").into(),
|
||||
typed::Variable::field_element("b").into(),
|
||||
],
|
||||
TypedExpressionList::function_call(
|
||||
DeclarationFunctionKey::with_location((*MODULE_ID).clone(), "foo").signature(
|
||||
|
@ -5477,12 +5477,12 @@ mod tests {
|
|||
|
||||
let mut f = function0();
|
||||
f.value.arguments = vec![
|
||||
absy::Parameter::private(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Parameter::private(
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
absy::Parameter::private(
|
||||
absy::Variable::new("a", UnresolvedType::Boolean.mock()).mock(),
|
||||
untyped::Parameter::private(
|
||||
untyped::Variable::new("a", UnresolvedType::Boolean.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
];
|
||||
|
@ -5520,8 +5520,8 @@ mod tests {
|
|||
)
|
||||
.mock()];
|
||||
|
||||
let main1_arguments = vec![crate::absy::Parameter {
|
||||
id: absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
let main1_arguments = vec![zokrates_ast::untyped::Parameter {
|
||||
id: untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
private: false,
|
||||
}
|
||||
.mock()];
|
||||
|
@ -5599,7 +5599,7 @@ mod tests {
|
|||
let mut checker: Checker<Bn128Field> = Checker::default();
|
||||
let _: Result<TypedStatement<Bn128Field>, Vec<ErrorInner>> = checker.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
&*MODULE_ID,
|
||||
|
@ -5608,7 +5608,7 @@ mod tests {
|
|||
let s2_checked: Result<TypedStatement<Bn128Field>, Vec<ErrorInner>> = checker
|
||||
.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
&*MODULE_ID,
|
||||
|
@ -5633,7 +5633,7 @@ mod tests {
|
|||
let mut checker: Checker<Bn128Field> = Checker::default();
|
||||
let _: Result<TypedStatement<Bn128Field>, Vec<ErrorInner>> = checker.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
&*MODULE_ID,
|
||||
|
@ -5642,7 +5642,7 @@ mod tests {
|
|||
let s2_checked: Result<TypedStatement<Bn128Field>, Vec<ErrorInner>> = checker
|
||||
.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::Boolean.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::Boolean.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
&*MODULE_ID,
|
||||
|
@ -5659,7 +5659,7 @@ mod tests {
|
|||
|
||||
mod structs {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::StructMember;
|
||||
use zokrates_ast::typed::types::StructMember;
|
||||
|
||||
/// solver function to create a module at location "" with a single symbol `Foo { foo: field }`
|
||||
fn create_module_with_foo(
|
||||
|
@ -6395,8 +6395,8 @@ mod tests {
|
|||
|
||||
let mut foo_field = function0();
|
||||
|
||||
foo_field.value.arguments = vec![absy::Parameter::private(
|
||||
absy::Variable {
|
||||
foo_field.value.arguments = vec![untyped::Parameter::private(
|
||||
untyped::Variable {
|
||||
id: "a",
|
||||
_type: UnresolvedType::FieldElement.mock(),
|
||||
}
|
||||
|
@ -6416,8 +6416,8 @@ mod tests {
|
|||
|
||||
let mut foo_u32 = function0();
|
||||
|
||||
foo_u32.value.arguments = vec![absy::Parameter::private(
|
||||
absy::Variable {
|
||||
foo_u32.value.arguments = vec![untyped::Parameter::private(
|
||||
untyped::Variable {
|
||||
id: "a",
|
||||
_type: UnresolvedType::Uint(32).mock(),
|
||||
}
|
||||
|
@ -6453,17 +6453,17 @@ mod tests {
|
|||
UnresolvedSignature::new().outputs(vec![UnresolvedType::FieldElement.mock()]);
|
||||
|
||||
let m = Module::with_symbols(vec![
|
||||
absy::SymbolDeclaration {
|
||||
untyped::SymbolDeclaration {
|
||||
id: "foo",
|
||||
symbol: Symbol::Here(SymbolDefinition::Function(foo_field)),
|
||||
}
|
||||
.mock(),
|
||||
absy::SymbolDeclaration {
|
||||
untyped::SymbolDeclaration {
|
||||
id: "foo",
|
||||
symbol: Symbol::Here(SymbolDefinition::Function(foo_u32)),
|
||||
}
|
||||
.mock(),
|
||||
absy::SymbolDeclaration {
|
||||
untyped::SymbolDeclaration {
|
||||
id: "main",
|
||||
symbol: Symbol::Here(SymbolDefinition::Function(main)),
|
||||
}
|
||||
|
@ -6502,7 +6502,7 @@ mod tests {
|
|||
checker
|
||||
.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
untyped::Variable::new("a", UnresolvedType::FieldElement.mock()).mock(),
|
||||
)
|
||||
.mock(),
|
||||
&*MODULE_ID,
|
||||
|
@ -6512,9 +6512,9 @@ mod tests {
|
|||
|
||||
assert_eq!(
|
||||
checker.check_assignee(a, &*MODULE_ID, &TypeMap::new()),
|
||||
Ok(TypedAssignee::Identifier(
|
||||
typed_absy::Variable::field_element("a")
|
||||
))
|
||||
Ok(TypedAssignee::Identifier(typed::Variable::field_element(
|
||||
"a"
|
||||
)))
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -6534,7 +6534,7 @@ mod tests {
|
|||
checker
|
||||
.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new(
|
||||
untyped::Variable::new(
|
||||
"a",
|
||||
UnresolvedType::array(
|
||||
UnresolvedType::FieldElement.mock(),
|
||||
|
@ -6553,9 +6553,10 @@ mod tests {
|
|||
assert_eq!(
|
||||
checker.check_assignee(a, &*MODULE_ID, &TypeMap::new()),
|
||||
Ok(TypedAssignee::Select(
|
||||
box TypedAssignee::Identifier(typed_absy::Variable::field_array(
|
||||
box TypedAssignee::Identifier(typed::Variable::array(
|
||||
"a",
|
||||
33u32.into()
|
||||
GType::FieldElement,
|
||||
33u32
|
||||
)),
|
||||
box 2u32.into()
|
||||
))
|
||||
|
@ -6584,7 +6585,7 @@ mod tests {
|
|||
checker
|
||||
.check_statement(
|
||||
Statement::Declaration(
|
||||
absy::Variable::new(
|
||||
untyped::Variable::new(
|
||||
"a",
|
||||
UnresolvedType::array(
|
||||
UnresolvedType::array(
|
||||
|
@ -6608,7 +6609,7 @@ mod tests {
|
|||
checker.check_assignee(a, &*MODULE_ID, &TypeMap::new()),
|
||||
Ok(TypedAssignee::Select(
|
||||
box TypedAssignee::Select(
|
||||
box TypedAssignee::Identifier(typed_absy::Variable::array(
|
||||
box TypedAssignee::Identifier(typed::Variable::array(
|
||||
"a",
|
||||
Type::array((Type::FieldElement, 33u32)),
|
||||
42u32,
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
// `if c then a else b fi` becomes `if c then { a } else { b } fi`, and down the line any statements resulting from trating `a` and `b` can be safely kept inside the respective blocks.
|
||||
|
||||
use crate::typed_absy::*;
|
||||
use zokrates_ast::typed::folder::*;
|
||||
use zokrates_ast::typed::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub struct Isolator;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::typed_absy::{
|
||||
use zokrates_ast::typed::{
|
||||
folder::*, BlockExpression, BooleanExpression, Conditional, ConditionalExpression,
|
||||
ConditionalOrExpression, CoreIdentifier, Expr, Identifier, TypedProgram, TypedStatement,
|
||||
Variable,
|
||||
|
@ -89,7 +89,7 @@ impl<'ast, T: Field> Folder<'ast, T> for ConditionRedefiner<'ast, T> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::typed_absy::{
|
||||
use zokrates_ast::typed::{
|
||||
Block, BooleanExpression, Conditional, ConditionalKind, FieldElementExpression, Type,
|
||||
};
|
||||
use zokrates_field::Bn128Field;
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use crate::typed_absy::TypedProgram;
|
||||
use crate::typed_absy::{
|
||||
use std::fmt;
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::typed::TypedProgram;
|
||||
use zokrates_ast::typed::{
|
||||
result_folder::ResultFolder,
|
||||
result_folder::{fold_expression_list_inner, fold_uint_expression_inner},
|
||||
Constant, TypedExpressionListInner, Types, UBitwidth, UExpressionInner,
|
||||
};
|
||||
use std::fmt;
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub struct ConstantArgumentChecker;
|
||||
|
|
|
@ -1,973 +0,0 @@
|
|||
use crate::static_analysis::Propagator;
|
||||
use crate::typed_absy::result_folder::*;
|
||||
use crate::typed_absy::types::DeclarationConstant;
|
||||
use crate::typed_absy::*;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::fmt;
|
||||
use zokrates_field::Field;
|
||||
|
||||
type ProgramConstants<'ast, T> =
|
||||
HashMap<OwnedTypedModuleId, HashMap<Identifier<'ast>, TypedExpression<'ast, T>>>;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
Type(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Error::Type(s) => write!(f, "{}", s),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct ConstantInliner<'ast, T> {
|
||||
modules: TypedModules<'ast, T>,
|
||||
location: OwnedTypedModuleId,
|
||||
constants: ProgramConstants<'ast, T>,
|
||||
}
|
||||
|
||||
impl<'ast, 'a, T: Field> ConstantInliner<'ast, T> {
|
||||
pub fn new(
|
||||
modules: TypedModules<'ast, T>,
|
||||
location: OwnedTypedModuleId,
|
||||
constants: ProgramConstants<'ast, T>,
|
||||
) -> Self {
|
||||
ConstantInliner {
|
||||
modules,
|
||||
location,
|
||||
constants,
|
||||
}
|
||||
}
|
||||
pub fn inline(p: TypedProgram<'ast, T>) -> Result<TypedProgram<'ast, T>, Error> {
|
||||
let constants = ProgramConstants::new();
|
||||
let mut inliner = ConstantInliner::new(p.modules.clone(), p.main.clone(), constants);
|
||||
inliner.fold_program(p)
|
||||
}
|
||||
|
||||
fn change_location(&mut self, location: OwnedTypedModuleId) -> OwnedTypedModuleId {
|
||||
let prev = self.location.clone();
|
||||
self.location = location;
|
||||
self.constants.entry(self.location.clone()).or_default();
|
||||
prev
|
||||
}
|
||||
|
||||
fn treated(&self, id: &TypedModuleId) -> bool {
|
||||
self.constants.contains_key(id)
|
||||
}
|
||||
|
||||
fn get_constant(
|
||||
&self,
|
||||
id: &CanonicalConstantIdentifier<'ast>,
|
||||
) -> Option<TypedExpression<'ast, T>> {
|
||||
self.constants
|
||||
.get(&id.module)
|
||||
.and_then(|constants| constants.get(&id.id.into()))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
fn get_constant_for_identifier(
|
||||
&self,
|
||||
id: &Identifier<'ast>,
|
||||
) -> Option<TypedExpression<'ast, T>> {
|
||||
self.constants
|
||||
.get(&self.location)
|
||||
.and_then(|constants| constants.get(&id))
|
||||
.cloned()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T: Field> ResultFolder<'ast, T> for ConstantInliner<'ast, T> {
|
||||
type Error = Error;
|
||||
|
||||
fn fold_program(
|
||||
&mut self,
|
||||
p: TypedProgram<'ast, T>,
|
||||
) -> Result<TypedProgram<'ast, T>, Self::Error> {
|
||||
self.fold_module_id(p.main.clone())?;
|
||||
|
||||
Ok(TypedProgram {
|
||||
modules: std::mem::take(&mut self.modules),
|
||||
..p
|
||||
})
|
||||
}
|
||||
|
||||
fn fold_module_id(
|
||||
&mut self,
|
||||
id: OwnedTypedModuleId,
|
||||
) -> Result<OwnedTypedModuleId, Self::Error> {
|
||||
// anytime we encounter a module id, visit the corresponding module if it hasn't been done yet
|
||||
if !self.treated(&id) {
|
||||
let current_m_id = self.change_location(id.clone());
|
||||
let m = self.modules.remove(&id).unwrap();
|
||||
let m = self.fold_module(m)?;
|
||||
self.modules.insert(id.clone(), m);
|
||||
self.change_location(current_m_id);
|
||||
}
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn fold_module(
|
||||
&mut self,
|
||||
m: TypedModule<'ast, T>,
|
||||
) -> Result<TypedModule<'ast, T>, Self::Error> {
|
||||
Ok(TypedModule {
|
||||
constants: m
|
||||
.constants
|
||||
.into_iter()
|
||||
.map(|(id, tc)| {
|
||||
|
||||
let id = self.fold_canonical_constant_identifier(id)?;
|
||||
|
||||
let constant = match tc {
|
||||
TypedConstantSymbol::There(imported_id) => {
|
||||
// visit the imported symbol. This triggers visiting the corresponding module if needed
|
||||
let imported_id = self.fold_canonical_constant_identifier(imported_id)?;
|
||||
// after that, the constant must have been defined defined in the global map. It is already reduced
|
||||
// to a literal, so running propagation isn't required
|
||||
self.get_constant(&imported_id).unwrap()
|
||||
}
|
||||
TypedConstantSymbol::Here(c) => {
|
||||
let non_propagated_constant = fold_constant(self, c)?.expression;
|
||||
// folding the constant above only reduces it to an expression containing only literals, not to a single literal.
|
||||
// propagating with an empty map of constants reduces it to a single literal
|
||||
Propagator::with_constants(&mut HashMap::default())
|
||||
.fold_expression(non_propagated_constant)
|
||||
.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
if crate::typed_absy::types::try_from_g_type::<_, UExpression<'ast, T>>(*id.ty.clone()).unwrap() == constant.get_type() {
|
||||
// add to the constant map. The value added is always a single litteral
|
||||
self.constants
|
||||
.get_mut(&self.location)
|
||||
.unwrap()
|
||||
.insert(id.id.into(), constant.clone());
|
||||
|
||||
Ok((
|
||||
id,
|
||||
TypedConstantSymbol::Here(TypedConstant {
|
||||
expression: constant,
|
||||
}),
|
||||
))
|
||||
} else {
|
||||
Err(Error::Type(format!("Expression of type `{}` cannot be assigned to constant `{}` of type `{}`", constant.get_type(), id.id, id.ty)))
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
functions: m
|
||||
.functions
|
||||
.into_iter()
|
||||
.map::<Result<_, Self::Error>, _>(|(key, fun)| {
|
||||
Ok((
|
||||
self.fold_declaration_function_key(key)?,
|
||||
self.fold_function_symbol(fun)?,
|
||||
))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
|
||||
fn fold_declaration_constant(
|
||||
&mut self,
|
||||
c: DeclarationConstant<'ast>,
|
||||
) -> Result<DeclarationConstant<'ast>, Self::Error> {
|
||||
match c {
|
||||
// replace constants by their concrete value in declaration types
|
||||
DeclarationConstant::Constant(id) => {
|
||||
let id = CanonicalConstantIdentifier {
|
||||
module: self.fold_module_id(id.module)?,
|
||||
..id
|
||||
};
|
||||
|
||||
Ok(DeclarationConstant::Concrete(match self.get_constant(&id).unwrap() {
|
||||
TypedExpression::Uint(UExpression {
|
||||
inner: UExpressionInner::Value(v),
|
||||
..
|
||||
}) => v as u32,
|
||||
_ => unreachable!("all constants found in declaration types should be reduceable to u32 literals"),
|
||||
}))
|
||||
}
|
||||
c => Ok(c),
|
||||
}
|
||||
}
|
||||
|
||||
fn fold_field_expression(
|
||||
&mut self,
|
||||
e: FieldElementExpression<'ast, T>,
|
||||
) -> Result<FieldElementExpression<'ast, T>, Self::Error> {
|
||||
match e {
|
||||
FieldElementExpression::Identifier(ref id) => {
|
||||
match self.get_constant_for_identifier(id) {
|
||||
Some(c) => Ok(c.try_into().unwrap()),
|
||||
None => fold_field_expression(self, e),
|
||||
}
|
||||
}
|
||||
e => fold_field_expression(self, e),
|
||||
}
|
||||
}
|
||||
|
||||
fn fold_boolean_expression(
|
||||
&mut self,
|
||||
e: BooleanExpression<'ast, T>,
|
||||
) -> Result<BooleanExpression<'ast, T>, Self::Error> {
|
||||
match e {
|
||||
BooleanExpression::Identifier(ref id) => match self.get_constant_for_identifier(id) {
|
||||
Some(c) => Ok(c.try_into().unwrap()),
|
||||
None => fold_boolean_expression(self, e),
|
||||
},
|
||||
e => fold_boolean_expression(self, e),
|
||||
}
|
||||
}
|
||||
|
||||
fn fold_uint_expression_inner(
|
||||
&mut self,
|
||||
size: UBitwidth,
|
||||
e: UExpressionInner<'ast, T>,
|
||||
) -> Result<UExpressionInner<'ast, T>, Self::Error> {
|
||||
match e {
|
||||
UExpressionInner::Identifier(ref id) => match self.get_constant_for_identifier(id) {
|
||||
Some(c) => {
|
||||
let e: UExpression<'ast, T> = c.try_into().unwrap();
|
||||
Ok(e.into_inner())
|
||||
}
|
||||
None => fold_uint_expression_inner(self, size, e),
|
||||
},
|
||||
e => fold_uint_expression_inner(self, size, e),
|
||||
}
|
||||
}
|
||||
|
||||
fn fold_array_expression_inner(
|
||||
&mut self,
|
||||
ty: &ArrayType<'ast, T>,
|
||||
e: ArrayExpressionInner<'ast, T>,
|
||||
) -> Result<ArrayExpressionInner<'ast, T>, Self::Error> {
|
||||
match e {
|
||||
ArrayExpressionInner::Identifier(ref id) => {
|
||||
match self.get_constant_for_identifier(id) {
|
||||
Some(c) => {
|
||||
let e: ArrayExpression<'ast, T> = c.try_into().unwrap();
|
||||
Ok(e.into_inner())
|
||||
}
|
||||
None => fold_array_expression_inner(self, ty, e),
|
||||
}
|
||||
}
|
||||
e => fold_array_expression_inner(self, ty, e),
|
||||
}
|
||||
}
|
||||
|
||||
fn fold_struct_expression_inner(
|
||||
&mut self,
|
||||
ty: &StructType<'ast, T>,
|
||||
e: StructExpressionInner<'ast, T>,
|
||||
) -> Result<StructExpressionInner<'ast, T>, Self::Error> {
|
||||
match e {
|
||||
StructExpressionInner::Identifier(ref id) => match self.get_constant_for_identifier(id)
|
||||
{
|
||||
Some(c) => {
|
||||
let e: StructExpression<'ast, T> = c.try_into().unwrap();
|
||||
Ok(e.into_inner())
|
||||
}
|
||||
None => fold_struct_expression_inner(self, ty, e),
|
||||
},
|
||||
e => fold_struct_expression_inner(self, ty, e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::DeclarationSignature;
|
||||
use crate::typed_absy::{
|
||||
DeclarationArrayType, DeclarationFunctionKey, DeclarationType, FieldElementExpression,
|
||||
GType, Identifier, TypedConstant, TypedExpression, TypedFunction, TypedFunctionSymbol,
|
||||
TypedStatement,
|
||||
};
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
#[test]
|
||||
fn inline_const_field() {
|
||||
// const field a = 1
|
||||
//
|
||||
// def main() -> field:
|
||||
// return a
|
||||
|
||||
let const_id = "a";
|
||||
let main: TypedFunction<Bn128Field> = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
FieldElementExpression::Identifier(Identifier::from(const_id)).into(),
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
};
|
||||
|
||||
let constants: TypedConstantSymbols<_> = vec![(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(TypedExpression::FieldElement(
|
||||
FieldElementExpression::Number(Bn128Field::from(1)),
|
||||
))),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let program = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: constants.clone(),
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = ConstantInliner::inline(program);
|
||||
|
||||
let expected_main = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
FieldElementExpression::Number(Bn128Field::from(1)).into(),
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
};
|
||||
|
||||
let expected_program: TypedProgram<Bn128Field> = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(expected_main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants,
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(program, Ok(expected_program))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_const_boolean() {
|
||||
// const bool a = true
|
||||
//
|
||||
// def main() -> bool:
|
||||
// return a
|
||||
|
||||
let const_id = "a";
|
||||
let main: TypedFunction<Bn128Field> = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![BooleanExpression::Identifier(
|
||||
Identifier::from(const_id),
|
||||
)
|
||||
.into()])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Boolean]),
|
||||
};
|
||||
|
||||
let constants: TypedConstantSymbols<_> = vec![(
|
||||
CanonicalConstantIdentifier::new(const_id, "main".into(), DeclarationType::Boolean),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(TypedExpression::Boolean(
|
||||
BooleanExpression::Value(true),
|
||||
))),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let program = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Boolean]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: constants.clone(),
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = ConstantInliner::inline(program);
|
||||
|
||||
let expected_main = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
BooleanExpression::Value(true).into()
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Boolean]),
|
||||
};
|
||||
|
||||
let expected_program: TypedProgram<Bn128Field> = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Boolean]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(expected_main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants,
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(program, Ok(expected_program))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_const_uint() {
|
||||
// const u32 a = 0x00000001
|
||||
//
|
||||
// def main() -> u32:
|
||||
// return a
|
||||
|
||||
let const_id = "a";
|
||||
let main: TypedFunction<Bn128Field> = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![UExpressionInner::Identifier(
|
||||
Identifier::from(const_id),
|
||||
)
|
||||
.annotate(UBitwidth::B32)
|
||||
.into()])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Uint(UBitwidth::B32)]),
|
||||
};
|
||||
|
||||
let constants: TypedConstantSymbols<_> = vec![(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_id,
|
||||
"main".into(),
|
||||
DeclarationType::Uint(UBitwidth::B32),
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(
|
||||
UExpressionInner::Value(1u128)
|
||||
.annotate(UBitwidth::B32)
|
||||
.into(),
|
||||
)),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let program = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Uint(UBitwidth::B32)]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: constants.clone(),
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = ConstantInliner::inline(program);
|
||||
|
||||
let expected_main = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![UExpressionInner::Value(1u128)
|
||||
.annotate(UBitwidth::B32)
|
||||
.into()])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Uint(UBitwidth::B32)]),
|
||||
};
|
||||
|
||||
let expected_program: TypedProgram<Bn128Field> = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::Uint(UBitwidth::B32)]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(expected_main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants,
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(program, Ok(expected_program))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_const_field_array() {
|
||||
// const field[2] a = [2, 2]
|
||||
//
|
||||
// def main() -> field:
|
||||
// return a[0] + a[1]
|
||||
|
||||
let const_id = "a";
|
||||
let main: TypedFunction<Bn128Field> = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![FieldElementExpression::Add(
|
||||
FieldElementExpression::select(
|
||||
ArrayExpressionInner::Identifier(Identifier::from(const_id))
|
||||
.annotate(GType::FieldElement, 2u32),
|
||||
UExpressionInner::Value(0u128).annotate(UBitwidth::B32),
|
||||
)
|
||||
.into(),
|
||||
FieldElementExpression::select(
|
||||
ArrayExpressionInner::Identifier(Identifier::from(const_id))
|
||||
.annotate(GType::FieldElement, 2u32),
|
||||
UExpressionInner::Value(1u128).annotate(UBitwidth::B32),
|
||||
)
|
||||
.into(),
|
||||
)
|
||||
.into()])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
};
|
||||
|
||||
let constants: TypedConstantSymbols<_> = vec![(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_id,
|
||||
"main".into(),
|
||||
DeclarationType::Array(DeclarationArrayType::new(
|
||||
DeclarationType::FieldElement,
|
||||
2u32,
|
||||
)),
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(TypedExpression::Array(
|
||||
ArrayExpressionInner::Value(
|
||||
vec![
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
]
|
||||
.into(),
|
||||
)
|
||||
.annotate(GType::FieldElement, 2u32),
|
||||
))),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let program = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: constants.clone(),
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = ConstantInliner::inline(program);
|
||||
|
||||
let expected_main = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![FieldElementExpression::Add(
|
||||
FieldElementExpression::select(
|
||||
ArrayExpressionInner::Value(
|
||||
vec![
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
]
|
||||
.into(),
|
||||
)
|
||||
.annotate(GType::FieldElement, 2u32),
|
||||
UExpressionInner::Value(0u128).annotate(UBitwidth::B32),
|
||||
)
|
||||
.into(),
|
||||
FieldElementExpression::select(
|
||||
ArrayExpressionInner::Value(
|
||||
vec![
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
]
|
||||
.into(),
|
||||
)
|
||||
.annotate(GType::FieldElement, 2u32),
|
||||
UExpressionInner::Value(1u128).annotate(UBitwidth::B32),
|
||||
)
|
||||
.into(),
|
||||
)
|
||||
.into()])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
};
|
||||
|
||||
let expected_program: TypedProgram<Bn128Field> = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(expected_main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants,
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(program, Ok(expected_program))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_nested_const_field() {
|
||||
// const field a = 1
|
||||
// const field b = a + 1
|
||||
//
|
||||
// def main() -> field:
|
||||
// return b
|
||||
|
||||
let const_a_id = "a";
|
||||
let const_b_id = "b";
|
||||
|
||||
let main: TypedFunction<Bn128Field> = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
FieldElementExpression::Identifier(Identifier::from(const_b_id)).into(),
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
};
|
||||
|
||||
let program = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: vec![
|
||||
(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_a_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(
|
||||
TypedExpression::FieldElement(FieldElementExpression::Number(
|
||||
Bn128Field::from(1),
|
||||
)),
|
||||
)),
|
||||
),
|
||||
(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_b_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(
|
||||
TypedExpression::FieldElement(FieldElementExpression::Add(
|
||||
box FieldElementExpression::Identifier(Identifier::from(
|
||||
const_a_id,
|
||||
)),
|
||||
box FieldElementExpression::Number(Bn128Field::from(1)),
|
||||
)),
|
||||
)),
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = ConstantInliner::inline(program);
|
||||
|
||||
let expected_main = TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
FieldElementExpression::Number(Bn128Field::from(2)).into(),
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
};
|
||||
|
||||
let expected_program: TypedProgram<Bn128Field> = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![(
|
||||
"main".into(),
|
||||
TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(expected_main),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: vec![
|
||||
(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_a_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(
|
||||
TypedExpression::FieldElement(FieldElementExpression::Number(
|
||||
Bn128Field::from(1),
|
||||
)),
|
||||
)),
|
||||
),
|
||||
(
|
||||
CanonicalConstantIdentifier::new(
|
||||
const_b_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(
|
||||
TypedExpression::FieldElement(FieldElementExpression::Number(
|
||||
Bn128Field::from(2),
|
||||
)),
|
||||
)),
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(program, Ok(expected_program))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_imported_constant() {
|
||||
// ---------------------
|
||||
// module `foo`
|
||||
// --------------------
|
||||
// const field FOO = 42
|
||||
//
|
||||
// def main():
|
||||
// return
|
||||
//
|
||||
// ---------------------
|
||||
// module `main`
|
||||
// ---------------------
|
||||
// from "foo" import FOO
|
||||
//
|
||||
// def main() -> field:
|
||||
// return FOO
|
||||
|
||||
let foo_const_id = "FOO";
|
||||
let foo_module = TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main")
|
||||
.signature(DeclarationSignature::new().inputs(vec![]).outputs(vec![])),
|
||||
TypedFunctionSymbol::Here(TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![],
|
||||
signature: DeclarationSignature::new().inputs(vec![]).outputs(vec![]),
|
||||
}),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: vec![(
|
||||
CanonicalConstantIdentifier::new(
|
||||
foo_const_id,
|
||||
"foo".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(TypedExpression::FieldElement(
|
||||
FieldElementExpression::Number(Bn128Field::from(42)),
|
||||
))),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let main_module = TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
FieldElementExpression::Identifier(Identifier::from(foo_const_id)).into(),
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
}),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: vec![(
|
||||
CanonicalConstantIdentifier::new(
|
||||
foo_const_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::There(CanonicalConstantIdentifier::new(
|
||||
foo_const_id,
|
||||
"foo".into(),
|
||||
DeclarationType::FieldElement,
|
||||
)),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![
|
||||
("main".into(), main_module),
|
||||
("foo".into(), foo_module.clone()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let program = ConstantInliner::inline(program);
|
||||
let expected_main_module = TypedModule {
|
||||
functions: vec![(
|
||||
DeclarationFunctionKey::with_location("main", "main").signature(
|
||||
DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
),
|
||||
TypedFunctionSymbol::Here(TypedFunction {
|
||||
arguments: vec![],
|
||||
statements: vec![TypedStatement::Return(vec![
|
||||
FieldElementExpression::Number(Bn128Field::from(42)).into(),
|
||||
])],
|
||||
signature: DeclarationSignature::new()
|
||||
.inputs(vec![])
|
||||
.outputs(vec![DeclarationType::FieldElement]),
|
||||
}),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
constants: vec![(
|
||||
CanonicalConstantIdentifier::new(
|
||||
foo_const_id,
|
||||
"main".into(),
|
||||
DeclarationType::FieldElement,
|
||||
),
|
||||
TypedConstantSymbol::Here(TypedConstant::new(TypedExpression::FieldElement(
|
||||
FieldElementExpression::Number(Bn128Field::from(42)),
|
||||
))),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let expected_program: TypedProgram<Bn128Field> = TypedProgram {
|
||||
main: "main".into(),
|
||||
modules: vec![
|
||||
("main".into(), expected_main_module),
|
||||
("foo".into(), foo_module),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
assert_eq!(program, Ok(expected_program))
|
||||
}
|
||||
}
|
|
@ -2,9 +2,9 @@
|
|||
// This does *not* reduce constants to their literal value
|
||||
// This step cannot fail as the imports were checked during semantics
|
||||
|
||||
use crate::typed_absy::*;
|
||||
use std::collections::HashMap;
|
||||
use zokrates_ast::typed::folder::*;
|
||||
use zokrates_ast::typed::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
// a map of the canonical constants in this program. with all imported constants reduced to their canonical value
|
||||
|
@ -109,8 +109,8 @@ impl<'ast, T: Field> Folder<'ast, T> for ConstantResolver<'ast, T> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::DeclarationSignature;
|
||||
use crate::typed_absy::{
|
||||
use zokrates_ast::typed::types::DeclarationSignature;
|
||||
use zokrates_ast::typed::{
|
||||
DeclarationArrayType, DeclarationFunctionKey, DeclarationType, FieldElementExpression,
|
||||
GType, Identifier, TypedConstant, TypedExpression, TypedFunction, TypedFunctionSymbol,
|
||||
TypedStatement,
|
||||
|
|
|
@ -4,11 +4,12 @@
|
|||
//! @author Thibaut Schaeffer <thibaut@schaeff.fr>
|
||||
//! @date 2018
|
||||
|
||||
use crate::flat_absy::*;
|
||||
use std::collections::HashMap;
|
||||
use zokrates_ast::flat::folder::*;
|
||||
use zokrates_ast::flat::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Propagator<T> {
|
||||
constants: HashMap<Variable, T>,
|
||||
}
|
||||
|
@ -77,39 +78,45 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn add() {
|
||||
let mut propagator = Propagator::default();
|
||||
|
||||
let e = FlatExpression::Add(
|
||||
box FlatExpression::Number(Bn128Field::from(2)),
|
||||
box FlatExpression::Number(Bn128Field::from(3)),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
e.propagate(&mut HashMap::new()),
|
||||
propagator.fold_expression(e),
|
||||
FlatExpression::Number(Bn128Field::from(5))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sub() {
|
||||
let mut propagator = Propagator::default();
|
||||
|
||||
let e = FlatExpression::Sub(
|
||||
box FlatExpression::Number(Bn128Field::from(3)),
|
||||
box FlatExpression::Number(Bn128Field::from(2)),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
e.propagate(&mut HashMap::new()),
|
||||
propagator.fold_expression(e),
|
||||
FlatExpression::Number(Bn128Field::from(1))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mult() {
|
||||
let mut propagator = Propagator::default();
|
||||
|
||||
let e = FlatExpression::Mult(
|
||||
box FlatExpression::Number(Bn128Field::from(3)),
|
||||
box FlatExpression::Number(Bn128Field::from(2)),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
e.propagate(&mut HashMap::new()),
|
||||
propagator.fold_expression(e),
|
||||
FlatExpression::Number(Bn128Field::from(6))
|
||||
);
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -15,7 +15,6 @@ mod propagation;
|
|||
mod reducer;
|
||||
mod struct_concretizer;
|
||||
mod uint_optimizer;
|
||||
mod unconstrained_vars;
|
||||
mod variable_write_remover;
|
||||
mod zir_propagation;
|
||||
|
||||
|
@ -32,10 +31,9 @@ use self::variable_write_remover::VariableWriteRemover;
|
|||
use crate::compile::CompileConfig;
|
||||
use crate::static_analysis::constant_resolver::ConstantResolver;
|
||||
use crate::static_analysis::zir_propagation::ZirPropagator;
|
||||
use crate::typed_absy::{abi::Abi, TypedProgram};
|
||||
use crate::zir::ZirProgram;
|
||||
use std::fmt;
|
||||
pub use unconstrained_vars::UnconstrainedVariableDetector;
|
||||
use zokrates_ast::typed::{abi::Abi, TypedProgram};
|
||||
use zokrates_ast::zir::ZirProgram;
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::typed_absy::{
|
||||
use std::fmt;
|
||||
use zokrates_ast::typed::{
|
||||
result_folder::*, Expr, SelectExpression, SelectOrExpression, Type, TypedAssignee,
|
||||
TypedProgram, UExpressionInner,
|
||||
};
|
||||
use std::fmt;
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -47,7 +47,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for OutOfBoundsChecker {
|
|||
) -> Result<TypedAssignee<'ast, T>, Error> {
|
||||
match a {
|
||||
TypedAssignee::Select(box array, box index) => {
|
||||
use crate::typed_absy::Typed;
|
||||
use zokrates_ast::typed::Typed;
|
||||
|
||||
let array = self.fold_assignee(array)?;
|
||||
|
||||
|
|
|
@ -7,13 +7,13 @@
|
|||
//! @author Thibaut Schaeffer <thibaut@schaeff.fr>
|
||||
//! @date 2018
|
||||
|
||||
use crate::typed_absy::result_folder::*;
|
||||
use crate::typed_absy::types::Type;
|
||||
use crate::typed_absy::*;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fmt;
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::typed::result_folder::*;
|
||||
use zokrates_ast::typed::types::Type;
|
||||
use zokrates_ast::typed::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub type Constants<'ast, T> = HashMap<Identifier<'ast>, TypedExpression<'ast, T>>;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// given a (partial) map of values for program constants, replace where applicable constants by their value
|
||||
|
||||
use crate::static_analysis::reducer::ConstantDefinitions;
|
||||
use crate::typed_absy::{
|
||||
use zokrates_ast::typed::{
|
||||
folder::*, ArrayExpression, ArrayExpressionInner, ArrayType, BooleanExpression, CoreIdentifier,
|
||||
DeclarationConstant, Expr, FieldElementExpression, Identifier, StructExpression,
|
||||
StructExpressionInner, StructType, TypedProgram, TypedSymbolDeclaration, UBitwidth,
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
use crate::static_analysis::reducer::{
|
||||
constants_reader::ConstantsReader, reduce_function, ConstantDefinitions, Error,
|
||||
};
|
||||
use crate::typed_absy::{
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
use zokrates_ast::typed::{
|
||||
result_folder::*, types::ConcreteGenericsAssignment, OwnedTypedModuleId, TypedConstant,
|
||||
TypedConstantSymbol, TypedConstantSymbolDeclaration, TypedModuleId, TypedProgram,
|
||||
TypedSymbolDeclaration, UExpression,
|
||||
};
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub struct ConstantsWriter<'ast, T> {
|
||||
|
@ -105,11 +105,11 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ConstantsWriter<'ast, T> {
|
|||
|
||||
// if constants were used in the rhs, they are now defined in the map
|
||||
// replace them in the expression
|
||||
use crate::typed_absy::folder::Folder;
|
||||
use zokrates_ast::typed::folder::Folder;
|
||||
|
||||
let c = ConstantsReader::with_constants(&self.constants).fold_constant(c);
|
||||
|
||||
use crate::typed_absy::{DeclarationSignature, TypedFunction, TypedStatement};
|
||||
use zokrates_ast::typed::{DeclarationSignature, TypedFunction, TypedStatement};
|
||||
|
||||
// wrap this expression in a function
|
||||
let wrapper = TypedFunction {
|
||||
|
@ -130,13 +130,13 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ConstantsWriter<'ast, T> {
|
|||
assert_eq!(expressions.len(), 1);
|
||||
let constant_expression = expressions.pop().unwrap();
|
||||
|
||||
use crate::typed_absy::Constant;
|
||||
use zokrates_ast::typed::Constant;
|
||||
if !constant_expression.is_constant() {
|
||||
return Err(Error::ConstantReduction(id.id.to_string(), id.module));
|
||||
};
|
||||
|
||||
use crate::typed_absy::Typed;
|
||||
if crate::typed_absy::types::try_from_g_type::<_, UExpression<'ast, T>>(
|
||||
use zokrates_ast::typed::Typed;
|
||||
if zokrates_ast::typed::types::try_from_g_type::<_, UExpression<'ast, T>>(
|
||||
c.ty.clone(),
|
||||
)
|
||||
.unwrap()
|
||||
|
|
|
@ -28,16 +28,16 @@
|
|||
use crate::static_analysis::reducer::Output;
|
||||
use crate::static_analysis::reducer::ShallowTransformer;
|
||||
use crate::static_analysis::reducer::Versions;
|
||||
use crate::typed_absy::types::{ConcreteGenericsAssignment, IntoTypes};
|
||||
use crate::typed_absy::CoreIdentifier;
|
||||
use crate::typed_absy::Identifier;
|
||||
use crate::typed_absy::TypedAssignee;
|
||||
use crate::typed_absy::{
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::typed::types::{ConcreteGenericsAssignment, IntoTypes};
|
||||
use zokrates_ast::typed::CoreIdentifier;
|
||||
use zokrates_ast::typed::Identifier;
|
||||
use zokrates_ast::typed::TypedAssignee;
|
||||
use zokrates_ast::typed::{
|
||||
ConcreteFunctionKey, ConcreteSignature, ConcreteVariable, DeclarationFunctionKey, Expr,
|
||||
Signature, TypedExpression, TypedFunctionSymbol, TypedFunctionSymbolDeclaration, TypedProgram,
|
||||
TypedStatement, Types, UExpression, UExpressionInner, Variable,
|
||||
};
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub enum InlineError<'ast, T> {
|
||||
|
@ -89,7 +89,7 @@ pub fn inline_call<'a, 'ast, T: Field, E: Expr<'ast, T>>(
|
|||
) -> InlineResult<'ast, T> {
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use crate::typed_absy::Typed;
|
||||
use zokrates_ast::typed::Typed;
|
||||
|
||||
let output_types = output.clone().into_types();
|
||||
|
||||
|
|
|
@ -17,14 +17,14 @@ mod inline;
|
|||
mod shallow_ssa;
|
||||
|
||||
use self::inline::{inline_call, InlineError};
|
||||
use crate::typed_absy::result_folder::*;
|
||||
use crate::typed_absy::types::ConcreteGenericsAssignment;
|
||||
use crate::typed_absy::types::GGenericsAssignment;
|
||||
use crate::typed_absy::CanonicalConstantIdentifier;
|
||||
use crate::typed_absy::Folder;
|
||||
use std::collections::HashMap;
|
||||
use zokrates_ast::typed::result_folder::*;
|
||||
use zokrates_ast::typed::types::ConcreteGenericsAssignment;
|
||||
use zokrates_ast::typed::types::GGenericsAssignment;
|
||||
use zokrates_ast::typed::CanonicalConstantIdentifier;
|
||||
use zokrates_ast::typed::Folder;
|
||||
|
||||
use crate::typed_absy::{
|
||||
use zokrates_ast::typed::{
|
||||
ArrayExpressionInner, ArrayType, BlockExpression, CoreIdentifier, Expr, FunctionCall,
|
||||
FunctionCallExpression, FunctionCallOrExpression, Id, Identifier, OwnedTypedModuleId,
|
||||
TypedExpression, TypedExpressionList, TypedExpressionListInner, TypedFunction,
|
||||
|
@ -640,9 +640,9 @@ fn compute_hash<T: Field>(f: &TypedFunction<T>) -> u64 {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::DeclarationConstant;
|
||||
use crate::typed_absy::types::DeclarationSignature;
|
||||
use crate::typed_absy::{
|
||||
use zokrates_ast::typed::types::DeclarationConstant;
|
||||
use zokrates_ast::typed::types::DeclarationSignature;
|
||||
use zokrates_ast::typed::{
|
||||
ArrayExpression, ArrayExpressionInner, DeclarationFunctionKey, DeclarationType,
|
||||
DeclarationVariable, FieldElementExpression, GenericIdentifier, Identifier,
|
||||
OwnedTypedModuleId, Select, Type, TypedExpression, TypedExpressionList,
|
||||
|
|
|
@ -24,10 +24,10 @@
|
|||
// endfor
|
||||
// return b_3 // we leave versions b_1 and b_2 to make b accessible and modifiable inside the for-loop
|
||||
|
||||
use crate::typed_absy::types::ConcreteGenericsAssignment;
|
||||
use crate::typed_absy::types::Type;
|
||||
use crate::typed_absy::*;
|
||||
use zokrates_ast::typed::folder::*;
|
||||
use zokrates_ast::typed::types::ConcreteGenericsAssignment;
|
||||
use zokrates_ast::typed::types::Type;
|
||||
use zokrates_ast::typed::*;
|
||||
|
||||
use zokrates_field::Field;
|
||||
|
||||
|
@ -192,7 +192,7 @@ impl<'ast, 'a, T: Field> Folder<'ast, T> for ShallowTransformer<'ast, 'a> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::DeclarationSignature;
|
||||
use zokrates_ast::typed::types::DeclarationSignature;
|
||||
use zokrates_field::Bn128Field;
|
||||
mod normal {
|
||||
use super::*;
|
||||
|
@ -336,7 +336,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn incremental_multiple_definition() {
|
||||
use crate::typed_absy::types::Type;
|
||||
use zokrates_ast::typed::types::Type;
|
||||
|
||||
// field a
|
||||
// a = 2
|
||||
|
@ -575,7 +575,7 @@ mod tests {
|
|||
|
||||
mod for_loop {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::GGenericsAssignment;
|
||||
use zokrates_ast::typed::types::GGenericsAssignment;
|
||||
#[test]
|
||||
fn treat_loop() {
|
||||
// def main<K>(field a) -> field:
|
||||
|
@ -773,7 +773,7 @@ mod tests {
|
|||
|
||||
mod function_call {
|
||||
use super::*;
|
||||
use crate::typed_absy::types::GGenericsAssignment;
|
||||
use zokrates_ast::typed::types::GGenericsAssignment;
|
||||
// test that function calls are left in
|
||||
#[test]
|
||||
fn treat_calls() {
|
||||
|
|
|
@ -5,15 +5,15 @@
|
|||
// for structs, `Foo<N> { field[N] a }` is propagated to `Foo<42> { field[N] a }`. The missing step is replacing `N` by `42`
|
||||
// *inside* the canonical type, so that it can be concretized in the same way arrays are.
|
||||
|
||||
use crate::typed_absy::{
|
||||
use std::marker::PhantomData;
|
||||
use zokrates_ast::typed::folder::*;
|
||||
use zokrates_ast::typed::{
|
||||
types::{
|
||||
ConcreteGenericsAssignment, DeclarationArrayType, DeclarationConstant,
|
||||
DeclarationStructMember, GGenericsAssignment,
|
||||
},
|
||||
DeclarationStructType, GenericIdentifier, TypedProgram,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
use zokrates_ast::typed::folder::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub struct StructConcretizer<'ast, T> {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::typed_absy::TypedModule;
|
||||
use crate::typed_absy::{TypedFunctionSymbol, TypedProgram};
|
||||
use zokrates_ast::typed::TypedModule;
|
||||
use zokrates_ast::typed::{TypedFunctionSymbol, TypedProgram};
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub struct Trimmer;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::zir::folder::*;
|
||||
use crate::zir::*;
|
||||
use std::collections::HashMap;
|
||||
use std::ops::{BitAnd, Shl, Shr};
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::zir::folder::*;
|
||||
use zokrates_ast::zir::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
//! @author Thibaut Schaeffer <thibaut@schaeff.fr>
|
||||
//! @date 2018
|
||||
|
||||
use crate::typed_absy::types::{MemberId, Type};
|
||||
use crate::typed_absy::*;
|
||||
use std::collections::HashSet;
|
||||
use zokrates_ast::typed::folder::*;
|
||||
use zokrates_ast::typed::types::{MemberId, Type};
|
||||
use zokrates_ast::typed::*;
|
||||
use zokrates_field::Field;
|
||||
|
||||
pub struct VariableWriteRemover;
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use crate::zir::result_folder::fold_statement;
|
||||
use crate::zir::result_folder::ResultFolder;
|
||||
use crate::zir::types::UBitwidth;
|
||||
use crate::zir::{
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use zokrates_ast::zir::result_folder::fold_statement;
|
||||
use zokrates_ast::zir::result_folder::ResultFolder;
|
||||
use zokrates_ast::zir::types::UBitwidth;
|
||||
use zokrates_ast::zir::{
|
||||
BooleanExpression, FieldElementExpression, Identifier, RuntimeError, UExpression,
|
||||
UExpressionInner, ZirExpression, ZirProgram, ZirStatement,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use zokrates_field::Field;
|
||||
|
||||
type Constants<'ast, T> = HashMap<Identifier<'ast>, ZirExpression<'ast, T>>;
|
||||
|
@ -704,13 +704,9 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ZirPropagator<'ast, T> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::zir::RuntimeError;
|
||||
use zokrates_ast::zir::RuntimeError;
|
||||
use zokrates_field::Bn128Field;
|
||||
|
||||
pub fn mock() -> RuntimeError {
|
||||
RuntimeError::SourceAssertion(String::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn propagation() {
|
||||
// assert([x, 1] == [y, 1])
|
||||
|
@ -725,7 +721,7 @@ mod tests {
|
|||
box FieldElementExpression::Number(Bn128Field::from(1)),
|
||||
),
|
||||
),
|
||||
mock(),
|
||||
RuntimeError::mock(),
|
||||
)];
|
||||
|
||||
let mut propagator = ZirPropagator::default();
|
||||
|
@ -745,7 +741,7 @@ mod tests {
|
|||
box FieldElementExpression::Identifier("x".into()),
|
||||
box FieldElementExpression::Identifier("y".into()),
|
||||
),
|
||||
mock()
|
||||
RuntimeError::mock()
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
|
|
@ -3,8 +3,18 @@ name = "zokrates_interpreter"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["bellman", "ark"]
|
||||
bellman = ["zokrates_field/bellman", "pairing_ce"]
|
||||
ark = ["ark-bls12-377"]
|
||||
|
||||
[dependencies]
|
||||
zokrates_field = { version = "0.5", path = "../zokrates_field", default-features = false }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast", default-features = false }
|
||||
num = { version = "0.1.36", default-features = false }
|
||||
num-bigint = { version = "0.2", default-features = false }
|
||||
pairing_ce = { version = "^0.21", optional = true }
|
||||
ark-bls12-377 = { version = "^0.3.0", features = ["curve"], default-features = false, optional = true }
|
||||
zokrates_embed = { version = "0.1.0", path = "../zokrates_embed" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use num::traits::ops::checked::CheckedDiv;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
use zokrates_ast::ir::{
|
||||
LinComb, ProgIterator, QuadComb, RuntimeError, Solver, Statement, Variable, Witness,
|
||||
|
@ -207,7 +207,7 @@ impl Interpreter {
|
|||
generate_sha256_round_witness::<Bn256>(&i, &h)
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
use bellman_ce::pairing::ff::{PrimeField, PrimeFieldRepr};
|
||||
use pairing_ce::ff::{PrimeField, PrimeFieldRepr};
|
||||
let mut res: Vec<u8> = vec![];
|
||||
x.into_repr().write_le(&mut res).unwrap();
|
||||
T::from_byte_vector(res)
|
||||
|
@ -237,7 +237,7 @@ impl Interpreter {
|
|||
#[derive(Debug)]
|
||||
pub struct EvaluationError;
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
#[derive(PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub enum Error {
|
||||
UnsatisfiedConstraint { error: Option<RuntimeError> },
|
||||
Solver,
|
||||
|
|
15
zokrates_libsnark/Cargo.toml
Normal file
15
zokrates_libsnark/Cargo.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "zokrates_libsnark"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
zokrates_proof_systems = { version = "0.1", path = "../zokrates_proof_systems" }
|
||||
|
||||
[dev-dependencies]
|
||||
zokrates_interpreter = { version = "0.1", path = "../zokrates_interpreter" }
|
||||
|
||||
[build-dependencies]
|
||||
cc = { version = "1.0", features = ["parallel"] }
|
||||
cmake = { version = "=0.1.45" }
|
|
@ -1,10 +1,4 @@
|
|||
#[cfg(feature = "libsnark")]
|
||||
extern crate cc;
|
||||
#[cfg(feature = "libsnark")]
|
||||
extern crate cmake;
|
||||
|
||||
fn main() {
|
||||
#[cfg(feature = "libsnark")]
|
||||
{
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
15
zokrates_proof_systems/Cargo.toml
Normal file
15
zokrates_proof_systems/Cargo.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "zokrates_proof_systems"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
zokrates_field = { version = "0.5.0", path = "../zokrates_field", default-features = false }
|
||||
hex = "0.4.2"
|
||||
regex = "0.2"
|
||||
cfg-if = "0.1"
|
||||
ethabi = "17.0.0"
|
||||
primitive-types = { version = "0.11", features = ["rlp"] }
|
||||
rand_0_4 = { version = "0.4", package = "rand" }
|
|
@ -1,8 +1,3 @@
|
|||
#[cfg(feature = "bellman")]
|
||||
pub mod bellman;
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub mod libsnark;
|
||||
|
||||
pub mod to_token;
|
||||
|
||||
mod scheme;
|
||||
|
@ -15,20 +10,11 @@ use zokrates_ast::ir;
|
|||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zokrates_field::{Bls12_377Field, Bls12_381Field, Bn128Field, Field};
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "bellman")] {
|
||||
use rand_0_4::Rng;
|
||||
use std::io::{Read, Write};
|
||||
use zokrates_field::BellmanFieldExtensions;
|
||||
}
|
||||
}
|
||||
use rand_0_4::Rng;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
pub trait NotBw6_761Field {}
|
||||
impl NotBw6_761Field for Bls12_377Field {}
|
||||
impl NotBw6_761Field for Bls12_381Field {}
|
||||
impl NotBw6_761Field for Bn128Field {}
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct SetupKeypair<V> {
|
||||
|
@ -50,7 +36,7 @@ pub struct Proof<T: Field, S: Scheme<T>> {
|
|||
|
||||
#[allow(dead_code)]
|
||||
impl<T: Field, S: Scheme<T>> Proof<T, S> {
|
||||
fn new(proof: S::ProofPoints, inputs: Vec<String>) -> Self {
|
||||
pub fn new(proof: S::ProofPoints, inputs: Vec<String>) -> Self {
|
||||
Proof { proof, inputs }
|
||||
}
|
||||
}
|
||||
|
@ -62,13 +48,29 @@ pub type Fq2 = (String, String);
|
|||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct G1Affine(pub Fq, pub Fq);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(untagged)]
|
||||
pub enum G2Affine {
|
||||
Fq2(G2AffineFq2),
|
||||
Fq(G2AffineFq),
|
||||
}
|
||||
|
||||
impl ToString for G2Affine {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
G2Affine::Fq(e) => e.to_string(),
|
||||
G2Affine::Fq2(e) => e.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When G2 is defined on Fq2 field
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct G2Affine(Fq2, Fq2);
|
||||
pub struct G2AffineFq2(pub Fq2, pub Fq2);
|
||||
|
||||
// When G2 is defined on a Fq field (BW6_761 curve)
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct G2AffineFq(Fq, Fq);
|
||||
pub struct G2AffineFq(pub Fq, pub Fq);
|
||||
|
||||
impl ToString for G1Affine {
|
||||
fn to_string(&self) -> String {
|
||||
|
@ -81,7 +83,7 @@ impl ToString for G2AffineFq {
|
|||
format!("{}, {}", self.0, self.1)
|
||||
}
|
||||
}
|
||||
impl ToString for G2Affine {
|
||||
impl ToString for G2AffineFq2 {
|
||||
fn to_string(&self) -> String {
|
||||
format!(
|
||||
"[{}, {}], [{}, {}]",
|
||||
|
@ -117,8 +119,7 @@ pub trait UniversalBackend<T: Field, S: UniversalScheme<T>>: Backend<T, S> {
|
|||
) -> Result<SetupKeypair<S::VerificationKey>, String>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "bellman")]
|
||||
pub trait MpcBackend<T: Field + BellmanFieldExtensions, S: Scheme<T>> {
|
||||
pub trait MpcBackend<T: Field, S: Scheme<T>> {
|
||||
fn initialize<R: Read, W: Write, I: IntoIterator<Item = ir::Statement<T>>>(
|
||||
program: ir::ProgIterator<T, I>,
|
||||
phase1_radix: &mut R,
|
|
@ -1,12 +1,9 @@
|
|||
use crate::proof_system::scheme::{NonUniversalScheme, Scheme};
|
||||
use crate::proof_system::solidity::{solidity_pairing_lib, SOLIDITY_G2_ADDITION_LIB};
|
||||
use crate::proof_system::{
|
||||
G1Affine, G2Affine, G2AffineFq, NotBw6_761Field, SolidityCompatibleField,
|
||||
SolidityCompatibleScheme,
|
||||
};
|
||||
use crate::scheme::{NonUniversalScheme, Scheme};
|
||||
use crate::solidity::{solidity_pairing_lib, SOLIDITY_G2_ADDITION_LIB};
|
||||
use crate::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme};
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zokrates_field::{Bw6_761Field, Field};
|
||||
use zokrates_field::Field;
|
||||
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
pub struct GM17;
|
||||
|
@ -28,21 +25,14 @@ pub struct VerificationKey<G1, G2> {
|
|||
pub query: Vec<G1>,
|
||||
}
|
||||
|
||||
impl<T: Field + NotBw6_761Field> NonUniversalScheme<T> for GM17 {}
|
||||
impl<T: Field> NonUniversalScheme<T> for GM17 {}
|
||||
|
||||
impl NonUniversalScheme<Bw6_761Field> for GM17 {}
|
||||
|
||||
impl<T: Field + NotBw6_761Field> Scheme<T> for GM17 {
|
||||
impl<T: Field> Scheme<T> for GM17 {
|
||||
type VerificationKey = VerificationKey<G1Affine, G2Affine>;
|
||||
type ProofPoints = ProofPoints<G1Affine, G2Affine>;
|
||||
}
|
||||
|
||||
impl Scheme<Bw6_761Field> for GM17 {
|
||||
type VerificationKey = VerificationKey<G1Affine, G2AffineFq>;
|
||||
type ProofPoints = ProofPoints<G1Affine, G2AffineFq>;
|
||||
}
|
||||
|
||||
impl<T: SolidityCompatibleField + NotBw6_761Field> SolidityCompatibleScheme<T> for GM17 {
|
||||
impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for GM17 {
|
||||
type Proof = Self::ProofPoints;
|
||||
|
||||
fn export_solidity_verifier(vk: <GM17 as Scheme<T>>::VerificationKey) -> String {
|
|
@ -1,8 +1,6 @@
|
|||
use crate::proof_system::scheme::{NonUniversalScheme, Scheme};
|
||||
use crate::proof_system::solidity::solidity_pairing_lib;
|
||||
use crate::proof_system::{
|
||||
G1Affine, G2Affine, MpcScheme, SolidityCompatibleField, SolidityCompatibleScheme,
|
||||
};
|
||||
use crate::scheme::{NonUniversalScheme, Scheme};
|
||||
use crate::solidity::solidity_pairing_lib;
|
||||
use crate::{G1Affine, G2Affine, MpcScheme, SolidityCompatibleField, SolidityCompatibleScheme};
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zokrates_field::Field;
|
|
@ -1,8 +1,6 @@
|
|||
use crate::proof_system::scheme::{Scheme, UniversalScheme};
|
||||
use crate::proof_system::solidity::{
|
||||
solidity_pairing_lib, SolidityCompatibleField, SolidityCompatibleScheme,
|
||||
};
|
||||
use crate::proof_system::{Fr, G1Affine, G2Affine, NotBw6_761Field};
|
||||
use crate::scheme::{Scheme, UniversalScheme};
|
||||
use crate::solidity::{solidity_pairing_lib, SolidityCompatibleField, SolidityCompatibleScheme};
|
||||
use crate::{Fr, G1Affine, G2Affine};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zokrates_field::Field;
|
||||
|
||||
|
@ -86,7 +84,7 @@ impl<T: Field> Scheme<T> for Marlin {
|
|||
|
||||
impl<T: Field> UniversalScheme<T> for Marlin {}
|
||||
|
||||
impl<T: SolidityCompatibleField + NotBw6_761Field> SolidityCompatibleScheme<T> for Marlin {
|
||||
impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for Marlin {
|
||||
type Proof = SolidityProof<Fr, G1Affine>;
|
||||
|
||||
fn export_solidity_verifier(vk: <Marlin as Scheme<T>>::VerificationKey) -> String {
|
|
@ -1,6 +1,6 @@
|
|||
use crate::proof_system::scheme::{NonUniversalScheme, Scheme};
|
||||
use crate::proof_system::solidity::solidity_pairing_lib;
|
||||
use crate::proof_system::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme};
|
||||
use crate::scheme::{NonUniversalScheme, Scheme};
|
||||
use crate::solidity::solidity_pairing_lib;
|
||||
use crate::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme};
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zokrates_field::Field;
|
|
@ -1,4 +1,4 @@
|
|||
use crate::proof_system::Scheme;
|
||||
use crate::Scheme;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use zokrates_field::{Bn128Field, Field};
|
||||
|
|
@ -2,8 +2,8 @@ use ethabi::Token;
|
|||
use primitive_types::U256;
|
||||
|
||||
use super::{
|
||||
Fr, G1Affine, G2Affine, Marlin, NotBw6_761Field, SolidityCompatibleField,
|
||||
SolidityCompatibleScheme, G16, GM17, PGHR13,
|
||||
Fr, G1Affine, G2Affine, Marlin, SolidityCompatibleField, SolidityCompatibleScheme, G16, GM17,
|
||||
PGHR13,
|
||||
};
|
||||
|
||||
/// Helper methods for parsing group structure
|
||||
|
@ -15,16 +15,19 @@ pub fn encode_g1_element(g: &G1Affine) -> (U256, U256) {
|
|||
}
|
||||
|
||||
pub fn encode_g2_element(g: &G2Affine) -> ((U256, U256), (U256, U256)) {
|
||||
(
|
||||
(
|
||||
U256::from(&hex::decode(&g.0 .0.trim_start_matches("0x")).unwrap()[..]),
|
||||
U256::from(&hex::decode(&g.0 .1.trim_start_matches("0x")).unwrap()[..]),
|
||||
match g {
|
||||
G2Affine::Fq2(g) => (
|
||||
(
|
||||
U256::from(&hex::decode(&g.0 .0.trim_start_matches("0x")).unwrap()[..]),
|
||||
U256::from(&hex::decode(&g.0 .1.trim_start_matches("0x")).unwrap()[..]),
|
||||
),
|
||||
(
|
||||
U256::from(&hex::decode(&g.1 .0.trim_start_matches("0x")).unwrap()[..]),
|
||||
U256::from(&hex::decode(&g.1 .1.trim_start_matches("0x")).unwrap()[..]),
|
||||
),
|
||||
),
|
||||
(
|
||||
U256::from(&hex::decode(&g.1 .0.trim_start_matches("0x")).unwrap()[..]),
|
||||
U256::from(&hex::decode(&g.1 .1.trim_start_matches("0x")).unwrap()[..]),
|
||||
),
|
||||
)
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_fr_element(f: &Fr) -> U256 {
|
||||
|
@ -124,7 +127,7 @@ impl<T: SolidityCompatibleField> ToToken<T> for G16 {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: SolidityCompatibleField + NotBw6_761Field> ToToken<T> for GM17 {
|
||||
impl<T: SolidityCompatibleField> ToToken<T> for GM17 {
|
||||
fn to_token(proof: Self::Proof) -> Token {
|
||||
let a = {
|
||||
let (x, y) = encode_g1_element(&proof.a);
|
||||
|
@ -155,7 +158,7 @@ impl<T: SolidityCompatibleField + NotBw6_761Field> ToToken<T> for GM17 {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: SolidityCompatibleField + NotBw6_761Field> ToToken<T> for Marlin {
|
||||
impl<T: SolidityCompatibleField> ToToken<T> for Marlin {
|
||||
fn to_token(proof: Self::Proof) -> Token {
|
||||
let comms_1_token = Token::Array(
|
||||
proof
|
|
@ -7,11 +7,20 @@ edition = "2018"
|
|||
[dependencies]
|
||||
zokrates_field = { version = "0.5.0", path = "../zokrates_field" }
|
||||
zokrates_core = { version = "0.6", path = "../zokrates_core" }
|
||||
zokrates_ast = { version = "0.1", path = "../zokrates_ast" }
|
||||
zokrates_interpreter = { version = "0.1", path = "../zokrates_interpreter" }
|
||||
zokrates_fs_resolver = { version = "0.5", path = "../zokrates_fs_resolver" }
|
||||
zokrates_abi = { version = "0.1", path = "../zokrates_abi" }
|
||||
zokrates_common = { version = "0.1", path = "../zokrates_common" }
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
typed-arena = "1.4.1"
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "^0.3.0"
|
||||
zokrates_ark = { version = "0.1", path = "../zokrates_ark" }
|
||||
zokrates_proof_systems = { version = "0.1", path = "../zokrates_proof_systems" }
|
||||
|
||||
|
||||
[lib]
|
||||
|
|
|
@ -5,11 +5,8 @@ use std::fs::File;
|
|||
use std::io::BufReader;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::{
|
||||
compile::{compile, CompileConfig},
|
||||
typed_absy::ConcreteType,
|
||||
};
|
||||
use zokrates_ast::typed::ConcreteType;
|
||||
use zokrates_core::compile::{compile, CompileConfig};
|
||||
use zokrates_field::{Bls12_377Field, Bls12_381Field, Bn128Field, Bw6_761Field, Field};
|
||||
use zokrates_fs_resolver::FileSystemResolver;
|
||||
|
||||
|
@ -43,7 +40,7 @@ struct Test {
|
|||
pub output: TestResult,
|
||||
}
|
||||
|
||||
type TestResult = Result<Output, ir::Error>;
|
||||
type TestResult = Result<Output, zokrates_interpreter::Error>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
|
||||
struct Output {
|
||||
|
@ -67,7 +64,10 @@ fn try_parse_abi_val<T: Field>(
|
|||
zokrates_abi::parse_strict_json(s, types).map(|v| v.encode())
|
||||
}
|
||||
|
||||
fn compare(result: Result<Output, ir::Error>, expected: TestResult) -> Result<(), String> {
|
||||
fn compare(
|
||||
result: Result<Output, zokrates_interpreter::Error>,
|
||||
expected: TestResult,
|
||||
) -> Result<(), String> {
|
||||
if result != expected {
|
||||
return Err(format!("Expected {:?} but found {:?}", expected, result));
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ fn compile_and_run<T: Field>(t: Tests) {
|
|||
);
|
||||
};
|
||||
|
||||
let interpreter = zokrates_core::ir::Interpreter::default();
|
||||
let interpreter = zokrates_interpreter::Interpreter::default();
|
||||
let with_abi = t.abi.unwrap_or(true);
|
||||
|
||||
for test in t.tests.into_iter() {
|
||||
|
@ -167,7 +167,7 @@ fn compile_and_run<T: Field>(t: Tests) {
|
|||
let output = interpreter.execute(bin.clone(), &input);
|
||||
|
||||
use zokrates_abi::Decode;
|
||||
let output: Result<Output, ir::Error> = output.map(|witness| Output {
|
||||
let output: Result<Output, zokrates_interpreter::Error> = output.map(|witness| Output {
|
||||
values: zokrates_abi::Values::decode(
|
||||
witness.return_values(),
|
||||
if with_abi {
|
||||
|
@ -178,7 +178,7 @@ fn compile_and_run<T: Field>(t: Tests) {
|
|||
.iter()
|
||||
.flat_map(|t| {
|
||||
(0..t.get_primitive_count())
|
||||
.map(|_| zokrates_core::typed_absy::ConcreteType::FieldElement)
|
||||
.map(|_| zokrates_ast::typed::ConcreteType::FieldElement)
|
||||
})
|
||||
.collect()
|
||||
},
|
||||
|
|
|
@ -6,12 +6,10 @@ use std::io;
|
|||
use typed_arena::Arena;
|
||||
use zokrates_common::Resolver;
|
||||
use zokrates_core::compile::CompileConfig;
|
||||
use zokrates_core::{
|
||||
compile::{compile, CompilationArtifacts},
|
||||
ir::Interpreter,
|
||||
};
|
||||
use zokrates_core::compile::{compile, CompilationArtifacts};
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_fs_resolver::FileSystemResolver;
|
||||
use zokrates_interpreter::Interpreter;
|
||||
|
||||
#[test]
|
||||
fn lt_field() {
|
|
@ -4,23 +4,21 @@ extern crate wasm_bindgen_test;
|
|||
extern crate zokrates_core;
|
||||
extern crate zokrates_field;
|
||||
use wasm_bindgen_test::*;
|
||||
use zokrates_core::flat_absy::{FlatParameter, FlatVariable};
|
||||
use zokrates_core::ir::{Interpreter, Prog, Statement};
|
||||
use zokrates_core::proof_system::{Backend, NonUniversalBackend};
|
||||
use zokrates_ast::flat::{Parameter, Variable};
|
||||
use zokrates_ast::ir::{Prog, Statement};
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_interpreter::Interpreter;
|
||||
use zokrates_proof_systems::{Backend, NonUniversalBackend};
|
||||
|
||||
use zokrates_core::proof_system::ark::Ark;
|
||||
use zokrates_core::proof_system::groth16::G16;
|
||||
use zokrates_ark::Ark;
|
||||
use zokrates_proof_systems::groth16::G16;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn generate_proof() {
|
||||
let program: Prog<Bn128Field> = Prog {
|
||||
arguments: vec![FlatParameter::public(FlatVariable::new(0))],
|
||||
arguments: vec![Parameter::public(Variable::new(0))],
|
||||
return_count: 1,
|
||||
statements: vec![Statement::constraint(
|
||||
FlatVariable::new(0),
|
||||
FlatVariable::new(0),
|
||||
)],
|
||||
statements: vec![Statement::constraint(Variable::new(0), Variable::new(0))],
|
||||
};
|
||||
|
||||
let interpreter = Interpreter::default();
|
Loading…
Reference in a new issue