Skip to content

Commit

Permalink
fix(kzg10): add scalar_mul() to Commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
gy001 committed Dec 13, 2024
1 parent 092215b commit 8232016
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 104 deletions.
55 changes: 1 addition & 54 deletions src/kzg10_hiding_m.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
51 changes: 1 addition & 50 deletions src/kzg10_hiding_z.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
6 changes: 6 additions & 0 deletions src/kzg10_non_hiding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 8232016

Please sign in to comment.