-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptedValuesBuilder.cs
More file actions
165 lines (134 loc) · 4.67 KB
/
EncryptedValuesBuilder.cs
File metadata and controls
165 lines (134 loc) · 4.67 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
164
165
using Fhe;
using Nethereum.Util;
using Nethermind.Int256; // https://github.com/NethermindEth/int256/tree/main
using FhevmSDK.Tools;
using System.Globalization;
namespace FhevmSDK;
public sealed class EncryptedValuesBuilder : IDisposable
{
private readonly CompactCiphertextListBuilder _builder;
private int _valueCount;
private int _bitCount;
private readonly List<FheValueType> _valueTypes = new();
public EncryptedValuesBuilder(CompactPublicKeyInfo compactPublicKey)
{
_builder = CompactCiphertextListBuilder.Create(compactPublicKey.PublicKey);
}
public void Dispose()
{
_builder.Dispose();
}
private void CheckLimit(FheValueType valueType)
{
int addedBits = FheValueHelper.GetBitCount(valueType);
if (_bitCount + addedBits > 2048)
throw new InvalidOperationException("Packing more than 2048 bits in a single input ciphertext is not supported");
if (_valueCount + 1 > 256)
throw new InvalidOperationException("Packing more than 256 variables in a single input ciphertext is not supported");
_bitCount += addedBits;
++_valueCount;
_valueTypes.Add(valueType);
}
internal IReadOnlyList<FheValueType> GetValueTypes() =>
_valueTypes;
public EncryptedValuesBuilder PushBool(bool value)
{
CheckLimit(FheValueType.Bool);
_builder.PushBool(value);
return this;
}
public EncryptedValuesBuilder PushU8(byte value)
{
CheckLimit(FheValueType.UInt8);
_builder.PushU8(value);
return this;
}
public EncryptedValuesBuilder PushU16(ushort value)
{
CheckLimit(FheValueType.UInt16);
_builder.PushU16(value);
return this;
}
public EncryptedValuesBuilder PushU32(uint value)
{
CheckLimit(FheValueType.UInt32);
_builder.PushU32(value);
return this;
}
public EncryptedValuesBuilder PushU64(ulong value)
{
CheckLimit(FheValueType.UInt64);
_builder.PushU64(value);
return this;
}
public EncryptedValuesBuilder PushU128(UInt128 value)
{
CheckLimit(FheValueType.UInt128);
_builder.PushU128(value);
return this;
}
public EncryptedValuesBuilder PushU256(UInt256 value)
{
CheckLimit(FheValueType.UInt256);
_builder.PushU256(value);
return this;
}
public EncryptedValuesBuilder PushAddress(string value)
{
if (!AddressHelper.IsAddress(value))
throw new InvalidDataException("Invalid address");
CheckLimit(FheValueType.Address);
_builder.Push160(UInt256.Parse(Helpers.Remove0xIfAny(value), NumberStyles.HexNumber));
return this;
}
/*
public EncryptedValuesBuilder PushBytes64(ReadOnlySpan<byte> value)
{
if (value.Length != 64)
throw new InvalidDataException("Invalid Span length, should be 64");
CheckLimit(FheValueType.Bytes64);
//const bigIntValue = bytesToBigInt(value);
builder.push_u512(bigIntValue);
return this;
}
public EncryptedValuesBuilder PushBytes128(ReadOnlySpan<byte> value)
{
if (value.Length != 128)
throw new InvalidDataException("Invalid Span length, should be 128");
checkLimit(FheValueType.Bytes128);
//const bigIntValue = bytesToBigInt(value);
builder.push_u1024(bigIntValue);
return this;
}
public EncryptedValuesBuilder PushBytes256(ReadOnlySpan<byte> value)
{
if (value.Length != 256)
throw new InvalidDataException("Invalid Span length, should be 256");
CheckLimit(FheValueType.Bytes256);
//const bigIntValue = bytesToBigInt(value);
builder.push_u2048(bigIntValue);
return this;
}
*/
internal byte[] Encrypt(
PublicParamsInfo publicParams,
string aclContractAddress,
ulong chainId,
string contractAddress,
string userAddress)
{
byte[] auxData =
[
.. Convert.FromHexString(Helpers.Remove0xIfAny(contractAddress)),
.. Convert.FromHexString(Helpers.Remove0xIfAny(userAddress)),
.. Convert.FromHexString(Helpers.Remove0xIfAny(aclContractAddress)),
.. Convert.FromHexString($"{chainId:X64}"),
];
using var provenCompactCiphertextList = ProvenCompactCiphertextList.BuildWithProof(
_builder,
publicParams.PublicParams,
auxData,
ZkComputeLoad.Verify);
return provenCompactCiphertextList.SafeSerialize();
}
}