package meerkat.crypto.utils.concrete;

import meerkat.crypto.utils.Arithmetic;
import org.factcenter.qilin.primitives.concrete.Zpstar;

import java.math.BigInteger;

/**
 * Created by Tzlil on 3/17/2016.
 * an implementation of Arithmetic<BigInteger> over prime fields: integers modulo p
 */
public class Fp implements Arithmetic<BigInteger> {
    public final BigInteger p;
    private final Zpstar zp;

    /**
     * constructor
     * @param p prime
     */
    public Fp(BigInteger p) {
        this.p = p;
        this.zp = new Zpstar(p);
    }

    @Override
    public BigInteger add(BigInteger a, BigInteger b){
        return a.add(b).mod(p);
    }

    @Override
    public BigInteger sub(BigInteger a, BigInteger b){
        return a.add(p).subtract(b).mod(p);
    }

    @Override
    public BigInteger mul(BigInteger a, BigInteger b){
        return zp.add(a,b);
    }

    @Override
    public BigInteger div(BigInteger a, BigInteger b){
        return mul(a,zp.negate(b));
    }
}
