Skip to content

Commit

Permalink
SAFROLE updates
Browse files Browse the repository at this point in the history
- Use the newer revision for our wrapped `ark-ec-vrfs` Rust library.
- Populate tickets entropy on the epoch marker.
- Add new constant for the max. no of ticket attempts. Tiny vectors need
  a value of 3.
- Handle test vector changes, post offenders moved from input to state, etc.
- Add additional check for useless tickets being submitted.
  • Loading branch information
greywolve committed Jan 9, 2025
1 parent 101a6ba commit 21e540f
Show file tree
Hide file tree
Showing 27 changed files with 415 additions and 410 deletions.
11 changes: 5 additions & 6 deletions bandersnatch/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bandersnatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ark-ec-vrfs ={ git = "https://github.com/davxy/ark-ec-vrfs", rev = "ea35e22", features = ["bandersnatch", "ring", "parallel"] }
ark-ec-vrfs ={ git = "https://github.com/davxy/ark-ec-vrfs", rev = "d90e180", features = ["bandersnatch", "ring", "parallel"] }
hex = "0.4.3"
libc = "0.2.161"

Expand Down
1 change: 1 addition & 0 deletions internal/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
WorkReportTimeoutPeriod = jamtime.Timeslot(5) // U = 5: The period in timeslots after which reported but unavailable work may be replaced.
ValidatorRotationPeriod = jamtime.Timeslot(10) // R = 10: The rotation period of validator-core assignments, in timeslots.
MaxTicketExtrinsicSize = 16 // The maximum number of tickets which may be submitted in a single extrinsic.
MaxTicketAttempts = 2 // N = 2: The number of ticket entries per validator.
MaxHistoricalTimeslotsForPreimageMeta = 3 // () Maximum number of historical timeslots for preimage metadata
SizeOfSegment = 4104 // WG = WP*WE = 4104: The size of a segment in octets.
MaxWorkPackageSize = 12 * 1 << 20 // WB = 12 MB: The maximum size of an encoded work-package in octets (including extrinsic data and import implications).
Expand Down
1 change: 1 addition & 0 deletions internal/common/constants_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
WorkReportTimeoutPeriod = jamtime.Timeslot(5)
ValidatorRotationPeriod = jamtime.Timeslot(4)
MaxTicketExtrinsicSize = 16
MaxTicketAttempts = 3
MaxHistoricalTimeslotsForPreimageMeta = 3
SizeOfSegment = 4104
MaxWorkPackageSize = 12 * 1 << 20
Expand Down
21 changes: 17 additions & 4 deletions internal/statetransition/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ func calculateTickets(safstate safrole.State, entropyPool state.EntropyPool, tic
// n ≡ [{y ▸ Y(ip), r ▸ ir} S i <− ET]
tickets := make([]block.Ticket, len(ticketProofs))
for i, tp := range ticketProofs {
// Equation 74: r ∈ NN implies entry index must be 0 or 1 (v.0.4.5)
if tp.EntryIndex > 1 {
// Equation 6.29: r ∈ N_N (v.0.5.4)
if tp.EntryIndex > common.MaxTicketAttempts-1 {
return []block.Ticket{}, errors.New("bad ticket attempt")
}

Expand Down Expand Up @@ -584,6 +584,18 @@ func UpdateSafroleState(
if len(allTickets) > jamtime.TimeslotsPerEpoch {
allTickets = allTickets[:jamtime.TimeslotsPerEpoch]
}

// Ensure all incoming tickets exist in the accumulator. No useless
// tickets are allowed. Equation 6.35: n ⊆ γ′a (v.0.5.4)
existingIds := make(map[crypto.BandersnatchOutputHash]struct{}, len(allTickets))
for _, ticket := range allTickets {
existingIds[ticket.Identifier] = struct{}{}
}
for _, ticket := range tickets {
if _, ok := existingIds[ticket.Identifier]; !ok {
return entropyPool, validatorState, SafroleOutput{}, errors.New("useless ticket")
}
}
newValidatorState.SafroleState.TicketAccumulator = allTickets
}

Expand Down Expand Up @@ -640,9 +652,10 @@ func UpdateSafroleState(
}

// Compute epoch marker (H_e).
// Equation 72: He ≡ (η′1, [kb S k <− γ′k]) if e′ > e (v.0.4.5)
// Equation 6.27: He ≡ (η0, n1, [kb S k <− γ′k]) if e′ > e (v.0.5.4)
output.EpochMark = &block.EpochMarker{
Entropy: newEntropyPool[1],
Entropy: entropyPool[0],
TicketsEntropy: entropyPool[1],
}
for i, vd := range newValidatorState.SafroleState.NextValidators {
output.EpochMark.Keys[i] = vd.Bandersnatch
Expand Down
36 changes: 19 additions & 17 deletions tests/integration/safrole_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func TestSafrole(t *testing.T) {
}
}

offenders := make([]ed25519.PublicKey, len(tv.Input.PostOffenders))
for i, po := range tv.Input.PostOffenders {
offenders := make([]ed25519.PublicKey, len(tv.PreState.PostOffenders))
for i, po := range tv.PreState.PostOffenders {
offenders[i] = ed25519.PublicKey(testutils.MustFromHex(t, po))
}

Expand Down Expand Up @@ -155,6 +155,7 @@ func TestSafrole(t *testing.T) {
if expectedOutput.EpochMark != nil {
expectedEpochMarker := &block.EpochMarker{}
expectedEpochMarker.Entropy = crypto.Hash(testutils.MustFromHex(t, expectedOutput.EpochMark.Entropy))
expectedEpochMarker.TicketsEntropy = crypto.Hash(testutils.MustFromHex(t, expectedOutput.EpochMark.TicketsEntropy))
for i, v := range expectedOutput.EpochMark.Validators {
expectedEpochMarker.Keys[i] = crypto.BandersnatchPublicKey(testutils.MustFromHex(t, v))
}
Expand Down Expand Up @@ -303,8 +304,9 @@ type SafroleTestVectorTicketEnvelope struct {
}

type SafroleTestVectorEpochMark struct {
Entropy string `json:"entropy"`
Validators []string `json:"validators"`
Entropy string `json:"entropy"`
TicketsEntropy string `json:"tickets_entropy"`
Validators []string `json:"validators"`
}

type SafroleTestVectorOutputMarks struct {
Expand All @@ -313,22 +315,22 @@ type SafroleTestVectorOutputMarks struct {
}

type SafroleTestVectorState struct {
Tau uint32 `json:"tau"`
Eta [4]string `json:"eta"`
Lambda []SafroleTestVectorValidatorData `json:"lambda"`
Kappa []SafroleTestVectorValidatorData `json:"kappa"`
GammaK []SafroleTestVectorValidatorData `json:"gamma_k"`
Iota []SafroleTestVectorValidatorData `json:"iota"`
GammaA []SafroleTestVectorTicketBody `json:"gamma_a"`
GammaS SafroleTestVectorTicketsOrKeys `json:"gamma_s"`
GammaZ string `json:"gamma_z"`
Tau uint32 `json:"tau"`
Eta [4]string `json:"eta"`
Lambda []SafroleTestVectorValidatorData `json:"lambda"`
Kappa []SafroleTestVectorValidatorData `json:"kappa"`
GammaK []SafroleTestVectorValidatorData `json:"gamma_k"`
Iota []SafroleTestVectorValidatorData `json:"iota"`
GammaA []SafroleTestVectorTicketBody `json:"gamma_a"`
GammaS SafroleTestVectorTicketsOrKeys `json:"gamma_s"`
GammaZ string `json:"gamma_z"`
PostOffenders []string `json:"post_offenders"`
}

type SafroleTestVectorInput struct {
Slot uint32 `json:"slot"`
Entropy string `json:"entropy"`
Extrinsic []SafroleTestVectorTicketEnvelope `json:"extrinsic"`
PostOffenders []string `json:"post_offenders"`
Slot uint32 `json:"slot"`
Entropy string `json:"entropy"`
Extrinsic []SafroleTestVectorTicketEnvelope `json:"extrinsic"`
}

type SafroleTestVectorOutput struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"input": {
"slot": 1,
"entropy": "0x8c2e6d327dfaa6ff8195513810496949210ad20a96e2b0672a3e1b9335080801",
"extrinsic": [],
"post_offenders": []
"extrinsic": []
},
"pre_state": {
"tau": 0,
Expand Down Expand Up @@ -182,7 +181,8 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
},
"output": {
"ok": {
Expand Down Expand Up @@ -367,6 +367,7 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"input": {
"slot": 1,
"entropy": "0xe4b188579aa828f694f769a31a965a11f2017288fbfdfa8734aadc80685ffff7",
"extrinsic": [],
"post_offenders": []
"extrinsic": []
},
"pre_state": {
"tau": 1,
Expand Down Expand Up @@ -182,7 +181,8 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
},
"output": {
"err": "bad_slot"
Expand Down Expand Up @@ -364,6 +364,7 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"input": {
"slot": 10,
"entropy": "0xe4b188579aa828f694f769a31a965a11f2017288fbfdfa8734aadc80685ffff7",
"extrinsic": [],
"post_offenders": []
"extrinsic": []
},
"pre_state": {
"tau": 1,
Expand Down Expand Up @@ -182,7 +181,8 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
},
"output": {
"ok": {
Expand Down Expand Up @@ -367,6 +367,7 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"input": {
"slot": 15,
"entropy": "0x92e69aed566b11bf02354c64e44235b79154207021b7db0f03ca62108f826f94",
"extrinsic": [],
"post_offenders": []
"extrinsic": []
},
"pre_state": {
"tau": 10,
Expand Down Expand Up @@ -182,12 +181,14 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
},
"output": {
"ok": {
"epoch_mark": {
"entropy": "0xf164ce89b10488598cb295e4eef273fb8977722f4d6b2754b970ac77b45fa29b",
"tickets_entropy": "0xee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25",
"validators": [
"0xaa2b95f7572875b0d0f186552ae745ba8222fc0b5bd456554bfe51c68938f8bc",
"0xf16e5352840afb47e206b5c89f560f2611835855cf2e6ebad1acc9520a72591d",
Expand Down Expand Up @@ -377,6 +378,7 @@
"0x3d5e5a51aab2b048f8686ecd79712a80e3265a114cc73f14bdb2a59233fb66d0"
]
},
"gamma_z": "0xb3750bba87e39fb38579c880ff3b5c4e0aa90df8ff8be1ddc5fdd615c6780955f8fd85d99fd92a3f1d4585eb7ae8d627b01dd76d41720d73c9361a1dd2e830871155834c55db72de38fb875a9470faedb8cae54b34f7bfe196a9caca00c2911592e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xb3750bba87e39fb38579c880ff3b5c4e0aa90df8ff8be1ddc5fdd615c6780955f8fd85d99fd92a3f1d4585eb7ae8d627b01dd76d41720d73c9361a1dd2e830871155834c55db72de38fb875a9470faedb8cae54b34f7bfe196a9caca00c2911592e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"input": {
"slot": 48,
"entropy": "0x8c2e6d327dfaa6ff8195513810496949210ad20a96e2b0672a3e1b9335080801",
"extrinsic": [],
"post_offenders": [
"0x837ce344bc9defceb0d7de7e9e9925096768b7adb4dad932e532eb6551e0ea02"
]
"extrinsic": []
},
"pre_state": {
"tau": 45,
Expand Down Expand Up @@ -184,12 +181,16 @@
"0x7f6190116d118d643a98878e294ccf62b509e214299931aad8ff9764181a4e33"
]
},
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0xa949a60ad754d683d398a0fb674a9bbe525ca26b0b0b9c8d79f210291b40d286d9886a9747a4587d497f2700baee229ca72c54ad652e03e74f35f075d0189a40d41e5ee65703beb5d7ae8394da07aecf9056b98c61156714fd1d9982367bee2992e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": [
"0x837ce344bc9defceb0d7de7e9e9925096768b7adb4dad932e532eb6551e0ea02"
]
},
"output": {
"ok": {
"epoch_mark": {
"entropy": "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
"tickets_entropy": "0xee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25",
"validators": [
"0xaa2b95f7572875b0d0f186552ae745ba8222fc0b5bd456554bfe51c68938f8bc",
"0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -379,6 +380,9 @@
"0x3d5e5a51aab2b048f8686ecd79712a80e3265a114cc73f14bdb2a59233fb66d0"
]
},
"gamma_z": "0x9282d1d1b77276674a249d64a67abd0b5f129233644fc1968688135d4875f5830943eab43463be74c069718daf91d35e93c7a05457a3fda5ecbbd7bfd74982b2d53a1e3e53ca5813bf8a376a55e1a16a9a2ddc18cbbd58e9f17e82ba544aca0292e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf"
"gamma_z": "0x9282d1d1b77276674a249d64a67abd0b5f129233644fc1968688135d4875f5830943eab43463be74c069718daf91d35e93c7a05457a3fda5ecbbd7bfd74982b2d53a1e3e53ca5813bf8a376a55e1a16a9a2ddc18cbbd58e9f17e82ba544aca0292e630ae2b14e758ab0960e372172203f4c9a41777dadd529971d7ab9d23ab29fe0e9c85ec450505dde7f5ac038274cf",
"post_offenders": [
"0x837ce344bc9defceb0d7de7e9e9925096768b7adb4dad932e532eb6551e0ea02"
]
}
}
Loading

0 comments on commit 21e540f

Please sign in to comment.