Skip to content

Commit abcbfa5

Browse files
committed
chore: modify return types to return unflattened structures
1 parent c6a85ed commit abcbfa5

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

bindings/csharp/csharp_code/PeerDASKZG.bindings/peerdaskzg.cs

+21-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public unsafe byte[] BlobToKzgCommitment(byte[] blob)
5858
return commitment;
5959
}
6060

61-
public unsafe byte[] ComputeCells(byte[] blob)
61+
public unsafe byte[][] ComputeCells(byte[] blob)
6262
{
6363
byte[] cells = new byte[BytesForAllCells];
6464

@@ -68,10 +68,10 @@ public unsafe byte[] ComputeCells(byte[] blob)
6868
CResult result = compute_cells(_context, Convert.ToUInt64(blob.Length), blobPtr, cellsPtr);
6969
ThrowOnError(result);
7070
}
71-
return cells;
71+
return DeflattenArray(cells, BytesPerCell);
7272
}
7373

74-
public unsafe (byte[], byte[]) ComputeCellsAndKZGProofs(byte[] blob)
74+
public unsafe (byte[][], byte[][]) ComputeCellsAndKZGProofs(byte[] blob)
7575
{
7676
byte[] cells = new byte[BytesForAllCells];
7777
byte[] proofs = new byte[BytesForAllProofs];
@@ -83,7 +83,7 @@ public unsafe (byte[], byte[]) ComputeCellsAndKZGProofs(byte[] blob)
8383
CResult result = compute_cells_and_kzg_proofs(_context, Convert.ToUInt64(blob.Length), blobPtr, cellsPtr, proofsPtr);
8484
ThrowOnError(result);
8585
}
86-
return (cells, proofs);
86+
return (DeflattenArray(cells, BytesPerCell), DeflattenArray(proofs, BytesPerCommitment));
8787
}
8888

8989
public unsafe bool VerifyCellKZGProof(byte[] cell, byte[] commitment, ulong cellId, byte[] proof)
@@ -125,7 +125,7 @@ public bool VerifyCellKZGProofBatch(byte[][] rowCommitments, ulong[] rowIndices,
125125
return verified;
126126
}
127127

128-
public byte[] RecoverAllCells(ulong[] cellIds, byte[][] cells)
128+
public byte[][] RecoverAllCells(ulong[] cellIds, byte[][] cells)
129129
{
130130
byte[] cellsFlattened = FlattenArray(cells);
131131

@@ -138,7 +138,7 @@ public byte[] RecoverAllCells(ulong[] cellIds, byte[][] cells)
138138
ThrowOnError(result);
139139
}
140140

141-
return recoveredCells;
141+
return DeflattenArray(recoveredCells, BytesPerCell);
142142
}
143143

144144
private static void ThrowOnError(CResult result)
@@ -167,7 +167,7 @@ private static void ThrowOnError(CResult result)
167167
}
168168
}
169169

170-
public static byte[] FlattenArray(byte[][] jaggedArray)
170+
private static byte[] FlattenArray(byte[][] jaggedArray)
171171
{
172172
int totalLength = 0;
173173

@@ -192,6 +192,20 @@ public static byte[] FlattenArray(byte[][] jaggedArray)
192192
return flattenedArray;
193193
}
194194

195+
private static byte[][] DeflattenArray(byte[] flattenedArray, int length)
196+
{
197+
int numArrays = flattenedArray.Length / length;
198+
byte[][] jaggedArray = new byte[numArrays][];
199+
200+
for (int i = 0; i < numArrays; i++)
201+
{
202+
jaggedArray[i] = new byte[length];
203+
Array.Copy(flattenedArray, i * length, jaggedArray[i], 0, length);
204+
}
205+
206+
return jaggedArray;
207+
}
208+
195209
public enum ContextSetting
196210
{
197211
ProvingOnly,

bindings/csharp/csharp_code/PeerDASKZG.test/RefTests.cs

+7-22
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,6 @@ private static byte[] GetBytes(string hex)
4747
return Convert.FromHexString(hex[2..]);
4848
}
4949

50-
private static byte[] GetFlatBytes(List<string> strings)
51-
{
52-
List<byte[]> stringBytes = strings.Select(GetBytes).ToList();
53-
54-
byte[] flatBytes = new byte[stringBytes.Sum(b => b.Length)];
55-
int offset = 0;
56-
foreach (byte[] bytes in stringBytes)
57-
{
58-
Buffer.BlockCopy(bytes, 0, flatBytes, offset, bytes.Length);
59-
offset += bytes.Length;
60-
}
61-
62-
return flatBytes;
63-
}
64-
6550
private static byte[][] GetByteArrays(List<string> strings)
6651
{
6752
return strings.Select(GetBytes).ToArray();
@@ -146,14 +131,14 @@ public void TestComputeCells()
146131
ComputeCellsTest test = _deserializer.Deserialize<ComputeCellsTest>(yaml);
147132
Assert.That(test, Is.Not.EqualTo(null));
148133

149-
byte[] cells;
134+
byte[][] cells;
150135
byte[] blob = GetBytes(test.Input.Blob);
151136

152137
try
153138
{
154139
cells = _context.ComputeCells(blob);
155140
Assert.That(test.Output, Is.Not.EqualTo(null));
156-
byte[] expectedCells = GetFlatBytes(test.Output);
141+
byte[][] expectedCells = GetByteArrays(test.Output);
157142
Assert.That(cells, Is.EqualTo(expectedCells));
158143
}
159144
catch
@@ -197,11 +182,11 @@ public void TestComputeCellsAndKzgProofs()
197182

198183
try
199184
{
200-
(byte[] cells, byte[] proofs) = _context.ComputeCellsAndKZGProofs(blob);
185+
(byte[][] cells, byte[][] proofs) = _context.ComputeCellsAndKZGProofs(blob);
201186
Assert.That(test.Output, Is.Not.EqualTo(null));
202-
byte[] expectedCells = GetFlatBytes(test.Output.ElementAt(0));
187+
byte[][] expectedCells = GetByteArrays(test.Output.ElementAt(0));
203188
Assert.That(cells, Is.EqualTo(expectedCells));
204-
byte[] expectedProofs = GetFlatBytes(test.Output.ElementAt(1));
189+
byte[][] expectedProofs = GetByteArrays(test.Output.ElementAt(1));
205190
Assert.That(proofs, Is.EqualTo(expectedProofs));
206191
}
207192
catch
@@ -352,9 +337,9 @@ public void TestRecoverAllCells()
352337

353338
try
354339
{
355-
byte[] recoveredCells = _context.RecoverAllCells(cellIds, cells);
340+
byte[][] recoveredCells = _context.RecoverAllCells(cellIds, cells);
356341
Assert.That(test.Output, Is.Not.EqualTo(null));
357-
byte[] expectedRecoveredCells = GetFlatBytes(test.Output);
342+
byte[][] expectedRecoveredCells = GetByteArrays(test.Output);
358343
Assert.That(recoveredCells, Is.EqualTo(expectedRecoveredCells));
359344
}
360345
catch

0 commit comments

Comments
 (0)