division improvements
This commit is contained in:
parent
bab58d8b7c
commit
7c86a2a650
2 changed files with 8 additions and 7 deletions
|
@ -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 {
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue