From cb162edd30f8544df41aba923731373127641a3b Mon Sep 17 00:00:00 2001
From: cmsigrist <capucine.berger@epfl.ch>
Date: Mon, 20 Jun 2022 17:01:34 +0200
Subject: [PATCH 1/2] Fix the ballot encoding

---
 web/frontend/src/pages/ballot/Show.tsx        |  4 ++--
 .../pages/ballot/components/VoteEncode.tsx    | 21 ++++++++++++-------
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/web/frontend/src/pages/ballot/Show.tsx b/web/frontend/src/pages/ballot/Show.tsx
index 8753444d2..975a3c02a 100644
--- a/web/frontend/src/pages/ballot/Show.tsx
+++ b/web/frontend/src/pages/ballot/Show.tsx
@@ -27,7 +27,7 @@ const Ballot: FC = () => {
 
   const { electionId } = useParams();
   const UserID = sessionStorage.getItem('id');
-  const { loading, configObj, electionID, status, pubKey, chunksPerBallot } =
+  const { loading, configObj, electionID, status, pubKey, ballotSize, chunksPerBallot } =
     useElection(electionId);
   const { configuration, answers, setAnswers } = useConfiguration(configObj);
 
@@ -75,7 +75,7 @@ const Ballot: FC = () => {
 
   const sendBallot = async () => {
     try {
-      const ballotChunks = voteEncode(answers, chunksPerBallot);
+      const ballotChunks = voteEncode(answers, ballotSize, chunksPerBallot);
       const EGPairs = Array<Buffer[]>();
       ballotChunks.forEach((chunk) =>
         EGPairs.push(encryptVote(chunk, Buffer.from(hexToBytes(pubKey).buffer), edCurve))
diff --git a/web/frontend/src/pages/ballot/components/VoteEncode.tsx b/web/frontend/src/pages/ballot/components/VoteEncode.tsx
index 05f635bd9..85cd60a61 100644
--- a/web/frontend/src/pages/ballot/components/VoteEncode.tsx
+++ b/web/frontend/src/pages/ballot/components/VoteEncode.tsx
@@ -2,7 +2,11 @@ import { Buffer } from 'buffer';
 import ShortUniqueId from 'short-unique-id';
 import { Answers, RANK, SELECT, TEXT } from 'types/configuration';
 
-export function voteEncode(answers: Answers, chunksPerBallot: number): string[] {
+export function voteEncode(
+  answers: Answers,
+  ballotSize: number,
+  chunksPerBallot: number
+): string[] {
   // contains the special string representation of the result
   let encodedBallot = '';
 
@@ -34,22 +38,23 @@ export function voteEncode(answers: Answers, chunksPerBallot: number): string[]
 
   encodedBallot += '\n';
 
-  const chunkSize = 29;
-  // ballot size
-  const ballotSize = chunksPerBallot * chunkSize;
   const encodedBallotSize = Buffer.byteLength(encodedBallot);
 
-  // add padding if necessary
+  // add padding if necessary until encodedBallot.length == ballotSize
   if (encodedBallotSize < ballotSize) {
     const padding = new ShortUniqueId({ length: ballotSize - encodedBallotSize });
     encodedBallot += padding();
   }
 
+  const chunkSize = 29;
   const ballotChunks: string[] = [];
 
-  // divide into chunks of 29 bytes, where 1 character === 1 byte
-  for (let i = 0; i < ballotSize; i += chunkSize) {
-    ballotChunks.push(encodedBallot.substring(i, i + chunkSize));
+  // divide into chunksPerBallot chunks, where 1 character === 1 byte
+  for (let i = 0; i < chunksPerBallot; i += 1) {
+    const start = i * chunkSize;
+    // substring(start, start + chunkSize), if (start + chunkSize) > string.length
+    // then (start + chunkSize) is treated as if it was equal to string.length
+    ballotChunks.push(encodedBallot.substring(start, start + chunkSize));
   }
 
   return ballotChunks;

From a11f9f104df8db3afa55b9e8d4f8065e64577e5b Mon Sep 17 00:00:00 2001
From: cmsigrist <capucine.berger@epfl.ch>
Date: Mon, 20 Jun 2022 17:04:37 +0200
Subject: [PATCH 2/2] Update doc

---
 docs/ballot_encoding.md | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/docs/ballot_encoding.md b/docs/ballot_encoding.md
index 01c89ae4d..936e2c4ed 100644
--- a/docs/ballot_encoding.md
+++ b/docs/ballot_encoding.md
@@ -46,14 +46,14 @@ A possible encoding of an answer would be (by string concatenation):
 "text:cd13:base64("Noémien"),base64("Pierluca")\n"
 ```
 
-## Size of the ballot
+## Size of the ballot 
 
 In order to maintain complete voter anonymity and untraceability of ballots throughout the 
 election process, it is important that all encrypted ballots have the same size. To this aim, 
-the election has an attribute called "BallotSize" (multiple of 29) which is the size 
+the election has an attribute called "BallotSize" which is the size 
 that all ballots should have before they're encrypted. Smaller ballots should therefore be 
 padded in order to reach this size. To denote the end of the ballot and the start of the padding,
-we use an empty line (\n\n). For a ballot size of 116, our ballot from the previous example 
+we use an empty line (\n\n). For a ballot size of 117, our ballot from the previous example 
 would then become:
 
 ```
@@ -63,5 +63,11 @@ would then become:
 
 "text:cd13:base64("Noémien"),base64("Pierluca")\n\n" +
 
-"ndtTx5uxmvnllH1T7NgLOREguUWbN"
+"ndtTx5uxmvnllH1T7NgLORuUWbN"
 ```
+
+## Chunks of ballot
+
+The encoded ballot must then be divided into chunks of 29 or less bytes since the maximum size supported by the kyber library for the encryption is of 29 bytes.
+
+For the previous example we would then have 5 chunks, the first 4 would contain 29 bytes, while the last chunk would contain a single byte.
\ No newline at end of file