@ -1,121 +1,73 @@ |
|||
This repository includes the following third-party open-source code. |
|||
|
|||
* The code in src/scalar/ristretto255.rs is derived from [bls12-381](https://github.com/zkcrypto/bls12_381). |
|||
* The code in `src/scalar/ristretto255.rs` is derived from [bls12-381](https://github.com/zkcrypto/bls12_381). |
|||
Specifically, from [src/bls12_381/scalar.rs](https://github.com/zkcrypto/bls12_381/blob/master/src/scalar.rs) and [src/bls12_381/util.rs](https://github.com/zkcrypto/bls12_381/blob/master/src/util.rs), which has the following copyright and license. |
|||
|
|||
Permission is hereby granted, free of charge, to any |
|||
person obtaining a copy of this software and associated |
|||
documentation files (the "Software"), to deal in the |
|||
Software without restriction, including without |
|||
limitation the rights to use, copy, modify, merge, |
|||
publish, distribute, sublicense, and/or sell copies of |
|||
the Software, and to permit persons to whom the Software |
|||
is furnished to do so, subject to the following |
|||
conditions: |
|||
|
|||
The above copyright notice and this permission notice |
|||
shall be included in all copies or substantial portions |
|||
of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
|||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
|||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
|||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
|||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|||
DEALINGS IN THE SOFTWARE. |
|||
|
|||
|
|||
* The invert and batch_invert methods in src/scalar/ristretto255.rs is from [curve25519-dalek](https://github.com/dalek-cryptography/curve25519-dalek), which has the following copyright and license. |
|||
|
|||
Copyright (c) 2016-2019 Isis Agora Lovecruft, Henry de Valence. All rights reserved. |
|||
|
|||
Redistribution and use in source and binary forms, with or without |
|||
modification, are permitted provided that the following conditions are |
|||
met: |
|||
|
|||
1. Redistributions of source code must retain the above copyright |
|||
notice, this list of conditions and the following disclaimer. |
|||
|
|||
2. Redistributions in binary form must reproduce the above copyright |
|||
notice, this list of conditions and the following disclaimer in the |
|||
documentation and/or other materials provided with the distribution. |
|||
|
|||
3. Neither the name of the copyright holder nor the names of its |
|||
contributors may be used to endorse or promote products derived from |
|||
this software without specific prior written permission. |
|||
|
|||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|||
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|||
|
|||
======================================================================== |
|||
|
|||
Portions of curve25519-dalek were originally derived from Adam Langley's |
|||
Go ed25519 implementation, found at <https://github.com/agl/ed25519/>, |
|||
under the following licence: |
|||
|
|||
======================================================================== |
|||
|
|||
Copyright (c) 2012 The Go Authors. All rights reserved. |
|||
|
|||
Redistribution and use in source and binary forms, with or without |
|||
modification, are permitted provided that the following conditions are |
|||
met: |
|||
|
|||
* Redistributions of source code must retain the above copyright |
|||
notice, this list of conditions and the following disclaimer. |
|||
* Redistributions in binary form must reproduce the above |
|||
copyright notice, this list of conditions and the following disclaimer |
|||
in the documentation and/or other materials provided with the |
|||
distribution. |
|||
* Neither the name of Google Inc. nor the names of its |
|||
contributors may be used to endorse or promote products derived from |
|||
this software without specific prior written permission. |
|||
|
|||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
|||
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|||
|
|||
|
|||
* The src/nizk/bullet.rs is derived from [bulletproofs](https://github.com/dalek-cryptography/bulletproofs/), which has the following license: |
|||
|
|||
MIT License |
|||
|
|||
Copyright (c) 2018 Chain, Inc. |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
|||
Permission is hereby granted, free of charge, to any |
|||
person obtaining a copy of this software and associated |
|||
documentation files (the "Software"), to deal in the |
|||
Software without restriction, including without |
|||
limitation the rights to use, copy, modify, merge, |
|||
publish, distribute, sublicense, and/or sell copies of |
|||
the Software, and to permit persons to whom the Software |
|||
is furnished to do so, subject to the following |
|||
conditions: |
|||
|
|||
The above copyright notice and this permission notice |
|||
shall be included in all copies or substantial portions |
|||
of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
|||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
|||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
|||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
|||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|||
DEALINGS IN THE SOFTWARE. |
|||
|
|||
|
|||
* The `invert` and `batch_invert` methods in src/scalar/ristretto255.rs is from [curve25519-dalek](https://github.com/dalek-cryptography/curve25519-dalek), which has the following license. |
|||
|
|||
Copyright (c) 2016-2019 Isis Agora Lovecruft, Henry de Valence. All rights reserved. |
|||
|
|||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
|||
|
|||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
|||
|
|||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
|||
|
|||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
|||
|
|||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|||
|
|||
======================================================================== |
|||
|
|||
Portions of curve25519-dalek were originally derived from Adam Langley's Go ed25519 implementation, found at <https://github.com/agl/ed25519/>, under the following licence: |
|||
|
|||
======================================================================== |
|||
|
|||
Copyright (c) 2012 The Go Authors. All rights reserved. |
|||
|
|||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
|||
|
|||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
|||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
|||
* Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
|||
|
|||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|||
|
|||
|
|||
* The module `src/nizk/bullet.rs` is derived from [bulletproofs](https://github.com/dalek-cryptography/bulletproofs/), which has the following license: |
|||
|
|||
MIT License |
|||
|
|||
Copyright (c) 2018 Chain, Inc. |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@ -1,282 +0,0 @@ |
|||
use super::dense_mlpoly::EqPolynomial;
|
|||
use super::errors::ProofVerifyError;
|
|||
use super::r1csinstance::{
|
|||
R1CSCommitment, R1CSCommitmentGens, R1CSDecommitment, R1CSEvalProof, R1CSInstance,
|
|||
R1CSInstanceEvals, R1CSInstanceSize,
|
|||
};
|
|||
use super::r1csproof::{R1CSGens, R1CSProof};
|
|||
use super::random::RandomTape;
|
|||
use super::scalar::Scalar;
|
|||
use super::timer::Timer;
|
|||
use super::transcript::{AppendToTranscript, ProofTranscript};
|
|||
use merlin::Transcript;
|
|||
use serde::{Deserialize, Serialize};
|
|||
|
|||
pub struct SNARKGens {
|
|||
gens_r1cs_sat: R1CSGens,
|
|||
gens_r1cs_eval: R1CSCommitmentGens,
|
|||
}
|
|||
|
|||
impl SNARKGens {
|
|||
pub fn new(size: &R1CSInstanceSize) -> Self {
|
|||
let gens_r1cs_sat = R1CSGens::new(size.get_num_cons(), size.get_num_vars(), b"gens_r1cs_sat");
|
|||
let gens_r1cs_eval = R1CSCommitmentGens::new(size, b"gens_r1cs_eval");
|
|||
SNARKGens {
|
|||
gens_r1cs_sat,
|
|||
gens_r1cs_eval,
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
#[derive(Serialize, Deserialize, Debug)]
|
|||
pub struct SNARK {
|
|||
r1cs_sat_proof: R1CSProof,
|
|||
inst_evals: R1CSInstanceEvals,
|
|||
r1cs_eval_proof: R1CSEvalProof,
|
|||
}
|
|||
|
|||
impl SNARK {
|
|||
fn protocol_name() -> &'static [u8] {
|
|||
b"Spartan SNARK proof"
|
|||
}
|
|||
|
|||
/// A public computation to create a commitment to an R1CS instance
|
|||
pub fn encode(inst: &R1CSInstance, gens: &SNARKGens) -> (R1CSCommitment, R1CSDecommitment) {
|
|||
inst.commit(&gens.gens_r1cs_eval)
|
|||
}
|
|||
|
|||
/// A method to produce a proof of the satisfiability of an R1CS instance
|
|||
pub fn prove(
|
|||
inst: &R1CSInstance,
|
|||
decomm: &R1CSDecommitment,
|
|||
vars: Vec<Scalar>,
|
|||
input: &[Scalar],
|
|||
gens: &SNARKGens,
|
|||
transcript: &mut Transcript,
|
|||
) -> Self {
|
|||
// we create a Transcript object seeded with a random Scalar
|
|||
// to aid the prover produce its randomness
|
|||
let mut random_tape = RandomTape::new(b"proof");
|
|||
transcript.append_protocol_name(SNARK::protocol_name());
|
|||
let (r1cs_sat_proof, rx, ry) = {
|
|||
let (proof, rx, ry) = R1CSProof::prove(
|
|||
inst,
|
|||
vars,
|
|||
input,
|
|||
&gens.gens_r1cs_sat,
|
|||
transcript,
|
|||
&mut random_tape,
|
|||
);
|
|||
let proof_encoded: Vec<u8> = bincode::serialize(&proof).unwrap();
|
|||
Timer::print(&format!("len_r1cs_sat_proof {:?}", proof_encoded.len()));
|
|||
|
|||
(proof, rx, ry)
|
|||
};
|
|||
|
|||
// We send evaluations of A, B, C at r = (rx, ry) as claims
|
|||
// to enable the verifier complete the first sum-check
|
|||
let timer_eval = Timer::new("eval_sparse_polys");
|
|||
let inst_evals = {
|
|||
let eval_table_rx = EqPolynomial::new(rx.clone()).evals();
|
|||
let eval_table_ry = EqPolynomial::new(ry.clone()).evals();
|
|||
inst.evaluate_with_tables(&eval_table_rx, &eval_table_ry)
|
|||
};
|
|||
inst_evals.append_to_transcript(b"r1cs_inst_evals", transcript);
|
|||
timer_eval.stop();
|
|||
|
|||
let r1cs_eval_proof = {
|
|||
let proof = R1CSEvalProof::prove(
|
|||
decomm,
|
|||
&rx,
|
|||
&ry,
|
|||
&inst_evals,
|
|||
&gens.gens_r1cs_eval,
|
|||
transcript,
|
|||
&mut random_tape,
|
|||
);
|
|||
|
|||
let proof_encoded: Vec<u8> = bincode::serialize(&proof).unwrap();
|
|||
Timer::print(&format!("len_r1cs_eval_proof {:?}", proof_encoded.len()));
|
|||
proof
|
|||
};
|
|||
|
|||
SNARK {
|
|||
r1cs_sat_proof,
|
|||
inst_evals,
|
|||
r1cs_eval_proof,
|
|||
}
|
|||
}
|
|||
|
|||
/// A method to verify the proof of the satisfiability of an R1CS instance
|
|||
pub fn verify(
|
|||
&self,
|
|||
comm: &R1CSCommitment,
|
|||
input: &[Scalar],
|
|||
transcript: &mut Transcript,
|
|||
gens: &SNARKGens,
|
|||
) -> Result<(), ProofVerifyError> {
|
|||
transcript.append_protocol_name(SNARK::protocol_name());
|
|||
|
|||
let timer_sat_proof = Timer::new("verify_sat_proof");
|
|||
assert_eq!(input.len(), comm.get_num_inputs());
|
|||
let (rx, ry) = self
|
|||
.r1cs_sat_proof
|
|||
.verify(
|
|||
comm.get_num_vars(),
|
|||
comm.get_num_cons(),
|
|||
input,
|
|||
&self.inst_evals,
|
|||
transcript,
|
|||
&gens.gens_r1cs_sat,
|
|||
)
|
|||
.unwrap();
|
|||
timer_sat_proof.stop();
|
|||
|
|||
let timer_eval_proof = Timer::new("verify_eval_proof");
|
|||
self
|
|||
.inst_evals
|
|||
.append_to_transcript(b"r1cs_inst_evals", transcript);
|
|||
assert!(self
|
|||
.r1cs_eval_proof
|
|||
.verify(
|
|||
comm,
|
|||
&rx,
|
|||
&ry,
|
|||
&self.inst_evals,
|
|||
&gens.gens_r1cs_eval,
|
|||
transcript
|
|||
)
|
|||
.is_ok());
|
|||
timer_eval_proof.stop();
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
|
|||
pub struct NIZKGens {
|
|||
gens_r1cs_sat: R1CSGens,
|
|||
}
|
|||
|
|||
impl NIZKGens {
|
|||
pub fn new(num_cons: usize, num_vars: usize) -> Self {
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
NIZKGens { gens_r1cs_sat }
|
|||
}
|
|||
}
|
|||
|
|||
#[derive(Serialize, Deserialize, Debug)]
|
|||
pub struct NIZK {
|
|||
r1cs_sat_proof: R1CSProof,
|
|||
r: (Vec<Scalar>, Vec<Scalar>),
|
|||
}
|
|||
|
|||
impl NIZK {
|
|||
fn protocol_name() -> &'static [u8] {
|
|||
b"Spartan NIZK proof"
|
|||
}
|
|||
|
|||
/// A method to produce a proof of the satisfiability of an R1CS instance
|
|||
pub fn prove(
|
|||
inst: &R1CSInstance,
|
|||
vars: Vec<Scalar>,
|
|||
input: &[Scalar],
|
|||
gens: &NIZKGens,
|
|||
transcript: &mut Transcript,
|
|||
) -> Self {
|
|||
// we create a Transcript object seeded with a random Scalar
|
|||
// to aid the prover produce its randomness
|
|||
let mut random_tape = RandomTape::new(b"proof");
|
|||
transcript.append_protocol_name(NIZK::protocol_name());
|
|||
let (r1cs_sat_proof, rx, ry) = {
|
|||
let (proof, rx, ry) = R1CSProof::prove(
|
|||
inst,
|
|||
vars,
|
|||
input,
|
|||
&gens.gens_r1cs_sat,
|
|||
transcript,
|
|||
&mut random_tape,
|
|||
);
|
|||
let proof_encoded: Vec<u8> = bincode::serialize(&proof).unwrap();
|
|||
Timer::print(&format!("len_r1cs_sat_proof {:?}", proof_encoded.len()));
|
|||
|
|||
(proof, rx, ry)
|
|||
};
|
|||
|
|||
NIZK {
|
|||
r1cs_sat_proof,
|
|||
r: (rx, ry),
|
|||
}
|
|||
}
|
|||
|
|||
/// A method to verify the proof of the satisfiability of an R1CS instance
|
|||
pub fn verify(
|
|||
&self,
|
|||
inst: &R1CSInstance,
|
|||
input: &[Scalar],
|
|||
transcript: &mut Transcript,
|
|||
gens: &NIZKGens,
|
|||
) -> Result<(), ProofVerifyError> {
|
|||
transcript.append_protocol_name(NIZK::protocol_name());
|
|||
|
|||
// We send evaluations of A, B, C at r = (rx, ry) as claims
|
|||
// to enable the verifier complete the first sum-check
|
|||
let timer_eval = Timer::new("eval_sparse_polys");
|
|||
let (claimed_rx, claimed_ry) = &self.r;
|
|||
let inst_evals = {
|
|||
let eval_table_rx = EqPolynomial::new(claimed_rx.clone()).evals();
|
|||
let eval_table_ry = EqPolynomial::new(claimed_ry.clone()).evals();
|
|||
inst.evaluate_with_tables(&eval_table_rx, &eval_table_ry)
|
|||
};
|
|||
timer_eval.stop();
|
|||
|
|||
let timer_sat_proof = Timer::new("verify_sat_proof");
|
|||
assert_eq!(input.len(), inst.get_num_inputs());
|
|||
let (rx, ry) = self
|
|||
.r1cs_sat_proof
|
|||
.verify(
|
|||
inst.get_num_vars(),
|
|||
inst.get_num_cons(),
|
|||
input,
|
|||
&inst_evals,
|
|||
transcript,
|
|||
&gens.gens_r1cs_sat,
|
|||
)
|
|||
.unwrap();
|
|||
|
|||
// verify if claimed rx and ry are correct
|
|||
assert_eq!(rx, *claimed_rx);
|
|||
assert_eq!(ry, *claimed_ry);
|
|||
timer_sat_proof.stop();
|
|||
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
|
|||
#[cfg(test)]
|
|||
mod tests {
|
|||
use super::*;
|
|||
|
|||
#[test]
|
|||
pub fn check_snark() {
|
|||
let num_vars = 256;
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let r1cs_size = inst.size();
|
|||
|
|||
// produce public generators
|
|||
let gens = SNARKGens::new(&r1cs_size);
|
|||
|
|||
// create a commitment to R1CSInstance
|
|||
let (comm, decomm) = SNARK::encode(&inst, &gens);
|
|||
|
|||
// produce a proof
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
|
|||
|
|||
// verify the proof
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(&comm, &input, &mut verifier_transcript, &gens)
|
|||
.is_ok());
|
|||
}
|
|||
}
|