github.com/emmansun/gmsm@v0.29.1/internal/sm2ec/fiat/sm2p256scalar_invert.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  // Code generated by addchain. DO NOT EDIT.
     5  package fiat
     6  
     7  // Invert sets e = 1/x, and returns e.
     8  //
     9  // If x == 0, Invert returns e = 0.
    10  func (e *SM2P256OrderElement) Invert(x *SM2P256OrderElement) *SM2P256OrderElement {
    11  	// Inversion is implemented as exponentiation with exponent p − 2.
    12  	// The sequence of 41 multiplications and 253 squarings is derived from the
    13  	// following addition chain generated with github.com/mmcloughlin/addchain v0.4.0.
    14  	//
    15  	//	_10      = 2*1
    16  	//	_11      = 1 + _10
    17  	//	_100     = 1 + _11
    18  	//	_101     = 1 + _100
    19  	//	_111     = _10 + _101
    20  	//	_1001    = _10 + _111
    21  	//	_1101    = _100 + _1001
    22  	//	_1111    = _10 + _1101
    23  	//	_11110   = 2*_1111
    24  	//	_11111   = 1 + _11110
    25  	//	_111110  = 2*_11111
    26  	//	_111111  = 1 + _111110
    27  	//	_1111110 = 2*_111111
    28  	//	i20      = _1111110 << 6 + _1111110
    29  	//	x18      = i20 << 5 + _111111
    30  	//	x31      = x18 << 13 + i20 + 1
    31  	//	i42      = 2*x31
    32  	//	i44      = i42 << 2
    33  	//	i140     = ((i44 << 32 + i44) << 29 + i42) << 33
    34  	//	i150     = ((i44 + i140 + _111) << 4 + _111) << 3
    35  	//	i170     = ((1 + i150) << 11 + _1111) << 6 + _11111
    36  	//	i183     = ((i170 << 5 + _1101) << 3 + _11) << 3
    37  	//	i198     = ((1 + i183) << 7 + _111) << 5 + _11
    38  	//	i219     = ((i198 << 9 + _101) << 5 + _101) << 5
    39  	//	i231     = ((_1101 + i219) << 5 + _1001) << 4 + _1101
    40  	//	i244     = ((i231 << 2 + _11) << 7 + _111111) << 2
    41  	//	i262     = ((1 + i244) << 10 + _1001) << 5 + _111
    42  	//	i277     = ((i262 << 5 + _111) << 4 + _101) << 4
    43  	//	return     ((_101 + i277) << 9 + _1001) << 5 + 1
    44  	//
    45  	var z = new(SM2P256OrderElement).Set(e)
    46  	var t0 = new(SM2P256OrderElement)
    47  	var t1 = new(SM2P256OrderElement)
    48  	var t2 = new(SM2P256OrderElement)
    49  	var t3 = new(SM2P256OrderElement)
    50  	var t4 = new(SM2P256OrderElement)
    51  	var t5 = new(SM2P256OrderElement)
    52  	var t6 = new(SM2P256OrderElement)
    53  	var t7 = new(SM2P256OrderElement)
    54  	var t8 = new(SM2P256OrderElement)
    55  	var t9 = new(SM2P256OrderElement)
    56  
    57  	t2.Square(x)
    58  	t3.Mul(x, t2)
    59  	t4.Mul(x, t3)
    60  	t0.Mul(x, t4)
    61  	t1.Mul(t2, t0)
    62  	z.Mul(t2, t1)
    63  	t4.Mul(t4, z)
    64  	t6.Mul(t2, t4)
    65  	t2.Square(t6)
    66  	t5.Mul(x, t2)
    67  	t2.Square(t5)
    68  	t2.Mul(x, t2)
    69  	t7.Square(t2)
    70  	t8.Square(t7)
    71  	for s := 1; s < 6; s++ {
    72  		t8.Square(t8)
    73  	}
    74  	t7.Mul(t7, t8)
    75  	t8.Square(t7)
    76  	for s := 1; s < 5; s++ {
    77  		t8.Square(t8)
    78  	}
    79  	t8.Mul(t2, t8)
    80  	for s := 0; s < 13; s++ {
    81  		t8.Square(t8)
    82  	}
    83  	t7.Mul(t7, t8)
    84  	t7.Mul(x, t7)
    85  	t8.Square(t7)
    86  	t7.Square(t8)
    87  	for s := 1; s < 2; s++ {
    88  		t7.Square(t7)
    89  	}
    90  	t9.Square(t7)
    91  	for s := 1; s < 32; s++ {
    92  		t9.Square(t9)
    93  	}
    94  	t9.Mul(t7, t9)
    95  	for s := 0; s < 29; s++ {
    96  		t9.Square(t9)
    97  	}
    98  	t8.Mul(t8, t9)
    99  	for s := 0; s < 33; s++ {
   100  		t8.Square(t8)
   101  	}
   102  	t7.Mul(t7, t8)
   103  	t7.Mul(t1, t7)
   104  	for s := 0; s < 4; s++ {
   105  		t7.Square(t7)
   106  	}
   107  	t7.Mul(t1, t7)
   108  	for s := 0; s < 3; s++ {
   109  		t7.Square(t7)
   110  	}
   111  	t7.Mul(x, t7)
   112  	for s := 0; s < 11; s++ {
   113  		t7.Square(t7)
   114  	}
   115  	t6.Mul(t6, t7)
   116  	for s := 0; s < 6; s++ {
   117  		t6.Square(t6)
   118  	}
   119  	t5.Mul(t5, t6)
   120  	for s := 0; s < 5; s++ {
   121  		t5.Square(t5)
   122  	}
   123  	t5.Mul(t4, t5)
   124  	for s := 0; s < 3; s++ {
   125  		t5.Square(t5)
   126  	}
   127  	t5.Mul(t3, t5)
   128  	for s := 0; s < 3; s++ {
   129  		t5.Square(t5)
   130  	}
   131  	t5.Mul(x, t5)
   132  	for s := 0; s < 7; s++ {
   133  		t5.Square(t5)
   134  	}
   135  	t5.Mul(t1, t5)
   136  	for s := 0; s < 5; s++ {
   137  		t5.Square(t5)
   138  	}
   139  	t5.Mul(t3, t5)
   140  	for s := 0; s < 9; s++ {
   141  		t5.Square(t5)
   142  	}
   143  	t5.Mul(t0, t5)
   144  	for s := 0; s < 5; s++ {
   145  		t5.Square(t5)
   146  	}
   147  	t5.Mul(t0, t5)
   148  	for s := 0; s < 5; s++ {
   149  		t5.Square(t5)
   150  	}
   151  	t5.Mul(t4, t5)
   152  	for s := 0; s < 5; s++ {
   153  		t5.Square(t5)
   154  	}
   155  	t5.Mul(z, t5)
   156  	for s := 0; s < 4; s++ {
   157  		t5.Square(t5)
   158  	}
   159  	t4.Mul(t4, t5)
   160  	for s := 0; s < 2; s++ {
   161  		t4.Square(t4)
   162  	}
   163  	t3.Mul(t3, t4)
   164  	for s := 0; s < 7; s++ {
   165  		t3.Square(t3)
   166  	}
   167  	t2.Mul(t2, t3)
   168  	for s := 0; s < 2; s++ {
   169  		t2.Square(t2)
   170  	}
   171  	t2.Mul(x, t2)
   172  	for s := 0; s < 10; s++ {
   173  		t2.Square(t2)
   174  	}
   175  	t2.Mul(z, t2)
   176  	for s := 0; s < 5; s++ {
   177  		t2.Square(t2)
   178  	}
   179  	t2.Mul(t1, t2)
   180  	for s := 0; s < 5; s++ {
   181  		t2.Square(t2)
   182  	}
   183  	t1.Mul(t1, t2)
   184  	for s := 0; s < 4; s++ {
   185  		t1.Square(t1)
   186  	}
   187  	t1.Mul(t0, t1)
   188  	for s := 0; s < 4; s++ {
   189  		t1.Square(t1)
   190  	}
   191  	t0.Mul(t0, t1)
   192  	for s := 0; s < 9; s++ {
   193  		t0.Square(t0)
   194  	}
   195  	z.Mul(z, t0)
   196  	for s := 0; s < 5; s++ {
   197  		z.Square(z)
   198  	}
   199  	z.Mul(x, z)
   200  	return e.Set(z)
   201  }