Coder Social home page Coder Social logo

school's Issues

An example code that prevents p or q to be 1, but has errors and need to help

Hi Keyvan, I tried to do that task by this way but when I run this code it panicks so I wanted to ask for your help

use bellman::groth16::{
    create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
};
use bellman::{Circuit, ConstraintSystem, SynthesisError};
use bls12_381::{Bls12, Scalar};
use ff::PrimeField;
use rand::thread_rng;

#[derive(Debug, Default, Clone)]
pub struct NotPrime<S: PrimeField> {
    pub witness_p: Option<S>, // Secret ;)
    pub witness_q: Option<S>, // Secret ;)
    pub input_n: Option<S>,   // Public :D
}

impl<S: PrimeField> Circuit<S> for NotPrime<S> {
    fn synthesize<CS: ConstraintSystem<S>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
        let n = cs.alloc_input(
            || "n",
            || self.input_n.ok_or(SynthesisError::AssignmentMissing),
        )?;
        let p = cs.alloc(
            || "p",
            || self.witness_p.ok_or(SynthesisError::AssignmentMissing),
        )?;
        let q = cs.alloc(
            || "q",
            || self.witness_q.ok_or(SynthesisError::AssignmentMissing),
        )?;
        cs.enforce(|| "p * q == N", |lc| lc + p, |lc| lc + q, |lc| lc + n);

        let pq_sum = cs.alloc(
            || "p + q",
            || {
                let p = self.witness_p.unwrap();
                let q = self.witness_q.unwrap();
                Ok(p + q)
            },
        )?;

        cs.enforce(
            || "p + q = pq_sum",
            |lc| lc + p + q,
            |lc| lc + CS::one(),
            |lc| lc + pq_sum,
        );

        let pq_product = cs.alloc(
            || "p * q",
            || {
                let p = self.witness_p.unwrap();
                let q = self.witness_q.unwrap();
                Ok(p * q)
            },
        )?;

        cs.enforce(
            || "p * q = pq_product",
            |lc| lc + p,
            |lc| lc + q,
            |lc| lc + pq_product,
        );

        cs.enforce(
            || "pq_product > pq_sum",
            |lc| lc + pq_product,
            |lc| lc + CS::one(),
            |lc| lc + pq_sum,
        );

        Ok(())
    }
}

fn main() {
    let mut rng = thread_rng();
    let params = {
        let c = NotPrime {
            witness_p: None,
            witness_q: None,
            input_n: None,
        };

        generate_random_parameters::<Bls12, _, _>(c, &mut rng).unwrap()
    };
    let pvk = prepare_verifying_key(&params.vk);

    let c = NotPrime {
        witness_p: Some(Scalar::from(123)),
        witness_q: Some(Scalar::from(3)),
        input_n: Some(Scalar::from(369)),
    };
    let proof = create_random_proof(c, &params, &mut rng).unwrap();

    let inputs = [Scalar::from(369)];

    assert!(verify_proof(&pvk, &proof, &inputs).is_ok());
}

and the error is:

thread 'main' panicked at 'assertion failed: verify_proof(&pvk, &proof, &inputs).is_ok()', src/main.rs:97:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.