Skip to content

Commit

Permalink
test(kzg10, field): Add comprehensive unit tests for KZG10Commitment
Browse files Browse the repository at this point in the history
Implement tests for setup, commit, prove_eval, verify_eval,
prove_degree_bound, verify_degree_bound, prove_eval_and_degree,
and verify_eval_and_degree methods. Include edge cases and error handling.
  • Loading branch information
ahy231 committed Sep 30, 2024
1 parent f495bd0 commit 262daec
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,5 @@ def magic_field(value):
magic_field.zero = lambda : Fp.zero()
magic_field.one = lambda : Fp.one()
magic_field.primitive_element = lambda : Fp.primitive_element()
magic_field.random_element = lambda : Fp.random_element()
return magic_field
4 changes: 3 additions & 1 deletion src/kzg10.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ def commit(self, polynomial) -> Commitment:

if isinstance(polynomial, list):
polynomial = UniPolynomial(polynomial)
elif not isinstance(polynomial, type(Commitment.scalar)):
elif isinstance(polynomial, UniPolynomial):
pass
else:
raise TypeError("Invalid polynomial type")

if polynomial.degree > self.max_degree:
Expand Down
104 changes: 104 additions & 0 deletions tests/test_kzg10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import unittest

import sys

sys.path.append('../src')
sys.path.append('src')

from kzg10 import KZG10Commitment, Commitment
from group import DummyGroup
from unipolynomial import UniPolynomial
from sage.all import *
from field import magic

Fp = magic(GF(193))

class TestKZG10Commitment(unittest.TestCase):
def setUp(self):
self.G1 = DummyGroup(Fp)
self.G2 = DummyGroup(Fp)
self.max_degree = 10
self.kzg = KZG10Commitment(self.G1, self.G2, self.max_degree)

def test_setup(self):
self.assertEqual(len(self.kzg.srs), self.max_degree + 1)
self.assertEqual(len(self.kzg.srs2), self.max_degree + 1)
self.assertIsNotNone(self.kzg.h)
self.assertIsNotNone(self.kzg.h_s)

def test_commit(self):
poly = UniPolynomial([1, 2, 3]) # 1 + 2x + 3x^2
commitment = self.kzg.commit(poly)
self.assertIsInstance(commitment, Commitment)
self.assertEqual(commitment.group, self.G1)

def test_commit_invalid_type(self):
with self.assertRaises(TypeError):
self.kzg.commit("invalid")

def test_commit_degree_too_high(self):
poly = UniPolynomial([1] * (self.max_degree + 2))
with self.assertRaises(ValueError):
self.kzg.commit(poly)

def test_division_by_linear_divisor(self):
coeffs = [1, 2, 1] # x^2 + 2x + 1
d = 1
quotient, remainder = KZG10Commitment.division_by_linear_divisor(coeffs, d)
self.assertEqual(quotient, [3, 1]) # x + 3
self.assertEqual(remainder, 4) # f(1) = 1^2 + 2*1 + 1 = 4

def test_prove_eval(self):
poly = [1, 2, 3] # 1 + 2x + 3x^2
point = 2
value = 17 # f(2) = 1 + 2*2 + 3*2^2 = 17
proof = self.kzg.prove_eval(poly, point, value)
self.assertIsInstance(proof, Commitment)

def test_verify_eval(self):
poly = [1, 2, 3] # 1 + 2x + 3x^2
point = 2
value = 17 # f(2) = 1 + 2*2 + 3*2^2 = 17
commitment = self.kzg.commit(poly)
proof = self.kzg.prove_eval(poly, point, value)
result = self.kzg.verify_eval(commitment, proof, point, value)
self.assertTrue(result)

def test_prove_degree_bound(self):
poly = [1, 2, 3, 0, 0] # 1 + 2x + 3x^2
commitment = self.kzg.commit(poly)
degree_bound = 3
proof = self.kzg.prove_degree_bound(commitment, poly, degree_bound)
self.assertIsInstance(proof[0], Commitment)
self.assertIsInstance(proof[1], int)

def test_verify_degree_bound(self):
poly = [1, 2, 3, 0, 0] # 1 + 2x + 3x^2
commitment = self.kzg.commit(poly)
degree_bound = 3
proof = self.kzg.prove_degree_bound(commitment, poly, degree_bound)
result = self.kzg.verify_degree_bound(commitment, proof, degree_bound)
self.assertTrue(result)

def test_prove_eval_and_degree(self):
poly = [1, 2, 3, 0, 0] # 1 + 2x + 3x^2
commitment = self.kzg.commit(poly)
point = 2
value = 17 # f(2) = 1 + 2*2 + 3*2^2 = 17
degree_bound = 3
proof = self.kzg.prove_eval_and_degree(commitment, poly, point, value, degree_bound)
self.assertIsInstance(proof[0], Commitment)
self.assertIsInstance(proof[1], int)

def test_verify_eval_and_degree(self):
poly = [1, 2, 3, 0, 0] # 1 + 2x + 3x^2
commitment = self.kzg.commit(poly)
point = 2
value = 17 # f(2) = 1 + 2*2 + 3*2^2 = 17
degree_bound = 3
proof = self.kzg.prove_eval_and_degree(commitment, poly, point, value, degree_bound)
result = self.kzg.verify_eval_and_degree(commitment, proof, point, value, degree_bound)
self.assertTrue(result)

if __name__ == '__main__':
unittest.main()

0 comments on commit 262daec

Please sign in to comment.