Skip to content

feat(field): add PowPFOp to support prime field exponent #67

Closed
quanxi1 wants to merge 2 commits intomainfrom
feat/support-pf-exponent-pow
Closed

feat(field): add PowPFOp to support prime field exponent #67
quanxi1 wants to merge 2 commits intomainfrom
feat/support-pf-exponent-pow

Conversation

@quanxi1
Copy link
Contributor

@quanxi1 quanxi1 commented Aug 22, 2025

Description

This PR introduces PowPFOp which can take prime field element as an exponent so that HLO power operation can handle both scalar field type and primitive unsigned integer type as an exponent.

Related Issues/PRs

Checklist

@quanxi1 quanxi1 force-pushed the feat/support-pf-exponent-pow branch from 34f54c0 to cdcc259 Compare August 22, 2025 06:28
@quanxi1
Copy link
Contributor Author

quanxi1 commented Aug 22, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces PowPFOp to support exponentiation where the exponent is a prime field element. This is a great feature that enhances the flexibility of power operations. The implementation refactors the existing PowUIOp logic into a shared computePower helper function, which is a clean approach to avoid code duplication. The new functionality is also well-tested. I have one suggestion to improve the readability of the computePower function for better long-term maintainability.

Comment on lines +581 to +587
modulus = modulus.zext(modBitWidth * 2);
modulus = modulus * modulus - 1;
exp = b.create<arith::ExtUIOp>(
IntegerType::get(exp.getContext(), modulus.getBitWidth()), exp);
intType = IntegerType::get(exp.getContext(), modulus.getBitWidth());
exp = b.create<arith::RemUIOp>(
exp, b.create<arith::ConstantIntOp>(intType, modulus));

Choose a reason for hiding this comment

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

medium

The modulus variable is reused to store the group order (p^2 - 1), which can be confusing as it originally holds the prime modulus p. Using a separate, more descriptively named variable like groupOrder would make the logic for exponent reduction clearer and improve maintainability.

Suggested change
modulus = modulus.zext(modBitWidth * 2);
modulus = modulus * modulus - 1;
exp = b.create<arith::ExtUIOp>(
IntegerType::get(exp.getContext(), modulus.getBitWidth()), exp);
intType = IntegerType::get(exp.getContext(), modulus.getBitWidth());
exp = b.create<arith::RemUIOp>(
exp, b.create<arith::ConstantIntOp>(intType, modulus));
APInt groupOrder = modulus.zext(modBitWidth * 2);
groupOrder = groupOrder * groupOrder - 1;
exp = b.create<arith::ExtUIOp>(
IntegerType::get(exp.getContext(), groupOrder.getBitWidth()), exp);
intType = IntegerType::get(exp.getContext(), groupOrder.getBitWidth());
exp = b.create<arith::RemUIOp>(
exp, b.create<arith::ConstantIntOp>(intType, groupOrder));

@chokobole
Copy link
Collaborator

chore(feat): fix description: the scope should be field

@quanxi1 quanxi1 closed this Aug 27, 2025
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