Skip to content

Residue has improper behavior with even moduli #232

@kayabaNerve

Description

@kayabaNerve
        const limbs: usize = 256 / Limb::BITS;
        let params: DynResidueParams<limbs> = DynResidueParams::new(&U256::from_u8(10u8));
        for i in 0..10 {
            let x = DynResidue::new(&U256::from_u8(i as u8), params);
            println!("{:?}", x.retrieve());
        }

This should print 0 ..= 9. Instead,

Uint(0x0000000000000000000000000000000000000000000000000000000000000000)
Uint(0x0000000000000000000000000000000000000000000000000000000000000004)
Uint(0x0000000000000000000000000000000000000000000000000000000000000000)
Uint(0x0000000000000000000000000000000000000000000000000000000000000007)
Uint(0x0000000000000000000000000000000000000000000000000000000000000003)
Uint(0x0000000000000000000000000000000000000000000000000000000000000000)
Uint(0x0000000000000000000000000000000000000000000000000000000000000006)
Uint(0x0000000000000000000000000000000000000000000000000000000000000002)
Uint(0x0000000000000000000000000000000000000000000000000000000000000009)
Uint(0x0000000000000000000000000000000000000000000000000000000000000000)

This behavior also exists with the non-dyn form.

https://docs.rs/crypto-bigint/latest/crypto_bigint/macro.impl_modulus.html does not document any bounds on the modulus.
https://docs.rs/crypto-bigint/latest/crypto_bigint/modular/constant_mod/struct.Residue.html does not either.
https://docs.rs/crypto-bigint/latest/crypto_bigint/modular/constant_mod/trait.ResidueParams.html does.
https://docs.rs/crypto-bigint/latest/crypto_bigint/modular/runtime_mod/struct.DynResidue.html does.
https://docs.rs/crypto-bigint/latest/crypto_bigint/modular/runtime_mod/struct.DynResidueParams.html doesn't.

All of these should document all bounds on the moduli. Additionally, DynResidueParams::new should return a Result, or at least panic on an invalid moduli to prevent the above behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions