division improvements

This commit is contained in:
jacekpoz 2024-04-12 13:10:30 +02:00
parent bab58d8b7c
commit 7c86a2a650
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
2 changed files with 8 additions and 7 deletions

View file

@ -55,15 +55,15 @@ public:
throw std::invalid_argument("division by 0 is illegal"); throw std::invalid_argument("division by 0 is illegal");
} }
uint inverse = 1; GF<N> inverse = 1;
for (size_t i = 1; i < N; i++) { for (size_t i = 1; i < N; i++) {
if ((rhs._value * i) % N == 1) { if (rhs * i == GF<N>(1)) {
inverse = i; inverse = i;
break; break;
} }
} }
return GF<N>((this->_value * inverse) % N); return GF<N>(*this * inverse);
} }
GF<N> operator/(const uint &rhs) const { GF<N> operator/(const uint &rhs) const {

View file

@ -59,15 +59,16 @@ public class GF {
throw new IllegalArgumentException("division by 0 is illegal"); throw new IllegalArgumentException("division by 0 is illegal");
} }
int inverse = 1; GF inverse = new GF(this.characteristic, 1);
for (int i = 1; i < this.characteristic; i++) { for (int i = 1; i < this.characteristic; i++) {
if ((rhs.value * i) % this.characteristic == 1) { GF gfI = new GF(this.characteristic, i);
inverse = i; if (rhs.multiply(gfI).equals(new GF(this.characteristic, 1))) {
inverse = gfI;
break; break;
} }
} }
return new GF(this.characteristic, (this.value * inverse) % this.characteristic); return this.multiply(inverse);
} }
public void divideAssign(final GF rhs) throws IllegalArgumentException { public void divideAssign(final GF rhs) throws IllegalArgumentException {