masked_pyjamask_asm.S 26.4 KB
Newer Older
lwc-tester committed
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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
/*
  ===============================================================================

 Copyright (c) 2019, CryptoExperts and PQShield Ltd.
 
 All rights reserved. A copyright license for redistribution and use in
 source and binary forms, with or without modification, is hereby granted for
 non-commercial, experimental, research, public review and evaluation
 purposes, provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

  Authors: Dahmun Goudarzi, Matthieu Rivain

  ===============================================================================
*/

.text
.syntax unified

//==============================================================================
//=== Includes and definitions
//==============================================================================

#include "param.h"

#define SS96  3  // state size for Pyjamask-96
#define SS128 4  // state size for Pyjamask-128

	
//==============================================================================
//=== get_random_32 
//==============================================================================


// Macro random_init: 
// initialise rng_add to STM32F4 RNG address

.macro random_init rng_add
#ifdef FAST
    LDR \rng_add, =addr_rng  
#endif
.endm

// Macro get_random_32: 
// Get 32-bit random values from STM32F4 TRNG
// 2 options: 
//    - POOLING: checks the availability of fresh randomness then read it
//    - FAST:    simply reads the RNG

.macro get_random_32 rng_add, rnd


#ifdef FAST
    LDR \rnd, [\rng_add]	
#elif POOLING
    push {R9}
    BL get_random_32_pulling
    MOV \rnd, R9
    pop {R9}
	
#endif

.endm

// RNG_MODE = POOLING   	
#ifdef POOLING

pull_random:
	push {LR}

	ANDS	R8, R3, R4
	BNE	enddata

	ORR	R8, R5, R6
	ANDS	R8, R8, R3
	BEQ	enderrors

	LDR	R9, =addr_rng  
	LDR	R9, [R9]
	MOV	R7, #1
	
enddata:
enderrors:	
	
	pop {LR}
	BX LR
	
get_random_32_pulling:
	push {R1-R8, LR}

	LDR	R1, =MY_RNG_CR
	MOV	R2, #MY_RNG_CR_RNGEN
	
	LDR	R3, =MY_RNG_SR
	MOV	R4, #MY_RNG_SR_DRDY
	MOV	R5, #MY_RNG_SR_CECS
	MOV	R6, #MY_RNG_SR_SECS
	
	MOV	R7, #0
while_pulling:

	ANDS	R8, R3, R6
	IT	ne
	BICNE	R1, R1, R2
	IT	ne
	ORRNE	R1, R1, R2

	
	BL pull_random
	
	CMP	R7, #1
	BNE	while_pulling
	
	pop {R1-R8, LR}
	BX LR

#endif


	
//==============================================================================
//=== Function mat_mult
//==============================================================================

// -----------------------------------------------------------------------------
// Prototype: uint32_t mat_mult(uint32_t mat_col, uint32_t vec)
// -----------------------------------------------------------------------------
// Computes a matrix vector multiplication between a 32x32 circulant binary
// and a 32-bit vector
// -----------------------------------------------------------------------------
// Inputs:  
//   R0: mat_col (matrix column)
//   R1: vec (vector to be multiplied)
// Output: 
//   R0: product
// -----------------------------------------------------------------------------

// Macro acc_col: 
// accumulate the i-th column (which is mat_col rotated by i positions) 
// i.e. add the i-th column to the accumulator if the i-th bit of vec 
// equals 1 and add 0 otherwise
	
.macro acc_col acc, mat_col, vec, tmp, i
	
	// tmp <- ith bit of vec (into the left most bit i.e. sign bit)
	LSL	\tmp, \vec, #\i	 
	
	// tmp <- mat_col AND mask 
	// (where mask = 0 if sign bit = 0 / mask = 0xffffffff if sign bit = 1)
	AND	\tmp, \mat_col, \tmp, ASR #32
	
	// acc <- acc XOR (tmp <<< i)
	EOR	\acc, \acc, \tmp, ROR #\i

.endm
	
// Function mat_mult:
	
.global mat_mult
.type mat_mult, %function
	
mat_mult:
	push {LR}
	// Input:  R0 = Matrix Column M[i]
	// 	   R1 = State[i]
	// Output: R0 = Acc

	MOV	R2, #0					// Init accumulator register
	MOV	R3, #0					// Init temporary register
	
	acc_col R2, R0, R1, R3, 0 
	acc_col R2, R0, R1, R3, 1 
	acc_col R2, R0, R1, R3, 2 
	acc_col R2, R0, R1, R3, 3 
	acc_col R2, R0, R1, R3, 4 
	acc_col R2, R0, R1, R3, 5 
	acc_col R2, R0, R1, R3, 6 
	acc_col R2, R0, R1, R3, 7 
	acc_col R2, R0, R1, R3, 8 
	acc_col R2, R0, R1, R3, 9 
	acc_col R2, R0, R1, R3, 10
	acc_col R2, R0, R1, R3, 11
	acc_col R2, R0, R1, R3, 12
	acc_col R2, R0, R1, R3, 13
	acc_col R2, R0, R1, R3, 14
	acc_col R2, R0, R1, R3, 15
	acc_col R2, R0, R1, R3, 16
	acc_col R2, R0, R1, R3, 17
	acc_col R2, R0, R1, R3, 18
	acc_col R2, R0, R1, R3, 19
	acc_col R2, R0, R1, R3, 20
	acc_col R2, R0, R1, R3, 21
	acc_col R2, R0, R1, R3, 22
	acc_col R2, R0, R1, R3, 23
	acc_col R2, R0, R1, R3, 24
	acc_col R2, R0, R1, R3, 25
	acc_col R2, R0, R1, R3, 26
	acc_col R2, R0, R1, R3, 27
	acc_col R2, R0, R1, R3, 28
	acc_col R2, R0, R1, R3, 29
	acc_col R2, R0, R1, R3, 30
	acc_col R2, R0, R1, R3, 31

	MOV	R0, R2

	pop {LR}
	BX LR



//==============================================================================
//=== Functions   isw_macc_96_XYZ   with XYZ in {201, 012, 102}
//===             isw_macc_128_XYZ  with XYZ in {301, 012, 123, 203}
//==============================================================================

// -----------------------------------------------------------------------------
// Prototypes: 
//       void isw_macc_96_XYZ  (uint32_t state[MASKING_ORDER][3])
//       void isw_macc_128_XYZ (uint32_t state[MASKING_ORDER][4])
// -----------------------------------------------------------------------------
// Compute an ISW multiply-and-accumulate (MACC):
//    - compute 32-AND between the state rows of index Y and Z
//    - accumulate the product in the state row of index X
// (these computations are done by processing the shares of the state rows)
// -----------------------------------------------------------------------------
// Inputs:  
//   R0: state
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Notations:
//
//    - accumulator: c shared as (c_0, ..., c_MASKING_ORDER-1)
//    - operand 1:   a shared as (a_0, ..., a_MASKING_ORDER-1)
//    - operand 2:   b shared as (b_0, ..., b_MASKING_ORDER-1)
//
// The function computes c = c XOR (a AND b) by processing the shares 
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Register usage:
//
//   R0:       state address
//   R1-R3:    a_i, b_i, c_i
//   R4-R6:    a_i+1, b_i+1, c_i+1
//   R7-R9:    a_j, b_j, c_j
//   R10:      temporary register
//   R11:      loop counter j
//   R12:      loop counter i / random 
//   R14:      rng address
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Common macros
// -----------------------------------------------------------------------------

.macro isw_loop_iteration ai0, bi0, ci0, ai1, bi1, ci1

    AND     R10, \ai0, \bi0         // R10 = a_i AND b_i
    EOR     \ci0, \ci0, R10         // c_i = c_i XOR (a_i AND b_i)

    get_random_32 R14, R7           // R7 = r (random)

    EOR     \ci0, \ci0, R7          // c_i = c_i XOR r

    AND     R10,  \ai0, \bi1        // R10 = a_i AND b_i+1
    EOR     R10,  R10,  R7          // R10 = (a_i AND b_i+1) XOR r
    EOR     \ci1, \ci1, R10         // c_i+1 = c_i+1 XOR ((a_i AND b_i+1) XOR r)

    AND     R10,  \bi0, \ai1        // R10 = a_i+1 AND b_i
    EOR     \ci1, \ci1,  R10        // c_i+1 = c_i+1 XOR (a_i+1 AND b_i)    

    AND     R10,  \ai1, \bi1        // R10 = a_i+1 AND b_i+1
    EOR     \ci1, \ci1, R10         // c_i+1 = c_i+1 XOR (a_i+1 AND b_i+1)    

.endm

// -----------------------------------------------------------------------------

.macro isw_inner_loop_iteration ai0, bi0, ci0, ai1, bi1, ci1, aj, bj, cj

    // process pair (i, j) 

    get_random_32 R14, R12          // R12 = r (random)

    EOR     \ci0, \ci0, R12         // c_i = c_i XOR r

    AND     R10, \ai0, \bj          // R10 = a_i AND b_j
    EOR     R10, R10,  R12          // R10 = (a_i AND b_j) XOR r
    EOR     \cj, \cj,  R10          // c_j = c_j XOR ((a_i AND b_j) XOR r)

    AND     R10, \aj, \bi0          // R10 = a_j AND b_i
    EOR     \cj, \cj, R10           // c_j = c_j XOR (a_j AND b_i)

    // process pair (i+1, j) 

    get_random_32 R14, R12          // R12 = r (random)
    
    EOR     \ci1, \ci1, R12         // c_i+1 = c_i+1 XOR r

    AND     R10, \ai1, \bj          // R10 = a_i+1 AND b_j
    EOR     R10, R10,  R12          // R10 = (a_i+1 AND b_j) XOR r
    EOR     \cj, \cj,  R10          // c_j = c_j XOR ((a_i+1 AND b_j) XOR r)

    AND     R10, \aj, \bi1          // R10 = a_j AND b_i+1
    EOR     \cj, \cj, R10           // c_j = c_j XOR (a_j AND b_i+1)    

.endm

// -----------------------------------------------------------------------------

.macro isw_load_shares_96

    ADD     R10, R0, R12, LSL #2        // pointer to state[i] 
    LDM     R10, {R1,R2,R3,R4,R5,R6}    // load state[i] and state[i+1] 

.endm

// -----------------------------------------------------------------------------

.macro isw_inner_load_shares_96

    ADD     R10, R0, R11, LSL #2        // pointer to state[j]
    LDM     R10, {R7,R8,R9}             // load state[j]


.endm

// -----------------------------------------------------------------------------

.macro isw_store_shares_96 ci, cip1, shift

    ADD     R10, R0, R12, LSL #2              // pointer to state[i]
    STR     \ci,   [R10, #(4*\shift)]         // store c_i
    STR     \cip1, [R10, #(4*SS96+4*\shift)]  // store c_i+1

.endm

// -----------------------------------------------------------------------------

.macro isw_inner_store_shares_96 cj, shift
    
    ADD     R10, R0, R11, LSL #2       // pointer to state[j]
    STR     \cj, [R10, #(4*\shift)]     // store c_j

.endm

// -----------------------------------------------------------------------------

.macro isw_load_shares_128 cshift, ashift, bshift

    ADD     R10, R0, R12, LSL #2       // pointer to state[i] 

    LDR     R1, [R10, #(4*\ashift)]    // load a_i
    LDR     R2, [R10, #(4*\bshift)]    // load b_i
    LDR     R3, [R10, #(4*\cshift)]    // load c_i

    LDR     R4, [R10, #(4*SS128+4*\ashift)]    // load a_i+1
    LDR     R5, [R10, #(4*SS128+4*\bshift)]    // load b_i+1
    LDR     R6, [R10, #(4*SS128+4*\cshift)]    // load c_i+1

.endm

// -----------------------------------------------------------------------------

.macro isw_inner_load_shares_128 cshift, ashift, bshift

    ADD     R10, R0, R11, LSL #2        // pointer to state[j]
   
    LDR     R7, [R10, #(4*\ashift)]    // load a_j
    LDR     R8, [R10, #(4*\bshift)]    // load b_j
    LDR     R9, [R10, #(4*\cshift)]    // load c_j

.endm

// -----------------------------------------------------------------------------

.macro isw_store_shares_128 cshift

    ADD     R10, R0, R12, LSL #2              // pointer to state[i]
    STR     R3, [R10, #(4*\cshift)]           // store c_i
    STR     R6, [R10, #(4*SS128+4*\cshift)]    // store c_i+1

.endm

// -----------------------------------------------------------------------------

.macro isw_inner_store_shares_128 cshift
    
    ADD     R10, R0, R11, LSL #2        // pointer to state[j]
    STR     R9, [R10, #(4*\cshift)]     // store c_j

.endm

// -----------------------------------------------------------------------------
// isw_macc_96_201
// -----------------------------------------------------------------------------

.global isw_macc_96_201
.align 4

isw_macc_96_201:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_96_201:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_96 

        // process iteration (i,i+1)                 
        isw_loop_iteration R1, R2, R3, R4, R5, R6     

        ADD     R11, R12, #(2*SS96)      // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_96_201:

            // load a_j, b_j, c_j
            isw_inner_load_shares_96         

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R1, R2, R3, R4, R5, R6, R7, R8, R9
            
            // store c_j
            isw_inner_store_shares_96 R9, 2
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS96             
            CMP     R11, #(SS96*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_96_201
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_96 R3, R6, 2

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS96)
        CMP     R12, #(SS96*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_96_201
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_96 

    // process iteration (i,i+1)                 
    isw_loop_iteration R1, R2, R3, R4, R5, R6   
           
    // store c_i, c_i+1
    isw_store_shares_96 R3, R6, 2  
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_96_201

// -----------------------------------------------------------------------------
// isw_macc_96_012
// -----------------------------------------------------------------------------

.global isw_macc_96_012
.align 4

isw_macc_96_012:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_96_012:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_96 

        // process iteration (i,i+1)                 
        isw_loop_iteration R2, R3, R1, R5, R6, R4

        ADD     R11, R12, #(2*SS96)      // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_96_012:

            // load a_j, b_j, c_j
            isw_inner_load_shares_96         

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R2, R3, R1, R5, R6, R4, R8, R9, R7
            
            // store c_j
            isw_inner_store_shares_96 R7, 0
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS96             
            CMP     R11, #(SS96*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_96_012
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_96 R1, R4, 0

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS96)
        CMP     R12, #(SS96*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_96_012
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_96 

    // process iteration (i,i+1)                 
    isw_loop_iteration R2, R3, R1, R5, R6, R4
           
    // store c_i, c_i+1
    isw_store_shares_96 R1, R4, 0  
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_96_012

// -----------------------------------------------------------------------------
// isw_macc_96_102
// -----------------------------------------------------------------------------

.global isw_macc_96_102
.align 4

isw_macc_96_102:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_96_102:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_96 

        // process iteration (i,i+1)                 
        isw_loop_iteration R3, R1, R2, R6, R4, R5

        ADD     R11, R12, #(2*SS96)      // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_96_102:

            // load a_j, b_j, c_j
            isw_inner_load_shares_96         

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R3, R1, R2, R6, R4, R5, R9, R7, R8
            
            // store c_j
            isw_inner_store_shares_96 R8, 1
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS96             
            CMP     R11, #(SS96*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_96_102
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_96 R2, R5, 1

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS96)
        CMP     R12, #(SS96*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_96_102
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_96 

    // process iteration (i,i+1)                 
    isw_loop_iteration R3, R1, R2, R6, R4, R5
           
    // store c_i, c_i+1
    isw_store_shares_96 R2, R5, 1
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_96_102

// -----------------------------------------------------------------------------
// isw_macc_128_301
// -----------------------------------------------------------------------------

.global isw_macc_128_301
.align 4

isw_macc_128_301:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_128_301:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_128 3, 0, 1

        // process iteration (i,i+1)                 
        isw_loop_iteration R1, R2, R3, R4, R5, R6

        ADD     R11, R12, #(2*SS128)     // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_128_301:

            // load a_j, b_j, c_j
            isw_inner_load_shares_128 3, 0, 1

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R1, R2, R3, R4, R5, R6, R7, R8, R9
            
            // store c_j
            isw_inner_store_shares_128 3
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS128             
            CMP     R11, #(SS128*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_128_301
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_128 3

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS128)
        CMP     R12, #(SS128*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_128_301
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_128 3, 0, 1

    // process iteration (i,i+1)                 
    isw_loop_iteration R1, R2, R3, R4, R5, R6   
           
    // store c_i, c_i+1
    isw_store_shares_128 3
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_128_301

// -----------------------------------------------------------------------------
// isw_macc_128_012
// -----------------------------------------------------------------------------

.global isw_macc_128_012
.align 4

isw_macc_128_012:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_128_012:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_128 0, 1, 2

        // process iteration (i,i+1)                 
        isw_loop_iteration R1, R2, R3, R4, R5, R6

        ADD     R11, R12, #(2*SS128)     // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_128_012:

            // load a_j, b_j, c_j
            isw_inner_load_shares_128 0, 1, 2

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R1, R2, R3, R4, R5, R6, R7, R8, R9
            
            // store c_j
            isw_inner_store_shares_128 0
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS128             
            CMP     R11, #(SS128*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_128_012
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_128 0

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS128)
        CMP     R12, #(SS128*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_128_012
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_128 0, 1, 2

    // process iteration (i,i+1)                 
    isw_loop_iteration R1, R2, R3, R4, R5, R6   
           
    // store c_i, c_i+1
    isw_store_shares_128 0
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_128_012

// -----------------------------------------------------------------------------
// isw_macc_128_123
// -----------------------------------------------------------------------------

.global isw_macc_128_123
.align 4

isw_macc_128_123:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_128_123:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_128 1, 2, 3

        // process iteration (i,i+1)                 
        isw_loop_iteration R1, R2, R3, R4, R5, R6

        ADD     R11, R12, #(2*SS128)     // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_128_123:

            // load a_j, b_j, c_j
            isw_inner_load_shares_128 1, 2, 3

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R1, R2, R3, R4, R5, R6, R7, R8, R9
            
            // store c_j
            isw_inner_store_shares_128 1
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS128             
            CMP     R11, #(SS128*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_128_123
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_128 1

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS128)
        CMP     R12, #(SS128*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_128_123
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_128 1, 2, 3

    // process iteration (i,i+1)                 
    isw_loop_iteration R1, R2, R3, R4, R5, R6   
           
    // store c_i, c_i+1
    isw_store_shares_128 1
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_128_123

// -----------------------------------------------------------------------------
// isw_macc_128_203
// -----------------------------------------------------------------------------

.global isw_macc_128_203
.align 4

isw_macc_128_203:

    PUSH    {R4-R12, LR} 

    random_init R14               // R14 = RNG address

    MOV     R12, #0               // R12 = loop index i (x3)

____loop_isw_macc_128_203:

        // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
        isw_load_shares_128 2, 0, 3

        // process iteration (i,i+1)                 
        isw_loop_iteration R1, R2, R3, R4, R5, R6

        ADD     R11, R12, #(2*SS128)     // R11 = inner loop index j (x3)
        PUSH    {R12}                    // save R12 on stack

________inner_loop_isw_macc_128_203:

            // load a_j, b_j, c_j
            isw_inner_load_shares_128 2, 0, 3

            // process iterations (i,j) and (i+1,j)
            isw_inner_loop_iteration R1, R2, R3, R4, R5, R6, R7, R8, R9
            
            // store c_j
            isw_inner_store_shares_128 2
           
            //------------------------- loop increment and test
            ADD     R11, R11, #SS128             
            CMP     R11, #(SS128*MASKING_ORDER)
            BNE     ________inner_loop_isw_macc_128_203
            //---------------------------------------- end loop

        POP     {R12}             // restore R12 from stack
        
        // store c_i, c_i+1
        isw_store_shares_128 2

        //------------------------- loop increment and test
        ADD     R12, R12, #(2*SS128)
        CMP     R12, #(SS128*(MASKING_ORDER-2))
        BNE     ____loop_isw_macc_128_203
        //---------------------------------------- end loop


    // last iteration (without inner loop)
   
    // load a_i, b_i, c_i, a_i+1, b_i+1, c_i+1
    isw_load_shares_128 2, 0, 3

    // process iteration (i,i+1)                 
    isw_loop_iteration R1, R2, R3, R4, R5, R6   
           
    // store c_i, c_i+1
    isw_store_shares_128 2
    
    POP     {R4-R12, LR}
    BX      LR

// end isw_macc_128_203