2019-04-22

4354297443921806682861743161735094062
38632140595220392354280998614525578145353818029287874088356304829962854601866
pragma solidity ^0.5;

contract Ballot{
uint Publickey_n;

event e_get_pk(
    uint pk
);

event e_get_D(
    uint d
);
//voter
struct voter {
    uint vote;
}

//candidate
struct candidate {
    uint count;
    bool win;
}

//chairperson init voting
address chairperson;
mapping(address => voter) voters;
mapping(address => candidate) candidates;
address[] candidate_add;

constructor() public {
}

function initPk(uint pk) public {
    Publickey_n = pk;
    Publickey(pk);
    emit e_get_pk(pk);
}

function initVoter(address[] memory voter_address) public  {
    for (uint i=0;i 0);
    Evote = E(Evote,r);
    uint e = Evote;
    if (candidates[candidate_address].count != 0)
        candidates[candidate_address].count = e_add(candidates[candidate_address].count,Evote);
    else 
        candidates[candidate_address].count = Evote;
    voters[msg.sender].vote = 0;
    return(e);
}

function votes(address[] memory candidate_address,uint[] memory Evotes,uint[] memory r) public {
    require(voters[msg.sender].vote > 0);
    for (uint i=0;i 0){
        if((_exponent & 1) == 1) result = mulmod(result, _base, _modulus);
        _exponent = _exponent >> 1;
        _base = mulmod(_base, _base, _modulus);
    }
    return result;
}

//加密  {(g**m mod n*n)*(r**n mod n*n)}mod n*n
function E(uint m,uint r) public view returns(uint) {
    return mulmod(modpow(Publickey_g,m,Publickey_n_sq),modpow(r,Publickey_n,Publickey_n_sq),Publickey_n_sq);
}

//解密 
function D(uint c,uint sk) public returns(uint) {
    init_x_y;
    if (y<0) y += int(Publickey_n);
    gcd(int(Publickey_n),int((modpow(Publickey_g,sk,Publickey_n_sq)-1)/Publickey_n));
    uint res = mulmod(((modpow(c,sk,Publickey_n_sq)-1)/Publickey_n),uint(y),Publickey_n);
    emit e_get_D(res);
    return res;
}

function D1(uint c,uint sk) public returns(uint) {
    return ((modpow(c,sk,Publickey_n_sq)-1)/Publickey_n);
    //gcd(int(Publickey_n),int((modpow(c,sk,Publickey_n_sq)-1)/Publickey_n));
    //return mulmod(((modpow(c,sk,Publickey_n_sq)-1)/Publickey_n),uint(y),Publickey_n);
}

function D2(uint c,uint sk) public returns(uint) {
    return ((modpow(Publickey_g,sk,Publickey_n_sq)-1)/Publickey_n);
    //return mulmod(((modpow(c,sk,Publickey_n_sq)-1)/Publickey_n)/((modpow(Publickey_g,sk,Publickey_n_sq)-1)/Publickey_n),1,Publickey_n);
}

function e_add(uint a, uint b) public returns(uint) {
        /*Add one encrypted uinteger to another*/
  emit timeStamper(time, msg.sender, mulmod(a, b, Publickey_n_sq));
  return mulmod(a, b, Publickey_n_sq);
}

function e_add_const(uint a, uint n, uint _input_Publickey) public returns(uint) {
        /*Add constant n to an encrypted uinteger*/
  Publickey(_input_Publickey);
  emit timeStamper(time, msg.sender, mulmod(a, modpow(Publickey_g, n, Publickey_n_sq), Publickey_n_sq));
  return mulmod(a, modpow(Publickey_g, n, Publickey_n_sq), Publickey_n_sq);
}

function e_mul_const(uint a, uint n, uint _input_Publickey) public returns(uint) {
        /*Multiplies an encrypted uinteger by a constant*/
        Publickey(_input_Publickey);
        emit timeStamper(time, msg.sender, modpow(a, n, Publickey_n_sq));
        return modpow(a, n, Publickey_n_sq);
}

}

你可能感兴趣的:(2019-04-22)