Skip to content

Add change_modulus and change_q#532

Open
jnsiemer wants to merge 4 commits intodevfrom
set_modulus
Open

Add change_modulus and change_q#532
jnsiemer wants to merge 4 commits intodevfrom
set_modulus

Conversation

@jnsiemer
Copy link
Copy Markdown
Member

@jnsiemer jnsiemer commented May 6, 2026

Description

This PR implements...

  • change_modulus
  • change_q

for all non-NTT Zq datatypes.

Testing

  • I added basic working examples (possibly in doc-comment)
  • I added tests for large (pointer representation) values
  • I triggered all possible errors in my test in every possible way
  • I included tests for all reasonable edge cases

Checklist:

  • I have performed a self-review of my own code
    • The code provides good readability and maintainability s.t. it fulfills best practices like talking code, modularity, ...
      • The chosen implementation is not more complex than it has to be
    • My code should work as intended and no side effects occur (e.g. memory leaks)
    • The doc comments fit our style guide

@jnsiemer jnsiemer requested a review from Marvin-Beckmann May 6, 2026 10:36
@jnsiemer jnsiemer self-assigned this May 6, 2026
Copy link
Copy Markdown
Member

@Marvin-Beckmann Marvin-Beckmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current implementation is not working as intended after the mod is changed, see the respective comment


impl ModulusPolynomialRingZq {
/// Changes the modulus `q` of the given [`ModulusPolynomialRingZq`] to the new modulus `q`
/// and resets all NTT-related attributes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, I was wondering whether you thought about this :)

Comment on lines +37 to +44
pub fn change_q(&mut self, modulus: impl Into<Modulus>) {
self.ntt_basis = Rc::new(None);
self.non_zero = Vec::new();

let new_poly_zq =
PolyOverZq::from((self.get_representative_least_nonnegative_residue(), modulus));
self.modulus = Rc::new(new_poly_zq);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would cause problems I think.

The non-zero needs to be set fitting to the modulus, i.e., it captures the non-zero coefficients used for the reduction -> see the reduce call in PolynomialRingZq that uses it. By setting this to 0, I think that essentially the polynomial reduction would always assume that you are using a modulus polynomial of X^N, i.e., there is no proper reduction.

On that note: I just found, that I missed to describe the new attributes from the changed modulus reduction on the Modulus struct.

Please add a test for this case, i.e., do some higher degree modulo multiplication, where the context polynomial is also used for reduction for the tests in the Polynomial constructions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed the issue with the non-zero vector.
I'd suggest that you address the additionally missing parts described after "On that note" in a separate PR. You have a better idea what's needed / about the edge cases here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I changed the non_zero vector s.t. it contains all positions of non-zero coefficients in the ModulusPolynomialRingZq. Previously, the highest coefficient (always 1) wasn't part of the vector. The tests all still pass, which tells me that either a test reg. this was missing or it didn't change anything and we might be doing double work somewhere now. As the one who wrote this logic, could you please check which one we want / need and add tests accordingly?

@jnsiemer jnsiemer requested a review from Marvin-Beckmann May 6, 2026 12:22
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.

2 participants