STARK中的FRI代码解析
Posted mutourend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STARK中的FRI代码解析相关的知识,希望对你有一定的参考价值。
1. 引言
前序博客有:
- STARK入门知识
- STARKs and STARK VM: Proofs of Computational Integrity
- Fast Reed-Solomon Interactive Oracle Proofs of Proximity学习笔记
相关代码见:
测试用例有:
$ pytest
=================================================================== test session starts ====================================================================
platform darwin -- Python 3.9.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/lanyu/zyd/stark-anatomy/code
collected 21 items
test_fast_stark.py F [ 4%]
test_fri.py . [ 9%]
test_ip.py F [ 14%]
test_merkle.py . [ 19%]
test_multivariate.py .. [ 28%]
test_ntt.py ...... [ 57%]
test_rescue_prime.py .. [ 66%]
test_rpsss.py .. [ 76%]
test_stark.py F [ 80%]
test_univariate.py .... [100%]
========================================================================= FAILURES =========================================================================
_____________________________________________________________________ test_fast_stark ______________________________________________________________________
def test_fast_stark( ):
field = Field.main()
expansion_factor = 4
num_colinearity_checks = 2
security_level = 2
rp = RescuePrime()
output_element = field.sample(bytes(b'0xdeadbeef'))
for trial in range(0, 20):
input_element = output_element
print("running trial with input:", input_element.value)
output_element = rp.hash(input_element)
num_cycles = rp.N+1
state_width = rp.m
stark = FastStark(field, expansion_factor, num_colinearity_checks, security_level, state_width, num_cycles)
transition_zerofier, transition_zerofier_codeword, transition_zerofier_root = stark.preprocess()
# prove honestly
print("honest proof generation ...")
# prove
trace = rp.trace(input_element)
air = rp.transition_constraints(stark.omicron)
boundary = rp.boundary_constraints(output_element)
proof = stark.prove(trace, air, boundary, transition_zerofier, transition_zerofier_codeword)
# verify
verdict = stark.verify(proof, air, boundary, transition_zerofier_root)
assert(verdict == True), "valid stark proof fails to verify"
print("success \\\\o/")
print("verifying false claim ...")
# verify false claim
output_element_ = output_element + field.one()
boundary_ = rp.boundary_constraints(output_element_)
verdict = stark.verify(proof, air, boundary_, transition_zerofier_root)
assert(verdict == False), "invalid stark proof verifies"
print("proof rejected! \\\\o/")
# prove with false witness
print("attempting to prove with witness violating transition constraints (should not fail because using fast division) ...")
cycle = 1 + (int(os.urandom(1)[0]) % len(trace)-1)
register = int(os.urandom(1)[0]) % state_width
error = field.sample(os.urandom(17))
trace[cycle][register] = trace[cycle][register] + error
> proof = stark.prove(trace, air, boundary, transition_zerofier, transition_zerofier_codeword)
test_fast_stark.py:60:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
fast_stark.py:97: in prove
quotient = (trace_polynomials[s] - interpolant) / zerofier
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <univariate.Polynomial object at 0x10401b400>, other = <univariate.Polynomial object at 0x10401bc10>
def __truediv__( self, other ):
quo, rem = Polynomial.divide(self, other)
> assert(rem.is_zero()), "cannot perform polynomial division because remainder is not zero"
E AssertionError: cannot perform polynomial division because remainder is not zero
univariate.py:52: AssertionError
------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------
running trial with input: 228894434762048332457318
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 58193264164488245767062567324329845625
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 240442014346771117356623971907334212202
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 95249760241169493715201876097323148293
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 45460984185732302940594266735879849714
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 165587666758636920344584355360822203181
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 4978416156855947152397342202659038273
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 31067639684737653452690049877783213369
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 53584232234693103790669953564737900223
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 135431606538579019225905956689564915329
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 112559249994899591330577919758944336055
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 150140559072116678002193196297066253509
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 17074186389844667756333966236643273102
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 82836370955455571146008478781338270694
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 16254124710661698025820423055202445077
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 43769528645735998152380658131815329300
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 211513813789453019359177662714979719288
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 149981298211404449757454909679214149901
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
... but verification should fail :D
proof rejected! \\o/
running trial with input: 196164108357361058832227489249577920500
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with witness violating transition constraints (should not fail because using fast division) ...
______________________________________________________________________ test_serialize ______________________________________________________________________
def test_serialize( ):
proof1 = ProofStream()
proof1.push(1)
proof1.push(1: '1')
proof1.push([1])
proof1.push(2)
serialized = proof1.serialize()
> proof2 = ProofStream.deserialize(serialized)
E TypeError: deserialize() missing 1 required positional argument: 'bb'
test_ip.py:11: TypeError
________________________________________________________________________ test_stark ________________________________________________________________________
def test_stark( ):
field = Field.main()
expansion_factor = 4
num_colinearity_checks = 2
security_level = 2
rp = RescuePrime()
output_element = field.sample(bytes(b'0xdeadbeef'))
for trial in range(0, 20):
input_element = output_element
print("running trial with input:", input_element.value)
output_element = rp.hash(input_element)
num_cycles = rp.N+1
state_width = rp.m
stark = Stark(field, expansion_factor, num_colinearity_checks, security_level, state_width, num_cycles)
# prove honestly
print("honest proof generation ...")
# prove
trace = rp.trace(input_element)
air = rp.transition_constraints(stark.omicron)
boundary = rp.boundary_constraints(output_element)
proof = stark.prove(trace, air, boundary)
# verify
verdict = stark.verify(proof, air, boundary)
assert(verdict == True), "valid stark proof fails to verify"
print("success \\\\o/")
print("verifying false claim ...")
# verify false claim
output_element_ = output_element + field.one()
boundary_ = rp.boundary_constraints(output_element_)
verdict = stark.verify(proof, air, boundary_)
assert(verdict == False), "invalid stark proof verifies"
print("proof rejected! \\\\o/")
# verify with false witness
print("attempting to prove with false witness (should fail) ...")
cycle = int(os.urandom(1)[0]) % len(trace)
register = int(os.urandom(1)[0]) % state_width
error = field.sample(os.urandom(17))
trace[cycle][register] = trace[cycle][register] + error
> proof = stark.prove(trace, air, boundary) # should fail
test_stark.py:59:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
stark.py:111: in prove
transition_quotients = [tp / self.transition_zerofier() for tp in transition_polynomials]
stark.py:111: in <listcomp>
transition_quotients = [tp / self.transition_zerofier() for tp in transition_polynomials]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <univariate.Polynomial object at 0x104086130>, other = <univariate.Polynomial object at 0x10407d1c0>
def __truediv__( self, other ):
quo, rem = Polynomial.divide(self, other)
> assert(rem.is_zero()), "cannot perform polynomial division because remainder is not zero"
E AssertionError: cannot perform polynomial division because remainder is not zero
univariate.py:52: AssertionError
------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------
running trial with input: 228894434762048332457318
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 58193264164488245767062567324329845625
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 240442014346771117356623971907334212202
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 95249760241169493715201876097323148293
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 45460984185732302940594266735879849714
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 165587666758636920344584355360822203181
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 4978416156855947152397342202659038273
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 31067639684737653452690049877783213369
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 53584232234693103790669953564737900223
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 135431606538579019225905956689564915329
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 112559249994899591330577919758944336055
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 150140559072116678002193196297066253509
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 17074186389844667756333966236643273102
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 82836370955455571146008478781338270694
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 16254124710661698025820423055202445077
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 43769528645735998152380658131815329300
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 211513813789453019359177662714979719288
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 149981298211404449757454909679214149901
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 196164108357361058832227489249577920500
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
running trial with input: 141980557567619100445655384257862983893
honest proof generation ...
success \\o/
verifying false claim ...
proof rejected! \\o/
attempting to prove with false witness (should fail) ...
================================================================= short test summary info ==================================================================
FAILED test_fast_stark.py::test_fast_stark - AssertionError: cannot perform polynomial division because remainder is not zero
FAILED test_ip.py::test_serialize - TypeError: deserialize() missing 1 required positional argument: 'bb'
FAILED test_stark.py::test_stark - AssertionError: cannot perform polynomial division because remainder is not zero
======================================================== 3 failed, 18 passed in 2663.71s (0:44:23) =========================================================
2. FRI代码解析
$ pytest test_fri.py -s
================================================ test session starts =================================================
platform darwin -- Python 3.9.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/lanyu/zyd/stark-anatomy/code
collected 1 item
test_fri.py testing valid codeword ...
success! \\o/
testing invalid codeword ...
last codeword does not correspond to polynomial of low enough degree
observed de以上是关于STARK中的FRI代码解析的主要内容,如果未能解决你的问题,请参考以下文章