Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] first pass at framing arithmetic #10

Merged
merged 7 commits into from
Sep 9, 2024

Conversation

0xAlcibiades
Copy link
Member

No description provided.

// TODO: implement bernstein yang inversion
Self::zero_array()

pub const fn greater_than(&self, a: &[u64; L], b:&[u64;L])-> bool {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not truly constant time as different branches on the comparator may return at different times, we should instead always run through the whole loop and then return only afterwards.

r_squared,
n_prime,
}
correction: [0u64; L],
Copy link
Member Author

Choose a reason for hiding this comment

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

These values could be computed beforehand rather than having a mutation occur.

Copy link
Collaborator

Choose a reason for hiding this comment

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

agreed, this was left over from before I moved arithmetic to _internals, will remedy

// TODO (Implement Montgomery r squared)
Self::zero_array()
}
let diff = Self::subtraction_correction(modulus);
Copy link
Member Author

Choose a reason for hiding this comment

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

This could be passed in to the function rather than recomputed.

src/finite_field.rs Outdated Show resolved Hide resolved
let n = modulus[0]; //need only least significant bits
let mut n_prime = 1u64;
let mut i = 0;
while i < 64 {
Copy link
Member Author

Choose a reason for hiding this comment

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

Why this number, because 64 bits?

}

pub const fn to_montgomery(&self, a: &[u64; L]) -> [u64; L] {
// TODO (Implement to monty form)
Self::zero_array()
self.montgomery_multiply(a, &self.r_squared)
Copy link
Member Author

Choose a reason for hiding this comment

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

Some detail on why multiplying by r_squared gives us the monty form would be helpful.

}

pub const fn from_montgomery(&self, a: &[u64; L]) -> [u64; L] {
// TODO (Implement from monty form)
Self::zero_array()
self.montgomery_multiply(a, &Self::one_array())
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here on why multiplying by 1 (the identity, not an array filled with ones) gives us the normal form.

@@ -198,110 +206,177 @@ impl<const L: usize, const D: usize> FinitePrimeField<L, D> {
j += 1;
}

result
self.to_montgomery(&result)
Copy link
Member Author

Choose a reason for hiding this comment

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

Why here do we convert to_montgomery? Should this not already be in monty form at the end of this, or has it been reduced?

let mut negated = Self::zero_array();
let mut i = 0;
while i < L {
negated[i] = self.modulus[i].wrapping_sub(a[i]);
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm thinking some carry about the modulus needs to happen here.

}
}
/// The following performs the Bernstein-Yang inversion on a scalar.
pub const fn bernstein_yang_invert(&self, a: &[u64; L]) -> [u64; L] {
Copy link
Member Author

Choose a reason for hiding this comment

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

Perfect use case for dimensionals 💯

@0xAlcibiades 0xAlcibiades marked this pull request as draft July 16, 2024 02:49
@trbritt trbritt closed this Jul 16, 2024
@0xAlcibiades 0xAlcibiades reopened this Sep 9, 2024
@0xAlcibiades 0xAlcibiades changed the base branch from main to tristan/war-529-deep-sylow-optimization September 9, 2024 04:57
@0xAlcibiades
Copy link
Member Author

@trbritt please merge this together with #34

@trbritt trbritt marked this pull request as ready for review September 9, 2024 14:42
@trbritt trbritt merged commit bc19bcb into tristan/war-529-deep-sylow-optimization Sep 9, 2024
2 checks passed
@trbritt trbritt deleted the tristan/div-inv branch September 9, 2024 14:42
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