1
0
mirror of https://github.com/fluencelabs/lazy-snark synced 2025-06-26 13:11:49 +00:00

add fluence mock back

This commit is contained in:
Drygin Alexander
2019-06-22 23:24:01 +03:00
parent 39cdc6e0e5
commit e46caf1bb1
589 changed files with 2562 additions and 0 deletions
backend_fluence
.idea
Cargo.lockCargo.tomlrust-toolchain
src
target
.rustc_info.json
release
.cargo-lock
.fingerprint
arrayvec-ea55a7b21e304da7
autocfg-a21adae664065943
autocfg-ba156e2488837a29
byteorder-e7b4102bc0021d30
cfg-if-6c9c1b9b1a7d8d7a
cfg-if-daabbf7333629fd7
ff_derive_ce-c5ae21c1f8ab5ac7
fluence-sdk-macro-1c145f93000bea20
fluence-sdk-macro-59c1eea12a9b2c0d
fluence-sdk-main-6a27385252a26784
fluence-sdk-main-c564be5e2fc1903f
libc-b4ce19d8090a783a
log-38b7c2175f00b0f3
log-f76806c89c8607f3
num-bigint-66a8b9fa7a80d758
num-bigint-7ba5afc88e8c7a05
num-bigint-86a8ab2e4cc571ce
num-integer-0a2c58f76f00762b
num-integer-406fc3efec8aed11
num-integer-e8194b2713db02ec
num-traits-aef35c6e6450aa43
num-traits-b09213e89d96dd78
num-traits-b51f66da3c55f140
proc-macro2-9fe724c5f79cd05c
proc-macro2-a63c5f1e11c7060b
proc-macro2-ead4a744d39d3147
quote-99b34e2ee1846fb4
rand-27d1c77cb9bd74b4
rand_chacha-8528f9ea58c1ad98
rand_pcg-e58a7e152f11a848
ryu-0c4efe1ab59118e7
serde-d2d9e67dc1cdccdc
serde_derive-653c9486920110a7
syn-4f8a64e2e5dc6e89
syn-722248f601b148c3
unicode-xid-33d131be9b306246
deps
autocfg-a21adae664065943.dautocfg-ba156e2488837a29.dcfg_if-6c9c1b9b1a7d8d7a.dcfg_if-daabbf7333629fd7.dff_derive_ce-c5ae21c1f8ab5ac7.dfluence_sdk_macro-1c145f93000bea20.dfluence_sdk_macro-59c1eea12a9b2c0d.dfluence_sdk_main-6a27385252a26784.dfluence_sdk_main-c564be5e2fc1903f.dlibautocfg-a21adae664065943.rliblibautocfg-a21adae664065943.rmetalibautocfg-ba156e2488837a29.rliblibautocfg-ba156e2488837a29.rmetalibcfg_if-6c9c1b9b1a7d8d7a.rliblibcfg_if-6c9c1b9b1a7d8d7a.rmetalibcfg_if-daabbf7333629fd7.rliblibcfg_if-daabbf7333629fd7.rmetalibff_derive_ce-c5ae21c1f8ab5ac7.solibfluence_sdk_macro-1c145f93000bea20.solibfluence_sdk_macro-59c1eea12a9b2c0d.solibfluence_sdk_main-6a27385252a26784.rliblibfluence_sdk_main-6a27385252a26784.rmetalibfluence_sdk_main-c564be5e2fc1903f.rliblibfluence_sdk_main-c564be5e2fc1903f.rmetaliblog-38b7c2175f00b0f3.rlibliblog-38b7c2175f00b0f3.rmetaliblog-f76806c89c8607f3.rlibliblog-f76806c89c8607f3.rmetalibnum_bigint-86a8ab2e4cc571ce.rliblibnum_bigint-86a8ab2e4cc571ce.rmetalibnum_integer-e8194b2713db02ec.rliblibnum_integer-e8194b2713db02ec.rmetalibnum_traits-b09213e89d96dd78.rliblibnum_traits-b09213e89d96dd78.rmetalibproc_macro2-a63c5f1e11c7060b.rliblibproc_macro2-a63c5f1e11c7060b.rmetalibquote-99b34e2ee1846fb4.rliblibquote-99b34e2ee1846fb4.rmetalibserde_derive-653c9486920110a7.solibsyn-4f8a64e2e5dc6e89.rliblibsyn-4f8a64e2e5dc6e89.rmetalibsyn-722248f601b148c3.rliblibsyn-722248f601b148c3.rmetalibunicode_xid-33d131be9b306246.rliblibunicode_xid-33d131be9b306246.rmetalog-38b7c2175f00b0f3.dlog-f76806c89c8607f3.dnum_bigint-86a8ab2e4cc571ce.dnum_integer-e8194b2713db02ec.dnum_traits-b09213e89d96dd78.dproc_macro2-a63c5f1e11c7060b.dquote-99b34e2ee1846fb4.dserde_derive-653c9486920110a7.dsyn-4f8a64e2e5dc6e89.dsyn-722248f601b148c3.dunicode_xid-33d131be9b306246.d
wasm32-unknown-unknown
release
.cargo-lock
.fingerprint
arrayvec-26a712ec6eb8a596
arrayvec-e4d13d11b9c8e583
bellman_ce-45b031182769eed9
bit-vec-65b93952bb20551d
byteorder-2d0b1c06a365e0b8
byteorder-3c51ba592e6d3b50
cfg-if-11151b130f6208d0
cfg-if-90a93e2e13b6a483
crossbeam-582a84922b4d48d2
crossbeam-channel-a24f62230ef8a42c
crossbeam-deque-0ecf36f3818a0e84
crossbeam-epoch-0f1f8be3bd1f8b1e
crossbeam-queue-b0f8473866f717ac
crossbeam-utils-ac9b224bbe836056
dice-game-c860f00935fec605
ff_ce-7207766552b1a30f
fluence-2547ab1e78be0ac7
fluence-b7def85b68ba4b54
fluence-sdk-main-9679e542f83dccf0
fluence-sdk-main-e7a6a85e164e889e
futures-38e96e86071d74fb
futures-cpupool-550de56f1d8d1a24
hex-55bc06e734fdbc7c
itoa-ec63c91963630276
lazy_static-69049182576e0fa8
libc-36f745807a4fb21f
libc-55326d5584ab0a77
linked-hash-map-aa5546e76ac9b1be
log-b8727f7386747703
log-cc2cd388e376df6a
memoffset-3b5a58fb4cfaacbd
nodrop-d52ffa4a3305d830
num_cpus-da102c30c4b59bff
pairing_ce-41ba5819410f2c41
proc-macro2-a3e8f7c2c37c81da
proc-macro2-c831c221dd6c46cd
proof-980ed51e7fdbef8f
quote-1a9295e549dcc272
rand-4bc00f362b2262d3
rand-80814ef5a49ed624
rand-9128a36de5dcf07f
rand_chacha-5dfd7de6154814fd
rand_chacha-eaee9907d1289394
rand_core-57c1dffc6685b28e
rand_core-6fd76655bf5441fa
rand_hc-1fad6f195875b656
rand_isaac-c2d9f323490df63f
rand_jitter-fb9535fb5fbf048d
rand_os-2d64b54ed1cc87a7
rand_pcg-1b5944f201fd435d
rand_pcg-6d86cb130048d540
rand_xorshift-8ee3f3def2891a8d
ryu-192e7cc650fc687a
ryu-2756c64f98b85c08
scopeguard-4c6af0f8d2b4f98f
serde-2b62fd1d30c91ea9
serde-b23efdeec764650d
serde_json-6c1f727a9c510d64
smallvec-04cff9e897a28769
syn-fb4d2419a727852e
unicode-xid-b0dd144b6454e258
deps
arrayvec-e4d13d11b9c8e583.dbellman_ce-45b031182769eed9.dbellman_ce-45b031182769eed9.wasmbit_vec-65b93952bb20551d.dbyteorder-3c51ba592e6d3b50.dcfg_if-11151b130f6208d0.dcfg_if-90a93e2e13b6a483.dcrossbeam-582a84922b4d48d2.dcrossbeam_channel-a24f62230ef8a42c.dcrossbeam_deque-0ecf36f3818a0e84.dcrossbeam_epoch-0f1f8be3bd1f8b1e.dcrossbeam_queue-b0f8473866f717ac.dcrossbeam_utils-ac9b224bbe836056.ddice_game.ddice_game.wasmff_ce-7207766552b1a30f.dfluence-2547ab1e78be0ac7.dfluence-b7def85b68ba4b54.dfluence_sdk_main-9679e542f83dccf0.dfluence_sdk_main-e7a6a85e164e889e.dfutures-38e96e86071d74fb.dfutures_cpupool-550de56f1d8d1a24.dhex-55bc06e734fdbc7c.ditoa-ec63c91963630276.dlazy_static-69049182576e0fa8.dlibarrayvec-e4d13d11b9c8e583.rliblibarrayvec-e4d13d11b9c8e583.rmetalibbellman_ce-45b031182769eed9.alibbellman_ce-45b031182769eed9.rliblibbit_vec-65b93952bb20551d.rliblibbit_vec-65b93952bb20551d.rmetalibbyteorder-3c51ba592e6d3b50.rliblibbyteorder-3c51ba592e6d3b50.rmetalibc-36f745807a4fb21f.dlibcfg_if-11151b130f6208d0.rliblibcfg_if-11151b130f6208d0.rmetalibcfg_if-90a93e2e13b6a483.rliblibcfg_if-90a93e2e13b6a483.rmetalibcrossbeam-582a84922b4d48d2.rliblibcrossbeam-582a84922b4d48d2.rmetalibcrossbeam_channel-a24f62230ef8a42c.rliblibcrossbeam_channel-a24f62230ef8a42c.rmetalibcrossbeam_deque-0ecf36f3818a0e84.rliblibcrossbeam_deque-0ecf36f3818a0e84.rmetalibcrossbeam_epoch-0f1f8be3bd1f8b1e.rliblibcrossbeam_epoch-0f1f8be3bd1f8b1e.rmetalibcrossbeam_queue-b0f8473866f717ac.rliblibcrossbeam_queue-b0f8473866f717ac.rmetalibcrossbeam_utils-ac9b224bbe836056.rliblibcrossbeam_utils-ac9b224bbe836056.rmetalibff_ce-7207766552b1a30f.rliblibff_ce-7207766552b1a30f.rmetalibfluence-2547ab1e78be0ac7.rliblibfluence-2547ab1e78be0ac7.rmetalibfluence-b7def85b68ba4b54.rliblibfluence-b7def85b68ba4b54.rmetalibfluence_sdk_main-9679e542f83dccf0.rliblibfluence_sdk_main-9679e542f83dccf0.rmetalibfluence_sdk_main-e7a6a85e164e889e.rliblibfluence_sdk_main-e7a6a85e164e889e.rmetalibfutures-38e96e86071d74fb.rliblibfutures-38e96e86071d74fb.rmetalibfutures_cpupool-550de56f1d8d1a24.rliblibfutures_cpupool-550de56f1d8d1a24.rmetalibhex-55bc06e734fdbc7c.rliblibhex-55bc06e734fdbc7c.rmetalibitoa-ec63c91963630276.rliblibitoa-ec63c91963630276.rmetaliblazy_static-69049182576e0fa8.rlibliblazy_static-69049182576e0fa8.rmetaliblibc-36f745807a4fb21f.rlibliblibc-36f745807a4fb21f.rmetaliblinked_hash_map-aa5546e76ac9b1be.rlibliblinked_hash_map-aa5546e76ac9b1be.rmetaliblog-b8727f7386747703.rlibliblog-b8727f7386747703.rmetaliblog-cc2cd388e376df6a.rlibliblog-cc2cd388e376df6a.rmetalibmemoffset-3b5a58fb4cfaacbd.rliblibmemoffset-3b5a58fb4cfaacbd.rmetalibnodrop-d52ffa4a3305d830.rliblibnodrop-d52ffa4a3305d830.rmetalibnum_cpus-da102c30c4b59bff.rliblibnum_cpus-da102c30c4b59bff.rmetalibpairing_ce-41ba5819410f2c41.rliblibpairing_ce-41ba5819410f2c41.rmetalibproc_macro2-a3e8f7c2c37c81da.rliblibproc_macro2-a3e8f7c2c37c81da.rmetalibquote-1a9295e549dcc272.rliblibquote-1a9295e549dcc272.rmetalibrand-80814ef5a49ed624.rliblibrand-80814ef5a49ed624.rmetalibrand-9128a36de5dcf07f.rliblibrand-9128a36de5dcf07f.rmetalibrand_chacha-5dfd7de6154814fd.rliblibrand_chacha-5dfd7de6154814fd.rmetalibrand_core-57c1dffc6685b28e.rliblibrand_core-57c1dffc6685b28e.rmetalibrand_core-6fd76655bf5441fa.rliblibrand_core-6fd76655bf5441fa.rmetalibrand_hc-1fad6f195875b656.rliblibrand_hc-1fad6f195875b656.rmetalibrand_isaac-c2d9f323490df63f.rliblibrand_isaac-c2d9f323490df63f.rmetalibrand_jitter-fb9535fb5fbf048d.rliblibrand_jitter-fb9535fb5fbf048d.rmetalibrand_os-2d64b54ed1cc87a7.rliblibrand_os-2d64b54ed1cc87a7.rmetalibrand_pcg-1b5944f201fd435d.rliblibrand_pcg-1b5944f201fd435d.rmetalibrand_xorshift-8ee3f3def2891a8d.rliblibrand_xorshift-8ee3f3def2891a8d.rmetalibryu-192e7cc650fc687a.rliblibryu-192e7cc650fc687a.rmetalibscopeguard-4c6af0f8d2b4f98f.rliblibscopeguard-4c6af0f8d2b4f98f.rmetalibserde-b23efdeec764650d.rliblibserde-b23efdeec764650d.rmetalibserde_json-6c1f727a9c510d64.rliblibserde_json-6c1f727a9c510d64.rmetalibsmallvec-04cff9e897a28769.rliblibsmallvec-04cff9e897a28769.rmetalibsyn-fb4d2419a727852e.rliblibsyn-fb4d2419a727852e.rmetalibunicode_xid-b0dd144b6454e258.rliblibunicode_xid-b0dd144b6454e258.rmetalinked_hash_map-aa5546e76ac9b1be.dlog-b8727f7386747703.dlog-cc2cd388e376df6a.dmemoffset-3b5a58fb4cfaacbd.dnodrop-d52ffa4a3305d830.dnum_cpus-da102c30c4b59bff.dpairing_ce-41ba5819410f2c41.dproc_macro2-a3e8f7c2c37c81da.dproof.dproof.wasmquote-1a9295e549dcc272.drand-80814ef5a49ed624.drand-9128a36de5dcf07f.drand_chacha-5dfd7de6154814fd.drand_core-57c1dffc6685b28e.drand_core-6fd76655bf5441fa.drand_hc-1fad6f195875b656.drand_isaac-c2d9f323490df63f.drand_jitter-fb9535fb5fbf048d.drand_os-2d64b54ed1cc87a7.drand_pcg-1b5944f201fd435d.drand_xorshift-8ee3f3def2891a8d.dryu-192e7cc650fc687a.dscopeguard-4c6af0f8d2b4f98f.dserde-b23efdeec764650d.dserde_json-6c1f727a9c510d64.dsmallvec-04cff9e897a28769.dsyn-fb4d2419a727852e.dunicode_xid-b0dd144b6454e258.d
proof.dproof.wasm

@ -0,0 +1,19 @@
/*
* Copyright 2018 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::error::Error;
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;

@ -0,0 +1,46 @@
mod error_type;
mod proof_manager;
mod request_response;
use crate::error_type::AppResult;
use crate::proof_manager::ProofManager;
use crate::request_response::{Request, Response};
use fluence::sdk::*;
use serde_json::Value;
use std::cell::RefCell;
thread_local! {
static PROOF_MANAGER: RefCell<ProofManager> = RefCell::new(ProofManager::new());
}
fn do_request(req: String) -> AppResult<Value> {
let request: Request = serde_json::from_str(req.as_str())?;
match request {
Request::Verify {
proof_id,
public_par,
proof,
} => PROOF_MANAGER.with(|gm| gm.borrow_mut().verify(proof_id, public_par, proof)),
Request::Check {
proof_id,
} => PROOF_MANAGER.with(|gm| gm.borrow_mut().check(proof_id))
}
}
#[invocation_handler]
fn main(req: String) -> String {
match do_request(req) {
Ok(res) => res.to_string(),
Err(err) => {
let response = Response::Error {
message: err.to_string(),
};
serde_json::to_string(&response).unwrap()
}
}
}

@ -0,0 +1,359 @@
use crate::error_type::AppResult;
use crate::request_response::Response;
use bellman_ce::groth16::*;
use linked_hash_map::LinkedHashMap;
use serde_json::Value;
pub struct ProofManager {
// map from job id to verify status
proofs: LinkedHashMap<u64, u8>,
}
impl ProofManager {
pub fn new() -> Self {
ProofManager {
proofs: LinkedHashMap::new(),
}
}
pub fn verify(&mut self, proof_id: u64, public_par: [u64; 5], proof: [u64; 8]) -> AppResult<Value> {
// verify mock
let result: u8 = 1;
//test_xordemo();
// update proof status
self.proofs.insert(proof_id, result);
let response = Response::Verify {
result : result,
};
serde_json::to_value(response).map_err(Into::into)
}
pub fn check(&self, proof_id: u64) -> AppResult<Value> {
let status = self.proof_status(proof_id)?;
let response = Response::Check { verifed: status };
serde_json::to_value(response).map_err(Into::into)
}
fn proof_status(&self, proof_id: u64) -> AppResult<u8> {
let status = self
.proofs
.get(&proof_id)
.ok_or_else(|| format!("Proof with id {} wasn't found", proof_id))?;
Ok(*status)
}
/*
fn test_xordemo() {
let g1 = Fr::one();
let g2 = Fr::one();
let alpha = Fr::from_str("48577").unwrap();
let beta = Fr::from_str("22580").unwrap();
let gamma = Fr::from_str("53332").unwrap();
let delta = Fr::from_str("5481").unwrap();
let tau = Fr::from_str("3673").unwrap();
let params = {
let c = XORDemo::<DummyEngine> {
a: None,
b: None,
_marker: PhantomData
};
generate_parameters(
c,
g1,
g2,
alpha,
beta,
gamma,
delta,
tau
).unwrap()
};
// This will synthesize the constraint system:
//
// public inputs: a_0 = 1, a_1 = c
// aux inputs: a_2 = a, a_3 = b
// constraints:
// (a_0 - a_2) * (a_2) = 0
// (a_0 - a_3) * (a_3) = 0
// (a_2 + a_2) * (a_3) = (a_2 + a_3 - a_1)
// (a_0) * 0 = 0
// (a_1) * 0 = 0
// The evaluation domain is 8. The H query should
// have 7 elements (it's a quotient polynomial)
assert_eq!(7, params.h.len());
let mut root_of_unity = Fr::root_of_unity();
// We expect this to be a 2^10 root of unity
assert_eq!(Fr::one(), root_of_unity.pow(&[1 << 10]));
// Let's turn it into a 2^3 root of unity.
root_of_unity = root_of_unity.pow(&[1 << 7]);
assert_eq!(Fr::one(), root_of_unity.pow(&[1 << 3]));
assert_eq!(Fr::from_str("20201").unwrap(), root_of_unity);
// Let's compute all the points in our evaluation domain.
let mut points = Vec::with_capacity(8);
for i in 0..8 {
points.push(root_of_unity.pow(&[i]));
}
// Let's compute t(tau) = (tau - p_0)(tau - p_1)...
// = tau^8 - 1
let mut t_at_tau = tau.pow(&[8]);
t_at_tau.sub_assign(&Fr::one());
{
let mut tmp = Fr::one();
for p in &points {
let mut term = tau;
term.sub_assign(p);
tmp.mul_assign(&term);
}
assert_eq!(tmp, t_at_tau);
}
// We expect our H query to be 7 elements of the form...
// {tau^i t(tau) / delta}
let delta_inverse = delta.inverse().unwrap();
let gamma_inverse = gamma.inverse().unwrap();
{
let mut coeff = delta_inverse;
coeff.mul_assign(&t_at_tau);
let mut cur = Fr::one();
for h in params.h.iter() {
let mut tmp = cur;
tmp.mul_assign(&coeff);
assert_eq!(*h, tmp);
cur.mul_assign(&tau);
}
}
// The density of the IC query is 2 (2 inputs)
assert_eq!(2, params.vk.ic.len());
// The density of the L query is 2 (2 aux variables)
assert_eq!(2, params.l.len());
// The density of the A query is 4 (each variable is in at least one A term)
assert_eq!(4, params.a.len());
// The density of the B query is 2 (two variables are in at least one B term)
assert_eq!(2, params.b_g1.len());
assert_eq!(2, params.b_g2.len());
/*
Lagrange interpolation polynomials in our evaluation domain:
,-------------------------------. ,-------------------------------. ,-------------------------------.
| A TERM | | B TERM | | C TERM |
`-------------------------------. `-------------------------------' `-------------------------------'
| a_0 | a_1 | a_2 | a_3 | | a_0 | a_1 | a_2 | a_3 | | a_0 | a_1 | a_2 | a_3 |
| 1 | 0 | 64512 | 0 | | 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 64512 | | 0 | 0 | 0 | 1 | | 0 | 0 | 0 | 0 |
| 0 | 0 | 2 | 0 | | 0 | 0 | 0 | 1 | | 0 | 64512 | 1 | 1 |
| 1 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 | | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 |
`-------'-------'-------'-------' `-------'-------'-------'-------' `-------'-------'-------'-------'
Example for u_0:
sage: r = 64513
sage: Fr = GF(r)
sage: omega = (Fr(5)^63)^(2^7)
sage: tau = Fr(3673)
sage: R.<x> = PolynomialRing(Fr, 'x')
sage: def eval(tau, c0, c1, c2, c3, c4):
....: p = R.lagrange_polynomial([(omega^0, c0), (omega^1, c1), (omega^2, c2), (omega^3, c3), (omega^4, c4), (omega^5, 0), (omega^6, 0), (omega^7, 0)])
....: return p.substitute(tau)
sage: eval(tau, 1, 1, 0, 1, 0)
59158
*/
let u_i = [59158, 48317, 21767, 10402].iter().map(|e| {
Fr::from_str(&format!("{}", e)).unwrap()
}).collect::<Vec<Fr>>();
let v_i = [0, 0, 60619, 30791].iter().map(|e| {
Fr::from_str(&format!("{}", e)).unwrap()
}).collect::<Vec<Fr>>();
let w_i = [0, 23320, 41193, 41193].iter().map(|e| {
Fr::from_str(&format!("{}", e)).unwrap()
}).collect::<Vec<Fr>>();
for (u, a) in u_i.iter()
.zip(&params.a[..])
{
assert_eq!(u, a);
}
for (v, b) in v_i.iter()
.filter(|&&e| e != Fr::zero())
.zip(&params.b_g1[..])
{
assert_eq!(v, b);
}
for (v, b) in v_i.iter()
.filter(|&&e| e != Fr::zero())
.zip(&params.b_g2[..])
{
assert_eq!(v, b);
}
for i in 0..4 {
let mut tmp1 = beta;
tmp1.mul_assign(&u_i[i]);
let mut tmp2 = alpha;
tmp2.mul_assign(&v_i[i]);
tmp1.add_assign(&tmp2);
tmp1.add_assign(&w_i[i]);
if i < 2 {
// Check the correctness of the IC query elements
tmp1.mul_assign(&gamma_inverse);
assert_eq!(tmp1, params.vk.ic[i]);
} else {
// Check the correctness of the L query elements
tmp1.mul_assign(&delta_inverse);
assert_eq!(tmp1, params.l[i - 2]);
}
}
// Check consistency of the other elements
assert_eq!(alpha, params.vk.alpha_g1);
assert_eq!(beta, params.vk.beta_g1);
assert_eq!(beta, params.vk.beta_g2);
assert_eq!(gamma, params.vk.gamma_g2);
assert_eq!(delta, params.vk.delta_g1);
assert_eq!(delta, params.vk.delta_g2);
let pvk = prepare_verifying_key(&params.vk);
let r = Fr::from_str("27134").unwrap();
let s = Fr::from_str("17146").unwrap();
let proof = {
let c = XORDemo {
a: Some(true),
b: Some(false),
_marker: PhantomData
};
create_proof(
c,
&params,
r,
s
).unwrap()
};
// A(x) =
// a_0 * (44865*x^7 + 56449*x^6 + 44865*x^5 + 8064*x^4 + 3520*x^3 + 56449*x^2 + 3520*x + 40321) +
// a_1 * (8064*x^7 + 56449*x^6 + 8064*x^5 + 56449*x^4 + 8064*x^3 + 56449*x^2 + 8064*x + 56449) +
// a_2 * (16983*x^7 + 24192*x^6 + 63658*x^5 + 56449*x^4 + 16983*x^3 + 24192*x^2 + 63658*x + 56449) +
// a_3 * (5539*x^7 + 27797*x^6 + 6045*x^5 + 56449*x^4 + 58974*x^3 + 36716*x^2 + 58468*x + 8064) +
{
// proof A = alpha + A(tau) + delta * r
let mut expected_a = delta;
expected_a.mul_assign(&r);
expected_a.add_assign(&alpha);
expected_a.add_assign(&u_i[0]); // a_0 = 1
expected_a.add_assign(&u_i[1]); // a_1 = 1
expected_a.add_assign(&u_i[2]); // a_2 = 1
// a_3 = 0
assert_eq!(proof.a, expected_a);
}
// B(x) =
// a_0 * (0) +
// a_1 * (0) +
// a_2 * (56449*x^7 + 56449*x^6 + 56449*x^5 + 56449*x^4 + 56449*x^3 + 56449*x^2 + 56449*x + 56449) +
// a_3 * (31177*x^7 + 44780*x^6 + 21752*x^5 + 42255*x^3 + 35861*x^2 + 33842*x + 48385)
{
// proof B = beta + B(tau) + delta * s
let mut expected_b = delta;
expected_b.mul_assign(&s);
expected_b.add_assign(&beta);
expected_b.add_assign(&v_i[0]); // a_0 = 1
expected_b.add_assign(&v_i[1]); // a_1 = 1
expected_b.add_assign(&v_i[2]); // a_2 = 1
// a_3 = 0
assert_eq!(proof.b, expected_b);
}
// C(x) =
// a_0 * (0) +
// a_1 * (27797*x^7 + 56449*x^6 + 36716*x^5 + 8064*x^4 + 27797*x^3 + 56449*x^2 + 36716*x + 8064) +
// a_2 * (36716*x^7 + 8064*x^6 + 27797*x^5 + 56449*x^4 + 36716*x^3 + 8064*x^2 + 27797*x + 56449) +
// a_3 * (36716*x^7 + 8064*x^6 + 27797*x^5 + 56449*x^4 + 36716*x^3 + 8064*x^2 + 27797*x + 56449)
//
// If A * B = C at each point in the domain, then the following polynomial...
// P(x) = A(x) * B(x) - C(x)
// = 49752*x^14 + 13914*x^13 + 29243*x^12 + 27227*x^11 + 62362*x^10 + 35703*x^9 + 4032*x^8 + 14761*x^6 + 50599*x^5 + 35270*x^4 + 37286*x^3 + 2151*x^2 + 28810*x + 60481
//
// ... should be divisible by t(x), producing the quotient polynomial:
// h(x) = P(x) / t(x)
// = 49752*x^6 + 13914*x^5 + 29243*x^4 + 27227*x^3 + 62362*x^2 + 35703*x + 4032
{
let mut expected_c = Fr::zero();
// A * s
let mut tmp = proof.a;
tmp.mul_assign(&s);
expected_c.add_assign(&tmp);
// B * r
let mut tmp = proof.b;
tmp.mul_assign(&r);
expected_c.add_assign(&tmp);
// delta * r * s
let mut tmp = delta;
tmp.mul_assign(&r);
tmp.mul_assign(&s);
expected_c.sub_assign(&tmp);
// L query answer
// a_2 = 1, a_3 = 0
expected_c.add_assign(&params.l[0]);
// H query answer
for (i, coeff) in [5040, 11763, 10755, 63633, 128, 9747, 8739].iter().enumerate() {
let coeff = Fr::from_str(&format!("{}", coeff)).unwrap();
let mut tmp = params.h[i];
tmp.mul_assign(&coeff);
expected_c.add_assign(&tmp);
}
assert_eq!(expected_c, proof.c);
}
assert!(verify_proof(
&pvk,
&proof,
&[Fr::one()]
).unwrap());
}*/
}

@ -0,0 +1,23 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[serde(tag = "action")]
pub enum Request {
Verify {
proof_id: u64,
public_par: [u64; 5],
proof: [u64; 8],
},
Check {
proof_id: u64,
},
}
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum Response {
Verify { result: u8 },
Check { verifed: u8 },
Error { message: String },
}

@ -0,0 +1,130 @@
/*
* Copyright 2018 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::request_response::{Request, Response};
use crate::main;
// TODO: add more tests
#[test]
fn correct_bets() {
let response = Response::Join { player_id: 0 };
assert_eq!(
main(create_join_request()),
serde_json::to_string(&response).unwrap()
);
let response = Response::Join { player_id: 1 };
assert_eq!(
main(create_join_request()),
serde_json::to_string(&response).unwrap()
);
let response = Response::Bet {
outcome: 6,
player_balance: 85,
};
assert_eq!(
main(create_bet_json(0, 1, 15)),
serde_json::to_string(&response).unwrap()
);
let response = Response::Bet {
outcome: 4,
player_balance: 85,
};
assert_eq!(
main(create_bet_json(1, 1, 15)),
serde_json::to_string(&response).unwrap()
);
let response = Response::Bet {
outcome: 6,
player_balance: 510,
};
assert_eq!(
main(create_bet_json(0, 6, 85)),
serde_json::to_string(&response).unwrap()
);
let response = Response::Bet {
outcome: 2,
player_balance: 0,
};
assert_eq!(
main(create_bet_json(1, 1, 85)),
serde_json::to_string(&response).unwrap()
);
let response = Response::GetBalance {
player_balance: 510,
};
assert_eq!(
main(create_get_balance_json(0)),
serde_json::to_string(&response).unwrap()
);
}
#[test]
fn incorrect_bets() {
let response = Response::Join { player_id: 0 };
assert_eq!(
main(create_join_request()),
serde_json::to_string(&response).unwrap()
);
let response = Response::Error {
message: "Incorrect placement, please choose number from 1 to 6".to_string(),
};
assert_eq!(
main(create_bet_json(0, 7, 15)),
serde_json::to_string(&response).unwrap()
);
let response = Response::Error {
message: "Player with id 1 wasn\'t found".to_string(),
};
assert_eq!(
main(create_bet_json(1, 1, 0)),
serde_json::to_string(&response).unwrap()
);
let response = Response::Error {
message: "Player hasn\'t enough money: player\'s current balance is 100 while the bet is 4294967295".to_string()
};
assert_eq!(
main(create_bet_json(0, 6, std::u32::MAX)),
serde_json::to_string(&response).unwrap()
);
}
fn create_join_request() -> String {
let request = Request::Join;
serde_json::to_value(request).unwrap().to_string()
}
fn create_bet_json(player_id: u64, placement: u8, bet_amount: u32) -> String {
let request = Request::Bet {
player_id,
placement,
bet_amount,
};
serde_json::to_value(request).unwrap().to_string()
}
fn create_get_balance_json(player_id: u64) -> String {
let request = Request::GetBalance { player_id };
serde_json::to_value(request).unwrap().to_string()
}