-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Published jaxite_ec, a JAX based library to enable TPU to accelerate …
…Multi-Scalar Multiplication (MSM) for enabling faster Zero Knowledge Proof. PiperOrigin-RevId: 724030943
- Loading branch information
1 parent
d537bb2
commit d670ea8
Showing
36 changed files
with
6,250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
"""Big integer classes for jaxite_ec.""" | ||
|
||
import gmpy2 | ||
|
||
|
||
class GMPBigInteger: | ||
"""A class representing a big integer using gmpy2. | ||
This class provides basic arithmetic operations for big integers using gmpy2. | ||
""" | ||
|
||
def __init__(self, value) -> None: | ||
if isinstance(value, (int, gmpy2.mpz)): | ||
self.value = gmpy2.mpz(value) | ||
elif isinstance(value, GMPBigInteger): | ||
self.value = value.value | ||
else: | ||
raise TypeError("Unsupported type for GMPBigInteger initialization") | ||
|
||
def __add__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return GMPBigInteger( | ||
self.value | ||
+ gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
) | ||
return NotImplemented | ||
|
||
def __sub__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return GMPBigInteger( | ||
self.value | ||
- gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
) | ||
return NotImplemented | ||
|
||
def __mul__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return GMPBigInteger( | ||
self.value | ||
* gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
) | ||
return NotImplemented | ||
|
||
def __truediv__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
if ( | ||
gmpy2.mpz(other.value if isinstance(other, GMPBigInteger) else other) | ||
== 0 | ||
): | ||
raise ZeroDivisionError("division by zero") | ||
return GMPBigInteger( | ||
self.value | ||
// gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
) | ||
return NotImplemented | ||
|
||
def __mod__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return GMPBigInteger( | ||
self.value | ||
% gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
) | ||
return NotImplemented | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return self.value == gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
return NotImplemented | ||
|
||
def __ne__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return self.value != gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
return NotImplemented | ||
|
||
def __lt__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return self.value < gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
return NotImplemented | ||
|
||
def __le__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return self.value <= gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
return NotImplemented | ||
|
||
def __gt__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return self.value > gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
return NotImplemented | ||
|
||
def __ge__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return self.value >= gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
return NotImplemented | ||
|
||
def __pow__(self, exponent, modulus=None): | ||
if isinstance(exponent, (GMPBigInteger, int, gmpy2.mpz)): | ||
if isinstance(exponent, GMPBigInteger): | ||
exponent = gmpy2.mpz(exponent.value) | ||
if isinstance(modulus, GMPBigInteger): | ||
modulus = gmpy2.mpz(modulus.value) | ||
if modulus is None: | ||
return GMPBigInteger(self.value**exponent) | ||
else: | ||
return GMPBigInteger(gmpy2.powmod(self.value, exponent, modulus)) | ||
else: | ||
print(type(exponent)) | ||
raise TypeError("Exponent must be an integer") | ||
|
||
def __lshift__(self, shift): | ||
"""Left shift operator (<<).""" | ||
if isinstance(shift, GMPBigInteger): | ||
shift = shift.value | ||
return GMPBigInteger(self.value << shift) | ||
|
||
def __rshift__(self, shift): | ||
"""Right shift operator (>>).""" | ||
if isinstance(shift, GMPBigInteger): | ||
shift = shift.value | ||
return GMPBigInteger(self.value >> shift) | ||
|
||
def __and__(self, other): | ||
if isinstance(other, (GMPBigInteger, int)): | ||
return GMPBigInteger( | ||
self.value | ||
& gmpy2.mpz( | ||
other.value if isinstance(other, GMPBigInteger) else other | ||
) | ||
) | ||
return NotImplemented | ||
|
||
def ceil_log2(self): | ||
"""Calculate the base-2 logarithm of the GMPBigInteger.""" | ||
if self.value <= 0: | ||
raise ValueError("log2 is only defined for positive integers") | ||
return GMPBigInteger(gmpy2.ceil(gmpy2.log2(self.value))) | ||
|
||
def __int__(self): | ||
return int(self.value) | ||
|
||
def __str__(self): | ||
return str(self.value) | ||
|
||
def __repr__(self): | ||
return f"GMPBigInteger({self.value})" | ||
|
||
def hex_value_str(self) -> str: | ||
return hex(self.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Jaxite EC algorithm configuration file.""" | ||
|
||
config_BLS12_377 = { | ||
# A small prime field for simplicity | ||
'prime': 0x01AE3A4617C510EAC63B05C06CA1493B1A22D9F300F5138F1EF3622FBA094800170B5D44300000008508C00000000001, | ||
'order': 0x12AB655E9A2CA55660B44D1E5C37B00159AA76FED00000010A11800000000001, | ||
'a': 0, # Coefficient a = 0 | ||
'b': 1, # Coefficient b = 1 | ||
'generator': [ | ||
0x008848DEFE740A67C8FC6225BF87FF5485951E2CAA9D41BB188282C8BD37CB5CD5481512FFCD394EEAB9B16EB21BE9EF, | ||
0x01914A69C5102EFF1F674F5D30AFEEC4BD7FB348CA3E52D96D182AD44FB82305C2FE3D3634A9591AFD82DE55559C8EA6, | ||
], | ||
} |
Oops, something went wrong.