Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 76951bb

Browse files
authored
Merge pull request #60 from HarryR/jubjub-eddsa-tests
Jubjub EdDSA + Tests + solidity cleanups
2 parents 8107a01 + 1ab01d6 commit 76951bb

23 files changed

+808
-263
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ __pycache__
1212
.coverage.*
1313
/node_modules
1414
package-lock.json
15+
lextab.py
16+
yacctab.py

.solhint.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "default",
3+
"rules": {
4+
"indent": false,
5+
"var-name-mixedcase": false,
6+
"func-name-mixedcase": false,
7+
"func-param-name-mixedcase": false,
8+
"not-rely-on-time": false,
9+
"bracket-align": false,
10+
"expression-indent": false,
11+
"max-line-length": false,
12+
"two-lines-top-level-separator": false,
13+
"separate-by-one-line-in-contract": false
14+
}
15+
}

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ coverage-html:
105105
#######################################################################
106106

107107

108-
lint: python-pyflakes python-pylint cxx-lint
108+
lint: python-pyflakes python-pylint cxx-lint solidity-lint
109109

110110
python-pyflakes:
111111
$(PYTHON) -mpyflakes $(NAME)
@@ -141,6 +141,13 @@ mac-dependencies:
141141
#######################################################################
142142

143143

144+
solidity-lint:
145+
$(NPM) run lint
146+
147+
148+
#######################################################################
149+
150+
144151
nvm-install:
145152
./utils/nvm-install
146153
nvm install --lts

appendix/ejubjub.sage

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
p = 21888242871839275222246405745257275088548364400416034343698204186575808495617
2-
Fp = GF(p)
3-
R.<a,d,x,y>=QQ[]
4-
A=2*(a+d)/(a-d)
5-
B=4/(a-d)
6-
S=R.quotient(a*x^2+y^2-(1+d*x^2*y^2))
7-
u=(1+y)/(1-y)
8-
v=(1+y)/((1-y)*x)
9-
0 == S((B*v^2-u^3-A*u^2-u).numerator())
10-
11-
121
JUBJUB_C = 8 # Cofactor
132
JUBJUB_A = 168700 # Coefficient A
143
JUBJUB_D = 168696 # Coefficient D
154
MONT_A = 168698
16-
175
p = 21888242871839275222246405745257275088548364400416034343698204186575808495617
186
Fp = GF(p)
197
E = EllipticCurve(Fp, [0, MONT_A, 0, 1, 0])
@@ -23,6 +11,18 @@ assert E.order() == 218882428718392752222464057452572750886145117772685380736017
2311
assert E.quadratic_twist().order() == 21888242871839275222246405745257275088482217023563530613794683085564038006908
2412
# factor(E.quadratic_twist().order()) == 2^2 * 5472060717959818805561601436314318772120554255890882653448670771391009501727
2513

14+
assert E.trace_of_frobenius() not in [0, 1]
15+
16+
twistCofactor = 4
17+
curveCofactor = 8
18+
19+
curveOrder = E.order()
20+
21+
twistOrder = 2 * (p+1) - curveOrder
22+
assert E.quadratic_twist().order() == twistOrder
23+
assert is_prime(twistOrder // twistCofactor)
24+
assert is_prime(E.order() // curveCofactor)
25+
2626
jubjub_valid = lambda x, y: (JUBJUB_A * x^2 + y^2) == 1 + JUBJUB_D * x^2 * y^2
2727
mont_valid = lambda x, y: E.is_on_curve(x, y)
2828
mont_to_jubjub = lambda x, y: (x/y, (x-1)/(x+1))

contracts/EdDSA.sol

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2018 @HarryR
2+
// License: LGPL-3.0+
3+
4+
pragma solidity 0.4.24;
5+
6+
import "./JubJub.sol";
7+
8+
9+
contract EdDSA
10+
{
11+
function HashToInt( bytes data )
12+
public pure returns (uint256)
13+
{
14+
uint256 hashed = uint256(sha256(data));
15+
16+
// (2<<249) - 1
17+
uint256 mask = 1809251394333065553493296640760748560207343510400633813116524750123642650623;
18+
19+
return hashed & mask;
20+
}
21+
22+
function Verify( uint256[2] pubkey, uint256 hashed_msg, uint256[2] R, uint256 s )
23+
public view returns (bool)
24+
{
25+
uint256[2] memory B = JubJub.Generator();
26+
uint256[2] memory lhs;
27+
uint256[2] memory rhs;
28+
29+
(lhs[0], lhs[1]) = JubJub.scalarMult(B[0], B[1], s);
30+
31+
uint256 t = HashToInt(abi.encodePacked(
32+
R[0], R[1],
33+
pubkey[0], pubkey[1],
34+
hashed_msg
35+
));
36+
37+
(rhs[0], rhs[1]) = JubJub.scalarMult(pubkey[0], pubkey[1], t);
38+
39+
return lhs[0] == rhs[0] && lhs[1] == rhs[1];
40+
}
41+
}

0 commit comments

Comments
 (0)