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

ch.openchvote.electionauthority.plain.states.S200 Maven / Gradle / Ivy

/*
 * Copyright (C) 2024 Berner Fachhochschule https://e-voting.bfh.ch
 *
 *  - This program is free software: you can redistribute it and/or modify                           -
 *  - it under the terms of the GNU Affero General Public License as published by                    -
 *  - the Free Software Foundation, either version 3 of the License, or                              -
 *  - (at your option) any later version.                                                            -
 *  -                                                                                                -
 *  - This program is distributed in the hope that it will be useful,                                -
 *  - but WITHOUT ANY WARRANTY; without even the implied warranty of                                 -
 *  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                   -
 *  - GNU General Public License for more details.                                                   -
 *  -                                                                                                -
 *  - You should have received a copy of the GNU Affero General Public License                       -
 *  - along with this program. If not, see .                           -
 */
package ch.openchvote.electionauthority.plain.states;

import ch.openchvote.algorithms.Algorithm;
import ch.openchvote.algorithms.general.algorithms.GetPrimes;
import ch.openchvote.electionauthority.ElectionAuthority;
import ch.openchvote.electionauthority.plain.EventContext;
import ch.openchvote.electionauthority.plain.states.error.*;
import ch.openchvote.electionauthority.plain.tasks.TE1;
import ch.openchvote.electionauthority.plain.tasks.TE2;
import ch.openchvote.framework.annotations.state.Notify;
import ch.openchvote.framework.annotations.state.Phase;
import ch.openchvote.framework.communication.Message;
import ch.openchvote.framework.communication.Status;
import ch.openchvote.framework.exceptions.TaskException;
import ch.openchvote.framework.party.State;
import ch.openchvote.protocol.parameters.SecurityParameters;
import ch.openchvote.protocol.phases.Preparation;
import ch.openchvote.protocol.protocols.plain.content.message.MAE2;
import ch.openchvote.protocol.protocols.plain.content.message.MEE1;
import ch.openchvote.utilities.tools.Parallel;
import ch.openchvote.utilities.tools.VMGJFassade;
import ch.openchvote.utilities.tuples.Pair;

@SuppressWarnings("MissingJavadoc")
@Phase(Preparation.class)
@Notify(Status.Type.READY)
public final class S200 extends State {

    public S200(ElectionAuthority electionAuthority, EventContext eventContext) {
        super(electionAuthority, eventContext);
        this.registerMessageHandler(MEE1.class, this::handleMEE1);
        this.registerMessageHandler(MAE2.class, this::handleMAE2);
    }

    private void handleMEE1(Message message) {

        // decompose event context
        var eventId = this.eventContext.getEventId();
        var publicData = this.eventContext.getPublicData();
        var securityLevel = this.eventContext.getSecurityLevel();

        // select setup parameters
        var ES = publicData.get_ES().get();
        var k = ES.get_SP().getIndexOf(message.getSenderId());

        // select election parameters
        var EP = publicData.get_EP().get();

        // get and check message content
        var aux = new Pair<>(ES, EP);
        var MEE1 = this.party.getAndCheckContent(MEE1.class, message, aux, securityLevel);

        // update public data
        publicData.setContent(k, MEE1);

        try {
            // perform task
            TE1.run(k, publicData);

            // run self activation
            var stateId = this.getId();
            this.party.selfActivate(eventId, stateId, State::handleSelfActivation);

        } catch (Algorithm.Exception exception) {
            // move to error state
            this.party.updateState(this.eventContext, A1.class);
        } catch (TaskException exception) {
            // move to error state
            this.party.updateState(this.eventContext, B1.class);
        }
    }

    private void handleMAE2(Message message) {

        // decompose event context
        var eventId = this.eventContext.getEventId();
        var publicData = this.eventContext.getPublicData();
        var secretData = this.eventContext.getSecretData();
        var securityLevel = this.eventContext.getSecurityLevel();

        // select setup parameters
        var ES = publicData.get_ES().get();
        var SP = ES.get_SP();
        var s = SP.get_s();
        var bold_ea = SP.get_bold_ea();
        var j = SP.getIndexOf(this.party.getId());

        // select election parameters
        var EP = publicData.get_EP().get();

        // get and check message content
        var aux = new Pair<>(ES, EP);
        var MAE2 = this.party.getAndCheckContent(MAE2.class, message, aux, securityLevel);

        // update public data
        publicData.setContent(MAE2);

        // select election parameters
        var N_E = EP.get_N_E();
        var bold_n = EP.get_bold_n();

        // create precomputation tables (numbers taken from Table 12.5)
        var securityParameters = new SecurityParameters(securityLevel);
        var p = securityParameters.get_p();
        var q = securityParameters.get_q();
        var g = securityParameters.get_g();
        var p_hat = securityParameters.get_p_hat();
        var q_hat = securityParameters.get_q_hat();
        var g_hat = securityParameters.get_g_hat();
        VMGJFassade.precomputeTable(g, p, q, (s + 5) * N_E);
        VMGJFassade.precomputeTable(g, p, q, 2 * N_E);
        Parallel.forEachLoop(GetPrimes.run(bold_n.sum(), securityParameters), p_i -> VMGJFassade.precomputeTable(p_i, p, q, N_E));
        VMGJFassade.precomputeTable(g_hat, p_hat, q_hat, (3 * s + 12) * N_E);

        try {
            // perform task
            TE1.run(0, publicData);

            try {
                // perform task
                TE2.run(j, publicData, secretData);

                // select event data
                var pk_j = publicData.get_bold_pk().get(j);
                var pi_j = publicData.get_bold_pi().get(j);

                // send MEE1 to election authorities
                this.party.sendMessage(eventId, bold_ea, new MEE1(pk_j, pi_j), aux, securityLevel);

                // update state
                this.party.updateState(this.eventContext, S210.class);

                // run self activation
                var stateId = this.getId();
                this.party.selfActivate(eventId, stateId, State::handleSelfActivation);

            } catch (Algorithm.Exception exception) {
                // move to error state
                this.party.updateState(this.eventContext, A2.class);
            }
        } catch (Algorithm.Exception exception) {
            // move to error state
            this.party.updateState(this.eventContext, A1.class);
        } catch (TaskException exception) {
            // move to error state
            this.party.updateState(this.eventContext, B1.class);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy