forked from yvgny/MPC-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.go
More file actions
72 lines (57 loc) · 1.44 KB
/
vector.go
File metadata and controls
72 lines (57 loc) · 1.44 KB
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
package main
import (
"github.com/ldsec/lattigo/ring"
)
// Generate a random vector with each component lying in [0, T[
func newRandomVec(n, T uint64) []uint64 {
vec := make([]uint64, n)
t := ring.NewUint(T)
for i := range vec {
vec[i] = ring.RandInt(t).Uint64()
}
return vec
}
// Add two vectors component-wise and modulo T
func addVec(a, b []uint64, T uint64) []uint64 {
res := make([]uint64, len(a))
t := ring.NewUint(T)
for i := range res {
add := ring.NewUint(0)
add.Add(ring.NewUint(a[i]), ring.NewUint(b[i])).Mod(add, t)
res[i] = add.Uint64()
}
return res
}
// Subtract two vectors component-wise and modulo T
func subVec(a, b []uint64, T uint64) []uint64 {
res := make([]uint64, len(a))
t := ring.NewUint(T)
for i := range res {
sub := ring.NewUint(0)
sub.Sub(ring.NewUint(a[i]), ring.NewUint(b[i])).Mod(sub, t)
res[i] = sub.Uint64()
}
return res
}
// Multiply two vectors componement-wise and modulo T
func mulVec(a, b []uint64, T uint64) []uint64 {
res := make([]uint64, len(a))
t := ring.NewUint(T)
for i := range res {
mul := ring.NewUint(0)
mul.Mul(ring.NewUint(a[i]), ring.NewUint(b[i])).Mod(mul, t)
res[i] = mul.Uint64()
}
return res
}
// Negate each component of the vector, modulo T
func negVec(a []uint64, T uint64) []uint64 {
res := make([]uint64, len(a))
t := ring.NewUint(T)
for i := range res {
neg := ring.NewUint(a[i])
neg.Neg(neg).Mod(neg, t)
res[i] = neg.Uint64()
}
return res
}