...

Source file src/crypto/internal/nistec/fiat/p224_invert.go

Documentation: crypto/internal/nistec/fiat

     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  
     5  // Code generated by addchain. DO NOT EDIT.
     6  
     7  package fiat
     8  
     9  // Invert sets e = 1/x, and returns e.
    10  //
    11  // If x == 0, Invert returns e = 0.
    12  func (e *P224Element) Invert(x *P224Element) *P224Element {
    13  	// Inversion is implemented as exponentiation with exponent p − 2.
    14  	// The sequence of 11 multiplications and 223 squarings is derived from the
    15  	// following addition chain generated with github.com/mmcloughlin/addchain v0.4.0.
    16  	//
    17  	//	_10     = 2*1
    18  	//	_11     = 1 + _10
    19  	//	_110    = 2*_11
    20  	//	_111    = 1 + _110
    21  	//	_111000 = _111 << 3
    22  	//	_111111 = _111 + _111000
    23  	//	x12     = _111111 << 6 + _111111
    24  	//	x14     = x12 << 2 + _11
    25  	//	x17     = x14 << 3 + _111
    26  	//	x31     = x17 << 14 + x14
    27  	//	x48     = x31 << 17 + x17
    28  	//	x96     = x48 << 48 + x48
    29  	//	x127    = x96 << 31 + x31
    30  	//	return    x127 << 97 + x96
    31  	//
    32  
    33  	var z = new(P224Element).Set(e)
    34  	var t0 = new(P224Element)
    35  	var t1 = new(P224Element)
    36  	var t2 = new(P224Element)
    37  
    38  	z.Square(x)
    39  	t0.Mul(x, z)
    40  	z.Square(t0)
    41  	z.Mul(x, z)
    42  	t1.Square(z)
    43  	for s := 1; s < 3; s++ {
    44  		t1.Square(t1)
    45  	}
    46  	t1.Mul(z, t1)
    47  	t2.Square(t1)
    48  	for s := 1; s < 6; s++ {
    49  		t2.Square(t2)
    50  	}
    51  	t1.Mul(t1, t2)
    52  	for s := 0; s < 2; s++ {
    53  		t1.Square(t1)
    54  	}
    55  	t0.Mul(t0, t1)
    56  	t1.Square(t0)
    57  	for s := 1; s < 3; s++ {
    58  		t1.Square(t1)
    59  	}
    60  	z.Mul(z, t1)
    61  	t1.Square(z)
    62  	for s := 1; s < 14; s++ {
    63  		t1.Square(t1)
    64  	}
    65  	t0.Mul(t0, t1)
    66  	t1.Square(t0)
    67  	for s := 1; s < 17; s++ {
    68  		t1.Square(t1)
    69  	}
    70  	z.Mul(z, t1)
    71  	t1.Square(z)
    72  	for s := 1; s < 48; s++ {
    73  		t1.Square(t1)
    74  	}
    75  	z.Mul(z, t1)
    76  	t1.Square(z)
    77  	for s := 1; s < 31; s++ {
    78  		t1.Square(t1)
    79  	}
    80  	t0.Mul(t0, t1)
    81  	for s := 0; s < 97; s++ {
    82  		t0.Square(t0)
    83  	}
    84  	z.Mul(z, t0)
    85  
    86  	return e.Set(z)
    87  }
    88  

View as plain text