Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hosseind88 opened this issue Apr 17, 2023 · 0 comments

Comments

@hosseind88
Copy link

hosseind88 commented Apr 17, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant