-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from jtraglia/allow-big-ints
Change Scalar::new input from u128 to BigUint
- Loading branch information
Showing
4 changed files
with
57 additions
and
8 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
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 |
---|---|---|
@@ -1,24 +1,44 @@ | ||
from py_arkworks_bls12381 import Scalar | ||
|
||
BLS_MODULUS = 52435875175126190479447740508185965837690552500527637822603658699938581184513 | ||
|
||
# Initialisation - The default initialiser for a scalar is an u128 integer | ||
scalar = Scalar(12345) | ||
|
||
# It should be possible to instantiate BLS_MODULUS - 1 | ||
max_value = Scalar(BLS_MODULUS - 1) | ||
assert max_value + Scalar(2) == Scalar(1) | ||
|
||
# Equality -- We override eq and neq operators | ||
assert scalar == scalar | ||
assert Scalar(1234) != Scalar(4567) | ||
|
||
# Scalar Addition/subtraction/Negation -- We override the add/sub/neg operators | ||
# Scalar arithmetic -- We override the mul/div/add/sub/neg operators | ||
a = Scalar(3) | ||
b = Scalar(4) | ||
c = Scalar(5) | ||
assert a.square() + b.square() == c.square() | ||
assert a * a + b * b == c * c | ||
|
||
assert Scalar(12) / Scalar(3) == Scalar(4) | ||
|
||
try: | ||
assert Scalar(12) / Scalar(0) | ||
assert False | ||
except ZeroDivisionError: | ||
pass | ||
|
||
exp = Scalar(0xffff_ffff_ffff_fff) | ||
assert int(Scalar(2).pow(exp)) == pow(2, int(exp), BLS_MODULUS) | ||
|
||
neg_a = -a | ||
assert a + neg_a == Scalar(0) | ||
assert (a + neg_a).is_zero() | ||
|
||
# Serialisation | ||
compressed_bytes = scalar.to_le_bytes() | ||
deserialised_scalar = Scalar.from_le_bytes(compressed_bytes) | ||
assert scalar == deserialised_scalar | ||
assert scalar == deserialised_scalar | ||
|
||
# Conversion to int | ||
assert int(Scalar(12345)) == 12345 |
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
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