@maci-protocol/circuits 0.0.0-ci.f4bc8a6 → 0.0.0-ci.f4e2c46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -2
- package/README.md +2 -2
- package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
- package/build/ts/generateZkeys.js.map +1 -0
- package/build/ts/types.d.ts +13 -14
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +35 -19
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/MessageProcessor.circom +252 -0
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +200 -0
- package/circom/coordinator/non-qv/VoteTally.circom +162 -0
- package/circom/coordinator/qv/MessageProcessor.circom +250 -0
- package/circom/coordinator/qv/SingleMessageProcessor.circom +208 -0
- package/circom/coordinator/qv/VoteTally.circom +180 -0
- package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +8 -6
- package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
- package/circom/utils/MessageHasher.circom +57 -0
- package/circom/utils/MessageToCommand.circom +107 -0
- package/circom/utils/PoseidonHasher.circom +29 -0
- package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +12 -10
- package/circom/utils/VerifySignature.circom +39 -0
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +17 -15
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +17 -15
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
- package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
- package/circom/utils/trees/CheckRoot.circom +49 -0
- package/circom/utils/trees/LeafExists.circom +27 -0
- package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
- package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
- package/circom/utils/trees/QuinarySelector.circom +42 -0
- package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
- package/circom/utils/trees/Splicer.circom +76 -0
- package/circom/voter/PollJoined.circom +43 -0
- package/circom/voter/PollJoining.circom +54 -0
- package/package.json +20 -17
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
- package/circom/coordinator/non-qv/processMessages.circom +0 -447
- package/circom/coordinator/non-qv/tallyVotes.circom +0 -232
- package/circom/coordinator/qv/processMessages.circom +0 -449
- package/circom/coordinator/qv/tallyVotes.circom +0 -277
- package/circom/utils/hashers.circom +0 -78
- package/circom/utils/messageToCommand.circom +0 -78
- package/circom/utils/trees/incrementalMerkleTree.circom +0 -198
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
- package/circom/voter/poll.circom +0 -93
|
@@ -8,60 +8,69 @@ include "./bitify.circom";
|
|
|
8
8
|
include "./escalarmulany.circom";
|
|
9
9
|
include "./escalarmulfix.circom";
|
|
10
10
|
// local imports
|
|
11
|
-
include "./
|
|
11
|
+
include "./PoseidonHasher.circom";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Variant of the EdDSAPoseidonVerifier template from circomlib
|
|
15
15
|
* https://github.com/iden3/circomlib/blob/master/circuits/eddsa.circom
|
|
16
16
|
*/
|
|
17
|
-
template
|
|
17
|
+
template EdDSAPoseidonVerifier() {
|
|
18
|
+
// The prime subgroup order.
|
|
19
|
+
var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
|
|
20
|
+
|
|
21
|
+
// The base point of the BabyJubJub curve.
|
|
22
|
+
var BASE8[2] = [
|
|
23
|
+
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
24
|
+
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
25
|
+
];
|
|
26
|
+
|
|
18
27
|
// The x and y coordinates of the public key.
|
|
19
|
-
signal input
|
|
20
|
-
signal input
|
|
28
|
+
signal input publicKeyX;
|
|
29
|
+
signal input publicKeyY;
|
|
21
30
|
// Signature scalar.
|
|
22
|
-
signal input
|
|
31
|
+
signal input signatureScalar;
|
|
23
32
|
// The x and y coordinates of the signature point.
|
|
24
|
-
signal input
|
|
25
|
-
signal input
|
|
33
|
+
signal input signaturePointX;
|
|
34
|
+
signal input signaturePointY;
|
|
26
35
|
// Message hash.
|
|
27
|
-
signal input
|
|
36
|
+
signal input messageHash;
|
|
37
|
+
// Output signal for the validity of the signature.
|
|
38
|
+
signal output isValid;
|
|
28
39
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// convert the signature scalar S into its binary representation.
|
|
33
|
-
var computedNum2Bits[254] = Num2Bits(254)(S);
|
|
40
|
+
// Ensure signatureScalar<Subgroup Order.
|
|
41
|
+
// convert the signature scalar signatureScalar into its binary representation.
|
|
42
|
+
var computedNum2Bits[254] = Num2Bits(254)(signatureScalar);
|
|
34
43
|
|
|
35
44
|
var computedCompConstantIn[254] = computedNum2Bits;
|
|
36
45
|
computedCompConstantIn[253] = 0;
|
|
37
46
|
|
|
38
|
-
// A component that ensures
|
|
47
|
+
// A component that ensures signatureScalar is within a valid range,
|
|
39
48
|
// comparing it against a constant representing the subgroup order.
|
|
40
|
-
var computedCompConstant = CompConstant(
|
|
49
|
+
var computedCompConstant = CompConstant(SUBGROUP_ORDER - 1)(computedCompConstantIn);
|
|
41
50
|
|
|
42
|
-
// Calculate the h = H(R,A, msg).
|
|
43
|
-
var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([
|
|
51
|
+
// Calculate the h = H(R, A, msg).
|
|
52
|
+
var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([
|
|
53
|
+
signaturePointX,
|
|
54
|
+
signaturePointY,
|
|
55
|
+
publicKeyX,
|
|
56
|
+
publicKeyY,
|
|
57
|
+
messageHash
|
|
58
|
+
]));
|
|
44
59
|
|
|
45
60
|
// These components perform point doubling operations on the public key
|
|
46
61
|
// to align it within the correct subgroup as part of the verification process.
|
|
47
|
-
var (
|
|
48
|
-
var (
|
|
49
|
-
var (
|
|
62
|
+
var (computedDouble1XOut, computedDouble1YOut) = BabyDbl()(publicKeyX, publicKeyY);
|
|
63
|
+
var (computedDouble2XOut, computedDouble2YOut) = BabyDbl()(computedDouble1XOut, computedDouble1YOut);
|
|
64
|
+
var (computedDouble3XOut, computedDouble3YOut) = BabyDbl()(computedDouble2XOut, computedDouble2YOut);
|
|
50
65
|
|
|
51
66
|
// A component that performs scalar multiplication of the
|
|
52
67
|
// adjusted public key by the hash output, essential for the verification calculation.
|
|
53
|
-
var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [
|
|
68
|
+
var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDouble3XOut, computedDouble3YOut]);
|
|
54
69
|
|
|
55
70
|
// Compute the right side: right = R8 + right2.
|
|
56
|
-
var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(
|
|
57
|
-
|
|
58
|
-
// Calculate the left side: left = S * B8.
|
|
59
|
-
var BASE8[2] = [
|
|
60
|
-
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
61
|
-
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
62
|
-
];
|
|
71
|
+
var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(signaturePointX, signaturePointY, computedEscalarMulAny[0], computedEscalarMulAny[1]);
|
|
63
72
|
|
|
64
|
-
// Fixed-base scalar multiplication of a base point by
|
|
73
|
+
// Fixed-base scalar multiplication of a base point by signatureScalar.
|
|
65
74
|
var computedEscalarMulFix[2] = EscalarMulFix(254, BASE8)(computedNum2Bits);
|
|
66
75
|
|
|
67
76
|
// Components to check the equality of x and y coordinates
|
|
@@ -73,45 +82,10 @@ template EdDSAPoseidonVerifier_patched() {
|
|
|
73
82
|
// Components to handle edge cases and ensure that all conditions
|
|
74
83
|
// for a valid signature are met, including the
|
|
75
84
|
// public key not being zero and other integrity checks.
|
|
76
|
-
var computedIsAxZero = IsZero()(
|
|
85
|
+
var computedIsAxZero = IsZero()(publicKeyX);
|
|
77
86
|
var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
|
|
78
87
|
var computedIsCcZero = IsZero()(computedCompConstant);
|
|
79
88
|
var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero, 3]);
|
|
80
89
|
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
|
|
86
|
-
*/
|
|
87
|
-
template VerifySignature() {
|
|
88
|
-
// Public key of the signer, consisting of two coordinates [x, y].
|
|
89
|
-
signal input pubKey[2];
|
|
90
|
-
// R8 point from the signature, consisting of two coordinates [x, y].
|
|
91
|
-
signal input R8[2];
|
|
92
|
-
// Scalar component of the signature.
|
|
93
|
-
signal input S;
|
|
94
|
-
|
|
95
|
-
// Number of elements in the hash preimage.
|
|
96
|
-
var k = 4;
|
|
97
|
-
|
|
98
|
-
// The preimage data that was hashed, an array of four elements.
|
|
99
|
-
signal input preimage[k];
|
|
100
|
-
|
|
101
|
-
signal output valid;
|
|
102
|
-
|
|
103
|
-
// Hash the preimage using the Poseidon hashing function configured for four inputs.
|
|
104
|
-
var computedM = PoseidonHasher(4)(preimage);
|
|
105
|
-
|
|
106
|
-
// Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
|
|
107
|
-
var computedVerifier = EdDSAPoseidonVerifier_patched()(
|
|
108
|
-
pubKey[0],
|
|
109
|
-
pubKey[1],
|
|
110
|
-
S,
|
|
111
|
-
R8[0],
|
|
112
|
-
R8[1],
|
|
113
|
-
computedM
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
valid <== computedVerifier;
|
|
90
|
+
isValid <== computedIsValid;
|
|
117
91
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
include "./PoseidonHasher.circom";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hashes a MACI message and the public key used for message encryption.
|
|
7
|
+
* This template processes 10 message inputs and a 2-element public key
|
|
8
|
+
* combining them using the Poseidon hash function. The hashing process involves two stages:
|
|
9
|
+
* 1. hashing message parts data groups of five and,
|
|
10
|
+
* 2. hashing the grouped results alongside the first message input and
|
|
11
|
+
* the encryption public key to produce a final hash output.
|
|
12
|
+
*/
|
|
13
|
+
template MessageHasher() {
|
|
14
|
+
// Message parts
|
|
15
|
+
var MESSAGE_PARTS = 10;
|
|
16
|
+
var STATE_INDEX = 0;
|
|
17
|
+
var VOTE_OPTION_INDEX = 1;
|
|
18
|
+
var NEW_VOTE_WEIGHT = 2;
|
|
19
|
+
var NONCE = 3;
|
|
20
|
+
var POLL_ID = 4;
|
|
21
|
+
var SIGNATURE_POINT_X = 5;
|
|
22
|
+
var SIGNATURE_POINT_Y = 6;
|
|
23
|
+
var SIGNATURE_SCALAR = 7;
|
|
24
|
+
var ENCRYPTED_PUBLIC_KEY_X = 8;
|
|
25
|
+
var ENCRYPTED_PUBLIC_KEY_Y = 9;
|
|
26
|
+
|
|
27
|
+
// The MACI message is composed of 10 parts.
|
|
28
|
+
signal input data[MESSAGE_PARTS];
|
|
29
|
+
// the public key used to encrypt the message.
|
|
30
|
+
signal input encryptionPublicKey[2];
|
|
31
|
+
|
|
32
|
+
// we output an hash.
|
|
33
|
+
signal output hash;
|
|
34
|
+
|
|
35
|
+
var computedHasherPart1 = PoseidonHasher(5)([
|
|
36
|
+
data[STATE_INDEX],
|
|
37
|
+
data[VOTE_OPTION_INDEX],
|
|
38
|
+
data[NEW_VOTE_WEIGHT],
|
|
39
|
+
data[NONCE],
|
|
40
|
+
data[POLL_ID]
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
var computedHasherPart2 = PoseidonHasher(5)([
|
|
44
|
+
data[SIGNATURE_POINT_X],
|
|
45
|
+
data[SIGNATURE_POINT_Y],
|
|
46
|
+
data[SIGNATURE_SCALAR],
|
|
47
|
+
data[ENCRYPTED_PUBLIC_KEY_X],
|
|
48
|
+
data[ENCRYPTED_PUBLIC_KEY_Y]
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
hash <== PoseidonHasher(4)([
|
|
52
|
+
computedHasherPart1,
|
|
53
|
+
computedHasherPart2,
|
|
54
|
+
encryptionPublicKey[0],
|
|
55
|
+
encryptionPublicKey[1]
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./bitify.circom";
|
|
5
|
+
// zk-kit imports
|
|
6
|
+
include "./ecdh.circom";
|
|
7
|
+
include "./unpack-element.circom";
|
|
8
|
+
include "./poseidon-cipher.circom";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Converts a MACI message to a command by decrypting it.
|
|
12
|
+
* Processes encrypted MACI messages into structured MACI commands
|
|
13
|
+
* by decrypting using a shared key derived from ECDH. After decryption,
|
|
14
|
+
* unpacks and assigns decrypted values to specific command components.
|
|
15
|
+
*/
|
|
16
|
+
template MessageToCommand() {
|
|
17
|
+
var MESSAGE_LENGTH = 7;
|
|
18
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
19
|
+
var UNPACK_ELEMENT_LENGTH = 5;
|
|
20
|
+
var DECRYPTED_LENGTH = 9;
|
|
21
|
+
var MESSAGE_PARTS = 10;
|
|
22
|
+
|
|
23
|
+
// Element indices.
|
|
24
|
+
var ELEMENT_POLL_ID = 0;
|
|
25
|
+
var ELEMENT_NONCE = 1;
|
|
26
|
+
var ELEMENT_NEW_VOTE_WEIGHT = 2;
|
|
27
|
+
var ELEMENT_VOTE_OPTION_INDEX = 3;
|
|
28
|
+
var ELEMENT_STATE_INDEX = 4;
|
|
29
|
+
|
|
30
|
+
// Command indices.
|
|
31
|
+
var COMMAND_STATE_INDEX = 0;
|
|
32
|
+
var COMMAND_PUBLIC_KEY_X = 1;
|
|
33
|
+
var COMMAND_PUBLIC_KEY_Y = 2;
|
|
34
|
+
var COMMAND_SALT = 3;
|
|
35
|
+
|
|
36
|
+
// Decryptor indices.
|
|
37
|
+
var SIGNATURE_POINT_X = 4;
|
|
38
|
+
var SIGNATURE_POINT_Y = 5;
|
|
39
|
+
var SIGNATURE_SCALAR = 6;
|
|
40
|
+
|
|
41
|
+
// The message is an array of 10 parts.
|
|
42
|
+
signal input message[MESSAGE_PARTS];
|
|
43
|
+
// The encryption private key
|
|
44
|
+
signal input encryptionPrivateKey;
|
|
45
|
+
// The encryption public key
|
|
46
|
+
signal input encryptionPublicKey[2];
|
|
47
|
+
|
|
48
|
+
// Command parts.
|
|
49
|
+
signal output stateIndex;
|
|
50
|
+
// The new public key.
|
|
51
|
+
signal output newPublicKey[2];
|
|
52
|
+
// The vote option index.
|
|
53
|
+
signal output voteOptionIndex;
|
|
54
|
+
// The new vote weight.
|
|
55
|
+
signal output newVoteWeight;
|
|
56
|
+
// The nonce.
|
|
57
|
+
signal output nonce;
|
|
58
|
+
// The poll id.
|
|
59
|
+
signal output pollId;
|
|
60
|
+
// The salt.
|
|
61
|
+
signal output salt;
|
|
62
|
+
// The signature point.
|
|
63
|
+
signal output signaturePoint[2];
|
|
64
|
+
// The signature scalar.
|
|
65
|
+
signal output signatureScalar;
|
|
66
|
+
|
|
67
|
+
// Packed command.
|
|
68
|
+
signal output packedCommandOut[PACKED_COMMAND_LENGTH];
|
|
69
|
+
|
|
70
|
+
// Generate the shared key for decrypting the message.
|
|
71
|
+
var computedEcdh[2] = Ecdh()(encryptionPrivateKey, encryptionPublicKey);
|
|
72
|
+
|
|
73
|
+
// Decrypt the message using Poseidon decryption.
|
|
74
|
+
var computedDecryptor[DECRYPTED_LENGTH] = PoseidonDecryptWithoutCheck(MESSAGE_LENGTH)(
|
|
75
|
+
message,
|
|
76
|
+
0,
|
|
77
|
+
computedEcdh
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// Save the decrypted message into a packed command signal.
|
|
81
|
+
signal packedCommand[PACKED_COMMAND_LENGTH];
|
|
82
|
+
|
|
83
|
+
for (var i = 0; i < PACKED_COMMAND_LENGTH; i++) {
|
|
84
|
+
packedCommand[i] <== computedDecryptor[i];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
var computedUnpackElement[UNPACK_ELEMENT_LENGTH] = UnpackElement(UNPACK_ELEMENT_LENGTH)(
|
|
88
|
+
packedCommand[COMMAND_STATE_INDEX]
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Everything below were packed into the first element.
|
|
92
|
+
pollId <== computedUnpackElement[ELEMENT_POLL_ID];
|
|
93
|
+
nonce <== computedUnpackElement[ELEMENT_NONCE];
|
|
94
|
+
newVoteWeight <== computedUnpackElement[ELEMENT_NEW_VOTE_WEIGHT];
|
|
95
|
+
voteOptionIndex <== computedUnpackElement[ELEMENT_VOTE_OPTION_INDEX];
|
|
96
|
+
stateIndex <== computedUnpackElement[ELEMENT_STATE_INDEX];
|
|
97
|
+
|
|
98
|
+
newPublicKey[0] <== packedCommand[COMMAND_PUBLIC_KEY_X];
|
|
99
|
+
newPublicKey[1] <== packedCommand[COMMAND_PUBLIC_KEY_Y];
|
|
100
|
+
salt <== packedCommand[COMMAND_SALT];
|
|
101
|
+
|
|
102
|
+
signaturePoint[0] <== computedDecryptor[SIGNATURE_POINT_X];
|
|
103
|
+
signaturePoint[1] <== computedDecryptor[SIGNATURE_POINT_Y];
|
|
104
|
+
signatureScalar <== computedDecryptor[SIGNATURE_SCALAR];
|
|
105
|
+
|
|
106
|
+
packedCommandOut <== packedCommand;
|
|
107
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./poseidon-cipher.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Computes the Poseidon hash for an array of n inputs, including a default initial state
|
|
8
|
+
* of zero not counted in n. First, extends the inputs by prepending a zero, creating an array [0, inputs].
|
|
9
|
+
* Then, the Poseidon hash of the extended inputs is calculated, with the first element of the
|
|
10
|
+
* result assigned as the output.
|
|
11
|
+
*/
|
|
12
|
+
template PoseidonHasher(n) {
|
|
13
|
+
signal input inputs[n];
|
|
14
|
+
signal output out;
|
|
15
|
+
|
|
16
|
+
// [0, inputs].
|
|
17
|
+
var computedExtendedInputs[n + 1];
|
|
18
|
+
computedExtendedInputs[0] = 0;
|
|
19
|
+
|
|
20
|
+
for (var i = 0; i < n; i++) {
|
|
21
|
+
computedExtendedInputs[i + 1] = inputs[i];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Compute the Poseidon hash of the extended inputs.
|
|
25
|
+
var computedPoseidonPerm[n + 1];
|
|
26
|
+
computedPoseidonPerm = PoseidonPerm(n + 1)(computedExtendedInputs);
|
|
27
|
+
|
|
28
|
+
out <== computedPoseidonPerm[0];
|
|
29
|
+
}
|
|
@@ -9,28 +9,30 @@ include "./escalarmulfix.circom";
|
|
|
9
9
|
* Converts a private key to a public key on the BabyJubJub curve.
|
|
10
10
|
* The input private key needs to be hashed and then pruned before.
|
|
11
11
|
*/
|
|
12
|
-
template
|
|
12
|
+
template PrivateToPublicKey() {
|
|
13
13
|
// The base point of the BabyJubJub curve.
|
|
14
14
|
var BASE8[2] = [
|
|
15
15
|
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
16
16
|
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
17
17
|
];
|
|
18
18
|
|
|
19
|
-
//
|
|
20
|
-
var
|
|
19
|
+
// The prime subgroup order.
|
|
20
|
+
var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
signal
|
|
22
|
+
// The private key
|
|
23
|
+
signal input privateKey;
|
|
24
|
+
// The public key
|
|
25
|
+
signal output publicKey[2];
|
|
24
26
|
|
|
25
|
-
// Check if private key is in the prime subgroup order
|
|
26
|
-
var isLessThan = LessThan(251)([
|
|
27
|
+
// Check if private key is in the prime subgroup order
|
|
28
|
+
var isLessThan = LessThan(251)([privateKey, SUBGROUP_ORDER]);
|
|
27
29
|
isLessThan === 1;
|
|
28
30
|
|
|
29
31
|
// Convert the private key to bits.
|
|
30
|
-
var
|
|
32
|
+
var computedPrivateBits[253] = Num2Bits(253)(privateKey);
|
|
31
33
|
|
|
32
34
|
// Perform scalar multiplication with the basepoint.
|
|
33
|
-
var
|
|
35
|
+
var computedPublicKey[2] = EscalarMulFix(253, BASE8)(computedPrivateBits);
|
|
34
36
|
|
|
35
|
-
|
|
37
|
+
publicKey <== computedPublicKey;
|
|
36
38
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "./EdDSAPoseidonVerifier.circom";
|
|
5
|
+
include "./PoseidonHasher.circom";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
|
|
9
|
+
*/
|
|
10
|
+
template VerifySignature() {
|
|
11
|
+
// Number of elements in the hash preimage.
|
|
12
|
+
var PRE_IMAGE_LENGTH = 4;
|
|
13
|
+
|
|
14
|
+
// Public key of the signer, consisting of two coordinates [x, y].
|
|
15
|
+
signal input publicKey[2];
|
|
16
|
+
// signaturePoint point from the signature, consisting of two coordinates [x, y].
|
|
17
|
+
signal input signaturePoint[2];
|
|
18
|
+
// Scalar component of the signature.
|
|
19
|
+
signal input signatureScalar;
|
|
20
|
+
// The preimage data that was hashed, an array of four elements.
|
|
21
|
+
signal input preimage[PRE_IMAGE_LENGTH];
|
|
22
|
+
// The validity of the signature.
|
|
23
|
+
signal output isValid;
|
|
24
|
+
|
|
25
|
+
// Hash the preimage using the Poseidon hashing function configured for four inputs.
|
|
26
|
+
var computedPreimage = PoseidonHasher(4)(preimage);
|
|
27
|
+
|
|
28
|
+
// Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
|
|
29
|
+
var computedIsValid = EdDSAPoseidonVerifier()(
|
|
30
|
+
publicKey[0],
|
|
31
|
+
publicKey[1],
|
|
32
|
+
signatureScalar,
|
|
33
|
+
signaturePoint[0],
|
|
34
|
+
signaturePoint[1],
|
|
35
|
+
computedPreimage
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
isValid <== computedIsValid;
|
|
39
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../VerifySignature.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a MACI message is valid or not.
|
|
10
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
11
|
+
*/
|
|
12
|
+
template MessageValidatorFull() {
|
|
13
|
+
// Length of the packed command.
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 5;
|
|
17
|
+
|
|
18
|
+
// State index of the user.
|
|
19
|
+
signal input stateTreeIndex;
|
|
20
|
+
// Number of user sign-ups in the state tree.
|
|
21
|
+
signal input totalSignups;
|
|
22
|
+
// Vote option index.
|
|
23
|
+
signal input voteOptionIndex;
|
|
24
|
+
// Number of valid vote options for the poll.
|
|
25
|
+
signal input voteOptions;
|
|
26
|
+
// Ballot nonce.
|
|
27
|
+
signal input originalNonce;
|
|
28
|
+
// Command nonce.
|
|
29
|
+
signal input commandNonce;
|
|
30
|
+
// Packed command.
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
|
+
// Public key of the state leaf (user).
|
|
33
|
+
signal input publicKey[2];
|
|
34
|
+
// EdDSA signature of the command (R part).
|
|
35
|
+
signal input signaturePoint[2];
|
|
36
|
+
// EdDSA signature of the command (S part).
|
|
37
|
+
signal input signatureScalar;
|
|
38
|
+
// State leaf current voice credit balance.
|
|
39
|
+
signal input currentVoiceCreditBalance;
|
|
40
|
+
// Current number of votes for specific option.
|
|
41
|
+
signal input currentVotesForOption;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input voteWeight;
|
|
44
|
+
|
|
45
|
+
// True when the command is valid; otherwise false.
|
|
46
|
+
signal output isValid;
|
|
47
|
+
// True if the state leaf index is valid
|
|
48
|
+
signal output isStateLeafIndexValid;
|
|
49
|
+
// True if the vote option index is valid
|
|
50
|
+
signal output isVoteOptionIndexValid;
|
|
51
|
+
|
|
52
|
+
// Check (1) - The state leaf index must be valid.
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
54
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
55
|
+
// while 1 is for the first actual user.
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
57
|
+
|
|
58
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
59
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
|
+
|
|
61
|
+
// Check (3) - The nonce must be correct.
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
|
+
|
|
64
|
+
// Check (4) - The signature must be correct.
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
66
|
+
|
|
67
|
+
// Check (5) - There must be sufficient voice credits.
|
|
68
|
+
// The check ensure that currentVotesForOption + currentVoiceCreditBalance is equal to voteWeight.
|
|
69
|
+
var computedAreVoiceCreditsSpent = IsEqual()(
|
|
70
|
+
[
|
|
71
|
+
currentVotesForOption + currentVoiceCreditBalance,
|
|
72
|
+
voteWeight
|
|
73
|
+
]
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// When all five checks are correct, then isValid = 1.
|
|
77
|
+
var computedIsUpdateValid = IsEqual()(
|
|
78
|
+
[
|
|
79
|
+
TOTAL_CHECKS,
|
|
80
|
+
computedIsSignatureValid +
|
|
81
|
+
computedAreVoiceCreditsSpent +
|
|
82
|
+
computedIsNonceValid +
|
|
83
|
+
computedIsStateLeafIndexValid +
|
|
84
|
+
computedIsVoteOptionIndexValid
|
|
85
|
+
]
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
isValid <== computedIsUpdateValid;
|
|
89
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
90
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
91
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
10
|
+
* If the message is correct, updates the public key in the state leaf and the nonce
|
|
11
|
+
* in the ballot using multiplexer components.
|
|
12
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
13
|
+
*/
|
|
14
|
+
template StateLeafAndBallotTransformerFull() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
|
+
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input totalSignups;
|
|
20
|
+
// Number of valid vote options for the poll.
|
|
21
|
+
signal input voteOptions;
|
|
22
|
+
|
|
23
|
+
// The following signals represents a state leaf (signed up user).
|
|
24
|
+
// Public key.
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
|
+
// Current voice credit balance.
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
|
+
|
|
29
|
+
// The following signals represents a ballot.
|
|
30
|
+
// Nonce.
|
|
31
|
+
signal input ballotNonce;
|
|
32
|
+
// Current number of votes for specific option.
|
|
33
|
+
signal input ballotCurrentVotesForOption;
|
|
34
|
+
|
|
35
|
+
// The following signals represents a command.
|
|
36
|
+
// State index of the user.
|
|
37
|
+
signal input commandStateIndex;
|
|
38
|
+
// Public key of the user.
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
|
+
// Vote option index.
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
|
+
// Nonce.
|
|
45
|
+
signal input commandNonce;
|
|
46
|
+
// Poll identifier.
|
|
47
|
+
signal input commandPollId;
|
|
48
|
+
// Salt.
|
|
49
|
+
signal input commandSalt;
|
|
50
|
+
// EdDSA signature of the command (R part).
|
|
51
|
+
signal input commandSignaturePoint[2];
|
|
52
|
+
// EdDSA signature of the command (S part).
|
|
53
|
+
signal input commandSignatureScalar;
|
|
54
|
+
// Packed command.
|
|
55
|
+
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
|
+
|
|
58
|
+
// New state leaf (if the command is valid).
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
|
+
// New ballot (if the command is valid).
|
|
61
|
+
signal output newBallotNonce;
|
|
62
|
+
|
|
63
|
+
// True when the command is valid; otherwise false.
|
|
64
|
+
signal output isValid;
|
|
65
|
+
// True if the state leaf index is valid
|
|
66
|
+
signal output isStateLeafIndexValid;
|
|
67
|
+
// True if the vote option index is valid
|
|
68
|
+
signal output isVoteOptionIndexValid;
|
|
69
|
+
|
|
70
|
+
// Check if the command / message is valid.
|
|
71
|
+
var (
|
|
72
|
+
computedIsValid,
|
|
73
|
+
computedIsStateLeafIndexValid,
|
|
74
|
+
computedIsVoteOptionIndexValid
|
|
75
|
+
) = MessageValidatorFull()(
|
|
76
|
+
commandStateIndex,
|
|
77
|
+
totalSignups,
|
|
78
|
+
commandVoteOptionIndex,
|
|
79
|
+
voteOptions,
|
|
80
|
+
ballotNonce,
|
|
81
|
+
commandNonce,
|
|
82
|
+
packedCommand,
|
|
83
|
+
stateLeafPublicKey,
|
|
84
|
+
commandSignaturePoint,
|
|
85
|
+
commandSignatureScalar,
|
|
86
|
+
stateLeafVoiceCreditBalance,
|
|
87
|
+
ballotCurrentVotesForOption,
|
|
88
|
+
commandNewVoteWeight
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// If the message is valid then we swap out the public key.
|
|
92
|
+
// This means using a Mux1() for publicKey[0] and another one
|
|
93
|
+
// for publicKey[1].
|
|
94
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()(
|
|
95
|
+
[
|
|
96
|
+
stateLeafPublicKey[0],
|
|
97
|
+
commandPublicKey[0]
|
|
98
|
+
],
|
|
99
|
+
computedIsValid
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()(
|
|
103
|
+
[
|
|
104
|
+
stateLeafPublicKey[1],
|
|
105
|
+
commandPublicKey[1]
|
|
106
|
+
],
|
|
107
|
+
computedIsValid
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
111
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
112
|
+
|
|
113
|
+
// If the message is valid, then we swap out the ballot nonce
|
|
114
|
+
// using a Mux1().
|
|
115
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
116
|
+
|
|
117
|
+
newBallotNonce <== computedNewBallotNonceMux;
|
|
118
|
+
|
|
119
|
+
isValid <== computedIsValid;
|
|
120
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
121
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
122
|
+
}
|