From 8232016549e38f45d17d2e7a90e9d5d17540ed60 Mon Sep 17 00:00:00 2001 From: Yu Guo Date: Fri, 13 Dec 2024 18:03:31 +0800 Subject: [PATCH] fix(kzg10): add scalar_mul() to Commitment --- src/kzg10_hiding_m.py | 55 +---------------------------------------- src/kzg10_hiding_z.py | 51 +------------------------------------- src/kzg10_non_hiding.py | 6 +++++ 3 files changed, 8 insertions(+), 104 deletions(-) diff --git a/src/kzg10_hiding_m.py b/src/kzg10_hiding_m.py index f6c362d..ecef3f4 100644 --- a/src/kzg10_hiding_m.py +++ b/src/kzg10_hiding_m.py @@ -23,60 +23,7 @@ from random import randint from utils import next_power_of_two, is_power_of_two, bits_le_with_width, bit_reverse, log_2 from merlin.merlin_transcript import MerlinTranscript - -class Commitment: - """Represents a commitment in the KZG scheme.""" - def __init__(self, cm, oob=None): - self.cm = cm - self.group = type(cm) - self.scalar = Field - if oob is not None: - self.value = oob - - # @classmethod - # def set_scalar(cls, scalar): - # cls.scalar = scalar - - @staticmethod - def zero(self): - """Create a zero commitment.""" - return Commitment(self.group.zero()) - - def __add__(self, other): - """Add two commitments using + operator.""" - if not isinstance(other, Commitment) or self.group != other.group: - raise TypeError("Can only add commitments from the same group") - return Commitment(self.cm + other.cm) - - def __sub__(self, other): - """Subtract two commitments using - operator.""" - if not isinstance(other, Commitment) or self.group != other.group: - raise TypeError("Can only subtract commitments from the same group") - return Commitment(self.cm - other.cm) - - def __mul__(self, other): - """ - Multiply a commitment using * operator. - - Args: - other (UniPolynomial or scalar): The polynomial or scalar to multiply with this one. - - Returns: - Commitment: - """ - if not isinstance(other, (int, type(self.scalar))): # Scalar multiplication - raise TypeError("Unsupported operand type for *: '{}' and '{}'".format(type(self).__name__, type(other).__name__)) - return Commitment(self.cm.ec_mul(other)) - - def __rmul__(self, other): - """Multiply a commitment using * operator.""" - - if not isinstance(other, (int, type(self.scalar))): - raise TypeError("Unsupported operand type for *: '{}' and '{}'".format(type(self).__name__, type(other).__name__)) - return Commitment(self.cm.ec_mul(other)) - - def __repr__(self): - return f"Commitment({self.cm})" +from kzg10_non_hiding import Commitment class KZG10_PCS: """KZG10 commitment scheme implementation.""" diff --git a/src/kzg10_hiding_z.py b/src/kzg10_hiding_z.py index 3caf048..76ce402 100644 --- a/src/kzg10_hiding_z.py +++ b/src/kzg10_hiding_z.py @@ -13,56 +13,7 @@ from unipoly import UniPolynomial from random import randint from utils import is_power_of_two, bits_le_with_width, bit_reverse, log_2 - -class Commitment: - """Represents a commitment in the KZG scheme.""" - def __init__(self, cm, oob=None): - self.cm = cm - self.group = type(cm) - self.scalar = Field - if oob is not None: - self.value = oob - - @staticmethod - def zero(self): - """Create a zero commitment.""" - return Commitment(self.group.zero()) - - def __add__(self, other): - """Add two commitments using + operator.""" - if not isinstance(other, Commitment) or self.group != other.group: - raise TypeError("Can only add commitments from the same group") - return Commitment(self.cm + other.cm) - - def __sub__(self, other): - """Subtract two commitments using - operator.""" - if not isinstance(other, Commitment) or self.group != other.group: - raise TypeError("Can only subtract commitments from the same group") - return Commitment(self.cm - other.cm) - - def __mul__(self, other): - """ - Multiply a commitment using * operator. - - Args: - other (UniPolynomial or scalar): The polynomial or scalar to multiply with this one. - - Returns: - Commitment: - """ - if not isinstance(other, (int, type(self.scalar))): # Scalar multiplication - raise TypeError("Unsupported operand type for *: '{}' and '{}'".format(type(self).__name__, type(other).__name__)) - return Commitment(self.cm.ec_mul(other)) - - def __rmul__(self, other): - """Multiply a commitment using * operator.""" - - if not isinstance(other, (int, type(self.scalar))): - raise TypeError("Unsupported operand type for *: '{}' and '{}'".format(type(self).__name__, type(other).__name__)) - return Commitment(self.cm.ec_mul(other)) - - def __repr__(self): - return f"Commitment({self.cm})" +from kzg10_non_hiding import Commitment class KZG10_PCS: """KZG10 commitment scheme implementation.""" diff --git a/src/kzg10_non_hiding.py b/src/kzg10_non_hiding.py index 4bcb169..f0a2578 100644 --- a/src/kzg10_non_hiding.py +++ b/src/kzg10_non_hiding.py @@ -71,6 +71,12 @@ def __rmul__(self, other): def __repr__(self): return f"Commitment({self.cm})" + + def scalar_mul(self, scalar: Field): + """Multiply a commitment using * operator.""" + # if not isinstance(scalar, (int, type(self.scalar))): + # raise TypeError("Unsupported operand type for *: '{}' and '{}'".format(type(self).__name__, type(scalar).__name__)) + return Commitment(self.cm.ec_mul(scalar)) class KZG10_PCS: """KZG10 commitment scheme implementation."""