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

ch.openchvote.electionauthority.writein.states.S410 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.writein.states;

import ch.openchvote.algorithms.Algorithm;
import ch.openchvote.electionauthority.ElectionAuthority;
import ch.openchvote.electionauthority.writein.EventContext;
import ch.openchvote.electionauthority.writein.states.error.*;
import ch.openchvote.electionauthority.writein.tasks.TE12;
import ch.openchvote.electionauthority.writein.tasks.TE13;
import ch.openchvote.electionauthority.writein.tasks.TE14;
import ch.openchvote.framework.party.State;
import ch.openchvote.framework.annotations.state.Phase;
import ch.openchvote.framework.exceptions.TaskException;
import ch.openchvote.framework.communication.Message;
import ch.openchvote.protocol.parameters.SecurityParameters;
import ch.openchvote.protocol.protocols.writein.content.message.MEE3;
import ch.openchvote.protocol.protocols.writein.content.message.MEE4;
import ch.openchvote.protocol.phases.Tallying;
import ch.openchvote.utilities.tools.Parallel;
import ch.openchvote.utilities.tools.VMGJFassade;
import ch.openchvote.utilities.tuples.Pair;

@SuppressWarnings("MissingJavadoc")
@Phase(Tallying.class)
public final class S410 extends State {

    public S410(ElectionAuthority electionAuthority, EventContext eventContext) {
        super(electionAuthority, eventContext);
        this.registerMessageHandler(MEE3.class, this::handleMEE3);
        this.registerMessageHandler(MEE4.class, this::handleMEE4);
    }

    private void handleMEE3(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 MEE3 = this.party.getAndCheckContent(MEE3.class, message, aux, securityLevel);

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

        try {
            // perform task
            if (publicData.get_bold_E_tilde().isPresent(k - 1) && publicData.get_bold_pi_tilde().isPresent(k - 1)) {
                TE12.run(k, publicData);
            }
            if (publicData.get_bold_E_tilde().isPresent(k + 1) && publicData.get_bold_pi_tilde().isPresent(k + 1)) {
                TE12.run(k + 1, 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, A12.class);
        } catch (TaskException exception) {
            // move to error state
            this.party.updateState(this.eventContext, B12.class);
        }
    }

    private void handleMEE4(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 MEE4 = this.party.getAndCheckContent(MEE4.class, message, aux, securityLevel);

        // update event data
        publicData.setContent(k, MEE4);

        try {
            // perform task
            TE14.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, A14.class);
        } catch (TaskException exception) {
            // move to error state
            this.party.updateState(this.eventContext, B14.class);
        }
    }

    @SuppressWarnings("unused")
    @Override
    public void handleSelfActivation() {

        // 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 bold_ea = SP.get_bold_ea();
        var j = SP.getIndexOf(this.party.getId());

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

        // check if all MEE3 messages are available
        if (TE13.isReady(publicData)) {

            // select event data
            var pk = publicData.get_pk().get();
            var bold_pk_prime = publicData.get_bold_pk_prime().get();

            // release precomputation tables
            var securityParameters = new SecurityParameters(securityLevel);
            var p = securityParameters.get_p();
            VMGJFassade.releasePrecomputationTable(pk, p);
            Parallel.forEachLoop(bold_pk_prime, pk_prime -> VMGJFassade.releasePrecomputationTable(pk_prime, p));

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

                // select event data
                var bold_c_j = publicData.get_bold_C().get(j);
                var bold_D_j = publicData.get_arrow_bold_D().get(j);
                var pi_prime_j = publicData.get_bold_pi_prime().get(j);

                // send MEE4 to election authorities
                var aux = new Pair<>(ES, EP);
                this.party.sendMessage(eventId, bold_ea, new MEE4(bold_c_j, bold_D_j, pi_prime_j), aux, securityLevel);

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy