-
Notifications
You must be signed in to change notification settings - Fork 162
/
element_ops_amd64.go
107 lines (93 loc) · 2.88 KB
/
element_ops_amd64.go
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
//go:build !purego
// +build !purego
// Copyright 2020 ConsenSys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package fp
//go:noescape
func MulBy3(x *Element)
//go:noescape
func MulBy5(x *Element)
//go:noescape
func MulBy13(x *Element)
//go:noescape
func mul(res, x, y *Element)
//go:noescape
func fromMont(res *Element)
//go:noescape
func reduce(res *Element)
// Butterfly sets
//
// a = a + b (mod q)
// b = a - b (mod q)
//
//go:noescape
func Butterfly(a, b *Element)
// Mul z = x * y (mod q)
//
// x and y must be less than q
func (z *Element) Mul(x, y *Element) *Element {
// Implements CIOS multiplication -- section 2.3.2 of Tolga Acar's thesis
// https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
//
// The algorithm:
//
// for i=0 to N-1
// C := 0
// for j=0 to N-1
// (C,t[j]) := t[j] + x[j]*y[i] + C
// (t[N+1],t[N]) := t[N] + C
//
// C := 0
// m := t[0]*q'[0] mod D
// (C,_) := t[0] + m*q[0]
// for j=1 to N-1
// (C,t[j-1]) := t[j] + m*q[j] + C
//
// (C,t[N-1]) := t[N] + C
// t[N] := t[N+1] + C
//
// → N is the number of machine words needed to store the modulus q
// → D is the word size. For example, on a 64-bit architecture D is 2 64
// → x[i], y[i], q[i] is the ith word of the numbers x,y,q
// → q'[0] is the lowest word of the number -q⁻¹ mod r. This quantity is pre-computed, as it does not depend on the inputs.
// → t is a temporary array of size N+2
// → C, S are machine words. A pair (C,S) refers to (hi-bits, lo-bits) of a two-word number
//
// As described here https://hackmd.io/@gnark/modular_multiplication we can get rid of one carry chain and simplify:
// (also described in https://eprint.iacr.org/2022/1400.pdf annex)
//
// for i=0 to N-1
// (A,t[0]) := t[0] + x[0]*y[i]
// m := t[0]*q'[0] mod W
// C,_ := t[0] + m*q[0]
// for j=1 to N-1
// (A,t[j]) := t[j] + x[j]*y[i] + A
// (C,t[j-1]) := t[j] + m*q[j] + C
//
// t[N-1] = C + A
//
// This optimization saves 5N + 2 additions in the algorithm, and can be used whenever the highest bit
// of the modulus is zero (and not all of the remaining bits are set).
mul(z, x, y)
return z
}
// Square z = x * x (mod q)
//
// x must be less than q
func (z *Element) Square(x *Element) *Element {
// see Mul for doc.
mul(z, x, x)
return z
}