Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jubjub curve #2

Merged
merged 8 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ repos:
- id: name-tests-test
- id: debug-statements
- id: requirements-txt-fixer
- id: flake8
args: [., --count, --select=E901,E999,F821,F822,F823, --show-source, --statistics]
# - id: flake8
- repo: https://github.com/stefandeml/pre_commit_hooks
rev: 507a6c4e4bdb5ffc7d35c1227c177e7a9bb86965
hooks:
- id: detect_tab
- id: unittest
- repo: https://github.com/ambv/black
rev: 19.3b0
hooks:
- id: black
language_version: python3.6
34 changes: 4 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ Let's create a simple demo, called `demo.py`:
```python
import hashlib

from zokrates_pycrypto.curves import BabyJubJub
from zokrates_pycrypto.eddsa import PrivateKey, PublicKey
from zokrates_pycrypto.field import FQ
from zokrates_pycrypto.utils import write_signature_for_zokrates_cli

if __name__ == "__main__":
Expand All @@ -82,8 +82,8 @@ if __name__ == "__main__":

# sk = PrivateKey.from_rand()
# Seeded for debug purpose
key = FQ(1997011358982923168928344992199991480689546837621580239342656433234255379025)
sk = PrivateKey(key)
key = 1997011358982923168928344992199991480689546837621580239342656433234255379025
sk = PrivateKey(key, curve=BabyJubJub)
sig = sk.sign(msg)

pk = PublicKey.from_private(sk)
Expand Down Expand Up @@ -112,7 +112,7 @@ import "ecc/babyjubjubParams.code" as context
def main(private field[2] R, private field S, field[2] A, u32[8] M0, u32[8] M1) -> (bool):

BabyJubJubParams context = context()

bool isVerified = verifyEddsa(R, S, A, M0, M1, context)

return isVerified
Expand All @@ -122,32 +122,6 @@ After compiling this file we can now pass our input arguments into witness gener

`cat zokrates_inputs.txt | ./zokrates compute-witness`

## CLI Usage

`pycrypto` also provides a simple command-line interface to make it easy to integrate the used crypto primitives into your existing application code.

Some examples:

### Compute SNARK-friendly Pedersen hash
```bash
python cli.py hash 3755668da8deabd8cafbe1c26cda5a837ed5f832665c5ef94725f6884054d9083755668da8deabd8cafbe1c26cda5a837ed5f832665c5ef94725f6884054d908
```
where the first argument denotes the preimage as a hexstring.

### Create and verify an EdDSA signature
```bash
python cli.py keygen
# => 37e334c51386a5c92152f592ef264b82ad52cf2bbfb6cee1c363e67be97732a ab466cd8924518f07172c0f8c695c60f77c11357b461d787ef31864a163f3995
# Private and public key

python cli.py sig-gen 37e334c51386a5c92152f592ef264b82ad52cf2bbfb6cee1c363e67be97732a 11dd22
# => 172a1794976d7d0272148c4be3b7ad74fd3a82376cd5995fc4d274e3593c0e6c 24e96be628208a9800336d23bd31318d8a9b95bc9bd8f6f01cae207c05062523
# R and S element of EdDSA signature

python cli.py sig-verify ab466cd8924518f07172c0f8c695c60f77c11357b461d787ef31864a163f3995 11dd22 172a1794976d7d0272148c4be3b7ad74fd3a82376cd5995fc4d274e3593c0e6c 24e96be628208a9800336d23bd31318d8a9b95bc9bd8f6f01cae207c05062523
# => True
```

## Contributing

We happily welcome contributions. You can either pick an existing issue, or reach out on [Gitter](https://gitter.im/ZoKrates/Lobby).
Expand Down
167 changes: 0 additions & 167 deletions cli.py

This file was deleted.

2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pyflakes==2.1.1
pylint==2.3.1
six==1.12.0
toml==0.10.0
typed-ast==1.3.2
# typed-ast==1.3.2
wrapt==1.11.1
27 changes: 13 additions & 14 deletions tests/babyjubjub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

from os import urandom

from zokrates_pycrypto.field import FQ
from zokrates_pycrypto.babyjubjub import Point
from zokrates_pycrypto.babyjubjub import JUBJUB_E, JUBJUB_C, JUBJUB_L
from zokrates_pycrypto.fields import BN128Field as FQ
from zokrates_pycrypto.curves import BabyJubJub


class TestJubjub(unittest.TestCase):
def _point_g(self):
return Point.generator()
return BabyJubJub.generator()

def _point_g_dbl(self):
x = 17324563846726889236817837922625232543153115346355010501047597319863650987830
y = 20022170825455209233733649024450576091402881793145646502279487074566492066831
return Point(FQ(x), FQ(y))
return BabyJubJub(FQ(x), FQ(y))

# Hardcoded for now till we have automatic test generation for ZoKrates test framework
def _fe_rnd(self):
Expand All @@ -27,25 +26,25 @@ def test_double_via_add(self):

def test_cyclic(self):
G = self._point_g()
self.assertEqual(G.mult(JUBJUB_E + 1), G)
self.assertEqual(G.mult(BabyJubJub.JUBJUB_E + 1), G)

def test_mult_2(self):
G = self._point_g()
G_mult2 = G.mult(2)
self.assertEqual(G_mult2, self._point_g_dbl())

def test_lower_order_p(self):
lp = Point(
lp = BabyJubJub(
FQ(
4342719913949491028786768530115087822524712248835451589697801404893164183326
),
FQ(
4826523245007015323400664741523384119579596407052839571721035538011798951543
),
)
lp_c = lp.mult(JUBJUB_C)
self.assertEqual(lp_c, Point.infinity())
lp_l = lp.mult(JUBJUB_L)
lp_c = lp.mult(BabyJubJub.JUBJUB_C)
self.assertEqual(lp_c, BabyJubJub.infinity())
lp_l = lp.mult(BabyJubJub.JUBJUB_L)
self.assertEqual(lp_l, lp)

def test_multiplicative(self):
Expand All @@ -54,8 +53,8 @@ def test_multiplicative(self):
A = G.mult(a)
B = G.mult(b)

ab = (a.n * b.n) % JUBJUB_E # 7006652
AB = G.mult(ab)
ab = a.n * b.n % BabyJubJub.JUBJUB_E # 7006652
AB = G.mult(FQ(ab))
self.assertEqual(A.mult(b), AB)
self.assertEqual(B.mult(a), AB)

Expand All @@ -74,8 +73,8 @@ def test_associativity(self):

def test_identities(self):
G = self._point_g()
self.assertEqual(G + Point.infinity(), G)
self.assertEqual(G + G.neg(), Point.infinity())
self.assertEqual(G + BabyJubJub.infinity(), G)
self.assertEqual(G + G.neg(), BabyJubJub.infinity())


if __name__ == "__main__":
Expand Down
44 changes: 37 additions & 7 deletions tests/eddsa_test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
import unittest
from os import urandom

from zokrates_pycrypto.field import FQ
from zokrates_pycrypto.babyjubjub import Point
from zokrates_pycrypto.curves import BabyJubJub, JubJub
from zokrates_pycrypto.eddsa import PublicKey, PrivateKey


class TestEdDSA(unittest.TestCase):
def test_signverify(self):
def test_signverify_babyjubjub(self):
# Hardcoded for now till we have automatic test generation for ZoKrates test framework
key = FQ(
1997011358982923168928344992199991480689546837621580239342656433234255379025
)
key = 1997011358982923168928344992199991480689546837621580239342656433234255379025

sk = PrivateKey(key)
sk = PrivateKey(key, curve=BabyJubJub)
msg = urandom(32)
sig = sk.sign(msg)

pk = PublicKey.from_private(sk)
self.assertTrue(pk.verify(sig, msg))

def test_signverify_jubjub(self):
# Hardcoded for now till we have automatic test generation for ZoKrates test framework
key = 1997011358982923168928344992199991480689546837621580239342656433234255379025

sk = PrivateKey(key, curve=JubJub)
msg = urandom(32)
sig = sk.sign(msg)

pk = PublicKey.from_private(sk)
self.assertTrue(pk.verify(sig, msg))

def test_random_signverify_babyjubjub(self):
# Hardcoded for now till we have automatic test generation for ZoKrates test framework
key = 1997011358982923168928344992199991480689546837621580239342656433234255379025

sk = PrivateKey.from_rand(curve=BabyJubJub)
msg = urandom(32)
sig = sk.sign(msg)

pk = PublicKey.from_private(sk)
self.assertTrue(pk.verify(sig, msg))

def test_random_signverify_jubjub(self):
# Hardcoded for now till we have automatic test generation for ZoKrates test framework
key = 1997011358982923168928344992199991480689546837621580239342656433234255379025

sk = PrivateKey.from_rand(curve=JubJub)
msg = urandom(32)
sig = sk.sign(msg)

Expand Down
Loading