Skip to content

Commit c336824

Browse files
committed
Support XLM (Stellar)
1 parent a1e9a50 commit c336824

13 files changed

+729
-34
lines changed

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CFLAGS=-ggdb -O3 -Wall
88
# INCPATHS=-I$(shell brew --prefix)/include -I$(shell brew --prefix openssl)/include
99
# LIBPATHS=-L$(shell brew --prefix)/lib -L$(shell brew --prefix openssl)/lib
1010
# CFLAGS=-ggdb -O3 -Wall -Qunused-arguments $(INCPATHS) $(LIBPATHS)
11-
OBJS=vanitygen.o oclvanitygen.o oclvanityminer.o oclengine.o keyconv.o pattern.o util.o groestl.o sha3.o
11+
OBJS=vanitygen.o oclvanitygen.o oclvanityminer.o oclengine.o keyconv.o pattern.o util.o groestl.o sha3.o ed25519.o \
12+
stellar.o base32.o crc16.o
1213
PROGS=vanitygen++ keyconv oclvanitygen++ oclvanityminer
1314

1415
PLATFORM=$(shell uname -s)
@@ -28,7 +29,10 @@ most: vanitygen++ keyconv
2829

2930
all: $(PROGS)
3031

31-
vanitygen++: vanitygen.o pattern.o util.o groestl.o sha3.o
32+
ed25519: ed25519.o
33+
$(CC) $^ -o $@ $(CFLAGS) $(LIBS)
34+
35+
vanitygen++: vanitygen.o pattern.o util.o groestl.o sha3.o ed25519.o stellar.o base32.o crc16.o
3236
$(CC) $^ -o $@ $(CFLAGS) $(LIBS)
3337

3438
oclvanitygen++: oclvanitygen.o oclengine.o pattern.o util.o groestl.o sha3.o

base32.c

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/**
2+
* base32 (de)coder implementation as specified by RFC4648.
3+
*
4+
* Copyright (c) 2010 Adrien Kunysz
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
**/
24+
25+
#include <assert.h> // assert()
26+
#include <limits.h> // CHAR_BIT
27+
28+
#include "base32.h"
29+
30+
/**
31+
* Let this be a sequence of plain data before encoding:
32+
*
33+
* 01234567 01234567 01234567 01234567 01234567
34+
* +--------+--------+--------+--------+--------+
35+
* |< 0 >< 1| >< 2 ><|.3 >< 4.|>< 5 ><.|6 >< 7 >|
36+
* +--------+--------+--------+--------+--------+
37+
*
38+
* There are 5 octets of 8 bits each in each sequence.
39+
* There are 8 blocks of 5 bits each in each sequence.
40+
*
41+
* You probably want to refer to that graph when reading the algorithms in this
42+
* file. We use "octet" instead of "byte" intentionnaly as we really work with
43+
* 8 bits quantities. This implementation will probably not work properly on
44+
* systems that don't have exactly 8 bits per (unsigned) char.
45+
**/
46+
47+
static size_t min(size_t x, size_t y)
48+
{
49+
return x < y ? x : y;
50+
}
51+
52+
static const unsigned char PADDING_CHAR = 0; // '=';
53+
54+
/**
55+
* Pad the given buffer with len padding characters.
56+
*/
57+
static void pad(unsigned char *buf, int len)
58+
{
59+
int i;
60+
for (i = 0; i < len; i++)
61+
buf[i] = PADDING_CHAR;
62+
}
63+
64+
/**
65+
* This convert a 5 bits value into a base32 character.
66+
* Only the 5 least significant bits are used.
67+
*/
68+
static unsigned char encode_char(unsigned char c)
69+
{
70+
static unsigned char base32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
71+
return base32[c & 0x1F]; // 0001 1111
72+
}
73+
74+
/**
75+
* Decode given character into a 5 bits value.
76+
* Returns -1 iff the argument given was an invalid base32 character
77+
* or a padding character.
78+
*/
79+
static int decode_char(unsigned char c)
80+
{
81+
char retval = -1;
82+
83+
if (c >= 'A' && c <= 'Z')
84+
retval = c - 'A';
85+
if (c >= '2' && c <= '7')
86+
retval = c - '2' + 26;
87+
88+
assert(retval == -1 || ((retval & 0x1F) == retval));
89+
90+
return retval;
91+
}
92+
93+
/**
94+
* Given a block id between 0 and 7 inclusive, this will return the index of
95+
* the octet in which this block starts. For example, given 3 it will return 1
96+
* because block 3 starts in octet 1:
97+
*
98+
* +--------+--------+
99+
* | ......<|.3 >....|
100+
* +--------+--------+
101+
* octet 1 | octet 2
102+
*/
103+
static int get_octet(int block)
104+
{
105+
assert(block >= 0 && block < 8);
106+
return (block*5) / 8;
107+
}
108+
109+
/**
110+
* Given a block id between 0 and 7 inclusive, this will return how many bits
111+
* we can drop at the end of the octet in which this block starts.
112+
* For example, given block 0 it will return 3 because there are 3 bits
113+
* we don't care about at the end:
114+
*
115+
* +--------+-
116+
* |< 0 >...|
117+
* +--------+-
118+
*
119+
* Given block 1, it will return -2 because there
120+
* are actually two bits missing to have a complete block:
121+
*
122+
* +--------+-
123+
* |.....< 1|..
124+
* +--------+-
125+
**/
126+
static int get_offset(int block)
127+
{
128+
assert(block >= 0 && block < 8);
129+
return (8 - 5 - (5*block) % 8);
130+
}
131+
132+
/**
133+
* Like "b >> offset" but it will do the right thing with negative offset.
134+
* We need this as bitwise shifting by a negative offset is undefined
135+
* behavior.
136+
*/
137+
static unsigned char shift_right(unsigned char byte, char offset)
138+
{
139+
if (offset > 0)
140+
return byte >> offset;
141+
else
142+
return byte << -offset;
143+
}
144+
145+
static unsigned char shift_left(unsigned char byte, char offset)
146+
{
147+
return shift_right(byte, - offset);
148+
}
149+
150+
/**
151+
* Encode a sequence. A sequence is no longer than 5 octets by definition.
152+
* Thus passing a length greater than 5 to this function is an error. Encoding
153+
* sequences shorter than 5 octets is supported and padding will be added to the
154+
* output as per the specification.
155+
*/
156+
static void encode_sequence(const unsigned char *plain, int len, unsigned char *coded)
157+
{
158+
assert(CHAR_BIT == 8); // not sure this would work otherwise
159+
assert(len >= 0 && len <= 5);
160+
161+
int block;
162+
for (block = 0; block < 8; block++) {
163+
int octet = get_octet(block); // figure out which octet this block starts in
164+
int junk = get_offset(block); // how many bits do we drop from this octet?
165+
166+
if (octet >= len) { // we hit the end of the buffer
167+
pad(&coded[block], 8 - block);
168+
return;
169+
}
170+
171+
unsigned char c = shift_right(plain[octet], junk); // first part
172+
173+
if (junk < 0 // is there a second part?
174+
&& octet < len - 1) // is there still something to read?
175+
{
176+
c |= shift_right(plain[octet+1], 8 + junk);
177+
}
178+
coded[block] = encode_char(c);
179+
}
180+
}
181+
182+
void base32_encode(const unsigned char *plain, size_t len, unsigned char *coded)
183+
{
184+
// All the hard work is done in encode_sequence(),
185+
// here we just need to feed it the data sequence by sequence.
186+
size_t i, j;
187+
for (i = 0, j = 0; i < len; i += 5, j += 8) {
188+
encode_sequence(&plain[i], min(len - i, 5), &coded[j]);
189+
}
190+
}
191+
192+
static int decode_sequence(const unsigned char *coded, unsigned char *plain)
193+
{
194+
assert(CHAR_BIT == 8);
195+
assert(coded && plain);
196+
197+
plain[0] = 0;
198+
int block;
199+
for (block = 0; block < 8; block++) {
200+
int offset = get_offset(block);
201+
int octet = get_octet(block);
202+
203+
int c = decode_char(coded[block]);
204+
if (c < 0) // invalid char, stop here
205+
return octet;
206+
207+
plain[octet] |= shift_left(c, offset);
208+
if (offset < 0) { // does this block overflows to next octet?
209+
assert(octet < 4);
210+
plain[octet+1] = shift_left(c, 8 + offset);
211+
}
212+
}
213+
return 5;
214+
}
215+
216+
size_t base32_decode(const unsigned char *coded, unsigned char *plain)
217+
{
218+
size_t written = 0;
219+
size_t i, j;
220+
for (i = 0, j = 0; ; i += 8, j += 5) {
221+
int n = decode_sequence(&coded[i], &plain[j]);
222+
written += n;
223+
if (n < 5)
224+
return written;
225+
}
226+
}

base32.h

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* base32 (de)coder implementation as specified by RFC4648.
3+
*
4+
* Copyright (c) 2010 Adrien Kunysz
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
**/
24+
25+
#ifndef __BASE32_H_
26+
#define __BASE32_H_
27+
28+
#include <stddef.h> // size_t
29+
30+
/**
31+
* Returns the length of the output buffer required to encode len bytes of
32+
* data into base32. This is a macro to allow users to define buffer size at
33+
* compilation time.
34+
*/
35+
#define BASE32_LEN(len) (((len)/5)*8 + ((len) % 5 ? 8 : 0))
36+
37+
/**
38+
* Returns the length of the output buffer required to decode a base32 string
39+
* of len characters. Please note that len must be a multiple of 8 as per
40+
* definition of a base32 string. This is a macro to allow users to define
41+
* buffer size at compilation time.
42+
*/
43+
#define UNBASE32_LEN(len) (((len)/8)*5)
44+
45+
/**
46+
* Encode the data pointed to by plain into base32 and store the
47+
* result at the address pointed to by coded. The "coded" argument
48+
* must point to a location that has enough available space
49+
* to store the whole coded string. The resulting string will only
50+
* contain characters from the [A-Z2-7=] set. The "len" arguments
51+
* define how many bytes will be read from the "plain" buffer.
52+
**/
53+
void base32_encode(const unsigned char *plain, size_t len, unsigned char *coded);
54+
55+
/**
56+
* Decode the null terminated string pointed to by coded and write
57+
* the decoded data into the location pointed to by plain. The
58+
* "plain" argument must point to a location that has enough available
59+
* space to store the whole decoded string.
60+
* Returns the length of the decoded string. This may be less than
61+
* expected due to padding. If an invalid base32 character is found
62+
* in the coded string, decoding will stop at that point.
63+
**/
64+
size_t base32_decode(const unsigned char *coded, unsigned char *plain);
65+
66+
#endif

crc16.c

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "crc16.h"
2+
3+
unsigned short crcTable[256] = {
4+
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
5+
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
6+
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
7+
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
8+
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
9+
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
10+
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
11+
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
12+
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
13+
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
14+
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
15+
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
16+
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
17+
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
18+
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
19+
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
20+
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
21+
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
22+
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
23+
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
24+
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
25+
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
26+
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
27+
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
28+
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
29+
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
30+
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
31+
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
32+
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
33+
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
34+
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
35+
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
36+
};
37+
38+
// There are many variation for crc16
39+
// https://www.lammertbies.nl/comm/info/crc-calculation
40+
// 1 byte checksum 221
41+
// CRC-16 0xBB3D
42+
// CRC-16 (Modbus) 0x4B37
43+
// CRC-16 (Sick) 0x56A6
44+
// CRC-CCITT (XModem) 0x31C3 // <-- We use this one
45+
// CRC-CCITT (0xFFFF) 0x29B1
46+
// CRC-CCITT (0x1D0F) 0xE5CC
47+
// CRC-CCITT (Kermit) 0x8921
48+
// CRC-DNP 0x82EA
49+
// CRC-32 0xCBF43926
50+
unsigned short crc16(const unsigned char *in, unsigned short length) {
51+
unsigned short crc = 0x0000;
52+
unsigned short x;
53+
int j, i;
54+
55+
for (i = 0; i < length; i++) {
56+
x = in[i];
57+
j = (x ^ (crc >> 8)) & 0xFF;
58+
crc = crcTable[j] ^ (crc << 8);
59+
}
60+
61+
return (unsigned short)((crc ^ 0) & 0xFFFF);
62+
}

crc16.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef VANITYGEN_PLUSPLUS_CRC16_H
2+
#define VANITYGEN_PLUSPLUS_CRC16_H
3+
4+
unsigned short crc16(const unsigned char *in, unsigned short length);
5+
6+
#endif //VANITYGEN_PLUSPLUS_CRC16_H

0 commit comments

Comments
 (0)