Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions AnonymousVoting.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.3;
pragma solidity ^0.4.17;

/**
* @title ECCMath
Expand All @@ -13,9 +13,8 @@ library ECCMath {
/// @param a The number.
/// @param p The mmodulus.
/// @return x such that ax = 1 (mod p)
function invmod(uint a, uint p) internal constant returns (uint) {
if (a == 0 || a == p || p == 0)
throw;
function invmod(uint a, uint p) internal pure returns (uint) {
require(a != 0 && a != p && p != 0);
if (a > p)
a = a % p;
int t1;
Expand All @@ -39,13 +38,12 @@ library ECCMath {
/// @param e The exponent.
/// @param m The modulus.
/// @return x such that x = b**e (mod m)
function expmod(uint b, uint e, uint m) internal constant returns (uint r) {
function expmod(uint b, uint e, uint m) internal view returns (uint r) {
require(m != 0);
if (b == 0)
return 0;
if (e == 0)
return 1;
if (m == 0)
throw;
r = 1;
uint bit = 2 ** 255;
bit = bit;
Expand All @@ -69,7 +67,7 @@ library ECCMath {
/// @param z2Inv The square of zInv
/// @param prime The prime modulus.
/// @return (Px", Py", 1)
function toZ1(uint[3] memory P, uint zInv, uint z2Inv, uint prime) internal constant {
function toZ1(uint[3] memory P, uint zInv, uint z2Inv, uint prime) internal pure {
P[0] = mulmod(P[0], z2Inv, prime);
P[1] = mulmod(P[1], mulmod(zInv, z2Inv, prime), prime);
P[2] = 1;
Expand All @@ -80,7 +78,7 @@ library ECCMath {
/// @param PJ The point.
/// @param prime The prime modulus.
/// @return (Px", Py", 1)
function toZ1(uint[3] PJ, uint prime) internal constant {
function toZ1(uint[3] PJ, uint prime) internal pure {
uint zInv = invmod(PJ[2], prime);
uint zInv2 = mulmod(zInv, zInv, prime);
PJ[0] = mulmod(PJ[0], zInv2, prime);
Expand Down Expand Up @@ -115,7 +113,7 @@ library Secp256k1 {
// uint constant beta = "0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee";

/// @dev See Curve.onCurve
function onCurve(uint[2] P) internal constant returns (bool) {
function onCurve(uint[2] P) internal view returns (bool) {
uint p = pp;
if (0 == P[0] || P[0] == p || 0 == P[1] || P[1] == p)
return false;
Expand All @@ -125,21 +123,21 @@ library Secp256k1 {
}

/// @dev See Curve.isPubKey
function isPubKey(uint[2] memory P) internal constant returns (bool isPK) {
function isPubKey(uint[2] memory P) internal view returns (bool isPK) {
isPK = onCurve(P);
}

/// @dev See Curve.isPubKey
// TODO: We assume we are given affine co-ordinates for now
function isPubKey(uint[3] memory P) internal constant returns (bool isPK) {
function isPubKey(uint[3] memory P) internal view returns (bool isPK) {
uint[2] memory a_P;
a_P[0] = P[0];
a_P[1] = P[1];
isPK = onCurve(a_P);
}

/// @dev See Curve.validateSignature
function validateSignature(bytes32 message, uint[2] rs, uint[2] Q) internal constant returns (bool) {
function validateSignature(bytes32 message, uint[2] rs, uint[2] Q) internal view returns (bool) {
uint n = nn;
uint p = pp;
if(rs[0] == 0 || rs[0] >= n || rs[1] == 0 || rs[1] > lowSmax)
Expand All @@ -161,13 +159,13 @@ library Secp256k1 {
}

/// @dev See Curve.compress
function compress(uint[2] P) internal constant returns (uint8 yBit, uint x) {
function compress(uint[2] P) internal pure returns (uint8 yBit, uint x) {
x = P[0];
yBit = P[1] & 1 == 1 ? 1 : 0;
}

/// @dev See Curve.decompress
function decompress(uint8 yBit, uint x) internal constant returns (uint[2] P) {
function decompress(uint8 yBit, uint x) internal view returns (uint[2] P) {
uint p = pp;
var y2 = addmod(mulmod(x, mulmod(x, x, p), p), 7, p);
var y_ = ECCMath.expmod(y2, (p + 1) / 4, p);
Expand All @@ -179,7 +177,7 @@ library Secp256k1 {
// Point addition, P + Q
// inData: Px, Py, Pz, Qx, Qy, Qz
// outData: Rx, Ry, Rz
function _add(uint[3] memory P, uint[3] memory Q) internal constant returns (uint[3] memory R) {
function _add(uint[3] memory P, uint[3] memory Q) internal view returns (uint[3] memory R) {
if(P[2] == 0)
return Q;
if(Q[2] == 0)
Expand Down Expand Up @@ -218,7 +216,7 @@ library Secp256k1 {
// Point addition, P + Q. P Jacobian, Q affine.
// inData: Px, Py, Pz, Qx, Qy
// outData: Rx, Ry, Rz
function _addMixed(uint[3] memory P, uint[2] memory Q) internal constant returns (uint[3] memory R) {
function _addMixed(uint[3] memory P, uint[2] memory Q) internal view returns (uint[3] memory R) {
if(P[2] == 0)
return [Q[0], Q[1], 1];
if(Q[1] == 0)
Expand Down Expand Up @@ -258,7 +256,7 @@ library Secp256k1 {
}

// Same as addMixed but params are different and mutates P.
function _addMixedM(uint[3] memory P, uint[2] memory Q) internal constant {
function _addMixedM(uint[3] memory P, uint[2] memory Q) internal view {
if(P[1] == 0) {
P[0] = Q[0];
P[1] = Q[1];
Expand Down Expand Up @@ -304,7 +302,7 @@ library Secp256k1 {
// Point doubling, 2*P
// Params: Px, Py, Pz
// Not concerned about the 1 extra mulmod.
function _double(uint[3] memory P) internal constant returns (uint[3] memory Q) {
function _double(uint[3] memory P) internal view returns (uint[3] memory Q) {
uint p = pp;
if (P[2] == 0)
return;
Expand All @@ -320,7 +318,7 @@ library Secp256k1 {
}

// Same as double but mutates P and is internal only.
function _doubleM(uint[3] memory P) internal constant {
function _doubleM(uint[3] memory P) internal view {
uint p = pp;
if (P[2] == 0)
return;
Expand All @@ -338,7 +336,7 @@ library Secp256k1 {
// Multiplication dP. P affine, wNAF: w=5
// Params: d, Px, Py
// Output: Jacobian Q
function _mul(uint d, uint[2] memory P) internal constant returns (uint[3] memory Q) {
function _mul(uint d, uint[2] memory P) internal view returns (uint[3] memory Q) {
uint p = pp;
if (d == 0) // TODO
return;
Expand Down Expand Up @@ -433,7 +431,7 @@ contract owned {

/* Function to dictate that only the designated owner can call a function */
modifier onlyOwner {
if(owner != msg.sender) throw;
require(owner == msg.sender);
_;
}

Expand Down Expand Up @@ -520,9 +518,7 @@ contract AnonymousVoting is owned {
State public state;

modifier inState(State s) {
if(state != s) {
throw;
}
require(state == s);
_;
}

Expand All @@ -546,9 +542,7 @@ contract AnonymousVoting is owned {
function setEligible(address[] addr) onlyOwner {

// We can only handle up 50 people at the moment.
if(totaleligible > 50) {
throw;
}
require(totaleligible <= 50);

// Sign up the addresses
for(uint i=0; i<addr.length; i++) {
Expand Down Expand Up @@ -784,9 +778,7 @@ contract AnonymousVoting is owned {
function register(uint[2] xG, uint[3] vG, uint r) inState(State.SIGNUP) payable returns (bool) {

// HARD DEADLINE
if(block.timestamp > finishSignupPhase) {
throw; // throw returns the voter's ether, but exhausts their gas.
}
require(block.timestamp <= finishSignupPhase);

// Make sure the ether being deposited matches what we expect.
if(msg.value != depositrequired) {
Expand Down Expand Up @@ -1007,9 +999,7 @@ contract AnonymousVoting is owned {
for(uint i=0; i<totalregistered; i++) {

// Confirm all votes have been cast...
if(!votecast[voters[i].addr]) {
throw;
}
require(votecast[voters[i].addr]);

vote = voters[i].vote;

Expand Down
Loading