-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFhevmKeys.cs
More file actions
163 lines (131 loc) · 4.86 KB
/
FhevmKeys.cs
File metadata and controls
163 lines (131 loc) · 4.86 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
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
using Fhe;
using FhevmSDK.Tools;
using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace FhevmSDK;
public sealed class CompactPublicKeyInfo
{
public required CompactPublicKey PublicKey { get; init; }
public required string PublicKeyId { get; init; }
}
public sealed class PublicParamsInfo
{
public required CompactPkeCrs PublicParams { get; init; }
public required string PublicParamsId { get; init; }
}
public sealed class FhevmKeys : IDisposable
{
private readonly ConcurrentDictionary<string, Keys> keyurlCache = new();
public class Keys : IDisposable
{
public required CompactPublicKeyInfo CompactPublicKeyInfo { get; init; }
// 2048
public required PublicParamsInfo PublicParamsInfo { get; init; }
public void Dispose()
{
CompactPublicKeyInfo.PublicKey.Dispose();
PublicParamsInfo.PublicParams.Dispose();
}
}
private static class Json
{
public class FhePublicKey
{
[JsonPropertyName("data_id")]
public required string DataId { get; set; }
[JsonPropertyName("urls")]
public required string[] Urls { get; set; }
}
public class FheKeyInfo
{
[JsonPropertyName("fhe_public_key")]
public required FhePublicKey FhePublicKey { get; set; }
}
public class Response
{
[JsonPropertyName("fhe_key_info")]
public required FheKeyInfo[] FheKeyInfo { get; set; }
[JsonPropertyName("crs")]
public required Dictionary<string, FhePublicKey> Crs { get; set; }
}
public sealed class Container
{
[JsonPropertyName("response")]
public required Response Response { get; set; }
}
}
public void Dispose()
{
keyurlCache.Values.ForEach(v => v.Dispose());
keyurlCache.Clear();
}
public async Task<Keys> GetOrDownload(string relayerUrl, string? publicKeyId = null)
{
if (keyurlCache.TryGetValue(relayerUrl, out Keys? cachedKeys))
return cachedKeys;
using HttpClient client = new();
string json = await client.GetStringAsync($"{relayerUrl}/v1/keyurl");
Json.Response fhevmKeys =
JsonSerializer.Deserialize<Json.Container>(json)?.Response
?? throw new InvalidOperationException();
string pubKeyUrl;
// If no publicKeyId is provided, use the first one
// Warning: if there are multiple keys available, the first one will most likely never be the
// same between several calls (fetching the infos is non-deterministic)
if (publicKeyId == null)
{
Json.FhePublicKey fhePublicKey = fhevmKeys.FheKeyInfo[0].FhePublicKey;
pubKeyUrl = fhePublicKey.Urls[0];
publicKeyId = fhePublicKey.DataId;
}
else
{
// If a publicKeyId is provided, get the corresponding info
Json.FheKeyInfo keyInfo =
fhevmKeys.FheKeyInfo.FirstOrDefault(fki => fki.FhePublicKey.DataId == publicKeyId)
?? throw new InvalidDataException($"Could not find FHE key info with data_id {publicKeyId}");
// TODO: Get a given party's public key url instead of the first one
pubKeyUrl = keyInfo.FhePublicKey.Urls[0];
}
byte[] serializedPublicKey = await client.GetByteArrayAsync(pubKeyUrl);
string publicParamsUrl = fhevmKeys.Crs["2048"].Urls[0];
string publicParamsId = fhevmKeys.Crs["2048"].DataId;
byte[] publicParams2048 = await client.GetByteArrayAsync(publicParamsUrl);
CompactPublicKey? publicKey = null;
CompactPkeCrs? crs = null;
try
{
const ulong SERIALIZED_SIZE_LIMIT_PK = 1024 * 1024 * 512;
publicKey = CompactPublicKey.SafeDeserialize(serializedPublicKey, SERIALIZED_SIZE_LIMIT_PK);
const ulong SERIALIZED_SIZE_LIMIT_CRS = 1024 * 1024 * 512;
crs = CompactPkeCrs.SafeDeserialize(publicParams2048, SERIALIZED_SIZE_LIMIT_CRS);
Keys keys = new()
{
CompactPublicKeyInfo = new()
{
PublicKey = publicKey,
PublicKeyId = publicKeyId,
},
// 2048
PublicParamsInfo = new()
{
PublicParams = crs,
PublicParamsId = publicParamsId,
},
};
Keys addedKeys = keyurlCache.GetOrAdd(relayerUrl, keys);
if (addedKeys == keys)
{
publicKey = null;
crs = null;
}
return addedKeys;
}
finally
{
crs?.Dispose();
publicKey?.Dispose();
}
}
}