package meerkat.crypto.dkg.gjkr;

import meerkat.comm.Channel;

import java.math.BigInteger;
import java.util.Random;
import java.util.Set;

/**
 * Created by Tzlil on 3/29/2016.
 */
public class SDKGMaliciousUserImpl<T> extends User<T> {

    private final Protocol<T> maliciousSDKG;
    private final Set<Integer> falls;
    public SDKGMaliciousUserImpl(Protocol<T> sdkg, Protocol<T> maliciousSDKG
            , Channel channel, Set<Integer> falls) {
        super(sdkg, channel);
        this.falls = falls;
        this.maliciousSDKG = maliciousSDKG;
        maliciousSDKG.setParties(parties);
    }

    public static<T> Protocol<T> generateMaliciousSDKG(Protocol<T> sdkg,Channel channel,Random random){
        BigInteger q = sdkg.getQ();
        BigInteger zi = new BigInteger(q.bitLength(), random).mod(q);
        Protocol<T> malicious = new Protocol<T>(sdkg.getT(),sdkg.getN(),zi,random,sdkg.getQ()
                ,sdkg.getGenerator(),sdkg.getH(),sdkg.getGroup(),sdkg.getId(),sdkg.getEncoder());
        malicious.setChannel(channel);
        return malicious;
    }

    @Override
    public void stage1() {
        sdkg.computeAndBroadcastVerificationValues();
        sendSecrets(); //insteadof crypto.sendSecrets(channel);
    }

    @Override
    public void stage3() {
        maliciousSDKG.answerAllComplainingPlayers();
    }

    @Override
    public void stage4(){
        //do nothing
    }

    private void sendSecrets(){
        for (int j = 1; j <= n ; j++){
            if(j != id){
                if(falls.contains(j)){
                    maliciousSDKG.sendSecret(j);
                }else {
                    sdkg.sendSecret(j);
                }
            }
        }
    }

}
