ReferenceTests.cs
1 using Microsoft.Extensions.FileSystemGlobbing; 2 using NUnit.Framework; 3 using YamlDotNet.Serialization; 4 using YamlDotNet.Serialization.NamingConventions; 5 6 namespace Ckzg.Test; 7 8 [TestFixture] 9 public class ReferenceTests 10 { 11 [OneTimeSetUp] 12 public void Setup() 13 { 14 _ts = Ckzg.LoadTrustedSetup("trusted_setup.txt"); 15 _deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build(); 16 } 17 18 [OneTimeTearDown] 19 public void Teardown() 20 { 21 Ckzg.FreeTrustedSetup(_ts); 22 } 23 24 [TestCase] 25 public void TestSetupLoaded() 26 { 27 Assert.That(_ts, Is.Not.EqualTo(IntPtr.Zero)); 28 } 29 30 private IntPtr _ts; 31 private const string TestDir = "../../../../../../tests"; 32 private readonly string _blobToKzgCommitmentTests = Path.Join(TestDir, "blob_to_kzg_commitment"); 33 private readonly string _computeKzgProofTests = Path.Join(TestDir, "compute_kzg_proof"); 34 private readonly string _computeBlobKzgProofTests = Path.Join(TestDir, "compute_blob_kzg_proof"); 35 private readonly string _verifyKzgProofTests = Path.Join(TestDir, "verify_kzg_proof"); 36 private readonly string _verifyBlobKzgProofTests = Path.Join(TestDir, "verify_blob_kzg_proof"); 37 private readonly string _verifyBlobKzgProofBatchTests = Path.Join(TestDir, "verify_blob_kzg_proof_batch"); 38 private IDeserializer _deserializer; 39 40 #region Helper Functions 41 42 private static byte[] GetBytes(string hex) 43 { 44 return Convert.FromHexString(hex[2..]); 45 } 46 47 private static byte[] GetFlatBytes(List<string> strings) 48 { 49 List<byte[]> stringBytes = strings.Select(GetBytes).ToList(); 50 51 byte[] flatBytes = new byte[stringBytes.Sum(b => b.Length)]; 52 int offset = 0; 53 foreach (byte[] bytes in stringBytes) 54 { 55 Buffer.BlockCopy(bytes, 0, flatBytes, offset, bytes.Length); 56 offset += bytes.Length; 57 } 58 59 return flatBytes; 60 } 61 62 #endregion 63 64 #region BlobToKzgCommitment 65 66 private class BlobToKzgCommitmentInput 67 { 68 public string Blob { get; set; } = null!; 69 } 70 71 private class BlobToKzgCommitmentTest 72 { 73 public BlobToKzgCommitmentInput Input { get; set; } = null!; 74 public string? Output { get; set; } = null!; 75 } 76 77 [TestCase] 78 public void TestBlobToKzgCommitment() 79 { 80 Matcher matcher = new(); 81 matcher.AddIncludePatterns(new[] { "*/*/data.yaml" }); 82 83 IEnumerable<string> testFiles = matcher.GetResultsInFullPath(_blobToKzgCommitmentTests); 84 Assert.That(testFiles.Count(), Is.GreaterThan(0)); 85 86 foreach (string testFile in testFiles) 87 { 88 string yaml = File.ReadAllText(testFile); 89 BlobToKzgCommitmentTest test = _deserializer.Deserialize<BlobToKzgCommitmentTest>(yaml); 90 Assert.That(test, Is.Not.EqualTo(null)); 91 92 byte[] commitment = new byte[48]; 93 byte[] blob = GetBytes(test.Input.Blob); 94 95 try 96 { 97 Ckzg.BlobToKzgCommitment(commitment, blob, _ts); 98 Assert.That(test.Output, Is.Not.EqualTo(null)); 99 byte[] expectedCommitment = GetBytes(test.Output); 100 Assert.That(commitment, Is.EqualTo(expectedCommitment)); 101 } 102 catch 103 { 104 Assert.That(test.Output, Is.EqualTo(null)); 105 } 106 } 107 } 108 109 #endregion 110 111 #region ComputeKzgProof 112 113 private class ComputeKzgProofInput 114 { 115 public string Blob { get; set; } = null!; 116 public string Z { get; set; } = null!; 117 } 118 119 private class ComputeKzgProofTest 120 { 121 public ComputeKzgProofInput Input { get; set; } = null!; 122 public List<string>? Output { get; set; } = null!; 123 } 124 125 [TestCase] 126 public void TestComputeKzgProof() 127 { 128 Matcher matcher = new(); 129 matcher.AddIncludePatterns(new[] { "*/*/data.yaml" }); 130 131 IEnumerable<string> testFiles = matcher.GetResultsInFullPath(_computeKzgProofTests); 132 Assert.That(testFiles.Count(), Is.GreaterThan(0)); 133 134 foreach (string testFile in testFiles) 135 { 136 string yaml = File.ReadAllText(testFile); 137 ComputeKzgProofTest test = _deserializer.Deserialize<ComputeKzgProofTest>(yaml); 138 Assert.That(test, Is.Not.EqualTo(null)); 139 140 byte[] proof = new byte[48]; 141 byte[] y = new byte[32]; 142 byte[] blob = GetBytes(test.Input.Blob); 143 byte[] z = GetBytes(test.Input.Z); 144 145 try 146 { 147 Ckzg.ComputeKzgProof(proof, y, blob, z, _ts); 148 Assert.That(test.Output, Is.Not.EqualTo(null)); 149 byte[] expectedProof = GetBytes(test.Output.ElementAt(0)); 150 Assert.That(proof, Is.EqualTo(expectedProof)); 151 byte[] expectedY = GetBytes(test.Output.ElementAt(1)); 152 Assert.That(y, Is.EqualTo(expectedY)); 153 } 154 catch 155 { 156 Assert.That(test.Output, Is.EqualTo(null)); 157 } 158 } 159 } 160 161 #endregion 162 163 #region ComputeBlobKzgProof 164 165 private class ComputeBlobKzgProofInput 166 { 167 public string Blob { get; set; } = null!; 168 public string Commitment { get; set; } = null!; 169 } 170 171 private class ComputeBlobKzgProofTest 172 { 173 public ComputeBlobKzgProofInput Input { get; set; } = null!; 174 public string? Output { get; set; } = null!; 175 } 176 177 [TestCase] 178 public void TestComputeBlobKzgProof() 179 { 180 Matcher matcher = new(); 181 matcher.AddIncludePatterns(new[] { "*/*/data.yaml" }); 182 183 IEnumerable<string> testFiles = matcher.GetResultsInFullPath(_computeBlobKzgProofTests); 184 Assert.That(testFiles.Count(), Is.GreaterThan(0)); 185 186 foreach (string testFile in testFiles) 187 { 188 string yaml = File.ReadAllText(testFile); 189 ComputeBlobKzgProofTest test = _deserializer.Deserialize<ComputeBlobKzgProofTest>(yaml); 190 Assert.That(test, Is.Not.EqualTo(null)); 191 192 byte[] proof = new byte[48]; 193 byte[] blob = GetBytes(test.Input.Blob); 194 byte[] commitment = GetBytes(test.Input.Commitment); 195 196 try 197 { 198 Ckzg.ComputeBlobKzgProof(proof, blob, commitment, _ts); 199 Assert.That(test.Output, Is.Not.EqualTo(null)); 200 byte[] expectedProof = GetBytes(test.Output); 201 Assert.That(proof, Is.EqualTo(expectedProof)); 202 } 203 catch 204 { 205 Assert.That(test.Output, Is.EqualTo(null)); 206 } 207 } 208 } 209 210 #endregion 211 212 #region VerifyKzgProof 213 214 private class VerifyKzgProofInput 215 { 216 public string Commitment { get; set; } = null!; 217 public string Z { get; set; } = null!; 218 public string Y { get; set; } = null!; 219 public string Proof { get; set; } = null!; 220 } 221 222 private class VerifyKzgProofTest 223 { 224 public VerifyKzgProofInput Input { get; set; } = null!; 225 public bool? Output { get; set; } = null!; 226 } 227 228 [TestCase] 229 public void TestVerifyKzgProof() 230 { 231 Matcher matcher = new(); 232 matcher.AddIncludePatterns(new[] { "*/*/data.yaml" }); 233 234 IEnumerable<string> testFiles = matcher.GetResultsInFullPath(_verifyKzgProofTests); 235 Assert.That(testFiles.Count(), Is.GreaterThan(0)); 236 237 foreach (string testFile in testFiles) 238 { 239 string yaml = File.ReadAllText(testFile); 240 VerifyKzgProofTest test = _deserializer.Deserialize<VerifyKzgProofTest>(yaml); 241 Assert.That(test, Is.Not.EqualTo(null)); 242 243 byte[] commitment = GetBytes(test.Input.Commitment); 244 byte[] z = GetBytes(test.Input.Z); 245 byte[] y = GetBytes(test.Input.Y); 246 byte[] proof = GetBytes(test.Input.Proof); 247 248 try 249 { 250 bool isCorrect = Ckzg.VerifyKzgProof(commitment, z, y, proof, _ts); 251 Assert.That(isCorrect, Is.EqualTo(test.Output)); 252 } 253 catch 254 { 255 Assert.That(test.Output, Is.EqualTo(null)); 256 } 257 } 258 } 259 260 #endregion 261 262 #region VerifyBlobKzgProof 263 264 private class VerifyBlobKzgProofInput 265 { 266 public string Blob { get; set; } = null!; 267 public string Commitment { get; set; } = null!; 268 public string Proof { get; set; } = null!; 269 } 270 271 private class VerifyBlobKzgProofTest 272 { 273 public VerifyBlobKzgProofInput Input { get; set; } = null!; 274 public bool? Output { get; set; } = null!; 275 } 276 277 [TestCase] 278 public void TestVerifyBlobKzgProof() 279 { 280 Matcher matcher = new(); 281 matcher.AddIncludePatterns(new[] { "*/*/data.yaml" }); 282 283 IEnumerable<string> testFiles = matcher.GetResultsInFullPath(_verifyBlobKzgProofTests); 284 Assert.That(testFiles.Count(), Is.GreaterThan(0)); 285 286 foreach (string testFile in testFiles) 287 { 288 string yaml = File.ReadAllText(testFile); 289 VerifyBlobKzgProofTest test = _deserializer.Deserialize<VerifyBlobKzgProofTest>(yaml); 290 Assert.That(test, Is.Not.EqualTo(null)); 291 292 byte[] blob = GetBytes(test.Input.Blob); 293 byte[] commitment = GetBytes(test.Input.Commitment); 294 byte[] proof = GetBytes(test.Input.Proof); 295 try 296 { 297 bool isCorrect = Ckzg.VerifyBlobKzgProof(blob, commitment, proof, _ts); 298 Assert.That(isCorrect, Is.EqualTo(test.Output)); 299 } 300 catch 301 { 302 Assert.That(test.Output, Is.EqualTo(null)); 303 } 304 } 305 } 306 307 #endregion 308 309 #region VerifyBlobKzgProofBatch 310 311 private class VerifyBlobKzgProofBatchInput 312 { 313 public List<string> Blobs { get; set; } = null!; 314 public List<string> Commitments { get; set; } = null!; 315 public List<string> Proofs { get; set; } = null!; 316 } 317 318 private class VerifyBlobKzgProofBatchTest 319 { 320 public VerifyBlobKzgProofBatchInput Input { get; set; } = null!; 321 public bool? Output { get; set; } = null!; 322 } 323 324 [TestCase] 325 public void TestVerifyBlobKzgProofBatch() 326 { 327 Matcher matcher = new(); 328 matcher.AddIncludePatterns(new[] { "*/*/data.yaml" }); 329 330 IEnumerable<string> testFiles = matcher.GetResultsInFullPath(_verifyBlobKzgProofBatchTests); 331 Assert.That(testFiles.Count(), Is.GreaterThan(0)); 332 333 foreach (string testFile in testFiles) 334 { 335 string yaml = File.ReadAllText(testFile); 336 VerifyBlobKzgProofBatchTest test = _deserializer.Deserialize<VerifyBlobKzgProofBatchTest>(yaml); 337 Assert.That(test, Is.Not.EqualTo(null)); 338 339 byte[] blobs = GetFlatBytes(test.Input.Blobs); 340 byte[] commitments = GetFlatBytes(test.Input.Commitments); 341 byte[] proofs = GetFlatBytes(test.Input.Proofs); 342 int count = blobs.Length / Ckzg.BytesPerBlob; 343 344 try 345 { 346 bool isCorrect = Ckzg.VerifyBlobKzgProofBatch(blobs, commitments, proofs, count, _ts); 347 Assert.That(isCorrect, Is.EqualTo(test.Output)); 348 } 349 catch 350 { 351 Assert.That(test.Output, Is.EqualTo(null)); 352 } 353 } 354 } 355 356 #endregion 357 }