-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fri): implement FRI protocol with open and verify methods
Implement FRI (Fast Reed-Solomon Interactive Oracle Proof) protocol with open and verify methods. Add helper functions for RS encoding, folding, and query verification. Update tests to cover new functionality.
- Loading branch information
Showing
2 changed files
with
210 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from unittest import TestCase, main | ||
import sys | ||
|
||
sys.path.append('../src') | ||
sys.path.append('src') | ||
|
||
from fri import FRI | ||
from utils import is_power_of_two | ||
from unipolynomial import UniPolynomial | ||
class TestFRI(TestCase): | ||
def setUp(self): | ||
# Set up a scalar field for testing (e.g., integers modulo a prime) | ||
prime = 193 # A small prime for testing | ||
UniPolynomial.set_scalar(int, lambda x: x % prime) | ||
|
||
def test_fold(self): | ||
from sage.all import GF | ||
from field import magic | ||
|
||
Fp = magic(GF(193)) | ||
|
||
evals = FRI.rs_encode_single([2, 3, 4, 5], [Fp.primitive_element() ** (i * 192 // 16) for i in range(16)], 4) | ||
coset = Fp.primitive_element() ** (192 // len(evals)) | ||
alpha = Fp(7) | ||
|
||
evals = FRI.fold(evals, alpha, coset, debug=True) | ||
coset = coset ** 2 | ||
evals = FRI.fold(evals, alpha, coset, debug=True) | ||
|
||
assert evals[0] == evals[1] == evals[2] == evals[3] | ||
|
||
def test_low_degree(self): | ||
from sage.all import GF | ||
from field import magic | ||
from random import randint | ||
|
||
Fp = magic(GF(193)) | ||
|
||
assert Fp.primitive_element() ** 192 == 1 | ||
|
||
degree_bound = 8 | ||
blow_up_factor = 4 | ||
num_verifier_queries = 8 | ||
|
||
assert is_power_of_two(degree_bound) | ||
|
||
evals = FRI.rs_encode_single([randint(0, 193) for _ in range(degree_bound)], [Fp.primitive_element() ** (i * 192 // (degree_bound * 2 ** blow_up_factor)) for i in range(degree_bound * 2 ** blow_up_factor)], 2 ** blow_up_factor) | ||
proof = FRI.prove_low_degree(evals, 2 ** blow_up_factor, degree_bound, Fp.primitive_element() ** (192 // len(evals)), num_verifier_queries, debug=False) | ||
FRI.verify_low_degree(degree_bound, 2 ** blow_up_factor, proof, Fp.primitive_element() ** (192 // len(evals)), num_verifier_queries, debug=False) | ||
|
||
def test_open(self): | ||
from sage.all import GF | ||
from field import magic | ||
from random import randint | ||
|
||
Fp = magic(GF(193)) | ||
|
||
assert Fp.primitive_element() ** 192 == 1 | ||
|
||
rate = 4 | ||
evals_size = 4 | ||
coset = Fp.primitive_element() ** (192 // (evals_size * rate)) | ||
point = coset ** 0 * Fp.primitive_element() | ||
evals = [randint(0, 193) for i in range(evals_size)] | ||
value = UniPolynomial.uni_eval_from_evals(evals, point, [coset ** i for i in range(len(evals))]) | ||
proof = FRI.open(evals, rate, point, coset, [coset ** i for i in range(evals_size * rate)], debug=False) | ||
FRI.verify(proof['degree_bound'], evals_size, rate, proof, point, value, [coset ** i for i in range(evals_size * rate)], coset, debug=False) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |