Skip to content

Commit

Permalink
Fixed linting
Browse files Browse the repository at this point in the history
  • Loading branch information
frog711 committed Sep 28, 2023
1 parent 26c9c36 commit cd9e639
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 115 deletions.
1 change: 0 additions & 1 deletion src/helper/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright © 2019-2023, Karlsruhe Institute of Technology (KIT), Maximilian Noppel, Christoph Niederbudde

from algos.algorithms import build_bytearray_by_type
from algos.ellipticCurveEncodingDecoding import elliptic_curve_encoding
from algos.secp256k1 import Curve
import hashlib
import math
Expand Down
3 changes: 1 addition & 2 deletions src/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/python

# Copyright © 2019-2023, Karlsruhe Institute of Technology (KIT), Christoph Niederbudde
# Copyright © 2019-2023, Karlsruhe Institute of Technology (KIT), Christoph Niederbudde
19 changes: 12 additions & 7 deletions src/test/integrationtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

# Copyright © 2019-2023, Karlsruhe Institute of Technology (KIT), Christoph Niederbudde
import unittest
from parameterized import parameterized
from app.helper.classes import *
import logging
from app.verificationtool import verify_ballot_box, verify_mixing_input, get_signature_if_valid, initialize_gpg, close_gpg
import sys
from app.helper.secureJSON import *
from app.algos.verifications import *
import logging
import json
from parameterized import parameterized
from app.helper.classes import BallotBoxEntry
from app.verificationtool import verify_ballot_box, verify_mixing_input, get_signature_if_valid, initialize_gpg
from app.helper.secureJSON import loadSecureJSON

logger = logging.getLogger()
logger.level = logging.DEBUG
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)


class BallotBoxFlaggedTestClass(unittest.TestCase):
@parameterized.expand([["different_ballots",
"Ballot of voter voter4 incorrectly transferred from ballot-box to ballot-box-flagged"],
Expand Down Expand Up @@ -45,6 +46,7 @@ def test_valid(self, dataset):
valid = verify_ballot_box(path + dataset)
self.assertTrue(valid)


class InputMixPacketsTestClass(unittest.TestCase):
@parameterized.expand([["packet_to_small", "A packet on board mixing-input-packets for public label 0 has an invalid size"],
["packet_to_large", "A packet on board mixing-input-packets for public label 0 has an invalid size"],
Expand All @@ -65,6 +67,7 @@ def text_valid(self, folder):
valid = verify_mixing_input(path + folder)
self.assertTrue(valid)


class ReceiptTestClass(unittest.TestCase):
def testValidFingerprint(self):
content = """
Expand Down Expand Up @@ -98,7 +101,7 @@ def testValidFingerprint(self):

def testValidReceipt(self):
path = "../data/full_doc_ext"
key = loadSecureJSON(path,"bbox-ballotbox-key-cp.json")
key = loadSecureJSON(path, "bbox-ballotbox-key-cp.json")
gpg = initialize_gpg([key])
self.assertNotEqual(get_signature_if_valid(path + "/receipts", "b1.pdf", gpg, key), None)

Expand All @@ -108,6 +111,7 @@ def testInvalidReceipt(self):
gpg = initialize_gpg([key])
self.assertEqual(get_signature_if_valid(path + "/receipts", "b2.pdf", gpg, key), None)


class SeconDevicePublicParametersTestClass(unittest.testCase):
@parameterized.expand([])
def test_invalid(self, dataset, log, index):
Expand All @@ -124,5 +128,6 @@ def test_valid(self, dataset):
valid = verify_ballot_box(path + dataset)
self.assertTrue(valid)


if __name__ == "__main__":
unittest.main()
35 changes: 21 additions & 14 deletions src/test/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from algos.algorithms import numbers_from_seed, kdf, numbers_from_seed_range, build_bytearray_by_type, uniform_hash, revocation_token_fingerprint, independent_generators_for_ec_groups_of_prime_order
from algos.secp256k1 import Point, Curve, secp256k1_q


class NumbersFromSeedTestClass(unittest.TestCase):
"""
unittest.TestCase for algorithm 2
Expand All @@ -18,13 +19,13 @@ def test_from_doc(self):
i = 0
for r in gen:
if i == 0:
self.assertEqual(r,1732501504205220402900929820446308723705652945081825598593993913145942097001127020633138020218038968109094917857329663184563374015879596834703721749398989648)
self.assertEqual(r, 1732501504205220402900929820446308723705652945081825598593993913145942097001127020633138020218038968109094917857329663184563374015879596834703721749398989648)
elif i == 1:
self.assertEqual(r, 2207401303665503434031531355511922974889692817601183500259263742625914061046146142929376778072827450461936300533206904979740474482058840003720379960491023511)
elif i == 2:
self.assertEqual(r,1883889587903519477357838514223953979954201344665681798367023196328721975720052153913582122151913785273222921786889836987731296728825119604809609410157987402)
self.assertEqual(r, 1883889587903519477357838514223953979954201344665681798367023196328721975720052153913582122151913785273222921786889836987731296728825119604809609410157987402)
elif i == 3:
self.assertEqual(r,1423259849467217711185874799515607842842602785767879766623736284680209832704638390900412597196948750015976271793930713744890547611655064835165883323889981463)
self.assertEqual(r, 1423259849467217711185874799515607842842602785767879766623736284680209832704638390900412597196948750015976271793930713744890547611655064835165883323889981463)
else:
break
i = i + 1
Expand All @@ -37,9 +38,10 @@ def test_from_alg_4_testcase_2(self):
b = bytearray.fromhex("4e9bc4d8957bf9b0823847c42fb170c958dda6b36d671acc4c5ea02e165586cc91d444e68c3a817c28631aa09695b6db2085f8282f1edd6c36e9184eacd91ecb")
gen = numbers_from_seed(31, b)
for r in gen:
self.assertEqual(r,1444258901)
self.assertEqual(r, 1444258901)
break


class KeyDerivationFunctionTestClass(unittest.TestCase):
"""
unittest.TestCase for algorithm 1
Expand All @@ -55,6 +57,7 @@ def test_from_doc(self):
self.assertTrue(r[64] == 0x75)
self.assertTrue(len(r) == 65)


class NumbersFromSeedRangeTestClass(unittest.TestCase):
"""
unittest.TestCase for algorithm 3
Expand All @@ -70,12 +73,13 @@ def test_from_doc(self):
i = 0
for n in gen:
if i == 0:
self.assertEqual(n,1732501504205220402900929820446308723705652945081825598593993913145942097001127020633138020218038968109094917857329663184563374015879596834703721749398989648)
self.assertEqual(n, 1732501504205220402900929820446308723705652945081825598593993913145942097001127020633138020218038968109094917857329663184563374015879596834703721749398989648)
elif i == 1:
self.assertEqual(n,1423259849467217711185874799515607842842602785767879766623736284680209832704638390900412597196948750015976271793930713744890547611655064835165883323889981463)
self.assertEqual(n, 1423259849467217711185874799515607842842602785767879766623736284680209832704638390900412597196948750015976271793930713744890547611655064835165883323889981463)
else:
break
i = i+1
i = i + 1


class BuildByteArrayByTypeTestClass(unittest.TestCase):
"""
Expand Down Expand Up @@ -109,8 +113,8 @@ def test_ecc_cases_int(self):
:return:
"""
compressed_key = bytearray.fromhex("0275788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b")
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"),byteorder="big",signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"),byteorder="big",signed=False)
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"), byteorder="big", signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"), byteorder="big", signed=False)
self.assertEqual(build_bytearray_by_type(Point(x, y)), compressed_key)

def test_exception_cases(self):
Expand Down Expand Up @@ -143,11 +147,13 @@ def test_points_compressed_and_uncompressed(self):

self.assertEqual(uniform_hash(curve.q, *e), uniform_hash(curve.q, *points))


class BasicHashTestClass(unittest.TestCase):
def test_revocation_token(self):
token = "REVOCATION_TOKEN{ELECTION=XA78,VOTERS=[voter501,voter809]}"
self.assertEqual(revocation_token_fingerprint(secp256k1_q, token), "1f515cc47433d46a89be")


class UniformHashTestClass(unittest.TestCase):
"""
unittest.TestCase for the uniformHash(..) function. (Algorithm 4)
Expand All @@ -160,6 +166,7 @@ def test_from_doc(self):
self.assertEqual(uniform_hash(2126991829, "some data"), 414907466)
self.assertEqual(uniform_hash(2126991829, "some data", 98162874527223464716009286152), 1444258901)


class IndependentGeneratorsForECGroupsOfPrimeOrderTestClass(unittest.TestCase):
"""
unittest.TestCase for algorithm 7
Expand All @@ -171,21 +178,21 @@ def test_from_doc(self):
"""
curve = Curve()

for i in range(1,4):
for i in range(1, 4):
p = independent_generators_for_ec_groups_of_prime_order(curve.p, curve.a, curve.b, bytearray("seed".encode("utf-8")), i)
if i == 1:
self.assertEqual(p.x,int.from_bytes(bytearray.fromhex("879f580dfe31c74dc2b4289f1988e581c76e625761a863971c808e90ab6fd3c7"), byteorder="big", signed=False))
self.assertEqual(p.x, int.from_bytes(bytearray.fromhex("879f580dfe31c74dc2b4289f1988e581c76e625761a863971c808e90ab6fd3c7"), byteorder="big", signed=False))
self.assertEqual(p.y, int.from_bytes(
bytearray.fromhex("4c59d9061d35678d06c04fe9f61dd47d7ee9e35b9847e5f3f9ed532c509afc0f"), byteorder="big", signed=False))
if i == 2:
self.assertEqual(p.x,int.from_bytes(bytearray.fromhex("b6413eb866319a631509ad0e637ec260507383d7495ef66858f9a6a4bb8efac7"), byteorder="big", signed=False))
self.assertEqual(p.x, int.from_bytes(bytearray.fromhex("b6413eb866319a631509ad0e637ec260507383d7495ef66858f9a6a4bb8efac7"), byteorder="big", signed=False))
self.assertEqual(p.y, int.from_bytes(
bytearray.fromhex("adb88f8cdd62aad64e2518d383b4e6aa2013910964b6423c17c0100f96118ae1"), byteorder="big", signed=False))
if i == 3:
self.assertEqual(p.x,int.from_bytes(bytearray.fromhex("1845cc619ec1a70c743e6559938290b7dac3d63b3fd2cf8d6e0646d292a576e8"), byteorder="big", signed=False))
self.assertEqual(p.x, int.from_bytes(bytearray.fromhex("1845cc619ec1a70c743e6559938290b7dac3d63b3fd2cf8d6e0646d292a576e8"), byteorder="big", signed=False))
self.assertEqual(p.y, int.from_bytes(
bytearray.fromhex("439f7d97af4396e0441b7d292045cf78bc22187eec981e5d6fe2ebd0794f975a"), byteorder="big", signed=False))


if __name__ == '__main__':
unittest.main()
unittest.main()
29 changes: 16 additions & 13 deletions src/test/test_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest
from algos.secp256k1 import Point, Curve


class secp256k1CompressTestClass(unittest.TestCase):
"""
unittest.TestCase to test if the compression of points on the elliptic curve work
Expand All @@ -15,9 +16,10 @@ def test_good_cases(self):
"""
curve = Curve()
compressed_key = bytearray.fromhex("0275788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b")
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"),byteorder="big",signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"),byteorder="big",signed=False)
self.assertEqual(curve.compress(Point(x,y)), compressed_key)
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"), byteorder="big", signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"), byteorder="big", signed=False)
self.assertEqual(curve.compress(Point(x, y)), compressed_key)


class secp256k1DecompressTestClass(unittest.TestCase):
"""
Expand All @@ -30,11 +32,12 @@ def test_good_cases(self):
"""
curve = Curve()
compressed_key = bytearray.fromhex("0275788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b")
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"),byteorder="big",signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"),byteorder="big",signed=False)
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"), byteorder="big", signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"), byteorder="big", signed=False)
self.assertEqual(curve.decompress(compressed_key).x, x)
self.assertEqual(curve.decompress(compressed_key).y, y)


class PointTestClass(unittest.TestCase):
"""
unittest.Testcase to test if the constructor of Point is working with ints and bytearrays
Expand All @@ -45,13 +48,13 @@ def test_equality(self):
:return:
"""
xH = bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b")
x = int.from_bytes(xH,byteorder="big",signed=False)
x = int.from_bytes(xH, byteorder="big", signed=False)
yH = bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88")
y = int.from_bytes(yH,byteorder="big",signed=False)
y = int.from_bytes(yH, byteorder="big", signed=False)

p1 = Point(x,y)
p1 = Point(x, y)
p2 = Point(xH, yH)
self.assertEqual(p1,p2)
self.assertEqual(p1, p2)
self.assertTrue(p1.valid())
self.assertTrue(p2.valid())

Expand All @@ -60,7 +63,7 @@ def test_none(self):
:return:
"""
p1 = Point(None,None)
p1 = Point(None, None)
self.assertFalse(p1.valid())

def test_compression(self):
Expand All @@ -71,8 +74,7 @@ def test_compression(self):
compressed_key = bytearray.fromhex("0275788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b")
x = int.from_bytes(bytearray.fromhex("75788b8a22a04baad44c66ec80e86928597979bf1b287760ad4e3153293d613b"), byteorder="big", signed=False)
y = int.from_bytes(bytearray.fromhex("664663757d16eff0b993ac12a1ba16ee4784ac08206b12be50f4d954d9d74c88"), byteorder="big", signed=False)
self.assertEqual(Point(x,y).compress_as_bytearray(), compressed_key)

self.assertEqual(Point(x, y).compress_as_bytearray(), compressed_key)

def test_valid(self):
"""
Expand All @@ -89,5 +91,6 @@ def test_valid(self):
self.assertTrue(p1.valid())
self.assertTrue(p2.valid())


if __name__ == '__main__':
unittest.main()
unittest.main()
15 changes: 10 additions & 5 deletions src/test/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from algos.secp256k1 import Point
import math


class EllipticCurveEncodingTestClass(unittest.TestCase):
"""
unittest.TestCase for elliptic curve encoding
Expand All @@ -17,11 +18,12 @@ def test_from_doc(self):
:return:
"""
(x,y) = elliptic_curve_encoding(723700557733226221397318656304299424082937404160253525246609900049430216698)
(x, y) = elliptic_curve_encoding(723700557733226221397318656304299424082937404160253525246609900049430216698)

self.assertEqual(x, int.from_bytes(bytearray.fromhex("7fffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffffe21"), byteorder='big', signed=False))
self.assertEqual(y, int.from_bytes(bytearray.fromhex("2af4d53f09f4d4ede3caf3f0e06ccfc0f55289d83fed859ca504d6033bec629b"), byteorder='big', signed=False))


class EncodingMessageAsMultiPlaintextTestClass(unittest.TestCase):
"""
unittest.TestCase for algorithm 5
Expand All @@ -40,18 +42,20 @@ def test_from_doc(self):
self.assertEqual(r[8], 7758446)
self.assertEqual(r[9], 7143424)


class DecodingMessageFromMultiPlaintextTestClass(unittest.TestCase):
"""
unittest.TestCase for algorithm 6
"""
def test_from_doc(self):
multiplaintext = [625,7824754,7633269,6909808,7105386,6842214,6583137,8026211,7758446,7143424]
multiplaintext = [625, 7824754, 7633269, 6909808, 7105386, 6842214, 6583137, 8026211, 7758446, 7143424]
msg = decoding_message_from_multiplaintext(int(math.pow(2, 32)) - 1, multiplaintext)
self.assertEqual(msg, bytearray("qwertyuioplkjhgfdsazxcvbnm".encode("utf-8")))
multiplaintextDash = [625,7824754,7633269,6909808,7105386,6842214,6583137,8026211,7758446,7143425]
multiplaintextDash = [625, 7824754, 7633269, 6909808, 7105386, 6842214, 6583137, 8026211, 7758446, 7143425]
with self.assertRaises(AssertionError):
msg = decoding_message_from_multiplaintext(int(math.pow(2, 32)) - 1, multiplaintextDash)


class DecodingEncodingMessageTestClass(unittest.TestCase):
"""
unittest.TestCase
Expand All @@ -61,6 +65,7 @@ def test_encoding_decoding_combination(self):
msg = decoding_message_from_multiplaintext(int(math.pow(2, 32)) - 1, r)
self.assertEqual(msg, bytearray("Das ist ein Test".encode("utf-8")))


class EllipticCurveDecodingTestClass(unittest.TestCase):
"""
unittest.TestCase for elliptic curve decoding
Expand All @@ -74,7 +79,7 @@ def test_from_doc(self):
x = bytearray.fromhex("7fffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffffe21")
y = bytearray.fromhex("2af4d53f09f4d4ede3caf3f0e06ccfc0f55289d83fed859ca504d6033bec629b")

pt = Point(x,y)
pt = Point(x, y)

a = elliptic_curve_decoding(pt)
self.assertEqual(a,723700557733226221397318656304299424082937404160253525246609900049430216698)
self.assertEqual(a, 723700557733226221397318656304299424082937404160253525246609900049430216698)
Loading

0 comments on commit cd9e639

Please sign in to comment.