forked from kimlaine/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utilities.cs
189 lines (175 loc) · 6.41 KB
/
Utilities.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using Microsoft.Research.SEAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace SEALNetExamples
{
public static class Utilities
{
/// <summary>
/// Helper function: Prints the name of the example in a fancy banner.
/// </summary>
public static void PrintExampleBanner(string title)
{
if (!string.IsNullOrEmpty(title))
{
int titleLength = title.Length;
int bannerLength = titleLength + 2 * 10;
string bannerTop = "+" + new string('-', bannerLength - 2) + "+";
string bannerMiddle =
"|" + new string(' ', 9) + title + new string(' ', 9) + "|";
Console.WriteLine();
Console.WriteLine(bannerTop);
Console.WriteLine(bannerMiddle);
Console.WriteLine(bannerTop);
}
}
/// <summary>
/// Helper function: Prints the parameters in a SEALContext.
/// </summary>
public static void PrintParameters(SEALContext context)
{
// Verify parameters
if (null == context)
{
throw new ArgumentNullException("context is not set");
}
SEALContext.ContextData contextData = context.KeyContextData;
/*
Which scheme are we using?
*/
string schemeName = null;
switch (contextData.Parms.Scheme)
{
case SchemeType.BFV:
schemeName = "BFV";
break;
case SchemeType.CKKS:
schemeName = "CKKS";
break;
default:
throw new ArgumentException("unsupported scheme");
}
Console.WriteLine("/");
Console.WriteLine("| Encryption parameters:");
Console.WriteLine($"| Scheme: {schemeName}");
Console.WriteLine("| PolyModulusDegree: {0}",
contextData.Parms.PolyModulusDegree);
/*
Print the size of the true (product) coefficient modulus.
*/
Console.Write("| CoeffModulus size: {0} (",
contextData.TotalCoeffModulusBitCount);
List<SmallModulus> coeffModulus =
(List<SmallModulus>)contextData.Parms.CoeffModulus;
for (int i = 0; i < coeffModulus.Count - 1; i++)
{
Console.Write($"{coeffModulus[i].BitCount} + ");
}
Console.WriteLine($"{coeffModulus.Last().BitCount}) bits");
/*
For the BFV scheme print the PlainModulus parameter.
*/
if (contextData.Parms.Scheme == SchemeType.BFV)
{
Console.WriteLine("| PlainModulus: {0}",
contextData.Parms.PlainModulus.Value);
}
Console.WriteLine("\\");
}
/// <summary>
/// Helper function: Print the first and last printSize elements
/// of a 2 row matrix.
/// </summary>
public static void PrintMatrix(IEnumerable<ulong> matrixPar,
int rowSize, int printSize = 5)
{
ulong[] matrix = matrixPar.ToArray();
Console.WriteLine();
/*
We're not going to print every column of the matrix (may be big). Instead
print printSize slots from beginning and end of the matrix.
*/
Console.Write(" [");
for (int i = 0; i < printSize; i++)
{
Console.Write("{0,3}, ", matrix[i]);
}
Console.Write(" ...");
for (int i = rowSize - printSize; i < rowSize; i++)
{
Console.Write(", {0,3}", matrix[i]);
}
Console.WriteLine(" ]");
Console.Write(" [");
for (int i = rowSize; i < rowSize + printSize; i++)
{
Console.Write("{0,3}, ", matrix[i]);
}
Console.Write(" ...");
for (int i = 2 * rowSize - printSize; i < 2 * rowSize; i++)
{
Console.Write(", {0,3}", matrix[i]);
}
Console.WriteLine(" ]");
Console.WriteLine();
}
/// <summary>
/// Helper function: Convert a ulong to a hex string representation
/// </summary>
public static string ULongToString(ulong value)
{
byte[] bytes = BitConverter.GetBytes(value);
return BitConverter.ToString(bytes).Replace("-", "");
}
/// <summary>
/// Helper function: Prints a vector of floating-point values.
/// </summary>
public static void PrintVector<T>(
IEnumerable<T> vec, int printSize = 4, int prec = 3)
{
string numFormat = string.Format("{{0:N{0}}}", prec);
T[] veca = vec.ToArray();
int slotCount = veca.Length;
Console.WriteLine();
if (slotCount <= 2 * printSize)
{
Console.Write(" [");
for (int i = 0; i < slotCount; i++)
{
Console.Write(" " + string.Format(numFormat, veca[i]));
if (i != (slotCount - 1))
Console.Write(",");
else
Console.Write(" ]");
}
Console.WriteLine();
}
else
{
Console.Write(" [");
for (int i = 0; i < printSize; i++)
{
Console.Write(" "+ string.Format(numFormat, veca[i]) + ", ");
}
if (veca.Length > 2 * printSize)
{
Console.Write(" ...");
}
for (int i = slotCount - printSize; i < slotCount; i++)
{
Console.Write(", " + string.Format(numFormat, veca[i]));
}
Console.WriteLine(" ]");
}
Console.WriteLine();
}
public static void PrintLine([CallerLineNumber] int lineNumber = 0)
{
Console.Write("Line {0,3} --> ", lineNumber);
}
}
}