1+ #include < boost/test/unit_test.hpp>
2+
3+ #include < boost/atomic.hpp>
4+
5+ #include < openssl/err.h>
6+ #include < openssl/rand.h>
7+ #include < openssl/ec.h>
8+ #include < openssl/bn.h>
9+ #include < openssl/ecdsa.h>
10+ #include < openssl/obj_mac.h>
11+
12+ #include < ctime>
13+
14+ #include " ringsig.h"
15+ #include " main.h"
16+
17+ using namespace boost ::chrono;
18+
19+ // test_shadow --log_level=all --run_test=ringsig_tests
20+
21+ clock_t totalGenerate;
22+ clock_t totalVerify;
23+ clock_t start, stop;
24+
25+ void testRingSigs (int nRingSize)
26+ {
27+ uint8_t *pPubkeys = (uint8_t *) malloc (sizeof (uint8_t ) * EC_COMPRESSED_SIZE * nRingSize);
28+ uint8_t *pSigc = (uint8_t *) malloc (sizeof (uint8_t ) * EC_SECRET_SIZE * nRingSize);
29+ uint8_t *pSigr = (uint8_t *) malloc (sizeof (uint8_t ) * EC_SECRET_SIZE * nRingSize);
30+
31+ BOOST_REQUIRE (NULL != pPubkeys);
32+ BOOST_REQUIRE (NULL != pSigc);
33+ BOOST_REQUIRE (NULL != pSigr);
34+
35+ CKey key[nRingSize];
36+ for (int i = 0 ; i < nRingSize; ++i)
37+ {
38+ key[i].MakeNewKey (true );
39+
40+ CPubKey pk = key[i].GetPubKey ();
41+
42+ memcpy (&pPubkeys[i * EC_COMPRESSED_SIZE], pk.begin (), EC_COMPRESSED_SIZE);
43+ };
44+
45+ uint256 preimage;
46+ BOOST_CHECK (1 == RAND_bytes ((uint8_t *) preimage.begin (), 32 ));
47+ // BOOST_MESSAGE("Txn preimage: " << HexStr(preimage));
48+
49+ // BOOST_MESSAGE("nRingSize: " << nRingSize);
50+ int iSender = GetRandInt (nRingSize);
51+ // BOOST_MESSAGE("sender: " << iSender);
52+
53+ ec_secret sSpend ;
54+ ec_point pkSpend;
55+ ec_point keyImage;
56+
57+ memcpy (&sSpend .e [0 ], key[iSender].begin (), EC_SECRET_SIZE);
58+
59+ BOOST_REQUIRE (0 == SecretToPublicKey (sSpend , pkSpend));
60+
61+ BOOST_REQUIRE (0 == generateKeyImage (pkSpend, sSpend , keyImage));
62+
63+ start = clock ();
64+ BOOST_REQUIRE (0 == generateRingSignature (keyImage, preimage, nRingSize, iSender, sSpend , pPubkeys, pSigc, pSigr));
65+ stop = clock ();
66+ totalGenerate += stop - start;
67+
68+ start = clock ();
69+ BOOST_REQUIRE (0 == verifyRingSignature (keyImage, preimage, nRingSize, pPubkeys, pSigc, pSigr));
70+ stop = clock ();
71+ totalVerify += stop - start;
72+
73+ int sigSize = EC_COMPRESSED_SIZE + EC_SECRET_SIZE + (EC_SECRET_SIZE + EC_SECRET_SIZE + EC_COMPRESSED_SIZE) * nRingSize;
74+
75+ BOOST_MESSAGE (" nRingSize " << nRingSize << " , sigSize: " << bytesReadable (sigSize));
76+
77+ if (pPubkeys)
78+ free (pPubkeys);
79+ if (pSigc)
80+ free (pSigc);
81+ if (pSigr)
82+ free (pSigr);
83+ };
84+
85+ void testRingSigABs (int nRingSize)
86+ {
87+ uint8_t *pPubkeys = (uint8_t *) malloc (sizeof (uint8_t ) * EC_COMPRESSED_SIZE * nRingSize);
88+ uint8_t *pSigS = (uint8_t *) malloc (sizeof (uint8_t ) * EC_SECRET_SIZE * nRingSize);
89+
90+ BOOST_CHECK (NULL != pPubkeys);
91+ BOOST_CHECK (NULL != pSigS);
92+
93+ CKey key[nRingSize];
94+ for (int i = 0 ; i < nRingSize; ++i)
95+ {
96+ key[i].MakeNewKey (true );
97+
98+ CPubKey pk = key[i].GetPubKey ();
99+
100+ memcpy (&pPubkeys[i * EC_COMPRESSED_SIZE], pk.begin (), EC_COMPRESSED_SIZE);
101+ };
102+
103+ uint256 preimage;
104+ BOOST_CHECK (1 == RAND_bytes ((uint8_t *) preimage.begin (), 32 ));
105+ // BOOST_MESSAGE("Txn preimage: " << HexStr(preimage));
106+
107+ int iSender = GetRandInt (nRingSize);
108+ // BOOST_MESSAGE("sender: " << iSender);
109+
110+ ec_point pSigC;
111+
112+ ec_secret sSpend ;
113+ ec_point pkSpend;
114+ ec_point keyImage;
115+
116+ memcpy (&sSpend .e [0 ], key[iSender].begin (), EC_SECRET_SIZE);
117+
118+ BOOST_CHECK (0 == SecretToPublicKey (sSpend , pkSpend));
119+
120+ BOOST_REQUIRE (0 == generateKeyImage (pkSpend, sSpend , keyImage));
121+
122+ start = clock ();
123+ BOOST_REQUIRE (0 == generateRingSignatureAB (keyImage, preimage, nRingSize, iSender, sSpend , pPubkeys, pSigC, pSigS));
124+ stop = clock ();
125+ totalGenerate += stop - start;
126+
127+ start = clock ();
128+ BOOST_REQUIRE (0 == verifyRingSignatureAB (keyImage, preimage, nRingSize, pPubkeys, pSigC, pSigS));
129+ stop = clock ();
130+ totalVerify += stop - start;
131+
132+ int sigSize = EC_COMPRESSED_SIZE + EC_SECRET_SIZE + EC_SECRET_SIZE + (EC_SECRET_SIZE + EC_COMPRESSED_SIZE) * nRingSize;
133+
134+ BOOST_MESSAGE (" nRingSize " << nRingSize << " , sigSize: " << bytesReadable (sigSize));
135+
136+ if (pPubkeys)
137+ free (pPubkeys);
138+ if (pSigS)
139+ free (pSigS);
140+
141+ };
142+
143+ BOOST_AUTO_TEST_SUITE (ringsig_tests)
144+
145+ BOOST_AUTO_TEST_CASE(ringsig)
146+ {
147+ BOOST_REQUIRE (0 == initialiseRingSigs ());
148+
149+ BOOST_MESSAGE (" testRingSigs" );
150+
151+ for (int k = 1 ; k < 4 ; ++k)
152+ {
153+ // BOOST_MESSAGE("ringSize " << (k % 126 + 2));
154+ testRingSigs (k % 126 + 2 );
155+ };
156+ // testRingSigs(16);
157+
158+ BOOST_MESSAGE (" totalGenerate " << (double (totalGenerate) / CLOCKS_PER_SEC));
159+ BOOST_MESSAGE (" totalVerify " << (double (totalVerify) / CLOCKS_PER_SEC));
160+
161+ totalGenerate = 0 ;
162+ totalVerify = 0 ;
163+ BOOST_MESSAGE (" testRingSigABs" );
164+
165+ for (int k = 0 ; k < 32 ; ++k)
166+ {
167+ // BOOST_MESSAGE("ringSize " << (k % 126 + 2));
168+ // testRingSigABs(k % 126 + 2);
169+ };
170+ // testRingSigABs(16);
171+
172+ BOOST_MESSAGE (" totalGenerate " << (double (totalGenerate) / CLOCKS_PER_SEC));
173+ BOOST_MESSAGE (" totalVerify " << (double (totalVerify) / CLOCKS_PER_SEC));
174+
175+ BOOST_CHECK (0 == finaliseRingSigs ());
176+ }
177+
178+ BOOST_AUTO_TEST_SUITE_END ()
0 commit comments