Skip to content

BoxedResidue square error #441

@xuganyu96

Description

@xuganyu96

BoxedResidue::square() sometimes produces BoxedResidue whose montgomery_form is greater than its modulus, which will break BoxedResidue::add and BoxedResidue::sub.

Here is some code to reproduce the error:

use crypto_bigint::{
    modular::{BoxedResidue, BoxedResidueParams, DynResidue, DynResidueParams},
    BoxedUint, U128
};

fn main() {
    let residue = 0x20u128;
    let modulus = 0xB44677037A7DBDE04814256570DCBD8Du128;

    let boxed_modulus = BoxedUint::from(modulus);
    let boxed_params = BoxedResidueParams::new(boxed_modulus).unwrap();
    let boxed_residue = BoxedResidue::new(BoxedUint::from(residue), boxed_params);
    let boxed_square = boxed_residue.square();


    let dyn_modulus = U128::from_u128(modulus);
    let dyn_params = DynResidueParams::new(&dyn_modulus).unwrap();
    let dyn_residue = DynResidue::new(&U128::from_u128(residue), dyn_params);
    let dyn_square = dyn_residue.square();
    
    assert!(boxed_residue.as_montgomery().as_limbs() == dyn_residue.as_montgomery().as_limbs());
    assert!(boxed_square.as_montgomery() > boxed_square.params().modulus()); // this should not happen
    assert!(dyn_square.as_montgomery() <= dyn_square.params().modulus());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions