de.rub.nds.protocol.crypto.ec.FieldElementFp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protocol-attacker Show documentation
Show all versions of protocol-attacker Show documentation
Protocol-Attacker is a framework for the creation of protocol analysis tools
The newest version!
/*
* Protocol-Attacker - A Framework to create Protocol Analysis Tools
*
* Copyright 2023-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
*
* Licensed under Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package de.rub.nds.protocol.crypto.ec;
import java.math.BigInteger;
/** An element of the field F_p (with p being a prime number). */
public class FieldElementFp extends FieldElement {
/** Instantiates the element data in the field F_modulus. With modulus being a prime number. */
public FieldElementFp(BigInteger data, BigInteger modulus) {
super(data.mod(modulus), modulus);
}
private FieldElementFp() {
super(null, null);
}
@Override
public FieldElement add(FieldElement f) {
BigInteger tmp = this.getData().add(f.getData());
tmp = tmp.mod(this.getModulus());
return new FieldElementFp(tmp, this.getModulus());
}
@Override
public FieldElement mult(FieldElement f) {
BigInteger tmp = this.getData().multiply(f.getData());
tmp = tmp.mod(this.getModulus());
return new FieldElementFp(tmp, this.getModulus());
}
@Override
public FieldElement addInv() {
BigInteger tmp = this.getData().negate();
tmp = tmp.mod(this.getModulus());
return new FieldElementFp(tmp, this.getModulus());
}
@Override
public FieldElement multInv() {
if (this.getData().equals(BigInteger.ZERO)) {
throw new ArithmeticException();
}
BigInteger tmp = this.getData().modInverse(this.getModulus());
return new FieldElementFp(tmp, this.getModulus());
}
}