nim-codex/tests/codex/storageproofs/testpor.nim

176 lines
4.3 KiB
Nim
Raw Normal View History

import pkg/chronos
import pkg/asynctest
import pkg/blscurve/blst/blst_abi
import pkg/codex/streams
import pkg/codex/storageproofs as st
import pkg/codex/stores
import pkg/codex/manifest
import pkg/codex/chunker
import pkg/codex/rng
import pkg/codex/blocktype as bt
import ../helpers
const
Speed up development (#303) * [ci] Extract setup of nimbus build system into separate file * [ci] Cleanup NodeJS setup * [ci] Use amd64 by default * [ci] Separate steps for unit, contract and integration tests * [ci] Separate build job for coverage that is allowed to fail * [ci] Separate build job for Nim 1.2 * [ci] Calculate coverage on unit tests only This enables faster development cycles at the expense of code coverage accuracy. * [PoR] Test proofs of retrievability with a small block size To get a reasonable test execution time. * [ci] Set NIM_COMMIT environment variable To prevent subsequent `make` command to build a different version of Nim. * [ci] Checkout submodules recursively So that nimbus-build-system doesn't have to do it afterwards. * [ci] Update caches on every run Will automatically update caches when a new minor version of Nim is released. See also: https://github.com/actions/cache/blob/main/workarounds.md#update-a-cache * [ci] Set cache_nonce for all jobs, not just those in the matrix * [build] update to latest nimbus-build-system Requires an update to latest nim-nat-traversal * [erasure] Test erasure coding with a small block size To get a reasonable test execution time. * [erasure] fix typo * [PoR] Test PoR store with a small block size To get a reasonable test execution time. * [PoR] Test PoR network with a small block size To get a reasonable test execution time. * [ci] Ensure that unit tests are not dependent on Ethereum Start Ethereum node *after* the unit tests are run. * [ci] Cancel all other jobs when one fails Allows for faster feedback loops; a next attempt to run the CI will start sooner.
2022-11-14 13:50:00 +00:00
BlockSize = 31 * 4
SectorSize = 31
SectorsPerBlock = BlockSize div SectorSize
DataSetSize = BlockSize * 100
asyncchecksuite "BLS PoR":
var
chunker: RandomChunker
manifest: Manifest
store: BlockStore
ssk: st.SecretKey
spk: st.PublicKey
porStream: StoreStream
proofStream: StoreStream
setup:
chunker = RandomChunker.new(Rng.instance(), size = DataSetSize, chunkSize = BlockSize)
store = CacheStore.new(cacheSize = DataSetSize, chunkSize = BlockSize)
manifest = Manifest.new(blockSize = BlockSize).tryGet()
(spk, ssk) = st.keyGen()
porStream = StoreStream.new(store, manifest)
proofStream = StoreStream.new(store, manifest)
while (
let chunk = await chunker.getBytes();
chunk.len > 0):
let blk = bt.Block.new(chunk).tryGet()
manifest.add(blk.cid)
(await store.putBlock(blk)).tryGet()
teardown:
await close(porStream)
await close(proofStream)
proc createPor(): Future[PoR] =
return PoR.init(
porStream,
ssk,
spk,
BlockSize)
proc createProof(por: PoR, q: seq[QElement]): Future[Proof] =
return generateProof(
proofStream,
q,
por.authenticators,
SectorsPerBlock)
test "Test PoR without corruption":
let
por = await createPor()
q = generateQuery(por.tau, 22)
proof = await createProof(por, q)
check por.verifyProof(q, proof.mu, proof.sigma)
test "Test PoR with corruption - query: 22, corrupted blocks: 300, bytes: 10":
let
por = await createPor()
pos = await store.corruptBlocks(manifest, 30, 10)
q = generateQuery(por.tau, 22)
proof = await createProof(por, q)
check pos.len == 30
check not por.verifyProof(q, proof.mu, proof.sigma)
asyncchecksuite "Test Serialization":
var
chunker: RandomChunker
manifest: Manifest
store: BlockStore
ssk: st.SecretKey
spk: st.PublicKey
por: PoR
q: seq[QElement]
proof: Proof
porStream: StoreStream
proofStream: StoreStream
setup:
chunker = RandomChunker.new(Rng.instance(), size = DataSetSize, chunkSize = BlockSize)
store = CacheStore.new(cacheSize = DataSetSize, chunkSize = BlockSize)
manifest = Manifest.new(blockSize = BlockSize).tryGet()
while (
let chunk = await chunker.getBytes();
chunk.len > 0):
let blk = bt.Block.new(chunk).tryGet()
manifest.add(blk.cid)
(await store.putBlock(blk)).tryGet()
(spk, ssk) = st.keyGen()
porStream = StoreStream.new(store, manifest)
por = await PoR.init(
porStream,
ssk,
spk,
BlockSize)
q = generateQuery(por.tau, 22)
proofStream = StoreStream.new(store, manifest)
proof = await generateProof(
proofStream,
q,
por.authenticators,
SectorsPerBlock)
teardown:
await close(porStream)
await close(proofStream)
test "Serialize Public Key":
var
spkMessage = spk.toMessage()
check:
spk.signkey == spkMessage.fromMessage().tryGet().signkey
spk.key.blst_p2_is_equal(spkMessage.fromMessage().tryGet().key).bool
test "Serialize TauZero":
var
tauZeroMessage = por.tau.t.toMessage()
tauZero = tauZeroMessage.fromMessage().tryGet()
check:
por.tau.t.name == tauZero.name
por.tau.t.n == tauZero.n
for i in 0..<por.tau.t.u.len:
check blst_p1_is_equal(por.tau.t.u[i], tauZero.u[i]).bool
test "Serialize Tau":
var
tauMessage = por.tau.toMessage()
tau = tauMessage.fromMessage().tryGet()
check:
por.tau.signature == tau.signature
test "Serialize PoR":
let
porMessage = por.toMessage()
ppor = porMessage.fromMessage().tryGet()
for i in 0..<por.authenticators.len:
check blst_p1_is_equal(por.authenticators[i], ppor.authenticators[i]).bool
test "Serialize Proof":
let
proofMessage = proof.toMessage()
pproof = proofMessage.fromMessage().tryGet()
check:
proof.sigma.blst_p1_is_equal(pproof.sigma).bool
proof.mu == pproof.mu
check por.verifyProof(q, pproof.mu, pproof.sigma)