Skip to content

fix NonNativeAdditionGenerator bug#2

Open
qope wants to merge 1 commit into
0xPolygonZero:mainfrom
qope:fix_generator_bug
Open

fix NonNativeAdditionGenerator bug#2
qope wants to merge 1 commit into
0xPolygonZero:mainfrom
qope:fix_generator_bug

Conversation

@qope

@qope qope commented May 3, 2023

Copy link
Copy Markdown

The NonNativeAdditionGenerator does not take into account the case when a + b = modulus.

For example, the following test will fail:

#[cfg(test)]
mod tests {
    use crate::gadgets::nonnative::CircuitBuilderNonNative;
    use plonky2::{
        field::{secp256k1_base::Secp256K1Base, types::Field},
        iop::witness::PartialWitness,
        plonk::{
            circuit_builder::CircuitBuilder,
            circuit_data::CircuitConfig,
            config::{GenericConfig, PoseidonGoldilocksConfig},
        },
    };

    #[test]
    fn test_add_to_zero() {
        const D: usize = 2;
        type C = PoseidonGoldilocksConfig;
        type F = <C as GenericConfig<D>>::F;
        let config = CircuitConfig::standard_ecc_config();

        let pw = PartialWitness::<F>::new();
        let mut builder = CircuitBuilder::<F, D>::new(config);

        let a = Secp256K1Base::ONE;
        let b = Secp256K1Base::NEG_ONE;

        let a_t = builder.constant_nonnative(a);
        let b_t = builder.constant_nonnative(b);
        let c_t = builder.add_nonnative(&a_t, &b_t);

        let zero_t = builder.constant_nonnative(Secp256K1Base::ZERO);

        builder.connect_nonnative(&c_t, &zero_t);

        let data = builder.build::<C>();
        let proof = data.prove(pw).unwrap();
        data.verify(proof).unwrap();
    }
}

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

Successfully merging this pull request may close these issues.

1 participant