123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- /*
- * Copyright 2018 Scytl Secure Electronic Voting SA
- *
- * All rights reserved
- *
- * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
- */
- describe('Proofs service ...', function() {
- 'use strict';
- // Initialize variables common to all proof tests.
- beforeEach(initializeTestVars);
- // OR-proof tests.
- describe('should be able to ..', function() {
- var public_key_length = 1;
- var num_elements = 5;
- var max_data_length = 30;
- var num_progress_checks = num_elements * 2;
- var initialized = false;
- var publicKey;
- var elements;
- var index;
- var ciphertext;
- var witness;
- var data;
- var proof;
- var preComputedValues;
- var anotherElements;
- var anotherPublicKey;
- var anotherCiphertext;
- var anotherWitness;
- var aLongerPublicKey;
- var aLongerCiphertext;
- var aShorterElements;
- var aLongerElements;
- // Initialize variables common to OR-proof tests.
- beforeEach(function() {
- if (!initialized) {
- cryptolib('proofs.service', function(box) {
- publicKey = generateElGamalKeyPair(public_key_length).getPublicKey();
- elements = generatePlaintext(num_elements);
- index = generateRandomInteger(0, (num_elements - 1));
- ciphertext = generateCiphertext(publicKey, [elements[index]]);
- witness = ciphertext.getR();
- var dataLength = generateRandomInteger(1, max_data_length);
- data = generateRandomString(dataLength);
- proof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup);
- preComputedValues = box.proofs.service.preComputeORProof(
- publicKey, num_elements, _zpSubgroup);
- do {
- anotherElements = generatePlaintext(num_elements);
- } while (anotherElements.equals(elements) ||
- anotherElements[index].equals(elements[index]));
- do {
- anotherPublicKey =
- generateElGamalKeyPair(public_key_length).getPublicKey();
- } while (anotherPublicKey.getGroupElementsArray().equals(
- publicKey.getGroupElementsArray()));
- anotherCiphertext =
- generateCiphertext(anotherPublicKey, [elements[index]]);
- anotherWitness = anotherCiphertext.getR();
- do {
- aLongerPublicKey =
- generateElGamalKeyPair(public_key_length + 1).getPublicKey();
- } while (aLongerPublicKey.getGroupElementsArray().equals(
- publicKey.getGroupElementsArray()));
- aLongerCiphertext =
- generateCiphertext(aLongerPublicKey, [elements[0], elements[1]]);
- aShorterElements = generatePlaintext(1);
- aLongerElements = [];
- aLongerElements.addAll(elements);
- aLongerElements.push(elements[0]);
- });
- initialized = true;
- }
- });
- it('generate and verify an OR-proof', function() {
- cryptolib('proofs.service', function(box) {
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proof, _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and generate and verify an OR-proof, using pre-computed values',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, preComputedValues);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and generate and verify an OR-proof, using an empty string for the data option',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, '',
- _zpSubgroup);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, '', anotherProof, _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and generate and verify an OR-proof, using null for the data option',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, null,
- _zpSubgroup);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, null, anotherProof,
- _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and generate and verify an OR-proof, using null for the pre-computed values option',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, null);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('generate and verify an OR-proof, using serialization', function() {
- cryptolib('commons.mathematical', 'proofs', function(box) {
- var proofAsJson = proof.stringify();
- var proofFromJson = box.proofs.utils.deserializeProof(proofAsJson);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proofFromJson, _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('generate and verify an OR-proof, using pre-computed values and serialization',
- function() {
- cryptolib('commons.mathematical', 'proofs', function(box) {
- var preComputedValuesAsJson = preComputedValues.stringify();
- var preComputedValuesFromJson =
- box.proofs.utils.deserializeProofPreComputedValues(
- preComputedValuesAsJson);
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, preComputedValuesFromJson);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and measure progress when generating an OR-proof', function() {
- cryptolib('proofs.service', function(box) {
- initializeProgressMeter();
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data, _zpSubgroup,
- progressCallback, PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
- expect(_progressPercentSum)
- .toBe(getProgressPercentSum(
- num_progress_checks, PROGRESS_PERCENT_MIN_CHECK_INTERVAL));
- expect(_progressPercentLatest).toBe(100);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof, _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and measure progress when pre-computing an OR-proof', function() {
- cryptolib('proofs.service', function(box) {
- initializeProgressMeter();
- preComputedValues = box.proofs.service.preComputeORProof(
- publicKey, num_elements, _zpSubgroup, progressCallback,
- PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
- expect(_progressPercentSum)
- .toBe(getProgressPercentSum(
- num_progress_checks, PROGRESS_PERCENT_MIN_CHECK_INTERVAL));
- expect(_progressPercentLatest).toBe(100);
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data, _zpSubgroup,
- preComputedValues);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof, _zpSubgroup);
- expect(verification).toBeTruthy();
- });
- });
- it('and measure progress when verifying an OR-proof', function() {
- cryptolib('proofs.service', function(box) {
- initializeProgressMeter();
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proof, _zpSubgroup,
- progressCallback, PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
- expect(verification).toBeTruthy();
- expect(_progressPercentSum)
- .toBe(getProgressPercentSum(
- num_progress_checks, PROGRESS_PERCENT_MIN_CHECK_INTERVAL));
- expect(_progressPercentLatest).toBe(100);
- });
- });
- it('and fail to verify an OR-proof that was generated with the wrong public key',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- anotherPublicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup);
- var verification = box.proofs.service.verifyORProof(
- anotherPublicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeFalsy();
- });
- });
- it('and fail to verify an OR-proof that was generated with the wrong ciphertext',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, anotherCiphertext, witness, elements, index, data,
- _zpSubgroup);
- var verification = box.proofs.service.verifyORProof(
- publicKey, anotherCiphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeFalsy();
- });
- });
- it('and fail to verify an OR-proof that was generated with the wrong witness',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, anotherWitness, elements, index, data,
- _zpSubgroup);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeFalsy();
- });
- });
- it('and fail to verify an OR-proof that was generated with the wrong elements',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, anotherElements, index, data,
- _zpSubgroup);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, anotherElements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeFalsy();
- });
- });
- it('and fail to verify an OR-proof that was pre-computed with the wrong public key',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherPreComputedValues = box.proofs.service.preComputeORProof(
- anotherPublicKey, num_elements, _zpSubgroup);
- var anotherProof = box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, anotherPreComputedValues);
- var verification = box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeFalsy();
- });
- });
- it('and fail to verify an OR-proof that was pre-computed and generated with the wrong public key',
- function() {
- cryptolib('proofs.service', function(box) {
- var anotherPreComputedValues = box.proofs.service.preComputeORProof(
- anotherPublicKey, num_elements, _zpSubgroup);
- var anotherProof = box.proofs.service.createORProof(
- anotherPublicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, anotherPreComputedValues);
- var verification = box.proofs.service.verifyORProof(
- anotherPublicKey, ciphertext, elements, data, anotherProof,
- _zpSubgroup);
- expect(verification).toBeFalsy();
- });
- });
- it('and throw an exception when generating an OR-proof, using null input data',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- null, ciphertext, witness, elements, index, data, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, null, witness, elements, index, data, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, null, elements, index, data,
- _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, null, index, data,
- _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, null, data,
- _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data, null);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using the wrong mathematical group',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _anotherzpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using a public key with more than one element',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- aLongerPublicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using a ciphertext with more than two elements',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, aLongerCiphertext, witness, elements, index, data,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using less than two elements',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, aShorterElements, index, data,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using a negative index',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, -1, data,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using an index that is greater than the number of elements minus one',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, num_elements, data,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof from pre-computed values, using too many elements',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, aLongerElements, index, data,
- _zpSubgroup, preComputedValues);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using a progress callback function that is not a function',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
- }).toThrow();
- });
- });
- it('and throw an exception when generating an OR-proof, using a progress percent check interval that is not a number',
- function() {
- cryptolib('proofs.service', function(box) {
- var percentMeasuredLatest = 0;
- var progressCallback = function(progressPercent) {
- percentMeasuredLatest = progressPercent;
- };
- expect(function() {
- box.proofs.service.createORProof(
- publicKey, ciphertext, witness, elements, index, data,
- _zpSubgroup, progressCallback, progressCallback);
- }).toThrow();
- });
- });
- it('and throw an exception when pre-computing an OR-proof, using null input data',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.preComputeORProof(
- null, num_elements, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.preComputeORProof(publicKey, null, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.preComputeORProof(
- publicKey, num_elements, null);
- }).toThrow();
- });
- });
- it('and throw an exception when pre-computing an OR-proof, using a public key with more than one element',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.preComputeORProof(
- aLongerPublicKey, num_elements, _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when pre-computing an OR-proof, using less than two elements',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.preComputeORProof(publicKey, 1, _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when pre-computing an OR-proof, using a progress callback function that is not a function',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.preComputeORProof(
- publicKey, num_elements, _zpSubgroup,
- PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
- }).toThrow();
- });
- });
- it('and throw an exception when pre-computing an OR-proof, using a progress percent check interval that is not a number',
- function() {
- cryptolib('proofs.service', function(box) {
- var percentMeasuredLatest = 0;
- var progressCallback = function(progressPercent) {
- percentMeasuredLatest = progressPercent;
- };
- expect(function() {
- box.proofs.service.preComputeORProof(
- publicKey, num_elements, _zpSubgroup, progressCallback,
- progressCallback);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using null input data',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.verifyORProof(
- null, ciphertext, elements, data, proof, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, null, elements, data, proof, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, null, data, proof, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, null, _zpSubgroup);
- }).toThrow();
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proof, null);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using the wrong mathematical group',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proof,
- _anotherZpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using a public key with more than one element',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.verifyORProof(
- aLongerPublicKey, ciphertext, elements, data, proof,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using a ciphertext with more than two elements',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, aLongerCiphertext, elements, data, proof,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using less than two elements',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, aShorterElements, data, proof,
- _zpSubgroup);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using a progress callback function that is not a function',
- function() {
- cryptolib('proofs.service', function(box) {
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proof, _zpSubgroup,
- PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
- }).toThrow();
- });
- });
- it('and throw an exception when verifying an OR-proof, using a progress percent check interval that is not a number',
- function() {
- cryptolib('proofs.service', function(box) {
- var percentMeasuredLatest = 0;
- var progressCallback = function(progressPercent) {
- percentMeasuredLatest = progressPercent;
- };
- expect(function() {
- box.proofs.service.verifyORProof(
- publicKey, ciphertext, elements, data, proof, _zpSubgroup,
- progressCallback, progressCallback);
- }).toThrow();
- });
- });
- });
- });
|