Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* Copyright (c) (2017-2023) Apple Inc. All rights reserved.
 *
 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
 * is contained in the License.txt file distributed with corecrypto) and only to
 * people who accept that license. IMPORTANT:  Any license rights granted to you by
 * Apple Inc. (if any) are limited to internal use within your organization only on
 * devices and computers you own or control, for the sole purpose of verifying the
 * security characteristics and correct functioning of the Apple Software.  You may
 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#ifndef _CORECRYPTO_CCN_INTERNAL_H
#define _CORECRYPTO_CCN_INTERNAL_H

#include <corecrypto/ccn.h>
#include "cc_workspaces.h"
#include "cc_memory.h"
#include "cc_internal.h"

CC_PTRCHECK_CAPABLE_HEADER()

#if CCN_UNIT_SIZE == 8

 #if CC_DUNIT_SUPPORTED
typedef unsigned cc_dunit __attribute__((mode(TI)));
 #endif

#define cc_clz_nonzero cc_clz64
#define cc_ctz_nonzero cc_ctz64
#define CC_STORE_UNIT_BE(x, out) cc_store64_be(x, out)
#define CC_LOAD_UNIT_BE(x, out) (x = cc_load64_be(out))

#elif CCN_UNIT_SIZE == 4

typedef uint64_t cc_dunit;

#define cc_clz_nonzero cc_clz32
#define cc_ctz_nonzero cc_ctz32
#define CC_STORE_UNIT_BE(x, out) cc_store32_be(x, out)
#define CC_LOAD_UNIT_BE(x, out) (x = cc_load32_be(out))

#else

#error Unsupported CCN_UNIT_SIZE

#endif

#if CC_DUNIT_SUPPORTED

// r := x + y
#define CC_DUNIT_ADD(r, x, y, tmp)      \
    do {                             \
	tmp = ((cc_dunit)(x)) + (y); \
	r = (cc_unit)tmp;            \
    } while (0);

// r := x + y + (tmp >> 64)
#define CC_DUNIT_ADC(r, x, y, tmp)              \
    do {                                     \
	cc_unit _c = (tmp) >> CCN_UNIT_BITS; \
	tmp = ((cc_dunit)(x)) + (y) + _c;    \
	r = (cc_unit)tmp;                    \
    } while (0);

// r := x - y
#define CC_DUNIT_SUB(r, x, y, tmp)      \
    do {                             \
	tmp = ((cc_dunit)(x)) - (y); \
	r = (cc_unit)tmp;            \
    } while (0);

// r := x - y - (tmp >> 127)
#define CC_DUNIT_SBC(r, x, y, tmp)                        \
    do {                                               \
	cc_unit _b = (tmp) >> (2 * CCN_UNIT_BITS - 1); \
	tmp = ((cc_dunit)(x)) - (y) - _b;              \
	r = (cc_unit)tmp;                              \
    } while (0);

// (hi,lo) += (x * y)
#define CC_DUNIT_MUL(x, y, hi, lo, tmp)  \
    do {                              \
	tmp = (cc_dunit)(x) * (y);    \
	lo += (tmp) & CCN_UNIT_MASK;  \
	hi += (tmp) >> CCN_UNIT_BITS; \
    } while (0);

// (hi,lo) += (x * y) * i
#define CC_DUNIT_MULI(x, y, hi, lo, tmp, i)      \
    do {                                      \
	tmp = (cc_dunit)(x) * (y);            \
	lo += ((tmp) & CCN_UNIT_MASK) * (i);  \
	hi += ((tmp) >> CCN_UNIT_BITS) * (i); \
    } while (0);

// r := lo and (hi,lo) >>= 64
#define CC_STORE_LO(r, hi, lo)        \
    do {                           \
	r = (cc_unit)lo;           \
	hi += lo >> CCN_UNIT_BITS; \
	lo = hi & CCN_UNIT_MASK;   \
	hi >>= CCN_UNIT_BITS;      \
    } while (0);

#endif

CC_NONNULL((2, 3))
void ccn_set(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s);

CC_INLINE
CC_NONNULL((2, 4))
void
ccn_setn(cc_size n, cc_unit *cc_counted_by (n)r, const cc_size s_size, const cc_unit *cc_counted_by (s_size)s)
{
	cc_assert(n > 0 && s_size <= n);

	if (s_size > 0) {
		ccn_set(s_size, r, s);
	}

	ccn_zero(n - s_size, r + s_size);
}

CC_INLINE
CC_NONNULL((2))
void
ccn_clear(cc_size n, cc_unit *cc_sized_by (n)r)
{
	cc_clear(ccn_sizeof_n(n), r);
}

/* Returns the value of bit _k_ of _ccn_, both are only evaluated once.  */
CC_INLINE cc_unit
ccn_bit(const cc_unit *cc_indexable x, size_t k)
{
	return 1 & (x[k >> CCN_LOG2_BITS_PER_UNIT] >> (k & (CCN_UNIT_BITS - 1)));
}

/* |s - t| -> r return 1 iff t > s, 0 otherwise */
CC_WARN_RESULT
cc_unit ccn_abs(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, const cc_unit *cc_counted_by(n) t);

/* Returns the number of bits which are zero before the first one bit
 *  counting from least to most significant bit. */
CC_WARN_RESULT
CC_NONNULL((2))
size_t ccn_trailing_zeros(cc_size n, const cc_unit *s);

/*! @function ccn_shift_right
 *  @abstract Shifts s to the right by k bits, where 0 <= k < CCN_UNIT_BITS.
 *
 *  @param n Length of r and s
 *  @param r Resulting big int.
 *  @param s Big int to shift.
 *  @param k Number of bits to shift by.
 */
CC_NONNULL_ALL
void ccn_shift_right(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, size_t k) __asm__("_ccn_shift_right");

/*! @function ccn_shift_right_multi
 *  @abstract Constant-time, SPA-safe, right shift.
 *
 *  @param n Length of r and s as number of cc_units.
 *  @param r Destination, can overlap with s.
 *  @param s Input that's shifted by k bits.
 *  @param k Number of bits by which to shift s to the right.
 */
CC_NONNULL_ALL
void ccn_shift_right_multi(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, size_t k);

/* s << k -> r return bits shifted out of most significant word in bits [0, n>
 *  { N bit, scalar -> N bit } N = n * sizeof(cc_unit) * 8
 *  the _multi version doesn't return the shifted bits, but does support multiple
 *  word shifts */
CC_NONNULL_ALL
void ccn_shift_left(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, size_t k) __asm__("_ccn_shift_left");

CC_NONNULL_ALL
void ccn_shift_left_multi(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, size_t k);

// Conditionally swap the content of r0 and r1 buffers in constant time
// r0:r1 <- r1*k1 + s0*(k1-1)
CC_NONNULL_ALL
void ccn_cond_swap(cc_size n, cc_unit ki, cc_unit *cc_counted_by(n) r0, cc_unit *cc_counted_by(n) r1);

/*! @function ccn_cond_shift_right
 *  @abstract Constant-time, SPA-safe, conditional right shift.
 *
 *  @param n Length of each of r and a as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with a.
 *  @param a Input that's shifted by k bits, if s=1.
 *  @param k Number of bits by which to shift a to the right, if s=1.
 *         (k must not be larger than CCN_UNIT_BITS.)
 */
CC_NONNULL_ALL
void ccn_cond_shift_right(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) a, size_t k);

/*! @function ccn_cond_neg
 *  @abstract Constant-time, SPA-safe, conditional negation.
 *
 *  @param n Length of each of r and x as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with x.
 *  @param x Input that's negated, if s=1.
 */
void ccn_cond_neg(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) x);

/*! @function ccn_cond_shift_right_carry
 *  @abstract Constant-time, SPA-safe, conditional right shift.
 *
 *  @param n Length of each of r and a as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with a.
 *  @param a Input that's shifted by k bits, if s=1.
 *  @param k Number of bits by which to shift a to the right, if s=1.
 *         (k must not be larger than CCN_UNIT_BITS.)
 *  @param c Carry bit(s), the most significant bit(s) after shifting, if s=1.
 */
CC_NONNULL_ALL
void ccn_cond_shift_right_carry(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) a, size_t k, cc_unit c);

/*! @function ccn_cond_add
 *  @abstract Constant-time, SPA-safe, conditional addition.
 *          Computes r:= x + y, iff s = 1. Set r := x otherwise.
 *
 *  @param n Length of each of r, x, and y as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with x or y.
 *  @param x First addend.
 *  @param y Second addend.
 *
 *  @return The carry bit, if s=1. 0 otherwise.
 */
CC_WARN_RESULT CC_NONNULL_ALL
cc_unit ccn_cond_add(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) x, const cc_unit *cc_counted_by(n) y);

/*! @function ccn_cond_rsub
 *  @abstract Constant-time, SPA-safe, conditional reverse subtraction.
 *          Computes r := y - x, iff s = 1. Sets r := x otherwise.
 *
 *  @param n Length of each of r, x, and y as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with x or y.
 *  @param x Subtrahend.
 *  @param y Minuend.
 *
 *  @return The carry bit, if s=1. 0 otherwise.
 */
CC_WARN_RESULT CC_NONNULL_ALL
cc_unit ccn_cond_rsub(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) x, const cc_unit *cc_counted_by(n) y);

/*! @function ccn_cond_sub
 *  @abstract Constant-time, SPA-safe, conditional subtraction.
 *          Computes r := x - y, iff s = 1. Sets r := x otherwise.
 *
 *  @param n Length of each of r, x, and y as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with x or y.
 *  @param x Minuend.
 *  @param y Subtrahend.
 *
 *  @return The carry bit, if s=1. 0 otherwise.
 */
CC_WARN_RESULT CC_NONNULL_ALL
cc_unit ccn_cond_sub(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) x, const cc_unit *cc_counted_by(n) y);

/*! @function ccn_cond_clear
 *  @abstract Constant-time, SPA-safe, conditional zeroization.
 *          Sets r := 0, if s = 1. Does nothing otherwise.
 *
 *  @param n Length of r as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with x or y.
 */
CC_NONNULL_ALL
void ccn_cond_clear(cc_size n, cc_unit s, cc_unit *r);

/*! @function ccn_mux
 *  @abstract Constant-time, SPA-safe multiplexer. Sets r = (s ? a : b).
 *
 *  @discussion This works like a normal multiplexer (s & a) | (~s & b) but is
 *            slightly more complicated and expensive. Out of `s` we build
 *            half-word masks to hide extreme Hamming weights of operands.
 *
 *  @param n Length of each of r, a, and b as number of cc_units.
 *  @param s Selector bit (0 or 1).
 *  @param r Destination, can overlap with a or b.
 *  @param a Input selected when s=1.
 *  @param b Input selected when s=0.
 */
CC_NONNULL_ALL
void ccn_mux(cc_size n, cc_unit s, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) a, const cc_unit *cc_counted_by(n) b);

/*! @function ccn_gcd_ws
 *  @abstract Computes the greatest common divisor of s and t,
 *          r = gcd(s,t) / 2^k, and returns k.
 *
 *  @param ws Workspace.
 *  @param rn Length of r as a number of cc_units.
 *  @param r  Resulting GCD.
 *  @param sn Length of s as a number of cc_units.
 *  @param s  First number s.
 *  @param tn Length of t as a number of cc_units.
 *  @param t  First number t.
 *
 *  @return The factor of two to shift r by to compute the actual GCD.
 */
CC_WARN_RESULT
CC_NONNULL_ALL
size_t ccn_gcd_ws(cc_ws_t ws, cc_size rn, cc_unit *cc_counted_by(rn) r, cc_size sn, const cc_unit *cc_counted_by(sn) s, cc_size tn, const cc_unit *cc_counted_by(tn) t);

/*! @function ccn_lcm_ws
 *  @abstract Computes lcm(s,t), the least common multiple of s and t.
 *
 *  @param ws  Workspace.
 *  @param n   Length of s,t as a number of cc_units.
 *  @param r2n Resulting LCM of length 2*n.
 *  @param s   First number s.
 *  @param t   First number t.
 */
void ccn_lcm_ws(cc_ws_t ws, cc_size n, cc_unit *cc_unsafe_indexable r2n, const cc_unit *cc_counted_by(n)s, const cc_unit *cc_counted_by(n)t);

/* s * t -> r_2n                   r_2n must not overlap with s nor t
 *  { n bit, n bit -> 2 * n bit } n = count * sizeof(cc_unit) * 8
 *  { N bit, N bit -> 2N bit } N = ccn_bitsof(n) */
CC_NONNULL((2, 3, 4))
void ccn_mul(cc_size n, cc_unit *cc_unsafe_indexable r_2n, const cc_unit *cc_counted_by(n)s, const cc_unit *cc_counted_by(n)t) __asm__("_ccn_mul");

/* s[0..n) * v -> r[0..n)+return value
 *  { N bit, sizeof(cc_unit) * 8 bit -> N + sizeof(cc_unit) * 8 bit } N = n * sizeof(cc_unit) * 8 */
CC_WARN_RESULT
CC_NONNULL((2, 3))
cc_unit ccn_mul1(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, const cc_unit v);

/* s[0..n) * v[0..nv] -> r[0..n+nv)
*  { n bit, nv bit -> n + nv bit} n = count * sizeof(cc_unit) * 8
*  { N bit, NV bit -> N + NV bit} N = ccn_bitsof(n), NV = ccn_bitsof(nv)
*  r, s, and v should not overlap
*  Leaks n and nv through timing */
CC_NONNULL_ALL
void ccn_muln(cc_size n, cc_unit *cc_counted_by(n + nv) r, const cc_unit *cc_counted_by(n) s, cc_size nv, const cc_unit *cc_counted_by(n) v);

/* s[0..n) * v + r[0..n) -> r[0..n)+return value
 *  { N bit, sizeof(cc_unit) * 8 bit -> N + sizeof(cc_unit) * 8 bit } N = n * sizeof(cc_unit) * 8 */
CC_WARN_RESULT
CC_NONNULL((2, 3))
cc_unit ccn_addmul1(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, const cc_unit v);

/* s * t -> r_2n                   r_2n must not overlap with s nor t
 *  { n bit, n bit -> 2 * n bit } n = count * sizeof(cc_unit) * 8
 *  { N bit, N bit -> 2N bit } N = ccn_bitsof(n)
 *  Provide a workspace for potential speedup */
CC_NONNULL_ALL
void ccn_mul_ws(cc_ws_t ws, cc_size count, cc_unit *cc_unsafe_indexable r, const cc_unit *cc_counted_by(count)s, const cc_unit *cc_counted_by(count)t);

/* s^2 -> r
 *  { n bit -> 2 * n bit } */
CC_NONNULL_ALL
void ccn_sqr_ws(cc_ws_t ws, cc_size n, cc_unit *cc_unsafe_indexable r, const cc_unit *cc_counted_by(n)s);

/*! @function ccn_mod_ws
 *  @abstract Computes r = a % d.
 *
 *  @discussion Use CCN_DIVMOD_WORKSPACE_N(n) for the workspace.
 *
 *  @param ws  Workspace
 *  @param na  Length of a as a number of cc_units.
 *  @param a   The dividend a.
 *  @param n   Length of r,d as a number of cc_units.
 *  @param r   The resulting remainder.
 *  @param d   The divisor d.
 */
#define ccn_mod_ws(ws, na, a, n, r, d) ccn_divmod_ws(ws, na, a, 0, NULL, n, r, d)
#define ccn_mod(na, a, n, r, d) ccn_divmod(na, a, 0, NULL, n, r, d)

/*! @function ccn_neg
 *  @abstract Computes the two's complement of x.
 *
 *  @param n  Length of r and x
 *  @param r  Result of the negation
 *  @param x  Number to negate
 */
CC_NONNULL_ALL
void ccn_neg(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) x);

/*! @function ccn_invert
 *  @abstract Computes x^-1 (mod 2^w).
 *
 *  @param x  Number to invert
 *
 *  @return x^-1 (mod 2^w)
 */
CC_WARN_RESULT
CC_CONST CC_NONNULL_ALL
CC_INLINE cc_unit
ccn_invert(cc_unit x)
{
	cc_assert(x & 1);

	// Initial precision is 5 bits.
	cc_unit y = (3 * x) ^ 2;

	// Newton-Raphson iterations.
	// Precision doubles with every step.
	y *= 2 - y * x;
	y *= 2 - y * x;
	y *= 2 - y * x;
#if CCN_UNIT_SIZE == 8
	y *= 2 - y * x;
#endif

	cc_assert(y * x == 1);
	return y;
}

/*! @function ccn_div_exact_ws
 *  @abstract Computes q = a / d where a = 0 (mod d).
 *
 *  @param ws  Workspace
 *  @param n   Length of q,a,d as a number of cc_units.
 *  @param q   The resulting exact quotient.
 *  @param a   The dividend a.
 *  @param d   The divisor d.
 */
CC_NONNULL_ALL
void ccn_div_exact_ws(cc_ws_t ws, cc_size n, cc_unit *cc_counted_by(n) q, const cc_unit *cc_counted_by(n) a, const cc_unit *cc_counted_by(n) d);

/*! @function ccn_divides1
 *  @abstract Returns whether q divides x.
 *
 *  @param n  Length of x as a number of cc_units.
 *  @param x  The dividend x.
 *  @param q  The divisor q.
 *
 *  @return True if q divides x without remainder, false otherwise.
 */
CC_WARN_RESULT
CC_NONNULL_ALL
bool ccn_divides1(cc_size n, const cc_unit *cc_counted_by(n)x, cc_unit q);

/*! @function ccn_select
 *  @abstract Select r[i] in constant-time, not revealing i via cache-timing.
 *
 *  @param start Start index.
 *  @param end   End index (length of r).
 *  @param r     Big int r.
 *  @param i     Offset into r.
 *
 *  @return r[i], or zero if start > i or end < i.
 */
CC_WARN_RESULT
CC_INLINE cc_unit
ccn_select(cc_size start, cc_size end, const cc_unit *cc_counted_by(end)r, cc_size i)
{
	cc_unit ri = 0;

	for (cc_size j = start; j < end; j++) {
		cc_size i_neq_j; // i≠j?
		CC_HEAVISIDE_STEP(i_neq_j, i ^ j);
		ri |= r[j] & ((cc_unit)i_neq_j - 1);
	}

	return ri;
}

/*! @function ccn_invmod_ws
 *  @abstract Computes the inverse of x modulo m, r = x^-1 (mod m).
 *          Returns an error if there's no inverse, i.e. gcd(x,m) ≠ 1.
 *
 *  @discussion This is a very generic version of the binary XGCD algorithm. You
 *            don't want to use it when you have an odd modulus.
 *
 *            This function is meant to be used by RSA key generation, for
 *            computation of d = e^1 (mod lcm(p-1,q-1)), where m can be even.
 *
 *            x > m is allowed as long as xn == n, i.e. they occupy the same
 *            number of cc_units.
 *
 *  @param ws Workspace.
 *  @param n  Length of r and m as a number of cc_units.
 *  @param r  The resulting inverse r.
 *  @param xn Length of x as a number of cc_units.
 *  @param x  The number to invert.
 *  @param m  The modulus.
 *
 *  @return 0 on success, non-zero on failure. See cc_error.h for more details.
 */
CC_WARN_RESULT
int ccn_invmod_ws(cc_ws_t ws, cc_size n, cc_unit *cc_counted_by(n) r, cc_size xn, const cc_unit *cc_counted_by(xn) x, const cc_unit *cc_counted_by(n) m);

/*! @function ccn_mux_seed_mask
 *  @abstract Refreshes the internal state of the PRNG used to mask cmov/cswap
 *          operations with a given seed.
 *
 *  @discussion The seed should be of good entropy, i.e. generated by our default
 *            RNG. This function should be called before running algorithms that
 *            defend against side-channel attacks by using cmov/cswap. Examples
 *            are blinded modular exponentation (for RSA, DH, or MR) and EC
 *            scalar multiplication.
 *
 *  @param seed A seed value.
 */
void ccn_mux_seed_mask(cc_unit seed);

/*! @function ccn_divmod
 *  @abstract Computes a = q * d + r with r < d.
 *
 *  @param na  Length of a as a number of cc_units.
 *  @param a   The dividend a.
 *  @param nq  Length of q as a number of cc_units.
 *  @param q   The quotient q.
 *  @param n   Length of r and d as a number of cc_units.
 *  @param r   The remainder r.
 *  @param d   The divisor d.
 *
 *  @return 0 on success, non-zero on failure. See cc_error.h for more details.
 */
CC_NONNULL((2, 7)) CC_WARN_RESULT
int ccn_divmod(cc_size na, const cc_unit *cc_counted_by(na) a, cc_size nq, cc_unit *cc_counted_by(nq) q, cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) d);

CC_NONNULL((1, 3, 8))
void ccn_divmod_ws(cc_ws_t ws, cc_size na, const cc_unit *cc_counted_by(na) a, cc_size nq, cc_unit *cc_counted_by(nq) q, cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) d);

CC_NONNULL((2)) CC_SENTINEL
void ccn_zero_multi(cc_size n, cc_unit *r, ...);

CC_NONNULL((3, 4, 5))
cc_unit ccn_add_ws(cc_ws_t ws, cc_size count, cc_unit *r, const cc_unit *s, const cc_unit *t);

CC_NONNULL((3, 4, 5))
cc_unit ccn_sub_ws(cc_ws_t ws, cc_size count, cc_unit *r, const cc_unit *s, const cc_unit *t);

CC_NONNULL((3, 4))
cc_unit ccn_add1_ws(cc_ws_t ws, cc_size n, cc_unit *r, const cc_unit *s, cc_unit v);

/* s + t -> r return carry if result doesn't fit in n bits
 *  { N bit, NT bit -> N bit  NT <= N} N = n * sizeof(cc_unit) * 8 */
CC_NONNULL_ALL
cc_unit ccn_addn(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, cc_size nt, const cc_unit *cc_counted_by(nt) t);

/* s - v -> r return 1 iff v > s return 0 otherwise.
 *  { N bit, sizeof(cc_unit) * 8 bit -> N bit } N = n * sizeof(cc_unit) * 8 */
CC_NONNULL_ALL
cc_unit ccn_sub1(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, cc_unit v);

/* s - t -> r return 1 iff t > s
 *  { N bit, NT bit -> N bit  NT <= N} N = n * sizeof(cc_unit) * 8 */
CC_NONNULL_ALL
cc_unit ccn_subn(cc_size n, cc_unit *cc_counted_by(n) r, const cc_unit *cc_counted_by(n) s, cc_size nt, const cc_unit *cc_counted_by(nt) t);

/* Return the number of used units after stripping leading 0 units.  */
CC_NONNULL_ALL
cc_size ccn_n(cc_size n, const cc_unit *cc_counted_by(n)s);

/* Make a ccn of size ccn_nof(nbits) units with up to nbits sized random value. */
CC_NONNULL_ALL
int ccn_random_bits(cc_size nbits, cc_unit *cc_unsafe_indexable r, struct ccrng_state *rng);

/* Like ccn_random_bits, but uses ccrng_generate_fips under the hood. */
CC_NONNULL_ALL
int ccn_random_bits_fips(cc_size nbits, cc_unit *cc_unsafe_indexable r, struct ccrng_state *rng);

// Joint Sparse Form recoding context for EC double-scalar multiplication.
struct ccn_rjsf_state {
	uint8_t u[2];
	const cc_unit *s;
	const cc_unit *t;
};

/*! @function ccn_recode_jsf_init
 *  @abstract Initialize Joint Sparse Form recoding for EC scalars s and t.
 *
 *  @param r     JSF-recoding context.
 *  @param nbits Max. bit length of s and t.
 *  @param s     Scalar to be recoded.
 *  @param t     Scalar to be recoded.
 */
CC_NONNULL_ALL
void ccn_recode_jsf_init(struct ccn_rjsf_state *r, size_t nbits, const cc_unit *s, const cc_unit *t);

/*! @function ccn_recode_jsf_column
 *  @abstract Retrieve JSF-recoded digits for column k.
 *
 *  @param r JSF-recoding context.
 *  @param k Column index.
 *  @param c Digits (output).
 */
CC_NONNULL_ALL
void ccn_recode_jsf_column(struct ccn_rjsf_state *r, size_t k, int c[2]);

/*! @function ccn_recode_jsf_index
 *  @abstract Retrieve the lookup table index for given column digits.
 *
 *  @discussion For EC double-scalar multiplication, we assume a lookup table
 *            holding the four values [P, Q, P+Q, P-Q], in the same order.
 *
 *  @param c Column digits.
 *
 *  @return The lookup table index.
 */
CC_NONNULL_ALL CC_WARN_RESULT
size_t ccn_recode_jsf_index(int c[2]);

/*! @function ccn_recode_jsf_direction
 *  @abstract Retrieve the "direction" for given column digits.
 *
 *  @discussion For EC double-scalar multiplication, we assume a lookup table
 *            holding the four values [P, Q, P+Q, P-Q]. Negating each of
 *            these also yields [-P, -Q, -P-Q, -P+Q].
 *
 *            An EC double-and-add algorithm will either add or subtract a
 *            precomputed point to cover all possible digit combinations of two
 *            JSF-recoded EC scalars.
 *
 *  @param c Column digits.
 *
 *  @return The "direction". 1 for addition. -1 for subtraction.
 */
CC_NONNULL_ALL CC_WARN_RESULT
int ccn_recode_jsf_direction(int c[2]);

/*! @function ccn_read_le_bytes
 *  @abstract Copies a number given as little-endian bytes into `out`.
 *
 *  @param n   Number of limbs of `out`.
 *  @param in  Number to parse as little-endian bytes.
 *  @param out Output.
 */
CC_NONNULL_ALL
CC_INLINE void
ccn_read_le_bytes(cc_size n, const uint8_t *in, cc_unit *out)
{
	for (cc_size i = 0; i < n; i++) {
		out[i] = cc_load_le(&in[i * CCN_UNIT_SIZE]);
	}
}

/*! @function ccn_write_le_bytes
 *  @abstract Encodes a number as little-endian bytes into `out`.
 *
 *  @param n   Number of limbs of `in`.
 *  @param in  Number to encode as little-endian bytes.
 *  @param out Output.
 */
CC_NONNULL_ALL
CC_INLINE void
ccn_write_le_bytes(cc_size n, const cc_unit *in, uint8_t *out)
{
	for (cc_size i = 0; i < n; i++) {
		cc_store_le(in[i], &out[i * CCN_UNIT_SIZE]);
	}
}

/*! @function ccn_recode_ssw
 *  @abstract Recodes a given number into signed sliding windows.
 *
 *  @param n Number of limbs of `s`.
 *  @param s Number to recode.
 *  @param w Recode width, for windows in range (-2^w,2^w).
 *  @param r Output for the computed signed sliding windows.
 */
CC_NONNULL_ALL
void ccn_recode_ssw(cc_size n, const cc_unit *s, int w, int8_t *r);

#endif // _CORECRYPTO_CCN_INTERNAL_H