Debian Codegraph @

src/ringct/rctCryptoOps.c

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <assert.h>
32 #include <stdint.h>
33
34 #include "crypto/crypto-ops.h"
35
36 //DISABLE_VS_WARNINGS(4146 4244)
37
38 void sc_reduce32copy(unsigned char * scopy, const unsigned char *s) {
39 int64_t s0 = 2097151 & load_3(s);
40 int64_t s1 = 2097151 & (load_4(s + 2) >> 5);
41 int64_t s2 = 2097151 & (load_3(s + 5) >> 2);
42 int64_t s3 = 2097151 & (load_4(s + 7) >> 7);
43 int64_t s4 = 2097151 & (load_4(s + 10) >> 4);
44 int64_t s5 = 2097151 & (load_3(s + 13) >> 1);
45 int64_t s6 = 2097151 & (load_4(s + 15) >> 6);
46 int64_t s7 = 2097151 & (load_3(s + 18) >> 3);
47 int64_t s8 = 2097151 & load_3(s + 21);
48 int64_t s9 = 2097151 & (load_4(s + 23) >> 5);
49 int64_t s10 = 2097151 & (load_3(s + 26) >> 2);
50 int64_t s11 = (load_4(s + 28) >> 7);
51 int64_t s12 = 0;
52 int64_t carry0;
53 int64_t carry1;
54 int64_t carry2;
55 int64_t carry3;
56 int64_t carry4;
57 int64_t carry5;
58 int64_t carry6;
59 int64_t carry7;
60 int64_t carry8;
61 int64_t carry9;
62 int64_t carry10;
63 int64_t carry11;
64
65 carry0 = (s0 + (1<<20)) >> 21;
66 s1 += carry0;
67 s0 -= carry0 << 21;
68 carry2 = (s2 + (1<<20)) >> 21;
69 s3 += carry2;
70 s2 -= carry2 << 21;
71 carry4 = (s4 + (1<<20)) >> 21;
72 s5 += carry4;
73 s4 -= carry4 << 21;
74 carry6 = (s6 + (1<<20)) >> 21;
75 s7 += carry6;
76 s6 -= carry6 << 21;
77 carry8 = (s8 + (1<<20)) >> 21;
78 s9 += carry8;
79 s8 -= carry8 << 21;
80 carry10 = (s10 + (1<<20)) >> 21;
81 s11 += carry10;
82 s10 -= carry10 << 21;
83
84 carry1 = (s1 + (1<<20)) >> 21;
85 s2 += carry1;
86 s1 -= carry1 << 21;
87 carry3 = (s3 + (1<<20)) >> 21;
88 s4 += carry3;
89 s3 -= carry3 << 21;
90 carry5 = (s5 + (1<<20)) >> 21;
91 s6 += carry5;
92 s5 -= carry5 << 21;
93 carry7 = (s7 + (1<<20)) >> 21;
94 s8 += carry7;
95 s7 -= carry7 << 21;
96 carry9 = (s9 + (1<<20)) >> 21;
97 s10 += carry9;
98 s9 -= carry9 << 21;
99 carry11 = (s11 + (1<<20)) >> 21;
100 s12 += carry11;
101 s11 -= carry11 << 21;
102
103 s0 += s12 * 666643;
104 s1 += s12 * 470296;
105 s2 += s12 * 654183;
106 s3 -= s12 * 997805;
107 s4 += s12 * 136657;
108 s5 -= s12 * 683901;
109 s12 = 0;
110
111 carry0 = s0 >> 21;
112 s1 += carry0;
113 s0 -= carry0 << 21;
114 carry1 = s1 >> 21;
115 s2 += carry1;
116 s1 -= carry1 << 21;
117 carry2 = s2 >> 21;
118 s3 += carry2;
119 s2 -= carry2 << 21;
120 carry3 = s3 >> 21;
121 s4 += carry3;
122 s3 -= carry3 << 21;
123 carry4 = s4 >> 21;
124 s5 += carry4;
125 s4 -= carry4 << 21;
126 carry5 = s5 >> 21;
127 s6 += carry5;
128 s5 -= carry5 << 21;
129 carry6 = s6 >> 21;
130 s7 += carry6;
131 s6 -= carry6 << 21;
132 carry7 = s7 >> 21;
133 s8 += carry7;
134 s7 -= carry7 << 21;
135 carry8 = s8 >> 21;
136 s9 += carry8;
137 s8 -= carry8 << 21;
138 carry9 = s9 >> 21;
139 s10 += carry9;
140 s9 -= carry9 << 21;
141 carry10 = s10 >> 21;
142 s11 += carry10;
143 s10 -= carry10 << 21;
144 carry11 = s11 >> 21;
145 s12 += carry11;
146 s11 -= carry11 << 21;
147
148 s0 += s12 * 666643;
149 s1 += s12 * 470296;
150 s2 += s12 * 654183;
151 s3 -= s12 * 997805;
152 s4 += s12 * 136657;
153 s5 -= s12 * 683901;
154
155 carry0 = s0 >> 21;
156 s1 += carry0;
157 s0 -= carry0 << 21;
158 carry1 = s1 >> 21;
159 s2 += carry1;
160 s1 -= carry1 << 21;
161 carry2 = s2 >> 21;
162 s3 += carry2;
163 s2 -= carry2 << 21;
164 carry3 = s3 >> 21;
165 s4 += carry3;
166 s3 -= carry3 << 21;
167 carry4 = s4 >> 21;
168 s5 += carry4;
169 s4 -= carry4 << 21;
170 carry5 = s5 >> 21;
171 s6 += carry5;
172 s5 -= carry5 << 21;
173 carry6 = s6 >> 21;
174 s7 += carry6;
175 s6 -= carry6 << 21;
176 carry7 = s7 >> 21;
177 s8 += carry7;
178 s7 -= carry7 << 21;
179 carry8 = s8 >> 21;
180 s9 += carry8;
181 s8 -= carry8 << 21;
182 carry9 = s9 >> 21;
183 s10 += carry9;
184 s9 -= carry9 << 21;
185 carry10 = s10 >> 21;
186 s11 += carry10;
187 s10 -= carry10 << 21;
188
189 scopy[0] = s0 >> 0;
190 scopy[1] = s0 >> 8;
191 scopy[2] = (s0 >> 16) | (s1 << 5);
192 scopy[3] = s1 >> 3;
193 scopy[4] = s1 >> 11;
194 scopy[5] = (s1 >> 19) | (s2 << 2);
195 scopy[6] = s2 >> 6;
196 scopy[7] = (s2 >> 14) | (s3 << 7);
197 scopy[8] = s3 >> 1;
198 scopy[9] = s3 >> 9;
199 scopy[10] = (s3 >> 17) | (s4 << 4);
200 scopy[11] = s4 >> 4;
201 scopy[12] = s4 >> 12;
202 scopy[13] = (s4 >> 20) | (s5 << 1);
203 scopy[14] = s5 >> 7;
204 scopy[15] = (s5 >> 15) | (s6 << 6);
205 scopy[16] = s6 >> 2;
206 scopy[17] = s6 >> 10;
207 scopy[18] = (s6 >> 18) | (s7 << 3);
208 scopy[19] = s7 >> 5;
209 scopy[20] = s7 >> 13;
210 scopy[21] = s8 >> 0;
211 scopy[22] = s8 >> 8;
212 scopy[23] = (s8 >> 16) | (s9 << 5);
213 scopy[24] = s9 >> 3;
214 scopy[25] = s9 >> 11;
215 scopy[26] = (s9 >> 19) | (s10 << 2);
216 scopy[27] = s10 >> 6;
217 scopy[28] = (s10 >> 14) | (s11 << 7);
218 scopy[29] = s11 >> 1;
219 scopy[30] = s11 >> 9;
220 scopy[31] = s11 >> 17;
221 }