2022-05-24 05:24:15 +00:00
|
|
|
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
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize = 31'nb * 4
|
|
|
|
SectorSize = 31'nb
|
2022-05-24 05:24:15 +00:00
|
|
|
SectorsPerBlock = BlockSize div SectorSize
|
|
|
|
DataSetSize = BlockSize * 100
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "BLS PoR":
|
2022-05-24 05:24:15 +00:00
|
|
|
var
|
|
|
|
chunker: RandomChunker
|
|
|
|
manifest: Manifest
|
|
|
|
store: BlockStore
|
|
|
|
ssk: st.SecretKey
|
|
|
|
spk: st.PublicKey
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream: StoreStream
|
|
|
|
proofStream: StoreStream
|
2022-05-24 05:24:15 +00:00
|
|
|
|
|
|
|
setup:
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = DataSetSize.int, chunkSize = BlockSize)
|
2022-05-24 05:24:15 +00:00
|
|
|
store = CacheStore.new(cacheSize = DataSetSize, chunkSize = BlockSize)
|
|
|
|
manifest = Manifest.new(blockSize = BlockSize).tryGet()
|
|
|
|
(spk, ssk) = st.keyGen()
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream = StoreStream.new(store, manifest)
|
|
|
|
proofStream = StoreStream.new(store, manifest)
|
|
|
|
|
2022-05-24 05:24:15 +00:00
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
2022-05-24 05:24:15 +00:00
|
|
|
manifest.add(blk.cid)
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(blk)).tryGet()
|
2022-05-24 05:24:15 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
teardown:
|
|
|
|
await close(porStream)
|
|
|
|
await close(proofStream)
|
|
|
|
|
|
|
|
proc createPor(): Future[PoR] =
|
|
|
|
return PoR.init(
|
|
|
|
porStream,
|
2022-05-24 05:24:15 +00:00
|
|
|
ssk,
|
|
|
|
spk,
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize.int)
|
2023-06-22 18:01:21 +00:00
|
|
|
|
|
|
|
proc createProof(por: PoR, q: seq[QElement]): Future[Proof] =
|
|
|
|
return generateProof(
|
|
|
|
proofStream,
|
2022-05-24 05:24:15 +00:00
|
|
|
q,
|
|
|
|
por.authenticators,
|
|
|
|
SectorsPerBlock)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
test "Test PoR without corruption":
|
|
|
|
let
|
|
|
|
por = await createPor()
|
|
|
|
q = generateQuery(por.tau, 22)
|
|
|
|
proof = await createProof(por, q)
|
|
|
|
|
2022-05-24 05:24:15 +00:00
|
|
|
check por.verifyProof(q, proof.mu, proof.sigma)
|
|
|
|
|
|
|
|
test "Test PoR with corruption - query: 22, corrupted blocks: 300, bytes: 10":
|
|
|
|
let
|
2023-06-22 18:01:21 +00:00
|
|
|
por = await createPor()
|
2022-05-24 05:24:15 +00:00
|
|
|
pos = await store.corruptBlocks(manifest, 30, 10)
|
|
|
|
q = generateQuery(por.tau, 22)
|
2023-06-22 18:01:21 +00:00
|
|
|
proof = await createProof(por, q)
|
2022-05-24 05:24:15 +00:00
|
|
|
|
|
|
|
check pos.len == 30
|
|
|
|
check not por.verifyProof(q, proof.mu, proof.sigma)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Test Serialization":
|
2022-05-24 05:24:15 +00:00
|
|
|
var
|
|
|
|
chunker: RandomChunker
|
|
|
|
manifest: Manifest
|
|
|
|
store: BlockStore
|
|
|
|
ssk: st.SecretKey
|
|
|
|
spk: st.PublicKey
|
|
|
|
por: PoR
|
|
|
|
q: seq[QElement]
|
|
|
|
proof: Proof
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream: StoreStream
|
|
|
|
proofStream: StoreStream
|
2022-05-24 05:24:15 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
setup:
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = DataSetSize.int, chunkSize = BlockSize)
|
2022-05-24 05:24:15 +00:00
|
|
|
store = CacheStore.new(cacheSize = DataSetSize, chunkSize = BlockSize)
|
|
|
|
manifest = Manifest.new(blockSize = BlockSize).tryGet()
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
2022-05-24 05:24:15 +00:00
|
|
|
manifest.add(blk.cid)
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(blk)).tryGet()
|
2022-05-24 05:24:15 +00:00
|
|
|
|
|
|
|
(spk, ssk) = st.keyGen()
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream = StoreStream.new(store, manifest)
|
2022-05-24 05:24:15 +00:00
|
|
|
por = await PoR.init(
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream,
|
2022-05-24 05:24:15 +00:00
|
|
|
ssk,
|
|
|
|
spk,
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize.int)
|
2022-05-24 05:24:15 +00:00
|
|
|
q = generateQuery(por.tau, 22)
|
2023-06-22 18:01:21 +00:00
|
|
|
proofStream = StoreStream.new(store, manifest)
|
2022-05-24 05:24:15 +00:00
|
|
|
proof = await generateProof(
|
2023-06-22 18:01:21 +00:00
|
|
|
proofStream,
|
2022-05-24 05:24:15 +00:00
|
|
|
q,
|
|
|
|
por.authenticators,
|
|
|
|
SectorsPerBlock)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
teardown:
|
|
|
|
await close(porStream)
|
|
|
|
await close(proofStream)
|
|
|
|
|
2022-05-24 05:24:15 +00:00
|
|
|
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
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
check por.verifyProof(q, pproof.mu, pproof.sigma)
|