tasm_lib/verifier/stark_verify.rs
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
use itertools::Itertools;
use triton_vm::challenges::Challenges;
use triton_vm::prelude::*;
use triton_vm::proof_item::ProofItemVariant;
use triton_vm::proof_stream::ProofStream;
use triton_vm::table::master_table::MasterAuxTable;
use triton_vm::table::master_table::MasterMainTable;
use triton_vm::table::master_table::MasterTable;
use triton_vm::table::NUM_QUOTIENT_SEGMENTS;
use triton_vm::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::twenty_first::prelude::AlgebraicHasher;
use twenty_first::prelude::MerkleTreeInclusionProof;
use super::master_table::air_constraint_evaluation::AirConstraintEvaluation;
use super::master_table::air_constraint_evaluation::MemoryLayout;
use crate::arithmetic::bfe::primitive_root_of_unity::PrimitiveRootOfUnity;
use crate::array::horner_evaluation::HornerEvaluation;
use crate::array::inner_product_of_three_rows_with_weights::InnerProductOfThreeRowsWithWeights;
use crate::array::inner_product_of_three_rows_with_weights::MainElementType;
use crate::array::inner_product_of_xfes::InnerProductOfXfes;
use crate::data_type::DataType;
use crate::field;
use crate::hashing::algebraic_hasher::sample_scalar_one::SampleScalarOne;
use crate::hashing::algebraic_hasher::sample_scalars_static_length_dyn_malloc::SampleScalarsStaticLengthDynMalloc;
use crate::library::Library;
use crate::traits::basic_snippet::BasicSnippet;
use crate::verifier::challenges;
use crate::verifier::claim::instantiate_fiat_shamir_with_claim::InstantiateFiatShamirWithClaim;
use crate::verifier::claim::shared::claim_type;
use crate::verifier::fri;
use crate::verifier::fri::verify::FriSnippet;
use crate::verifier::fri::verify::FriVerify;
use crate::verifier::master_table::divide_out_zerofiers::DivideOutZerofiers;
use crate::verifier::master_table::verify_table_rows::ColumnType;
use crate::verifier::master_table::verify_table_rows::VerifyTableRows;
use crate::verifier::out_of_domain_points::OodPoint;
use crate::verifier::out_of_domain_points::OutOfDomainPoints;
use crate::verifier::vm_proof_iter::dequeue_next_as::DequeueNextAs;
use crate::verifier::vm_proof_iter::drop::Drop;
use crate::verifier::vm_proof_iter::new::New;
pub(crate) const NUM_PROOF_ITEMS_PER_FRI_ROUND: usize = 2;
pub(crate) const NUM_PROOF_ITEMS_EXCLUDING_FRI: usize = 15;
/// Verify a STARK proof.
///
/// Verify a STARK proof located in memory. Assumes the nondeterministic digests
/// stream has been updated with the digests extracted from the proof using
/// [`update_nondeterminism`](Self::update_nondeterminism). Crashes the VM if the
/// proof is invalid.
///
/// Stack signature:
/// - BEFORE: _ *claim *proof
/// - AFTER: _
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct StarkVerify {
stark: Stark,
memory_layout: MemoryLayout,
}
impl StarkVerify {
pub fn new_with_static_layout(stark: Stark) -> Self {
Self {
stark,
memory_layout: MemoryLayout::conventional_static(),
}
}
pub fn new_with_dynamic_layout(stark: Stark) -> Self {
Self {
stark,
memory_layout: MemoryLayout::conventional_dynamic(),
}
}
/// The number of nondeterministic digests that will be
/// consumed when this snippet verifies the given proof.
pub fn number_of_nondeterministic_digests_consumed(&self, proof: &Proof) -> usize {
const NUM_FULL_DOMAIN_AUTH_PATHS: usize = 4;
let padded_height = proof.padded_height().unwrap();
let fri_params = self.stark.fri(padded_height).unwrap();
let num_fri_rounds = fri_params.num_rounds();
let mut j = 0;
let mut tree_height: usize = fri_params.domain.length.ilog2().try_into().unwrap();
let mut acc = NUM_FULL_DOMAIN_AUTH_PATHS * tree_height * fri_params.num_collinearity_checks;
while j < num_fri_rounds {
acc += fri_params.num_collinearity_checks * tree_height;
j += 1;
tree_height -= 1;
}
acc
}
/// The number of nondeterministic individual tokens that will be consumed when
/// this snippet verifies the given (claim, proof) pair.
// Right now this number is zero, but that might change in the future.
pub fn number_of_nondeterministic_tokens_consumed(
&self,
_proof: &Proof,
_claim: &Claim,
) -> usize {
0
}
/// Prepares the non-determinism for verifying a STARK proof. Specifically,
/// extracts the digests for traversing authentication paths and appends them
/// to nondeterministic digests. Leaves memory and individual tokens intact.
pub fn update_nondeterminism(
&self,
nondeterminism: &mut NonDeterminism,
proof: &Proof,
claim: &Claim,
) {
nondeterminism
.digests
.append(&mut self.extract_nondeterministic_digests(proof, claim));
}
fn extract_nondeterministic_digests(&self, proof: &Proof, claim: &Claim) -> Vec<Digest> {
const NUM_DEEP_CODEWORD_COMPONENTS: usize = 3;
fn extract_paths<R: BFieldCodec>(
indices: Vec<usize>,
leaf_preimages: Vec<R>,
tree_height: usize,
authentication_structure: Vec<Digest>,
) -> Vec<Vec<Digest>> {
let indexed_leafs = indices
.into_iter()
.zip(leaf_preimages.iter().map(Tip5::hash))
.collect();
MerkleTreeInclusionProof {
tree_height,
indexed_leafs,
authentication_structure,
}
.into_authentication_paths()
.unwrap()
}
// We do need to carefully update the sponge state because otherwise
// we end up sampling indices that generate different authentication
// paths.
let mut proof_stream = ProofStream::try_from(proof).unwrap();
let log2_padded_height = proof_stream
.dequeue()
.unwrap()
.try_into_log2_padded_height()
.unwrap();
proof_stream.alter_fiat_shamir_state_with(claim);
// Main-table Merkle root
let _main_table_root = proof_stream
.dequeue()
.unwrap()
.try_into_merkle_root()
.unwrap();
// Auxiliary challenge weights
let _challenges = proof_stream.sample_scalars(Challenges::SAMPLE_COUNT);
// Auxiliary-table Merkle root
let _aux_mt_root = proof_stream
.dequeue()
.unwrap()
.try_into_merkle_root()
.unwrap();
// Quotient codeword weights
proof_stream.sample_scalars(MasterAuxTable::NUM_CONSTRAINTS);
// Quotient codeword Merkle root
let _quotient_root = proof_stream
.dequeue()
.unwrap()
.try_into_merkle_root()
.unwrap();
// Out-of-domain point current row
let _out_of_domain_point_curr_row = proof_stream.sample_scalars(1);
// Five out-of-domain values
proof_stream
.dequeue()
.unwrap()
.try_into_out_of_domain_main_row()
.unwrap();
proof_stream
.dequeue()
.unwrap()
.try_into_out_of_domain_aux_row()
.unwrap();
proof_stream
.dequeue()
.unwrap()
.try_into_out_of_domain_main_row()
.unwrap();
proof_stream
.dequeue()
.unwrap()
.try_into_out_of_domain_aux_row()
.unwrap();
proof_stream
.dequeue()
.unwrap()
.try_into_out_of_domain_quot_segments()
.unwrap();
// `beqd_weights`
proof_stream.sample_scalars(
MasterMainTable::NUM_COLUMNS
+ MasterAuxTable::NUM_COLUMNS
+ NUM_QUOTIENT_SEGMENTS
+ NUM_DEEP_CODEWORD_COMPONENTS,
);
// FRI digests
let padded_height = 1 << log2_padded_height;
let fri = self.stark.fri(padded_height).unwrap();
let fri_proof_stream = proof_stream.clone();
let fri_verify_result = fri.verify(&mut proof_stream).unwrap();
let indices = fri_verify_result.iter().map(|(i, _)| *i).collect_vec();
let tree_height = fri.domain.length.ilog2() as usize;
let fri_digests =
FriVerify::from(fri).extract_digests_required_for_proving(&fri_proof_stream);
// main
let main_table_rows = proof_stream
.dequeue()
.unwrap()
.try_into_master_main_table_rows()
.unwrap();
let main_authentication_structure = proof_stream
.dequeue()
.unwrap()
.try_into_authentication_structure()
.unwrap();
let main_tree_auth_paths = extract_paths(
indices.clone(),
main_table_rows,
tree_height,
main_authentication_structure,
);
// aux
let aux_table_rows = proof_stream
.dequeue()
.unwrap()
.try_into_master_aux_table_rows()
.unwrap();
let aux_authentication_structure = proof_stream
.dequeue()
.unwrap()
.try_into_authentication_structure()
.unwrap();
let aux_tree_auth_paths = extract_paths(
indices.clone(),
aux_table_rows,
tree_height,
aux_authentication_structure,
);
// quotient
let quot_table_rows = proof_stream
.dequeue()
.unwrap()
.try_into_quot_segments_elements()
.unwrap();
let quot_authentication_structure = proof_stream
.dequeue()
.unwrap()
.try_into_authentication_structure()
.unwrap();
let quot_tree_auth_paths = extract_paths(
indices,
quot_table_rows,
tree_height,
quot_authentication_structure,
);
let stark_digests = [
main_tree_auth_paths,
aux_tree_auth_paths,
quot_tree_auth_paths,
]
.concat()
.concat();
[fri_digests, stark_digests].concat()
}
}
impl BasicSnippet for StarkVerify {
fn inputs(&self) -> Vec<(DataType, String)> {
let claim_type = DataType::StructRef(claim_type());
vec![
(claim_type, "claim".to_string()),
(DataType::VoidPointer, "*proof".to_string()),
]
}
fn outputs(&self) -> Vec<(DataType, String)> {
vec![]
}
fn entrypoint(&self) -> String {
let memory_layout_category = self.memory_layout.label_friendly_name();
format!("tasmlib_verifier_stark_verify_{memory_layout_category}")
}
fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
const NUM_DEEP_CODEWORD_COMPONENTS: usize = 3;
const NUM_OOD_ROWS_WO_QUOTIENT: u32 = 4;
fn fri_snippet() -> FriSnippet {
#[cfg(not(test))]
{
FriSnippet {}
}
#[cfg(test)]
{
FriSnippet {
test_instance: FriVerify::dummy(),
}
}
}
let entrypoint = self.entrypoint();
let proof_to_vm_proof_iter = library.import(Box::new(New));
let drop_vm_proof_iter = library.import(Box::new(Drop));
let ood_curr_row_main_and_aux_value_pointer_alloc =
library.kmalloc(EXTENSION_DEGREE.try_into().unwrap());
let ood_next_row_main_and_aux_value_pointer_alloc =
library.kmalloc(EXTENSION_DEGREE.try_into().unwrap());
let ood_curr_row_quotient_segment_value_pointer_alloc =
library.kmalloc(EXTENSION_DEGREE.try_into().unwrap());
let out_of_domain_curr_row_quot_segments_pointer_alloc = library.kmalloc(1);
let instantiate_fiat_shamir_with_claim =
library.import(Box::new(InstantiateFiatShamirWithClaim));
let next_as_log_2_padded_height = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::Log2PaddedHeight,
}));
let next_as_merkleroot = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::MerkleRoot,
}));
let next_as_outofdomainmainrow = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::OutOfDomainMainRow,
}));
let next_as_outofdomainauxrow = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::OutOfDomainAuxRow,
}));
let next_as_outofdomainquotientsegments = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::OutOfDomainQuotientSegments,
}));
let next_as_maintablerows = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::MasterMainTableRows,
}));
let next_as_authentication_path = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::AuthenticationStructure,
}));
let next_as_auxtablerows = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::MasterAuxTableRows,
}));
let next_as_quotient_segment_elements = library.import(Box::new(DequeueNextAs {
proof_item: ProofItemVariant::QuotientSegmentsElements,
}));
let derive_fri_parameters =
library.import(Box::new(fri::derive_from_stark::DeriveFriFromStark {
stark: self.stark,
}));
let num_collinearity_checks_field = field!(FriVerify::num_collinearity_checks);
let domain_length_field = field!(FriVerify::domain_length);
let domain_offset_field = field!(FriVerify::domain_offset);
let domain_generator_field = field!(FriVerify::domain_generator);
let fri_verify = library.import(Box::new(fri_snippet()));
let get_challenges = library.import(Box::new(
challenges::new_generic_dyn_claim::NewGenericDynClaim::tvm_challenges(
self.memory_layout.challenges_pointer(),
),
));
let sample_quotient_codeword_weights =
library.import(Box::new(SampleScalarsStaticLengthDynMalloc {
num_elements: MasterAuxTable::NUM_CONSTRAINTS,
}));
let domain_generator = library.import(Box::new(PrimitiveRootOfUnity));
let sample_scalar_one = library.import(Box::new(SampleScalarOne));
let calculate_out_of_domain_points = library.import(Box::new(OutOfDomainPoints));
let divide_out_zerofiers = library.import(Box::new(DivideOutZerofiers));
let inner_product_quotient_summands = library.import(Box::new(InnerProductOfXfes {
length: MasterAuxTable::NUM_CONSTRAINTS,
}));
let horner_evaluation_of_ood_curr_row_quot_segments =
library.import(Box::new(HornerEvaluation {
num_coefficients: NUM_QUOTIENT_SEGMENTS,
}));
let sample_beqd_weights = library.import(Box::new(SampleScalarsStaticLengthDynMalloc {
num_elements: MasterMainTable::NUM_COLUMNS
+ MasterAuxTable::NUM_COLUMNS
+ NUM_QUOTIENT_SEGMENTS
+ NUM_DEEP_CODEWORD_COMPONENTS,
}));
let verify_main_table_rows = library.import(Box::new(VerifyTableRows {
column_type: ColumnType::Main,
}));
let verify_aux_table_rows = library.import(Box::new(VerifyTableRows {
column_type: ColumnType::Aux,
}));
let verify_quotient_segments = library.import(Box::new(VerifyTableRows {
column_type: ColumnType::Quotient,
}));
let inner_product_three_rows_with_weights_bfe_main = library.import(Box::new(
InnerProductOfThreeRowsWithWeights::triton_vm_parameters(MainElementType::Bfe),
));
let inner_product_three_rows_with_weights_xfe_main = library.import(Box::new(
InnerProductOfThreeRowsWithWeights::triton_vm_parameters(MainElementType::Xfe),
));
let inner_product_4_xfes = library.import(Box::new(InnerProductOfXfes { length: 4 }));
let quotient_segment_codeword_weights_from_be_weights = triton_asm!(
// _ *beqd_ws
push {(MasterMainTable::NUM_COLUMNS + MasterAuxTable::NUM_COLUMNS) * EXTENSION_DEGREE}
add
// _ *quotient_segment_weights
);
let deep_codeword_weights_read_address = |n: usize| {
assert!(n < NUM_DEEP_CODEWORD_COMPONENTS);
triton_asm!(
// _ *beqd_ws
push {(MasterMainTable::NUM_COLUMNS + MasterAuxTable::NUM_COLUMNS + NUM_QUOTIENT_SEGMENTS + n) * EXTENSION_DEGREE + {EXTENSION_DEGREE - 1}}
add
// _ *deep_codeword_weight[n]_last_word
)
};
let dequeue_four_ood_rows = triton_asm! {
// _ *proof_iter
dup 0
call {next_as_outofdomainmainrow}
hint out_of_domain_curr_main_row: Pointer = stack[0]
dup 1
call {next_as_outofdomainauxrow}
hint out_of_domain_curr_aux_row: Pointer = stack[0]
dup 2
call {next_as_outofdomainmainrow}
hint out_of_domain_next_main_row: Pointer = stack[0]
dup 3
call {next_as_outofdomainauxrow}
hint out_of_domain_next_aux_row: Pointer = stack[0]
// _ *proof_iter *curr_main *curr_aux *next_main *next_aux
};
// BEFORE:
// _ *p_iter - - - *quot_cw_ws - dom_gen [out_of_domain_curr_row] padded_height *proof_iter *curr_main *curr_aux *next_main *next_aux
// AFTER:
// _ *p_iter - - - *quot_cw_ws - dom_gen [out_of_domain_curr_row] padded_height *air_evaluation_result
let ood_pointers_alloc = library.kmalloc(NUM_OOD_ROWS_WO_QUOTIENT);
let evaluate_air_and_store_ood_pointers = match self.memory_layout {
MemoryLayout::Static(static_layout) => {
let static_eval =
library.import(Box::new(AirConstraintEvaluation::new_static(static_layout)));
triton_asm! {
push {ood_pointers_alloc.write_address()}
write_mem {ood_pointers_alloc.num_words()}
pop 2
// _ ... padded_height
call {static_eval}
// _ ... padded_height *air_evaluation_result
}
}
MemoryLayout::Dynamic(dynamic_layout) => {
let dynamic_eval = library.import(Box::new(AirConstraintEvaluation::new_dynamic(
dynamic_layout,
)));
triton_asm! {
// store pointers to static memory
dup 3
dup 3
dup 3
dup 3
push {ood_pointers_alloc.write_address()}
write_mem {ood_pointers_alloc.num_words()}
pop 1
// _ ... padded_height *proof_iter *curr_main *curr_aux *next_main *next_aux
call {dynamic_eval}
// _ ... padded_height *proof_iter *air_evaluation_result
pick 1 pop 1
// _ ... padded_height *air_evaluation_result
}
}
};
let put_ood_row_pointers_back_on_stack = triton_asm! {
// _
push {ood_pointers_alloc.read_address()}
read_mem {ood_pointers_alloc.num_words()}
pop 1
// _ *curr_main *curr_aux *next_main *next_aux
};
let challenges_ptr = self.memory_layout.challenges_pointer();
let assert_top_two_xfes_eq = triton_asm!(
// _ y2 y1 y0 x2 x1 x0
pick 3
eq
assert error_id 230
// _ y2 y1 x2 x1
pick 2
eq
assert error_id 231
// _ y2 x2
eq
assert error_id 232
// _
);
let main_loop_label = format!("{entrypoint}_main_loop");
let main_loop_body = triton_asm!(
// (u32, XFieldElement)
// _ remaining_rounds fri_gen fri_offset *etrow *btrow *qseg_elem *fri_revealed_idx *beqd_ws *oodpnts
// Calculate `current_fri_domain_value`
pick 2
read_mem 1
place 3
// _ remaining_rounds fri_gen fri_offset *etrow *btrow *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts fri_idx
dup 8
pow
dup 7
mul
push -1
mul
hint neg_fri_domain_point = stack[0]
// _ remaining_rounds fri_gen fri_offset *etrow *btrow *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt)
dup 6
dup 6
dup 4
call {inner_product_three_rows_with_weights_bfe_main} // expect arguments: *aux *main *ws
hint main_and_aux_opened_row_element: Xfe = stack[0..3]
// _ remaining_rounds fri_gen fri_offset *etrow *btrow *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [be_opnd_elem]
// Update `*btrow` and `*etrow` pointer values to point to previous element
pick 9
addi {-bfe!(EXTENSION_DEGREE * MasterAuxTable::NUM_COLUMNS)}
place 9
pick 8
addi {-bfe!(MasterMainTable::NUM_COLUMNS)}
place 8
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [be_opnd_elem]
push -1
xb_mul
hint neg_main_and_aux_opened_row_element: Xfe = stack[0..3]
// Calculate `cuotient_curr_row_deep_value`
dup 4
{&OutOfDomainPoints::read_ood_point(OodPoint::CurrentRowPowNumSegments)}
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [oodp_pow_nsegs]
dup 6
add
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [oodp_pow_nsegs - fdom_pnt]
x_invert
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [1/(oodp_pow_nsegs - fdom_pnt)]
dup 8
{"ient_segment_codeword_weights_from_be_weights}
dup 11
call {inner_product_4_xfes}
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [1/(oodp_pow_nsegs - fdom_pnt)] [inner_prod]
pick 13
addi {-bfe!(NUM_QUOTIENT_SEGMENTS * EXTENSION_DEGREE)}
place 13
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [1/(oodp_pow_nsegs - fdom_pnt)] [inner_prod]
push -1
xb_mul
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [1/(oodp_pow_nsegs - fdom_pnt)] [-inner_prod]
push {ood_curr_row_quotient_segment_value_pointer_alloc.read_address()}
read_mem {EXTENSION_DEGREE}
pop 1
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [1/(oodp_pow_nsegs - fdom_pnt)] [-inner_prod] [out_of_domain_curr_row_quotient_segment_value]
xx_add
xx_mul
hint quot_curr_row_deep_value: XFieldElement = stack[0..3]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [(out_of_domain_curr_row_quotient_segment_value - inner_prod) / (oodp_pow_nsegs - fdom_pnt)]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [quot_curr_row_deep_value]
/* Calculate $dv2 = quot_curr_row_deep_value * deep_codeword_weights[2]$ */
dup 8
{&deep_codeword_weights_read_address(2)}
read_mem {EXTENSION_DEGREE}
pop 1
xx_mul
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [quot_curr_row_deep_value * deep_codeword_weights[2]]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2]
dup 7
{&OutOfDomainPoints::read_ood_point(OodPoint::CurrentRow)}
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [ood_point_curr_row]
dup 9
add
x_invert
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [1/(ood_point_curr_row - fdom_pnt)]
push {ood_curr_row_main_and_aux_value_pointer_alloc.read_address()}
read_mem {EXTENSION_DEGREE}
pop 1
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [1/(ood_point_curr_row - fdom_pnt)] [out_of_domain_curr_row_main_and_aux_value]
dup 11
dup 11
dup 11
xx_add
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [1/(ood_point_curr_row - fdom_pnt)] [out_of_domain_curr_row_main_and_aux_value - be_opnd_elem]
xx_mul
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [(out_of_domain_curr_row_main_and_aux_value - be_opnd_elem)/(ood_point_curr_row - fdom_pnt)]
dup 11
{&deep_codeword_weights_read_address(0)}
read_mem {EXTENSION_DEGREE} // read deep_codeword_weights[0]
pop 1
xx_mul
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [deep_codeword_weights[0] * (out_of_domain_curr_row_main_and_aux_value - be_opnd_elem)/(ood_point_curr_row - fdom_pnt)]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2] [dv0]
xx_add
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [-be_opnd_elem] [dv2 + dv0]
hint dv2_plus_dv0: XFieldElement = stack[0..3]
pick 5
pick 5
pick 5
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [-be_opnd_elem]
push {ood_next_row_main_and_aux_value_pointer_alloc.read_address()}
read_mem {EXTENSION_DEGREE}
pop 1
xx_add
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [ood_next_row_be_value - be_opnd_elem]
dup 7
{&OutOfDomainPoints::read_ood_point(OodPoint::NextRow)}
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [ood_next_row_be_value - be_opnd_elem] [out_of_domain_point_next_row]
dup 9
add
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [ood_next_row_be_value - be_opnd_elem] [out_of_domain_point_next_row - fdom_pnt]
x_invert
xx_mul
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [(ood_next_row_be_value - be_opnd_elem)/(out_of_domain_point_next_row - fdom_pnt)]
dup 8
{&deep_codeword_weights_read_address(1)}
read_mem {EXTENSION_DEGREE} // read deep_codeword_weights[1]
pop 1
xx_mul
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [deep_codeword_weights[1] * (ood_next_row_be_value - be_opnd_elem)/(out_of_domain_point_next_row - fdom_pnt)]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0] [dv1]
xx_add
hint deep_value: XFieldElement = stack[0..3]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [dv2 + dv0 + dv1]
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts (-fdom_pnt) [deep_value]
pick 3
pop 1
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_xfe *beqd_ws *oodpnts [deep_value]
pick 5
read_mem {EXTENSION_DEGREE}
place 8
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_idx_prev *beqd_ws *oodpnts [deep_value] [fri_revealed_value]
{&assert_top_two_xfes_eq}
// _ remaining_rounds fri_gen fri_offset *etrow_prev *btrow_prev *qseg_elem_prev *fri_revealed_idx_prev *beqd_ws *oodpnts
);
let main_loop = triton_asm!(
// The loop goes from last index to 1st index
// Invariant: _ num_colli fri_gen fri_offset *etrow *btrow *qseg_elem *fri_revealed_elem *beqd_ws *oodpnts
{main_loop_label}:
// test end-condition
dup 8
push 0
eq
skiz
return
{&main_loop_body}
// Update counter
pick 8
addi -1
place 8
recurse
);
triton_asm!(
{entrypoint}:
sponge_init
// _ *claim *proof
call {proof_to_vm_proof_iter}
hint proof_iter = stack[0]
// _ *clm *proof_iter
/* Fiat-Shamir: Claim */
dup 1
call {instantiate_fiat_shamir_with_claim}
// _ *clm *p_iter
/* derive additional parameters */
dup 0
call {next_as_log_2_padded_height}
// _ *clm *p_iter *log_2_padded_height
read_mem 1
pop 1
// _ *clm *p_iter log_2_padded_height
push 2
pow
hint padded_height = stack[0]
// _ *clm *p_iter padded_height
dup 0
call {derive_fri_parameters}
hint fri = stack[0]
// _ *clm *p_iter padded_height *fri
/* Fiat-Shamir 1 */
dup 2
call {next_as_merkleroot}
hint b_mr = stack[0]
// _ *clm *p_iter padded_height *fri *b_mr
swap 4
// _ *b_mr *p_iter padded_height *fri *clm
call {get_challenges}
// _ *b_mr *p_iter padded_height *fri *challenges
// verify that the challenges are stored at the right place
push {challenges_ptr}
eq
assert error_id 233
// _ *b_mr *p_iter padded_height *fri
dup 2
call {next_as_merkleroot}
hint e_mr = stack[0]
// _ *b_mr *p_iter padded_height *fri *e_mr
call {sample_quotient_codeword_weights}
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws
hint quot_codeword_weights = stack[0]
dup 4
call {next_as_merkleroot}
hint quot_mr = stack[0]
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws *quot_mr
/* sample and calculate OOD points (not rows) */
push 0
dup 5
call {domain_generator}
hint trace_domain_generator = stack[0]
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws *quot_mr dom_gen
dup 0
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws *quot_mr dom_gen dom_gen
call {sample_scalar_one}
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws *quot_mr dom_gen dom_gen [ood_curr_row]
call {calculate_out_of_domain_points}
hint out_of_domain_points = stack[0]
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws *quot_mr dom_gen *oodpnts
/* out-of-domain quotient summands */
push 2
add
read_mem {EXTENSION_DEGREE}
push 1
add
// _ *b_mr *p_iter padded_height *fri *e_mr *quot_cw_ws *quot_mr dom_gen [out_of_domain_curr_row] *oodpnts
swap 9
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr dom_gen [out_of_domain_curr_row] padded_height
dup 10
{&dequeue_four_ood_rows}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr dom_gen [out_of_domain_curr_row] padded_height *proof_iter *curr_main *curr_aux *next_main *next_aux
{&evaluate_air_and_store_ood_pointers}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr dom_gen [out_of_domain_curr_row] padded_height *air_evaluation_result
swap 5
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr *air_evaluation_result [out_of_domain_curr_row] padded_height dom_gen
call {divide_out_zerofiers}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr *quotient_summands
{&put_ood_row_pointers_back_on_stack}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr *quotient_summands *ood_brow_curr *ood_erow_curr *odd_brow_nxt *ood_erow_nxt
dup 10
call {next_as_outofdomainquotientsegments}
hint out_of_domain_quotient_segments: Pointer = stack[0]
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr *quotient_summands *ood_brow_curr *ood_erow_curr *odd_brow_nxt *ood_erow_nxt *ood_quotient_segments
dup 0
push {out_of_domain_curr_row_quot_segments_pointer_alloc.write_address()}
write_mem {out_of_domain_curr_row_quot_segments_pointer_alloc.num_words()}
pop 1
/* Calculate `sum_of_evaluated_out_of_domain_quotient_segments` */
dup 10
{&OutOfDomainPoints::read_ood_point(OodPoint::CurrentRow)}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr *quotient_summands *ood_brow_curr *ood_erow_curr *odd_brow_nxt *ood_erow_nxt *ood_quotient_segments [ood_curr_row]
call {horner_evaluation_of_ood_curr_row_quot_segments}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *quot_cw_ws *quot_mr *quotient_summands *ood_brow_curr *ood_erow_curr *odd_brow_nxt *ood_erow_nxt [sum_of_evaluated_out_of_domain_quotient_segments]
/* Calculate inner product `out_of_domain_quotient_value` */
pick 4 place 9
pick 3 place 6
pick 8 pick 6
// _ *b_mr *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr [sum_of_evaluated_out_of_domain_quotient_segments] *quot_cw_ws *quotient_summands
call {inner_product_quotient_summands}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr [sum_of_evaluated_out_of_domain_quotient_segments] [out_of_domain_quotient_value]
/* Verify quotient's segments */
{&assert_top_two_xfes_eq}
// _ *b_mr *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr
/* Fiat-shamir 2 */
call {sample_beqd_weights}
hint beqd_weights = stack[0]
// _ *b_mr *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *beqd_ws
swap 10
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *b_mr
/* FRI */
// We need the `fri` data structure for field values later, so we preserve its pointer on the stack
dup 9
dup 8
call {fri_verify}
hint fri_revealed = stack[0]
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *b_mr *fri_revealed
/* Dequeue main-table rows and verify against its Merkle root */
dup 10
call {next_as_maintablerows}
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *b_mr *fri_revealed *btrows
dup 9
{&num_collinearity_checks_field}
read_mem 1
pop 1
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *b_mr *fri_revealed *btrows num_colli
dup 10
{&domain_length_field}
read_mem 1
pop 1
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *b_mr *fri_revealed *btrows num_colli dom_len
log_2_floor
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *b_mr *fri_revealed *btrows num_colli mt_height
pick 4
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *btrows num_colli mt_height *b_mr
dup 4
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *btrows num_colli mt_height *b_mr *fri_revealed
dup 4
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *btrows num_colli mt_height *b_mr *fri_revealed *btrows
call {verify_main_table_rows}
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *btrows
/* Dequeue and ignore main-table's authentication path */
dup 10
call {next_as_authentication_path}
pop 1
// _ *beqd_ws *p_iter *oodpnts *fri *e_mr *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *btrows
swap 7
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *e_mr
/* Dequeue aux-table rows and verify against its Merkle root */
dup 10
call {next_as_auxtablerows}
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *e_mr *etrows
pick 1
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_nxt *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *etrows *e_mr
dup 9
{&num_collinearity_checks_field}
read_mem 1
pop 1
dup 10
{&domain_length_field}
read_mem 1
pop 1
log_2_floor
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *etrows *e_mr num_colli mt_height
pick 2
dup 4
dup 4
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *etrows num_colli mt_height *e_mr *fri_revealed *etrows
call {verify_aux_table_rows}
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *quot_mr *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *etrows
/* Dequeue and ignore aux-table's authentication path */
dup 10
call {next_as_authentication_path}
pop 1
swap 5
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *quot_mr
/* Dequeue quotient-table rows and verify against its Merkle root */
dup 10
call {next_as_quotient_segment_elements}
swap 1
dup 9
{&num_collinearity_checks_field}
read_mem 1
pop 1
dup 10
{&domain_length_field}
read_mem 1
pop 1
log_2_floor
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems *quot_mr num_colli mt_height
pick 2
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems num_colli mt_height *quot_mr
dup 4
dup 4
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems num_colli mt_height *quot_mr *fri_revealed *qseg_elems
call {verify_quotient_segments}
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems
/* Various length asserts */
// assert!(num_combination_codeword_checks == quotient_segment_elements.len());
dup 8
{&num_collinearity_checks_field}
read_mem 1
pop 1
hint num_combination_codeword_checks = stack[0]
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems num_colli
// assert!(num_combination_codeword_checks == revealed_fri_indices_and_elements.len())
dup 2
read_mem 1
pop 1
dup 1
eq
assert error_id 234
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems num_colli
// assert!(num_combination_codeword_checks == main_table_rows.len());
dup 8
read_mem 1
pop 1
dup 1
eq
assert error_id 235
// assert!(num_combination_codeword_checks == aux_table_rows.len())
dup 6
read_mem 1
pop 1
dup 1
eq
assert error_id 236
// assert!(num_combination_codeword_checks == quotient_segment_elements.len());
dup 1
read_mem 1
pop 1
dup 1
eq
assert error_id 237
// _ *beqd_ws *p_iter *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems num_colli
/* Dequeue last authentication path, and verify that p_iter ends up in consistent state */
swap 12
swap 11
dup 0
call {next_as_authentication_path}
pop 1
call {drop_vm_proof_iter}
// _ num_colli *beqd_ws *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *fri_revealed *qseg_elems
/* Sum out-of-domain values */
// Goal for stack: `_ *ood_erow_curr *ood_brow_curr *beqd_ws`, preserving `*beqd_ws`.
dup 10
swap 2
// _ num_colli *beqd_ws *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *ood_brow_curr *ood_erow_curr *beqd_ws *qseg_elems *fri_revealed
swap 4
swap 1
swap 3
swap 2
// _ num_colli *beqd_ws *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *fri_revealed *qseg_elems *ood_erow_curr *ood_brow_curr *beqd_ws
call {inner_product_three_rows_with_weights_xfe_main} // expects arguments: *aux *main *ws
hint out_of_domain_curr_row_main_and_aux_value: XFieldElement = stack[0..3]
// _ num_colli *beqd_ws *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *fri_revealed *qseg_elems [ood_curr_beval]
push {ood_curr_row_main_and_aux_value_pointer_alloc.write_address()}
write_mem {ood_curr_row_main_and_aux_value_pointer_alloc.num_words()}
pop 1
// _ num_colli *beqd_ws *oodpnts *fri *btrows *odd_brow_next *etrows *ood_erow_nxt *fri_revealed *qseg_elems
// Goal: `_ *ood_erow_nxt *odd_brow_next *beqd_ws`, preserving `*beqd_ws`.
swap 2
swap 1
swap 4
dup 8
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems *ood_erow_nxt *odd_brow_next *beqd_ws
call {inner_product_three_rows_with_weights_xfe_main} // expects arguments: *aux *main *ws
hint out_of_domain_next_row_main_and_aux_value: XFieldElement = stack[0..3]
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems [ood_next_value]
push {ood_next_row_main_and_aux_value_pointer_alloc.write_address()}
write_mem {ood_next_row_main_and_aux_value_pointer_alloc.num_words()}
pop 1
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems
// Goal: `_ *quotient_segment_codeword_weights *ood_curr_row_quot_segments`
dup 6
{"ient_segment_codeword_weights_from_be_weights}
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems *quotient_segment_codeword_weights
push {out_of_domain_curr_row_quot_segments_pointer_alloc.read_address()}
read_mem {out_of_domain_curr_row_quot_segments_pointer_alloc.num_words()}
pop 1
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems *quotient_segment_codeword_weights *ood_curr_row_quot_segments
call {inner_product_4_xfes}
hint out_of_domain_curr_row_quotient_segment_value: XFieldElement = stack[0..3]
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems [out_of_domain_curr_row_quotient_segment_value]
push {ood_curr_row_quotient_segment_value_pointer_alloc.write_address()}
write_mem {ood_curr_row_quotient_segment_value_pointer_alloc.num_words()}
pop 1
// _ num_colli *beqd_ws *oodpnts *fri *btrows *fri_revealed *etrows *qseg_elems
// Put fri domain generator and domain offset on stack
swap 4
dup 0
{&domain_offset_field}
read_mem 1
pop 1
hint fri_domain_offset = stack[0]
// _ num_colli *beqd_ws *oodpnts *qseg_elems *btrows *fri_revealed *etrows *fri fri_offset
swap 1
{&domain_generator_field}
read_mem 1
pop 1
hint fri_domain_gen = stack[0]
// _ num_colli *beqd_ws *oodpnts *qseg_elems *btrows *fri_revealed *etrows fri_offset fri_gen
// adjust relevant pointers to point to last word in sequence, as they are traversed
// high-to-low in the main loop
// Adjust *fri_revealed (list) to point to last word
pick 3
dup 8
push {EXTENSION_DEGREE + 1} // size of element in `fri_revealed` list
mul
add
hint fri_revealed_elem = stack[0]
place 3
// Adjust *btrows (list) to point to last element
pick 4
addi 1
dup 8
addi -1
push {MasterMainTable::NUM_COLUMNS} // size of element of main row list
mul
add
hint main_table_row = stack[0]
place 4
// Adjust *etrows (list) to point to last element
pick 2
addi 1
dup 8
addi -1
push {MasterAuxTable::NUM_COLUMNS * EXTENSION_DEGREE} // size of element of aux row list
mul
add
hint aux_table_row = stack[0]
place 2
// Adjust *qseg_elems to point to last element
pick 5
addi 1
dup 8
addi -1
push {NUM_QUOTIENT_SEGMENTS * EXTENSION_DEGREE} // size of element of quot row list
mul
add
hint quotient_segment_elem = stack[0]
place 5
// _ num_colli *beqd_ws *oodpnts *qseg_elems *btrows *fri_revealed *etrows fri_offset fri_gen
/* reorganize stack for main-loop */
place 7
place 6
place 5
place 4
place 4
place 3
// _ num_colli fri_gen fri_offset *etrows_last_elem *btrows_last_elem *qseg_elems_last_elem *fri_revealed_last_elem *beqd_ws *oodpnts
call {main_loop_label}
// _ 0 fri_gen fri_offset *etrow_elem *btrows_elem *qseg_elem *fri_revealed_elem *beqd_ws *oodpnts
/* Cleanup stack */
pop 5 pop 4
return
{&main_loop}
)
}
}
#[cfg(test)]
pub mod tests {
use std::collections::HashMap;
use num_traits::ConstZero;
use tasm_object_derive::TasmObject;
use super::*;
use crate::execute_test;
use crate::maybe_write_debuggable_vm_state_to_disk;
use crate::memory::encode_to_memory;
use crate::memory::FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS;
use crate::test_helpers::maybe_write_tvm_output_to_disk;
use crate::verifier::claim::shared::insert_claim_into_static_memory;
use crate::verifier::master_table::air_constraint_evaluation::an_integral_but_profane_dynamic_memory_layout;
#[ignore = "Used for debugging when comparing two versions of the verifier"]
#[test]
fn verify_from_stored_proof_output() {
use std::fs::File;
let stark = File::open("stark.json").expect("stark file should open read only");
let stark: Stark = serde_json::from_reader(stark).unwrap();
let claim = File::open("claim.json").expect("claim file should open read only");
let claim_for_proof: Claim = serde_json::from_reader(claim).unwrap();
let proof = File::open("proof.json").expect("proof file should open read only");
let proof: Proof = serde_json::from_reader(proof).unwrap();
let snippet = StarkVerify {
stark,
memory_layout: MemoryLayout::conventional_dynamic(),
};
let mut nondeterminism = NonDeterminism::new(vec![]);
snippet.update_nondeterminism(&mut nondeterminism, &proof, &claim_for_proof);
let (claim_pointer, claim_size) =
insert_claim_into_static_memory(&mut nondeterminism.ram, &claim_for_proof);
let default_proof_pointer = BFieldElement::ZERO;
let mut init_stack = [
snippet.init_stack_for_isolated_run(),
vec![claim_pointer, default_proof_pointer],
]
.concat();
let code = snippet.link_for_isolated_run_populated_static_memory(claim_size);
let _final_tasm_state = execute_test(
&code,
&mut init_stack,
snippet.stack_diff(),
vec![],
nondeterminism,
None,
);
}
/// Run the verifier, and return the cycle count and inner padded
/// height for crude benchmarking.
fn test_verify_and_report_basic_features(
inner_nondeterminism: NonDeterminism,
inner_program: Program,
inner_public_input: &[BFieldElement],
stark: Stark,
layout: MemoryLayout,
) -> (usize, usize) {
let (mut non_determinism, claim_for_proof) = prove_and_get_non_determinism_and_claim(
inner_program.clone(),
inner_public_input,
inner_nondeterminism.clone(),
&stark,
);
let (claim_pointer, claim_size) =
insert_claim_into_static_memory(&mut non_determinism.ram, &claim_for_proof);
let default_proof_pointer = bfe!(0);
let snippet = StarkVerify {
stark,
memory_layout: layout,
};
let mut init_stack = [
snippet.init_stack_for_isolated_run(),
vec![claim_pointer, default_proof_pointer],
]
.concat();
let code = snippet.link_for_isolated_run_populated_static_memory(claim_size);
let program = Program::new(&code);
let mut vm_state = VMState::new(program, [].into(), non_determinism.clone());
vm_state.op_stack.stack = init_stack.clone();
maybe_write_debuggable_vm_state_to_disk(&vm_state);
let final_tasm_state = execute_test(
&code,
&mut init_stack,
snippet.stack_diff(),
vec![],
non_determinism,
None,
);
let (aet, _public_output) = VM::trace_execution(
inner_program,
(&claim_for_proof.input).into(),
inner_nondeterminism,
)
.unwrap();
let inner_padded_height = aet.padded_height();
(final_tasm_state.cycle_count as usize, inner_padded_height)
}
#[test]
fn different_fri_expansion_factors() {
const FACTORIAL_ARGUMENT: u32 = 3;
for log2_of_fri_expansion_factor in 2..=5 {
println!("log2_of_fri_expansion_factor: {log2_of_fri_expansion_factor}");
let factorial_program = factorial_program_with_io();
let stark = Stark::new(160, log2_of_fri_expansion_factor);
let (cycle_count, inner_padded_height) = test_verify_and_report_basic_features(
NonDeterminism::default(),
factorial_program,
&[FACTORIAL_ARGUMENT.into()],
stark,
MemoryLayout::conventional_static(),
);
println!(
"TASM-verifier of factorial({FACTORIAL_ARGUMENT}):\n
Fri expansion factor: {}\n
clock cycle count: {}.\n
Inner padded height was: {}",
1 << log2_of_fri_expansion_factor,
cycle_count,
inner_padded_height,
);
}
}
fn verify_tvm_proof_factorial_program_basic_properties(mem_layout: MemoryLayout) {
const FACTORIAL_ARGUMENT: u32 = 3;
let factorial_program = factorial_program_with_io();
let stark = Stark::default();
let (cycle_count, inner_padded_height) = test_verify_and_report_basic_features(
NonDeterminism::default(),
factorial_program,
&[FACTORIAL_ARGUMENT.into()],
stark,
mem_layout,
);
println!(
"TASM-verifier of factorial({FACTORIAL_ARGUMENT}):\n
clock cycle count: {}.\n
Inner padded height was: {}",
cycle_count, inner_padded_height,
);
}
#[test]
fn verify_tvm_proof_factorial_program_conventional_static_memlayout() {
verify_tvm_proof_factorial_program_basic_properties(MemoryLayout::conventional_static());
}
#[test]
fn verify_tvm_proof_factorial_program_conventional_dynamic_memlayout() {
verify_tvm_proof_factorial_program_basic_properties(MemoryLayout::conventional_dynamic());
}
#[test]
fn verify_tvm_proof_factorial_program_profane_dynamic_memlayout() {
verify_tvm_proof_factorial_program_basic_properties(MemoryLayout::Dynamic(
an_integral_but_profane_dynamic_memory_layout(),
));
}
pub(super) fn factorial_program_with_io() -> Program {
triton_program!(
read_io 1
push 1 // n accumulator
call factorial // 0 accumulator!
write_io 1
halt
factorial: // n acc
// if n == 0: return
dup 1 // n acc n
push 0 eq // n acc n==0
skiz // n acc
return // 0 acc
// else: multiply accumulator with n and recurse
dup 1 // n acc n
mul // n acc·n
pick 1 // acc·n n
addi -1 // acc·n n-1
place 1 // n-1 acc·n
recurse
)
}
/// Return data needed to verify a program execution's proof.
///
/// Prepares the caller so that the caller can call verify on a simple program
/// execution. Specifically, given an inner program, inner public input, inner
/// nondeterminism, and stark parameters; produce the proof, and use it to
/// populate non-determism (both memory and streams). Returns the claim that
/// the caller will then have to put into memory. Proof is stored on first
/// address of the ND-memory region.
pub fn prove_and_get_non_determinism_and_claim(
inner_program: Program,
inner_public_input: &[BFieldElement],
inner_nondeterminism: NonDeterminism,
stark: &Stark,
) -> (NonDeterminism, Claim) {
println!("Generating proof for non-determinism");
let inner_input = inner_public_input.to_vec();
let claim = Claim::about_program(&inner_program).with_input(inner_input.clone());
let (aet, inner_output) =
VM::trace_execution(inner_program, inner_input.into(), inner_nondeterminism).unwrap();
let claim = claim.with_output(inner_output);
triton_vm::profiler::start("inner program");
let proof = stark.prove(&claim, &aet).unwrap();
let profile = triton_vm::profiler::finish();
let padded_height = proof.padded_height().unwrap();
let report = profile
.with_cycle_count(aet.processor_trace.nrows())
.with_padded_height(padded_height)
.with_fri_domain_len(stark.fri(padded_height).unwrap().domain.length);
println!("Done generating proof for non-determinism");
println!("{report}");
assert!(
stark.verify(&claim, &proof).is_ok(),
"Proof from TVM must verify through TVM"
);
maybe_write_tvm_output_to_disk(stark, &claim, &proof);
let mut nondeterminism = NonDeterminism::new(vec![]);
let stark_verify = StarkVerify {
stark: *stark,
memory_layout: MemoryLayout::conventional_static(),
};
// Verify nd-digest count
let actual_num_extracted_digests = stark_verify
.extract_nondeterministic_digests(&proof, &claim)
.len();
let expected_num_extracted_digests =
stark_verify.number_of_nondeterministic_digests_consumed(&proof);
assert_eq!(
actual_num_extracted_digests, expected_num_extracted_digests,
"Number of extracted digests must match expected value"
);
stark_verify.update_nondeterminism(&mut nondeterminism, &proof, &claim);
encode_to_memory(
&mut nondeterminism.ram,
FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS,
&proof,
);
(nondeterminism, claim)
}
#[test]
fn verify_two_proofs() {
#[derive(Debug, Clone, BFieldCodec, TasmObject)]
struct TwoProofs {
proof1: Proof,
claim1: Claim,
proof2: Proof,
claim2: Claim,
}
let stark = Stark::default();
let stark_snippet = StarkVerify::new_with_dynamic_layout(stark);
let mut library = Library::new();
let stark_verify = library.import(Box::new(stark_snippet.clone()));
let proof1 = field!(TwoProofs::proof1);
let proof2 = field!(TwoProofs::proof2);
let claim1 = field!(TwoProofs::claim1);
let claim2 = field!(TwoProofs::claim2);
let verify_two_proofs_program = triton_asm! {
push {FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS}
// *two_proofs
dup 0
{&claim1}
dup 1
{&proof1}
// *two_proofs *claim1 *proof1
call {stark_verify}
dup 0
{&claim2}
dup 1
{&proof2}
// *two_proofs *claim2 *proof2
call {stark_verify}
halt
{&library.all_imports()}
};
let inner_input_1 = bfe_vec![3];
let factorial_program = factorial_program_with_io();
let (aet_1, inner_output_1) = VM::trace_execution(
factorial_program.clone(),
inner_input_1.clone().into(),
[].into(),
)
.unwrap();
let claim_1 = Claim::about_program(&factorial_program)
.with_input(inner_input_1.clone())
.with_output(inner_output_1.clone());
let proof_1 = stark.prove(&claim_1, &aet_1).unwrap();
let padded_height_1 = proof_1.padded_height().unwrap();
println!("padded_height_1: {padded_height_1}");
let inner_input_2 = bfe_vec![25];
let (aet_2, inner_output_2) = VM::trace_execution(
factorial_program.clone(),
inner_input_2.clone().into(),
[].into(),
)
.unwrap();
let claim_2 = Claim::about_program(&factorial_program)
.with_input(inner_input_2.clone())
.with_output(inner_output_2.clone());
let proof_2 = stark.prove(&claim_2, &aet_2).unwrap();
let padded_height_2 = proof_2.padded_height().unwrap();
println!("padded_height_2: {padded_height_2}");
let two_proofs = TwoProofs {
proof1: proof_1.clone(),
claim1: claim_1.clone(),
proof2: proof_2.clone(),
claim2: claim_2.clone(),
};
let mut memory = HashMap::<BFieldElement, BFieldElement>::new();
let outer_input = vec![];
encode_to_memory(
&mut memory,
FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS,
&two_proofs,
);
let mut outer_nondeterminism = NonDeterminism::new(vec![]).with_ram(memory);
let num_nd_digests_before = outer_nondeterminism.digests.len();
stark_snippet.update_nondeterminism(&mut outer_nondeterminism, &proof_1, &claim_1);
stark_snippet.update_nondeterminism(&mut outer_nondeterminism, &proof_2, &claim_2);
let num_nd_digests_after = outer_nondeterminism.digests.len();
assert_eq!(
num_nd_digests_after - num_nd_digests_before,
stark_snippet.number_of_nondeterministic_digests_consumed(&proof_1)
+ stark_snippet.number_of_nondeterministic_digests_consumed(&proof_2)
);
let program = Program::new(&verify_two_proofs_program);
let vm_state = VMState::new(
program.clone(),
outer_input.clone().into(),
outer_nondeterminism.clone(),
);
maybe_write_debuggable_vm_state_to_disk(&vm_state);
VM::run(program, outer_input.into(), outer_nondeterminism)
.expect("could not verify two STARK proofs");
println!(
"fact({}) == {} ∧ fact({}) == {}",
inner_input_1[0], inner_output_1[0], inner_input_2[0], inner_output_2[0]
);
assert_ne!(
padded_height_1, padded_height_2,
"proofs do not have different padded heights"
);
}
}
#[cfg(test)]
mod benches {
use std::cell::RefCell;
use std::rc::Rc;
use benches::tests::factorial_program_with_io;
use benches::tests::prove_and_get_non_determinism_and_claim;
use num_traits::ConstZero;
use super::*;
use crate::generate_full_profile;
use crate::linker::execute_bench;
use crate::linker::link_for_isolated_run;
use crate::memory::encode_to_memory;
use crate::snippet_bencher::write_benchmarks;
use crate::snippet_bencher::BenchmarkCase;
use crate::snippet_bencher::NamedBenchmarkResult;
use crate::test_helpers::prepend_program_with_stack_setup;
use crate::verifier::claim::shared::insert_claim_into_static_memory;
#[ignore = "Used for profiling the verification of a proof stored on disk."]
#[test]
fn profile_from_stored_proof_output() {
use std::fs::File;
let stark = File::open("stark.json").expect("stark file should open read only");
let stark: Stark = serde_json::from_reader(stark).unwrap();
let claim_file = File::open("claim.json").expect("claim file should open read only");
let claim_for_proof: Claim = serde_json::from_reader(claim_file).unwrap();
let proof = File::open("proof.json").expect("proof file should open read only");
let proof: Proof = serde_json::from_reader(proof).unwrap();
let snippet = StarkVerify {
stark,
memory_layout: MemoryLayout::conventional_static(),
};
let mut nondeterminism = NonDeterminism::new(vec![]);
snippet.update_nondeterminism(&mut nondeterminism, &proof, &claim_for_proof);
let (claim_pointer, claim_size) =
insert_claim_into_static_memory(&mut nondeterminism.ram, &claim_for_proof);
let default_proof_pointer = BFieldElement::ZERO;
let init_stack = [
snippet.init_stack_for_isolated_run(),
vec![claim_pointer, default_proof_pointer],
]
.concat();
let code = snippet.link_for_isolated_run_populated_static_memory(claim_size);
let program = prepend_program_with_stack_setup(&init_stack, &Program::new(&code));
let name = snippet.entrypoint();
let profile = generate_full_profile(
&name,
program,
&PublicInput::new(claim_for_proof.input),
&nondeterminism,
);
println!("{}", profile);
}
#[test]
fn benchmark_small_default_stark_static_memory() {
benchmark_verifier(
3,
1 << 8,
Stark::default(),
MemoryLayout::conventional_static(),
);
benchmark_verifier(
40,
1 << 9,
Stark::default(),
MemoryLayout::conventional_static(),
);
}
#[test]
fn benchmark_small_default_stark_dynamic_memory() {
benchmark_verifier(
3,
1 << 8,
Stark::default(),
MemoryLayout::conventional_dynamic(),
);
benchmark_verifier(
40,
1 << 9,
Stark::default(),
MemoryLayout::conventional_dynamic(),
);
}
#[ignore = "Takes a fairly long time. Intended to find optimal FRI expansion factor."]
#[test]
fn small_benchmark_different_fri_expansion_factors() {
for log2_of_fri_expansion_factor in 1..=5 {
let stark = Stark::new(160, log2_of_fri_expansion_factor);
benchmark_verifier(10, 1 << 8, stark, MemoryLayout::conventional_static());
benchmark_verifier(40, 1 << 9, stark, MemoryLayout::conventional_static());
benchmark_verifier(80, 1 << 10, stark, MemoryLayout::conventional_static());
}
}
#[ignore = "Takes a very long time. Intended to find optimal FRI expansion factor. Make sure to run
with `RUSTFLAGS=\"-C opt-level=3 -C debug-assertions=no\"`"]
#[test]
fn big_benchmark_different_fri_expansion_factors() {
let mem_layout = MemoryLayout::conventional_static();
for log2_of_fri_expansion_factor in 2..=3 {
let stark = Stark::new(160, log2_of_fri_expansion_factor);
benchmark_verifier(25600, 1 << 19, stark, mem_layout);
benchmark_verifier(51200, 1 << 20, stark, mem_layout);
benchmark_verifier(102400, 1 << 21, stark, mem_layout);
}
}
#[ignore = "Intended to generate data about verifier table heights as a function of inner padded
height. Make sure to run with `RUSTFLAGS=\"-C opt-level=3 -C debug-assertions=no\"`"]
#[test]
fn benchmark_verification_as_a_function_of_inner_padded_height() {
for (fact_arg, expected_inner_padded_height) in [
(10, 1 << 8),
(40, 1 << 9),
(80, 1 << 10),
(100, 1 << 11),
(200, 1 << 12),
(400, 1 << 13),
(800, 1 << 14),
(1600, 1 << 15),
(3200, 1 << 16),
(6400, 1 << 17),
(12800, 1 << 18),
(25600, 1 << 19),
(51200, 1 << 20),
(102400, 1 << 21),
] {
benchmark_verifier(
fact_arg,
expected_inner_padded_height,
Stark::default(),
MemoryLayout::conventional_static(),
);
}
}
fn benchmark_verifier(
factorial_argument: u32,
inner_padded_height: usize,
stark: Stark,
mem_layout: MemoryLayout,
) {
let (mut non_determinism, claim_for_proof) = prove_and_get_non_determinism_and_claim(
factorial_program_with_io(),
&[bfe!(factorial_argument)],
NonDeterminism::default(),
&stark,
);
let claim_pointer = BFieldElement::new(1 << 30);
encode_to_memory(&mut non_determinism.ram, claim_pointer, &claim_for_proof);
let default_proof_pointer = BFieldElement::ZERO;
let snippet = StarkVerify {
stark,
memory_layout: mem_layout,
};
let init_stack = [
snippet.init_stack_for_isolated_run(),
vec![claim_pointer, default_proof_pointer],
]
.concat();
let code = link_for_isolated_run(Rc::new(RefCell::new(snippet.clone())));
let benchmark = execute_bench(&code, &init_stack, vec![], non_determinism.clone(), None);
let benchmark = NamedBenchmarkResult {
name: format!(
"{}_inner_padded_height_{}_fri_exp_{}",
snippet.entrypoint(),
inner_padded_height,
stark.fri_expansion_factor
),
benchmark_result: benchmark,
case: BenchmarkCase::CommonCase,
};
write_benchmarks(vec![benchmark]);
let program = prepend_program_with_stack_setup(&init_stack, &Program::new(&code));
let name = snippet.entrypoint();
let profile = generate_full_profile(
&name,
program,
&PublicInput::new(claim_for_proof.input),
&non_determinism,
);
println!("{}", profile);
}
}