All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.rub.nds.protocol.crypto.ec.FieldElementFp Maven / Gradle / Ivy

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());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy