Skip to content

Commit bcfc8ed

Browse files
author
Wenhua Zhang
committed
feat(generate_shield_address): add generateShieldAddress http api
1 parent 76e89c8 commit bcfc8ed

12 files changed

Lines changed: 2843 additions & 0 deletions
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package org.tron.common.crypto;
2+
3+
/*
4+
* Copyright 2011 Google Inc.
5+
* Copyright 2014 Andreas Schildbach
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import org.apache.commons.lang3.ArrayUtils;
21+
import org.tron.common.utils.ByteArray;
22+
23+
24+
/**
25+
* A Sha256Hash just wraps a byte[] so that equals and hashcode work correctly, allowing it to be
26+
* used as keys in a map. It also checks that the length is correct and provides a bit more type
27+
* safety.
28+
*/
29+
public class Sha256Update {
30+
31+
private static int ROTR32(int x, int s) {
32+
return (((x) >>> (s)) | ((x) << (32 - (s))));
33+
}
34+
35+
private static int R(int x, int s) {
36+
return ((x) >>> (s));
37+
}
38+
39+
private static int S(int x, int s) {
40+
return ROTR32(x, s);
41+
}
42+
43+
private static int SIG0(int x) {
44+
return S(x, 2) ^ S(x, 13) ^ S(x, 22);
45+
}
46+
47+
private static int SIG1(int x) {
48+
return S(x, 6) ^ S(x, 11) ^ S(x, 25);
49+
}
50+
51+
private static int sig0(int x) {
52+
return S(x, 7) ^ S(x, 18) ^ R(x, 3);
53+
}
54+
55+
private static int sig1(int x) {
56+
return S(x, 17) ^ S(x, 19) ^ R(x, 10);
57+
}
58+
59+
private static int CH(int x, int y, int z) {
60+
return ((x & (y ^ z)) ^ z);
61+
}
62+
63+
private static int MAJ(int x, int y, int z) {
64+
return (((x | y) & z) | (x & y));
65+
}
66+
67+
private static int byte2Int(byte[] a, int offset) {
68+
int b = 0;
69+
b += ((int) (a[offset++]) << 24)&0xFF000000;
70+
b += ((int) (a[offset++]) << 16)&0x00FF0000;
71+
b += ((int) (a[offset++]) << 8)&0x0000FF00;
72+
b += ((int) (a[offset++]))&0x000000FF;
73+
return b;
74+
}
75+
76+
private static int byte2Int_(byte[] a, int offset) {
77+
int b = 0;
78+
b += ((int) (a[offset++]))&0x000000FF;
79+
b += ((int) (a[offset++]) << 8)&0x0000FF00;
80+
b += ((int) (a[offset++]) << 16)&0x00FF0000;
81+
b += ((int) (a[offset++]) << 24)&0xFF000000;
82+
return b;
83+
}
84+
85+
private static void int2Byte(byte[] a, int offset, int b) {
86+
a[offset++] = (byte) ((b & 0xFF000000) >>> 24);
87+
a[offset++] = (byte) ((b & 0x00FF0000) >>> 16);
88+
a[offset++] = (byte) ((b & 0x0000FF00) >>> 8);
89+
a[offset++] = (byte) ((b & 0x000000FF));
90+
}
91+
92+
public static byte[] Sha256OneBlock(byte[] message) {
93+
if (ArrayUtils.isEmpty(message) || message.length != 64) {
94+
return null;
95+
}
96+
byte[] Iv = ByteArray
97+
.fromHexString("6A09E667BB67AE853C6EF372A54FF53A510E527F9B05688C1F83D9AB5BE0CD19");
98+
int[] k = new int[]
99+
{
100+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
101+
0xab1c5ed5,
102+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
103+
0xc19bf174,
104+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc,
105+
0x76f988da,
106+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351,
107+
0x14292967,
108+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e,
109+
0x92722c85,
110+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585,
111+
0x106aa070,
112+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
113+
0x682e6ff3,
114+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
115+
0xc67178f2
116+
};
117+
byte[][] gi = new byte[][]
118+
{
119+
new byte[]{0, 1, 2, 3, 4, 5, 6, 7},
120+
new byte[]{7, 0, 1, 2, 3, 4, 5, 6},
121+
new byte[]{6, 7, 0, 1, 2, 3, 4, 5},
122+
new byte[]{5, 6, 7, 0, 1, 2, 3, 4},
123+
new byte[]{4, 5, 6, 7, 0, 1, 2, 3},
124+
new byte[]{3, 4, 5, 6, 7, 0, 1, 2},
125+
new byte[]{2, 3, 4, 5, 6, 7, 0, 1},
126+
new byte[]{1, 2, 3, 4, 5, 6, 7, 0}
127+
};
128+
129+
int u1Index;
130+
int u1Idx;
131+
int[] u4Data = new int[64];
132+
int[] u4H = new int[8];
133+
int A;
134+
int B;
135+
int C;
136+
int D;
137+
int E;
138+
int F;
139+
int G;
140+
int H;
141+
int temp;
142+
143+
for (u1Index = 0; u1Index < 32; u1Index += 0x04) {
144+
u4H[u1Index / 4] = byte2Int(Iv, u1Index);
145+
u4Data[u1Index / 4] = byte2Int(message, u1Index);
146+
}
147+
for (; u1Index < 64; u1Index += 0x04) {
148+
u4Data[u1Index / 4] = byte2Int(message, u1Index);
149+
}
150+
u1Index = 48;
151+
int offset = 16;
152+
while (u1Index-- > 0) {
153+
temp =
154+
sig1(u4Data[offset - 2]) + u4Data[offset - 7] + sig0(u4Data[offset - 15]) + u4Data[offset
155+
- 16];
156+
u4Data[offset++] = temp;
157+
}
158+
159+
A = u4H[0];
160+
B = u4H[1];
161+
C = u4H[2];
162+
D = u4H[3];
163+
E = u4H[4];
164+
F = u4H[5];
165+
G = u4H[6];
166+
H = u4H[7];
167+
168+
for (u1Index = 0; u1Index < 64; u1Index++) {
169+
u1Idx = (u1Index & 0x07);
170+
temp =
171+
u4H[gi[u1Idx][7]] + SIG1(u4H[gi[u1Idx][4]]) + CH(u4H[gi[u1Idx][4]], u4H[gi[u1Idx][5]],
172+
u4H[gi[u1Idx][6]]) + k[u1Index] + u4Data[u1Index];
173+
u4H[gi[u1Idx][7]] = temp + SIG0(u4H[gi[u1Idx][0]]) + MAJ(u4H[gi[u1Idx][0]], u4H[gi[u1Idx][1]],
174+
u4H[gi[u1Idx][2]]);
175+
u4H[gi[u1Idx][3]] += temp;
176+
}
177+
178+
u4H[0] += A;
179+
u4H[1] += B;
180+
u4H[2] += C;
181+
u4H[3] += D;
182+
u4H[4] += E;
183+
u4H[5] += F;
184+
u4H[6] += G;
185+
u4H[7] += H;
186+
187+
for (u1Index = 0; u1Index < 32; u1Index += 0x04) {
188+
int2Byte(Iv, u1Index, u4H[u1Index / 4]);
189+
}
190+
191+
return Iv;
192+
}
193+
194+
public static void main(String[] args){
195+
byte[] msg = ByteArray.fromHexString("6D38B7C9D29C104292D92219BDB70139AA86585B70B728FBADB2F5DE9CB4C14DFC338BAEDE9DA5B2D2EE9DA485F3151A57A935A1EDA8239A4EF020DE8518BC5E");
196+
byte[] hash = Sha256OneBlock(msg);
197+
System.out.println(ByteArray.toHexString(hash));
198+
}
199+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.tron.common.crypto.dh25519;
2+
3+
public class ByteIntegerConverter {
4+
5+
static long load_3(byte[] data, int offset)
6+
{
7+
long result;
8+
result = (data[offset + 0]&0xFFL);
9+
result |= (data[offset + 1]&0xFFL) << 8;
10+
result |= (data[offset + 2]&0xFFL) << 16;
11+
return result;
12+
}
13+
14+
static long load_4(byte[] data, int offset)
15+
{
16+
long result;
17+
result = (data[offset + 0]&0xFFL);
18+
result |= (data[offset + 1]&0xFFL) << 8;
19+
result |= (data[offset + 2]&0xFFL) << 16;
20+
result |= (data[offset + 3]&0xFFL) << 24;
21+
return result;
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.tron.common.crypto.dh25519;
2+
3+
public class FieldElement {
4+
int x0;
5+
int x1;
6+
int x2;
7+
int x3;
8+
int x4;
9+
int x5;
10+
int x6;
11+
int x7;
12+
int x8;
13+
int x9;
14+
}

0 commit comments

Comments
 (0)