1
2
3
4
5
6
7 package nistec
8
9 import "errors"
10
11
12
13
14 func p256OrdMul(res, in1, in2 *p256OrdElement)
15
16
17
18
19 func p256OrdSqr(res, in *p256OrdElement, n int)
20
21 func P256OrdInverse(k []byte) ([]byte, error) {
22 if len(k) != 32 {
23 return nil, errors.New("invalid scalar length")
24 }
25
26 x := new(p256OrdElement)
27 p256OrdBigToLittle(x, (*[32]byte)(k))
28 p256OrdReduce(x)
29
30
31
32
33
34 _1 := new(p256OrdElement)
35 _11 := new(p256OrdElement)
36 _101 := new(p256OrdElement)
37 _111 := new(p256OrdElement)
38 _1111 := new(p256OrdElement)
39 _10101 := new(p256OrdElement)
40 _101111 := new(p256OrdElement)
41 t := new(p256OrdElement)
42
43
44
45
46
47
48 RR := &p256OrdElement{0x83244c95be79eea2, 0x4699799c49bd6fa6,
49 0x2845b2392b6bec59, 0x66e12d94f3d95620}
50
51 p256OrdMul(_1, x, RR)
52 p256OrdSqr(x, _1, 1)
53 p256OrdMul(_11, x, _1)
54 p256OrdMul(_101, x, _11)
55 p256OrdMul(_111, x, _101)
56 p256OrdSqr(x, _101, 1)
57 p256OrdMul(_1111, _101, x)
58
59 p256OrdSqr(t, x, 1)
60 p256OrdMul(_10101, t, _1)
61 p256OrdSqr(x, _10101, 1)
62 p256OrdMul(_101111, _101, x)
63 p256OrdMul(x, _10101, x)
64 p256OrdSqr(t, x, 2)
65 p256OrdMul(t, t, _11)
66 p256OrdSqr(x, t, 8)
67 p256OrdMul(x, x, t)
68 p256OrdSqr(t, x, 16)
69 p256OrdMul(t, t, x)
70
71 p256OrdSqr(x, t, 64)
72 p256OrdMul(x, x, t)
73 p256OrdSqr(x, x, 32)
74 p256OrdMul(x, x, t)
75
76 sqrs := []int{
77 6, 5, 4, 5, 5,
78 4, 3, 3, 5, 9,
79 6, 2, 5, 6, 5,
80 4, 5, 5, 3, 10,
81 2, 5, 5, 3, 7, 6}
82 muls := []*p256OrdElement{
83 _101111, _111, _11, _1111, _10101,
84 _101, _101, _101, _111, _101111,
85 _1111, _1, _1, _1111, _111,
86 _111, _111, _101, _11, _101111,
87 _11, _11, _11, _1, _10101, _1111}
88
89 for i, s := range sqrs {
90 p256OrdSqr(x, x, s)
91 p256OrdMul(x, x, muls[i])
92 }
93
94
95
96 one := &p256OrdElement{1}
97 p256OrdMul(x, x, one)
98
99 var xOut [32]byte
100 p256OrdLittleToBig(&xOut, x)
101 return xOut[:], nil
102 }
103
View as plain text