File size: 29,325 Bytes
4cba1a6 | 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 | # Sovereign Forge Developer Guide
Complete technical guide for Sovereign Forge contributors.
## Architecture Deep Dive - Five Verification Layers
Layer 1: Verifier (src/verifier/sov_verifier.c)
- Performs exact int64_t arithmetic with overflow detection
- Executes matrix stack machine operations
- Validates memory safety via safe_alloc_matrix()
- No type checking, no obligation generation
- Return codes: VER_OK, VER_OVERFLOW, VER_UNDERFLOW, VER_BUFFER_OVERFLOW, VER_NULL_INPUT
Layer 2: Type System (src/typecheck/sov_types.c)
- Maintains type environment Gamma: variable names to (type, shape)
- Implements stack push/pop/peek with type tracking
- Infers types for matrix operations
- Tracks shape constraints: (rows, cols) pairs
- Detects type mismatches before execution
Layer 3: Obligation Generation (src/obligations/sov_obligations.c)
- Generates obligations from type inference failures
- Links each failed constraint to source location
- Creates obligation lists for external verification
- Obligation types: overflow_checks, underflow_checks, shape_matches, dimension_bounds
Layer 4: Certificate (src/certificate/sov_cert.c)
- Wraps execution history: initial stack, final stack, type environment
- Records program hash (SHA-256), instruction count, execution duration
- Encodes obligations in CBOR (RFC 8949) format
- Produces canonical JSON for round-trip verification
- Includes Blake3 + Ed25519 signatures for tamper detection
Layer 5: Receipt Chain (src/receipts/)
- Appends certificates to immutable WORM (write-once read-many) ledger
- Tracks certificate lineage: previous_hash to current_hash
- Stores on-chain metadata: timestamp, source authority, verification status
- Enables external verification without access to original program
## Formal Semantics
Type judgments: Gamma |- i v tau, O, Gamma-prime
Where:
- Gamma = type environment (input)
- |- = yields or entails
- i = instruction code
- v = produces or executes to
- tau = type of result (MatrixI64, VectorI64, ScalarI64)
- O = obligations (constraints to verify)
- Gamma-prime = type environment (output)
Example 1: Gamma |- push(42) v ScalarI64, {}, Gamma
Example 2: Gamma |- mul v MatrixI64, {shape_match(A.cols, B.rows)}, Gamma
### How Obligations Are Generated
1. During type inference: sov_infer_instruction() checks compatibility
2. On mismatch: obligation record appended to obligation list
3. Obligation fields: type, pc, source_loc, constraint_text
## Contributing to the Verifier Core
### Exact Arithmetic Invariants
Critical invariants:
1. No silent overflow: check for int64_t overflow
2. Exact semantics: exact integer math only
3. Bounded dimensions: max 65536
4. Safe allocation: malloc() paired with overflow checks
### Safe Arithmetic Functions
Use exclusively:
- __builtin_add_overflow(a, b, &result)
- __builtin_sub_overflow(a, b, &result)
- __builtin_mul_overflow(a, b, &result)
### Testing: Write Conformance Test
1. Define test in tests/conformance/test_verifier.c
2. Register test in registry
3. Add adversarial case in tests/adversarial/
4. Run: make test-asan && make check-sanitizers
### Sanitizer: Run before Submitting
- make test-asan (Address Sanitizer + UBSan)
- make run-fuzzer (libFuzzer, 10 seconds)
- make clean && make all && make run-tests
Address Sanitizer detects: overflows, use-after-free, double-free, leaks
UBSan detects: integer overflow, division by zero, nullptr, out-of-bounds
### Overflow: Use __builtin_*_overflow
CORRECT: if (__builtin_add_overflow(a, b, &result)) return VER_OVERFLOW;
WRONG: int64_t result = a + b;
## Adding New Verification Operations
### Step 1: spec/instruction-semantics.md
Add formal stack type transition.
### Step 2: spec/type-rules.md
Document preconditions, postconditions, obligations.
### Step 3: src/verifier/sov_verifier.c
1. Add operation code to .h
2. Implement verification function
3. Register in main dispatch
### Step 4: Add Tests
Conformance and adversarial test cases.
### Step 5: spec/verification-policy.md
Document obligation type and discharge.
## Formal Verification Workflow
### Step 1: Write Lean 4 Proof
Create theorem in proofs/lean4/.
### Step 2: Extract to C
lean extract sov_custom_op.lean --output sov_custom_op_extracted.c
### Step 3: Verify C Matches
Property tests in tests/property/.
### Step 4: Publish Together
Link theorem to implementation.
## Build System
### Makefile.sov Overview
Main build: Makefile.sov (repo root)
Variables: CC, CFLAGS, VERIFIER_SRCS, CERT_SRCS, OBL_SRCS, TYPE_SRCS
Tests: CONFORMANCE_TESTS, ADVERSARIAL_TESTS
### Available Targets
all - Build libsov_forge.a + tests
libsov_forge.a - Static library
test_verifier - Conformance (38 tests)
test_infer - Type inference
run-tests - Execute all
test-asan - With ASan + UBSan
check-sanitizers - Validate 46 tests
run-fuzzer - libFuzzer (10s)
clean - Remove artifacts
### Adding New Test
1. Create tests/conformance/test_new_feature.c
2. Update Makefile.sov
3. Register in runner
4. Build: make clean && make test-asan
## Code Style
### C99, K&R Braces, 80-Column Comments
Function:
VerifyResult sov_verify_operation(SafeMatrix *a, SafeMatrix *b)
{
if (!a || !b) {
return VER_NULL_INPUT;
}
return VER_OK;
}
K&R placement:
- Opening brace on same line as function/if/loop
- Closing brace on own line
### No C++ Features
ALLOWED: stdlib.h, string.h, stdint.h, limits.h
FORBIDDEN: vector, iostream, class
### Explicit Error Returns
CORRECT: return VER_OVERFLOW
WRONG: silent wrap or undefined behavior
### Public Functions: extern in .h, definition in .c
Header: extern declaration
Implementation: function body
## Security Review Checklist
### Before Code Review
- No unbounded loops: max 65536
- No string formatting: use memcpy
- No floats: int64_t only
- All mallocs: overflow check
- All buffer accesses: bounds check
### Overflow Checking
Addition: __builtin_add_overflow(a, b, &res)
Subtraction: __builtin_sub_overflow(a, b, &res)
Multiplication: __builtin_mul_overflow(a, b, &res)
Size: manual check before malloc
### Memory Safety
make clean
make CC=gcc CFLAGS="-fsanitize=address -g" test-asan
valgrind --leak-check=full ./tests/conformance/test_verifier
make CC=gcc CFLAGS="-fsanitize=undefined -g" test-asan
## Release Process
### Version Format
Development: vX.Y.Z-rcN
Stable: vX.Y.Z
### Signed Tags
git tag -s -m "Release v1.0.0" v1.0.0
git tag -v v1.0.0
git push origin v1.0.0
### GitHub Releases
gh release create v1.0.0 --title "Sovereign Forge v1.0.0"
make clean && make all
gpg --armor --detach-sign libsov_forge.a
sha256sum libsov_forge.a > libsov_forge.a.sha256
gh release upload v1.0.0 libsov_forge.a libsov_forge.a.asc libsov_forge.a.sha256
### Announce Release
GitHub Discussions
Academic mailing lists
Security communities
### Release Checklist
- Version bumped in src/verifier.h
- CHANGELOG.md updated
- All tests pass: make check-sanitizers
- Fuzzer runs 10s without crash: make run-fuzzer
- README.md current
- Signed tag created
- Artifacts built on clean checkout
- Reproducible build hash computed
- GitHub release created
- Announcement posted
- Tagged commit pushed
## Questions & Support
1. Read spec: spec/instruction-semantics.md, spec/type-rules.md
2. Check tests: tests/conformance/, tests/adversarial/
3. File issue: Include reproduction steps, sanitizer output
4. Submit PR: Reference issue, include tests, pass all checks
---
Last updated: 2026-07-29
Maintainers: Sovereign Forge Team
License: Apache 2.0 + AGPL 3.0
## Detailed Implementation Guide
### Safe Memory Management Deep Dive
#### Allocation with Overflow Prevention
Safe matrix allocation requires checking for multiplication overflow:
Step 1: Check rows * cols overflow size_t
if (rows > 0 && cols > SIZE_MAX / rows) return NULL;
Step 2: Compute safe element count
size_t nelems = rows * cols;
Step 3: Check nelems * sizeof(int64_t) overflow
if (nelems > 0 && nelems > SIZE_MAX / sizeof(int64_t)) return NULL;
Step 4: Compute safe byte size
size_t data_size = nelems * sizeof(int64_t);
Step 5: Allocate struct
SafeMatrix *m = (SafeMatrix *)malloc(sizeof(SafeMatrix));
if (!m) return NULL;
Step 6: Allocate data buffer
if (nelems > 0) {
m->data = (int64_t *)malloc(data_size);
if (!m->data) { free(m); return NULL; }
}
Step 7: Initialize and return
m->rows = rows; m->cols = cols; m->capacity = nelems;
return m;
#### Buffer Validation
Every buffer access must be validated first:
VerifyResult validate_matrix_buffer(
const int64_t *data, size_t rows, size_t cols, size_t capacity) {
if (!data) return VER_NULL_INPUT;
if (rows > 0 && cols > SIZE_MAX / rows) return VER_OVERFLOW;
size_t required = rows * cols;
if (required > capacity) return VER_BUFFER_OVERFLOW;
return VER_OK;
}
### Type System Implementation
#### Type Environment Management
Type environment stores bindings from variable names to types:
typedef struct {
char **var_names;
ValType *var_types;
Shape *var_shapes;
uint32_t num_vars;
} TypeEnv;
Operations:
- TypeEnv *sov_tyenv_new() creates new empty environment
- int sov_tyenv_add_var() adds binding (name, type, shape)
- int sov_tyenv_lookup_var() retrieves binding for name
- void sov_tyenv_free() deallocates environment
Capacity: 64 variables maximum to prevent unbounded allocation
#### Stack Operations with Type Tracking
Stack value structure includes ownership tracking:
typedef struct {
ValType type;
size_t rows;
size_t cols;
void *data;
int is_owned;
} StackValue;
Stack operations:
- Stack *sov_stack_new() creates stack
- int sov_stack_push() pushes value, check depth < 256
- int sov_stack_pop() pops value, check depth > 0
- StackValue *sov_stack_peek() access top without pop
- void sov_stack_free() deallocates stack
#### Type Inference Engine
Main inference function processes each instruction:
TypeCheckResult sov_infer_instruction(
TypeEnv *env, Stack *stack, uint8_t opcode, uint8_t *operands)
For each instruction:
1. Check preconditions (stack depth, type constraints)
2. Pop input values from stack
3. Infer output type and shape
4. Generate obligations if constraints violated
5. Push result onto stack
6. Update type environment if necessary
### Obligation Generation Details
#### Obligation Record Structure
typedef struct {
uint8_t type;
uint32_t pc;
uint32_t source_line;
char constraint_text[256];
uint8_t severity;
} Obligation;
Types: OBLIG_STACK_UNDERFLOW, OBLIG_STACK_OVERFLOW,
OBLIG_OVERFLOW, OBLIG_DIMENSION_BOUNDS, OBLIG_SHAPE_MISMATCH,
OBLIG_TYPE_MISMATCH, OBLIG_NULL_POINTER, OBLIG_MEMORY_BOUNDS
#### Obligation List Management
typedef struct {
Obligation *obligations;
uint32_t count;
uint32_t capacity;
} ObligationList;
Operations:
- ObligationList *sov_obligations_new() creates empty
- int sov_obligations_add() appends and expands if needed
- void sov_obligations_free() deallocates all
### Certificate Encoding
#### CBOR Serialization
Canonical CBOR encoding for portability:
Major types: 0=uint, 1=nint, 2=bstr, 3=tstr, 4=array, 5=map
Certificate structure (CBOR map):
0: program_hash (bytes, 32)
1: initial_stack (array of integers)
2: final_stack (array of integers)
3: obligations (array of maps)
4: timestamp (integer, UNIX seconds)
5: program_size (integer)
6: instruction_count (integer)
7: type_env_snapshot (map)
#### Canonical JSON Generation
JSON representation for human readability and verification:
- All object keys sorted alphabetically
- No whitespace (compact form)
- Numbers encoded as decimal strings for exact precision
- Hashes encoded as hex strings (lowercase)
#### Signature Generation
1. Compute Blake3 hash of canonical JSON
2. Sign Blake3 digest with Ed25519 private key
3. Include public key in certificate metadata
4. Signature = 64 bytes (Ed25519)
5. Public key = 32 bytes (Ed25519)
### Receipt Chain Architecture
#### WORM Ledger Structure
Write-once read-many ledger stores immutable receipt history:
Receipt structure:
index: receipt number (monotonically increasing)
certificate_hash: SHA-256 of certificate
previous_hash: SHA-256 of previous receipt
timestamp: UNIX seconds when appended
source_authority: identifier of appender
verification_status: VERIFIED or UNVERIFIED
Ledger file format (JSONL):
- One receipt per line
- Each line is a complete JSON object
- Immutable: never modify or delete lines
#### Hash Chain Verification
To verify receipt N at index I:
1. Read receipt N: has certificate_hash and previous_hash
2. Read receipt N-1: has certificate_hash
3. Compute SHA-256 of receipt N-1 serialization
4. Verify computed hash = previous_hash of N
5. Continue to first receipt (previous_hash should be zero)
### Test Structure
#### Conformance Test Framework
Conformance tests verify correct behavior with known inputs:
void test_operation_case(void) {
SafeMatrix *input = safe_alloc_matrix(...);
VerifyResult result = sov_verify_operation(...);
ASSERT_EQ(result, VER_OK);
ASSERT_EQ(output->data[i], EXPECTED_VALUE);
safe_free_matrix(input);
}
Test cases should cover:
- Boundary values (0, 1, MAX)
- Normal/typical inputs
- Edge cases (empty matrices, dimension 1)
- Full rows and columns
#### Adversarial Test Framework
Adversarial tests verify error handling:
void test_operation_failure(void) {
SafeMatrix *a = safe_alloc_matrix(2, 3);
SafeMatrix *b = safe_alloc_matrix(5, 4);
VerifyResult result = sov_verify_operation(a, b, ...);
ASSERT_EQ(result, VER_SHAPE_MISMATCH);
safe_free_matrix(a);
safe_free_matrix(b);
}
Error cases should cover:
- Null pointers (NULL input)
- Shape mismatches (cols != rows)
- Overflow inputs (INT64_MAX)
- Underflow stack depth (empty stack)
- Dimension bounds (>65536)
#### Fuzzing with libFuzzer
libFuzzer generates random test inputs:
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
Fuzz target:
1. Creates random matrices from fuzz input bytes
2. Calls verifier with random inputs
3. Checks for crashes, hangs, undefined behavior
Running: make run-fuzzer (runs for 10 seconds)
### Performance Considerations
#### Complexity Analysis
Matrix multiplication: O(m * n * p) for m x n times n x p
For max dimensions (65536 x 65536 x 65536):
- Would require massive memory
- Not practical in real systems
- Bounded by memory allocation
Element-wise operations: O(m * n)
#### Benchmarking
Benchmark template:
1. Allocate input matrices
2. Record start time with clock_gettime()
3. Execute operation
4. Record end time
5. Compute elapsed time in nanoseconds
6. Report per-element processing rate
### Integration Testing
#### Integration Test Scenarios
Scenario 1: Multi-operation sequence
- Load matrix A
- Load matrix B
- Multiply A*B = C
- Transpose C = D
- Verify final result
Scenario 2: Type inference with all operations
- Execute sequence of operations
- Verify type environment evolves correctly
- Check all shapes match expectations
Scenario 3: Full verification pipeline
- Execute program bytecode
- Generate type environment
- Generate obligations
- Create certificate
- Sign with Ed25519
- Append to WORM ledger
- Verify round-trip
#### Cross-layer Testing
Test Layer 1 (Verifier) alone: arithmetic, buffers
Test Layer 1-2: type inference, shapes
Test Layer 1-3: obligation generation
Test Layer 1-4: certificate serialization, signatures
Test Layer 1-5: full end-to-end with WORM ledger
### Documentation Standards
#### Function Documentation
Every public function requires complete documentation:
/*
* sov_verify_matrix_multiply - Matrix multiplication with overflow.
*
* Description:
* Multiplies matrix A (m x n) by matrix B (n x p) to produce
* result (m x p). Performs exact arithmetic with int64_t
* overflow detection. Shape constraint: a->cols must equal
* b->rows.
*
* Arguments:
* a: Input matrix (m x n), must not be NULL, capacity >= m*n
* b: Input matrix (n x p), must not be NULL, capacity >= n*p
* result: Output (m x p), must not be NULL, capacity >= m*p
*
* Returns:
* VER_OK on success.
* VER_NULL_INPUT if argument is NULL.
* VER_SHAPE_MISMATCH if a->cols != b->rows.
* VER_OVERFLOW if multiply or add overflows.
* VER_BUFFER_OVERFLOW if result buffer too small.
*
* Side effects:
* Modifies result->data in place with product matrix.
*
* Thread safety:
* Not thread-safe. Caller must synchronize accesses.
*/
#### Test Documentation
Each test file documents its purpose:
/*
* test_verifier.c - Conformance tests for verifier layer.
*
* Tests the arithmetic correctness and safety properties of all
* verifier operations. Each test covers one operation with
* specific inputs designed to validate the operation's type
* signature and overflow behavior.
*
* Test coverage:
* - 38 conformance tests for all operations
* - Known inputs and expected outputs
* - Boundary values (0, 1, MAX, overflow)
*/
### Code Review Checklist
When reviewing new operations:
1. Specification validation
- Stack type judgment in spec/instruction-semantics.md
- Type rule in spec/type-rules.md
- Obligation types in spec/verification-policy.md
2. Implementation quality
- All pointer arguments checked for NULL
- All arithmetic uses __builtin_*_overflow()
- All buffer accesses validated with bounds check
- No unbounded loops (max 65536)
- Memory allocation checks for overflow
3. Test coverage
- Conformance test with known values
- Adversarial test with shape mismatch
- Adversarial test with overflow
- Adversarial test with null pointer
- All tests pass with make test-asan
4. Documentation
- Function header comment complete
- Return codes documented
- Side effects documented
- Thread safety documented
5. Build system
- Builds with make all without warnings
- Tests registered in Makefile.sov
- make clean removes all artifacts
- make run-fuzzer completes without crash
6. Security
- No format strings
- No unbounded string operations
- No floating-point arithmetic
- No cryptographic shortcuts
- All overflow cases handled
### Continuous Integration
#### CI Pipeline Requirements
The CI system must verify:
1. Compilation stage
- Compile with gcc -std=c99 -Wall -Wextra
- Compile with clang for comparison
- All warnings are errors
2. Testing stage
- Run make test-asan (sanitizers)
- Run make run-fuzzer (10 seconds)
- Run make check-sanitizers (46 tests)
3. Coverage stage
- Generate coverage report
- Fail if coverage < 90%
4. Reproducibility stage
- Clean checkout
- Build with make all
- Compute SHA-256 of all artifacts
5. Security stage
- Run static analysis tools
- Check for common vulnerabilities
### Troubleshooting Common Issues
#### Build Failures
Issue: Undefined reference to function
Solution: Ensure function is defined in .c file and declared in .h
Issue: Compilation warning about unused variable
Solution: Remove unused variable or prefix with _ to suppress
Issue: Test fails but works locally
Solution: Check for timezone/locale dependencies, use UTC
#### Sanitizer Reports
Issue: SEGV (segmentation fault)
Solution: Null pointer dereference - add NULL check before use
Issue: Heap buffer overflow
Solution: Validate buffer size before access - check bounds
Issue: Undefined Behavior Sanitizer integer overflow
Solution: Use __builtin_*_overflow instead of raw operators
### Performance Optimization
#### Micro-optimizations
1. Inline simple functions to reduce call overhead
2. Avoid repeated bounds checks in tight loops
Bad: for (i) { validate_access(data, i); data[i] = ... }
Good: validate_once(); for (i) { data[i] = ... }
3. Use const pointers to enable compiler optimizations
const int64_t *src = a->data;
4. Cache matrix dimensions in local variables
size_t rows = m->rows; size_t cols = m->cols;
#### Profiling
Profile operations to identify bottlenecks:
perf record -g ./test_verifier
perf report
This shows:
- Time spent in each function
- Cache misses and page faults
- Branch prediction failures
---
Document History:
- 2026-07-29: Complete version with 1200+ lines
## Advanced Topics
### Extending to Higher-Order Tensors
The current architecture supports 2D matrices. To extend:
1. Update Shape structure:
typedef struct {
uint8_t ndim; /* number of dimensions (2-4) */
size_t dims[4]; /* dimensions for each axis */
} Shape;
2. Update StackValue to use extended Shape
3. Implement tensor operations:
- 3D tensor multiplication (batched matrix multiply)
- Tensor transpose (arbitrary axis permutation)
- Tensor reshape (change dimensions)
- Tensor slice (extract subregion)
4. Add type rules for tensor operations
5. Implement conformance and adversarial tests
### Parallelization Strategy
To parallelize matrix operations:
1. Partition matrix by rows (row-wise stripping)
- Each thread computes subset of result rows
- No false sharing if chunks large enough
2. Use OpenMP for thread pool:
#pragma omp parallel for
for (size_t i = 0; i < result_rows; i++) {
for (size_t j = 0; j < result_cols; j++) {
/* compute result[i][j] */
}
}
3. Ensure atomicity of overflow checks
- Overflow is local to each thread
- Reduce error codes after computation
### GPU Acceleration
To use GPU (CUDA/OpenCL):
1. Create GPU memory management layer
- Copy SafeMatrix from host to device
- Ensure device allocations check overflow
- Copy results back to host
2. Implement GPU kernels for each operation
- Matrix multiply (highly parallel)
- Element-wise operations
- Reductions (transpose, dot product)
3. Benchmark GPU vs CPU
- GPU beneficial for large matrices (>1000x1000)
- CPU better for small matrices (overhead)
### Distributed Computing
To support distributed matrices:
1. Partitioning strategy:
- 2D block partitioning across nodes
- Each node stores block and computation logic
2. Communication protocol:
- Send matrix blocks via MPI or gRPC
- Ensure integrity of transmitted data
3. Consistency model:
- Strong consistency: wait for all writes
- Eventual consistency: read-repair
### Persistence and Recovery
To support persistence:
1. Checkpointing strategy:
- Periodic snapshots of all matrices
- Save to disk with CRC checksums
2. Recovery on failure:
- Load latest checkpoint
- Verify checksum
- Replay subsequent operations
3. Transaction log:
- Write-ahead log (WAL) for durability
- Each operation logged before execution
- Replay log after crash
## API Documentation
### Public Headers
#### src/verifier/sov_verifier.h
Core verification functions:
SafeMatrix* safe_alloc_matrix(size_t rows, size_t cols)
- Allocate matrix with overflow checking
- Returns NULL on allocation failure
- Capacity automatically set
void safe_free_matrix(SafeMatrix *m)
- Deallocate matrix and its data
- Safe to call with NULL
VerifyResult validate_matrix_buffer(
const int64_t *data, size_t rows, size_t cols, size_t capacity)
- Validate buffer size before access
- Returns VER_OK or specific error
VerifyResult sov_verify_push_constant(SafeMatrix *stack, int64_t value)
- Push constant value onto stack
- Stack must be allocated
VerifyResult sov_verify_pop(SafeMatrix *stack)
- Pop value from stack
- Check underflow before pop
#### src/typecheck/sov_types.h
Type inference functions:
TypeEnv *sov_tyenv_new(void)
- Create new type environment
- Empty environment with capacity 64
int sov_tyenv_add_var(TypeEnv *env, const char *name,
ValType type, size_t rows, size_t cols)
- Add variable binding
- Check capacity not exceeded
#### src/obligations/sov_obligations.h
Obligation management:
ObligationList *sov_obligations_new(void)
- Create empty obligation list
- Initial capacity 32
int sov_obligations_add(ObligationList *list,
uint8_t type, uint32_t pc, const char *text)
- Append obligation to list
- Auto-expand if capacity exceeded
#### src/certificate/sov_cert.h
Certificate functions:
ProofCertificate *sov_cert_new(void)
- Create new certificate
- Version 1, timestamp set
int sov_cert_set_stacks(ProofCertificate *cert,
const int64_t *init, size_t init_len,
const int64_t *final, size_t final_len)
- Set initial and final stacks
- Allocate and copy data
int sov_cert_to_cbor(ProofCertificate *cert,
uint8_t *output, size_t output_size, size_t *written)
- Serialize certificate to CBOR
- Return number of bytes written
#### src/receipts/sov_receipts.h
Receipt ledger functions:
WormLedger *sov_ledger_open(const char *filepath)
- Open WORM ledger file
- Create if doesn't exist
int sov_ledger_append(WormLedger *ledger, const Receipt *receipt)
- Append receipt to ledger
- Assign index automatically
- Verify hash chain
Receipt *sov_ledger_read(WormLedger *ledger, uint64_t index)
- Read receipt at index
- Verify hash chain integrity
## Version History
### v1.0.0 (2026-07-29)
- Complete 5-layer architecture
- 46 conformance + adversarial tests
- ASan/UBSan sanitizer integration
- CBOR certificate encoding
- Ed25519 signature support
- WORM receipt ledger
### Future v1.1.0
- 3D/4D tensor support
- OpenMP parallelization
- GPU acceleration
- Distributed matrix support
---
Complete Sovereign Forge Developer Guide
Last updated: 2026-07-29
For questions: See Questions & Support section
## Appendices
### Appendix A: Common Error Codes
VER_OK = 0x00
Operation completed successfully.
VER_NULL_INPUT = 0x01
One or more required pointer arguments is NULL.
Caller must provide valid non-NULL pointers.
VER_UNDERFLOW = 0x02
Stack depth less than required by operation.
Operation needs N stack values but has M < N.
VER_OVERFLOW = 0x03
Integer arithmetic overflow detected in computation.
Result exceeds int64_t range [INT64_MIN, INT64_MAX].
VER_BUFFER_OVERFLOW = 0x04
Buffer access exceeds allocated capacity.
Attempt to access index >= capacity.
VER_SHAPE_MISMATCH = 0x05
Matrix dimensions incompatible for operation.
Example: multiply requires cols[A] == rows[B].
VER_DIMENSION_OVERFLOW = 0x06
Result matrix dimensions exceed maximum (65536).
Example: multiply 65536x65536 * 65536x65536.
VER_TYPE_MISMATCH = 0x07
Type system constraint violated.
Stack value type does not match expected type.
VER_MEMORY_ERROR = 0x08
Memory allocation failed (malloc returned NULL).
System out of memory or allocation too large.
### Appendix B: Operation Reference
All operations with stack transitions:
PUSH <value>
Stack: [...] -> [..., <value>]
Type: any scalar value
POP
Stack: [..., X] -> [...]
Type: X can be any type
ADD
Stack: [..., A, B] -> [..., A+B]
Type: scalar, scalar -> scalar
MUL (matrix multiply)
Stack: [..., A, B] -> [..., A*B]
Type: matrix m*n, matrix n*p -> matrix m*p
TRANSPOSE
Stack: [..., A] -> [..., A^T]
Type: matrix m*n -> matrix n*m
SCALE
Stack: [..., A, s] -> [..., s*A]
Type: matrix, scalar -> matrix
DOT
Stack: [..., A, B] -> [..., sum(A*B)]
Type: vector, vector -> scalar
### Appendix C: Build Commands Reference
make all
Build library and all tests. Default target.
make libsov_forge.a
Build static library only (no tests).
make test_verifier
Build and link verifier conformance tests.
make run-tests
Compile and run all tests with output.
make test-asan
Build with ASan/UBSan and run conformance+adversarial.
make check-sanitizers
Alias for test-asan (same functionality).
make run-fuzzer
Build libFuzzer and run for 10 seconds.
make clean
Remove all compiled objects, binaries, archives.
### Appendix D: Environment Setup
On Linux (Ubuntu/Debian):
sudo apt update
sudo apt install build-essential gcc clang valgrind
On macOS (Homebrew):
brew install gcc clang valgrind
On Windows (MSYS2):
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-toolchain
### Appendix E: Git Workflow
1. Clone repository:
git clone https://github.com/USER/sovereign-forge.git
cd sovereign-forge
2. Create feature branch:
git checkout -b feature/new-operation
3. Make changes:
vim src/verifier/sov_verifier.c
vim tests/conformance/test_verifier.c
4. Build and test:
make clean && make test-asan
5. Commit changes:
git add src/verifier/sov_verifier.c tests/conformance/test_verifier.c
git commit -m "feat: add new operation"
6. Push to GitHub:
git push origin feature/new-operation
7. Create pull request on GitHub
8. After review, merge to main:
git checkout main
git pull origin main
git merge --no-ff feature/new-operation
git push origin main
### Appendix F: Debugging Tips
#### Using GDB
gdb ./tests/conformance/test_verifier
(gdb) break sov_verify_multiply
(gdb) run
(gdb) print result->data[0]
(gdb) continue
#### Using Valgrind
valgrind --leak-check=full ./tests/conformance/test_verifier
#### Sanitizer Output Interpretation
SUMMARY: AddressSanitizer: 0 errors
Indicates no memory errors detected.
SUMMARY: UndefinedBehaviorSanitizer: integer-overflow
Indicates integer overflow without the builtin check.
Fix by wrapping with __builtin_*_overflow().
---
End of Sovereign Forge Developer Guide
Complete technical reference for all contributors
|