Files
aho_corasick
ansi_term
atty
backtrace
backtrace_sys
bitflags
blindbid
block_buffer
block_padding
bulletproofs
byte_tools
byteorder
cfg_if
chrono
clap
clear_on_drop
curve25519_dalek
digest
dusk_blindbidproof
dusk_tlv
dusk_uds
env_logger
failure
failure_derive
fake_simd
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
generic_array
humantime
keccak
lazy_static
libc
log
memchr
merlin
num_cpus
num_integer
num_traits
opaque_debug
packed_simd
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quick_error
quote
rand
rand_chacha
rand_core
rand_hc
rand_isaac
rand_jitter
rand_os
rand_pcg
rand_xorshift
regex
regex_syntax
rustc_demangle
serde
serde_derive
sha2
sha3
slab
strsim
subtle
syn
synstructure
termcolor
textwrap
thread_local
time
typenum
unicode_width
unicode_xid
vec_map
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use super::{generate_cs_transcript, Proof, CONSTANTS};
use crate::gadgets::proof_gadget;
use crate::Error;

use std::convert::TryFrom;
use std::io::Read;

use bulletproofs::r1cs::Verifier;
use bulletproofs::r1cs::{LinearCombination, R1CSProof, Variable};
use curve25519_dalek::ristretto::CompressedRistretto;
use curve25519_dalek::scalar::Scalar;
use dusk_tlv::TlvReader;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Verify {
    pub proof: R1CSProof,
    pub commitments: Vec<CompressedRistretto>,
    pub t_c: Vec<CompressedRistretto>,
    pub score: Scalar,
    pub z_img: Scalar,
    pub seed: Scalar,
    pub pub_list: Vec<Scalar>,
}

impl Verify {
    pub fn new(
        proof: R1CSProof,
        commitments: Vec<CompressedRistretto>,
        t_c: Vec<CompressedRistretto>,
        score: Scalar,
        z_img: Scalar,
        seed: Scalar,
        pub_list: Vec<Scalar>,
    ) -> Self {
        Verify {
            proof,
            commitments,
            t_c,
            score,
            z_img,
            seed,
            pub_list,
        }
    }

    pub fn verify(&self) -> Result<(), Error> {
        let (pc_gens, bp_gens, mut transcript) = generate_cs_transcript();

        // 1. Create a verifier
        let mut verifier = Verifier::new(&mut transcript);

        // 2. Commit high-level variables
        let vars: Vec<_> = self
            .commitments
            .iter()
            .map(|v| verifier.commit(*v))
            .collect();

        let t_c_v: Vec<Variable> = self
            .t_c
            .iter()
            .map(|v| verifier.commit(*v).into())
            .collect();

        // public list of numbers
        let l_v: Vec<LinearCombination> = self
            .pub_list
            .iter()
            .map(|&x| Scalar::from(x).into())
            .collect::<Vec<_>>();

        // 3. Build a CS
        proof_gadget(
            &mut verifier,
            vars[0].into(),
            vars[1].into(),
            vars[3].into(),
            self.score.into(),
            self.z_img.into(),
            self.seed.into(),
            &*CONSTANTS,
            t_c_v,
            l_v,
        );

        // 4. Verify the proof
        Ok(verifier.verify(&self.proof, &pc_gens, &bp_gens)?)
    }

    pub fn try_from_reader_variables<R: Read>(reader: R) -> Result<Self, Error> {
        let mut reader = TlvReader::new(reader);

        let proof = reader
            .next()
            .ok_or(Error::io_unexpected_eof("No proof data was provided"))??;
        let proof = Proof::try_from(proof)?;
        let (proof, commitments, t_c) = (proof.proof, proof.commitments, proof.t_c);

        let score = Deserialize::deserialize(&mut reader)?;
        let z_img = Deserialize::deserialize(&mut reader)?;
        let seed = Deserialize::deserialize(&mut reader)?;

        let mut pub_list = vec![];
        for bytes in reader.read_list::<Vec<u8>>()? {
            if bytes.len() != 32 {
                return Err(Error::io_invalid_data(
                    "Scalars Ristrettos can only be created from 32 bytes slices",
                ));
            }

            let mut p = [0x00u8; 32];
            p.copy_from_slice(bytes.as_slice());

            let p = Scalar::from_bits(p);
            pub_list.push(p);
        }

        Ok(Verify::new(
            proof,
            commitments,
            t_c,
            score,
            z_img,
            seed,
            pub_list,
        ))
    }
}