Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

19 changes: 11 additions & 8 deletions crates/bfv/params/src/presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ pub enum BfvPreset {
}

impl BfvPreset {
/// Current registry entries. Historical indices do not identify current presets.
/// Current registry entries. Historical indices do not identify current presets: registration
/// is append-only, and indices 0, 1 and 2 hold earlier tuples on live chains.
pub const ON_CHAIN_PARAMETER_SETS: [(u8, Self); 2] = [
(0, Self::InsecureThreshold64),
(2, Self::SecureThreshold8192),
(4, Self::InsecureThreshold64),
(3, Self::SecureThreshold8192),
];

/// Map a supported on-chain parameter-set index to a threshold `BfvPreset`.
/// Return `None` for the historical secure index 1 and other unsupported indices.
/// Return `None` for the historical indices 0, 1 and 2 and other unsupported indices.
pub fn from_on_chain_param_set(value: u8) -> Option<Self> {
Self::ON_CHAIN_PARAMETER_SETS
.iter()
Expand Down Expand Up @@ -749,14 +750,16 @@ mod tests {
}

#[test]
fn old_secure_parameter_slot_has_no_preset() {
fn historical_parameter_slots_have_no_preset() {
for historical in [0, 1, 2] {
assert_eq!(BfvPreset::from_on_chain_param_set(historical), None);
}
assert_eq!(
BfvPreset::from_on_chain_param_set(0),
BfvPreset::from_on_chain_param_set(4),
Some(BfvPreset::InsecureThreshold64)
);
assert_eq!(BfvPreset::from_on_chain_param_set(1), None);
assert_eq!(
BfvPreset::from_on_chain_param_set(2),
BfvPreset::from_on_chain_param_set(3),
Some(BfvPreset::SecureThreshold8192)
);
for (index, preset) in BfvPreset::ON_CHAIN_PARAMETER_SETS {
Expand Down
7 changes: 4 additions & 3 deletions crates/bfv/params/tests/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# BFV protocol compatibility vectors

`bfv_protocol.json` contains exact ABI bytes and parameter hashes for current threshold presets,
plus historical threshold-only v5/v1 configuration IDs. The v5 records use registry indices 0 and 2;
v1 records retain their original indices 0 and 1. These IDs support historical indexing, not new
request or node admission.
plus historical threshold-only v5/v1 configuration IDs. Current records use registry indices 4
(insecure) and 3 (secure). Historical records retain their original indices: the v5 records of the
v0.19 tuples use indices 0 and 2; the v1 records use indices 0 and 1. The threshold-only IDs support
historical indexing, not new request or node admission.

The vectors were calculated independently with ethers v6 `AbiCoder`, `id`, and `keccak256`:

Expand Down
22 changes: 20 additions & 2 deletions crates/bfv/params/tests/fixtures/bfv_protocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "current-insecure",
"preset": "INSECURE_THRESHOLD_64",
"paramSet": 0,
"paramSet": 4,
"circuitVersion": "interfold-bfv-v5",
"encodedParameters": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000010366400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000007fffd810000000000000000000000000000000000000000000000000000000007fff8010000000000000000000000000000000000000000000000000000000007fff781000000000000000000000000000000000000000000000000000000000000000b3730373739303333363030000000000000000000000000000000000000000000",
"parameterHash": "0x2898f8a9fbc317e2812695161febb2c738bf0548327f4805d0329c6f6bb3a844",
Expand All @@ -14,12 +14,30 @@
{
"name": "current-secure",
"preset": "SECURE_THRESHOLD_8192",
"paramSet": 2,
"paramSet": 3,
"circuitVersion": "interfold-bfv-v5",
"encodedParameters": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000010366400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000800000000db40010000000000000000000000000000000000000000000000000800000000d540010000000000000000000000000000000000000000000000000800000000cbc001000000000000000000000000000000000000000000000000000000000000002938393732323838393731353438313832313432313137393931343739343631343238353932363430300000000000000000000000000000000000000000000000",
"parameterHash": "0x4f4025982214c2149028270dccc0ad45c24c2441a3496cdf1a29f5d1e1b908c3",
"cryptoConfigId": "0xceded5557de51373b24a54e2cc2ea5b08818f0f5abdc3f87851136941d33260b"
},
{
"name": "historical-insecure-v5",
"preset": null,
"paramSet": 0,
"circuitVersion": "interfold-bfv-v5",
"encodedParameters": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000010366400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000007fffd810000000000000000000000000000000000000000000000000000000007fff8010000000000000000000000000000000000000000000000000000000007fff781000000000000000000000000000000000000000000000000000000000000000b3132363137393239333836000000000000000000000000000000000000000000",
"parameterHash": "0xaff065a7d6e3d8c830dfcd37c2af8a25b3dcc6cd74ba458894075e8264f1ab1e",
"cryptoConfigId": "0x92c8b373eb72cc3600a5a818cf8599db16e618ee3ece2dadd14f8608c1a43ac9"
},
{
"name": "historical-secure-v5",
"preset": null,
"paramSet": 2,
"circuitVersion": "interfold-bfv-v5",
"encodedParameters": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000010366400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000800000000db40010000000000000000000000000000000000000000000000000800000000d540010000000000000000000000000000000000000000000000000800000000cbc001000000000000000000000000000000000000000000000000000000000000002931373732333033393934333739383837383330353436303935353537303731313731373437383430300000000000000000000000000000000000000000000000",
"parameterHash": "0x1b2620f6a5919d5ee19a51f34026369816f479efd0404b56585ad37d31c52317",
"cryptoConfigId": "0xa174862efd4487031d423ca96516807775ade0191c714e513aab93d0cc289baa"
},
{
"name": "historical-insecure-v1",
"preset": null,
Expand Down
8 changes: 6 additions & 2 deletions crates/bfv/params/tests/profile_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ fn every_supported_identity_resolves_to_its_own_profile_only() {
assert_eq!(supported, 8);

let unknown = B256::repeat_byte(0x12);
let error = BfvProfile::from_crypto_config_id(0, CiphernodesCommitteeSize::Minimum, unknown)
.unwrap_err();
let error = BfvProfile::from_crypto_config_id(
BfvProfile::SsInsecure64.param_set(),
CiphernodesCommitteeSize::Minimum,
unknown,
)
.unwrap_err();
assert!(
matches!(&error, BfvProfileError::UnsupportedConfiguration(id) if *id == unknown.to_string()),
"{error}"
Expand Down
1 change: 1 addition & 0 deletions crates/evm/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ tracing.workspace = true
fhe.workspace = true
fhe-traits.workspace = true
rand.workspace = true
serde_json.workspace = true
124 changes: 106 additions & 18 deletions crates/evm/indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,28 +403,40 @@ impl<S: DataStore> InterfoldIndexer<S, ReadWrite> {
}
}

/// Statement versions of the threshold-only configuration IDs that requests of a historical
/// registry slot carry. Registration is append-only, so current presets use later slots and these
/// slots keep their original bytes: slots 0 and 2 hold the v0.19 tuples (slot 0 on testnets), and
/// slots 0 and 1 hold the earlier Mainnet tuples.
fn historical_statement_versions(param_set: u8) -> &'static [&'static str] {
match param_set {
0 => &[LEGACY_BFV_CONFIG_VERSION, "interfold-bfv-v1"],
1 => &["interfold-bfv-v1"],
2 => &[LEGACY_BFV_CONFIG_VERSION],
_ => &[],
}
}

/// Resolve the parameter bytes of a request. A current slot resolves through its profile identity
/// to the local preset. A historical slot resolves to its registry bytes, and only under the
/// threshold-only identity those exact bytes had when the request was made.
fn request_bfv_params(
param_set: u8,
committee_size: u8,
request_config_id: B256,
historical_params: Option<&[u8]>,
) -> Result<Vec<u8>> {
if let Some(preset) = BfvPreset::from_on_chain_param_set(param_set) {
let params = encode_bfv_params(&BfvParamSet::from(preset).build_arc());
if let Ok(committee) = CiphernodesCommitteeSize::try_from(committee_size) {
if let Ok(profile) =
BfvProfile::from_crypto_config_id(param_set, committee, request_config_id)
{
profile.ensure_admission_enabled()?;
return Ok(params);
return Ok(encode_bfv_params(&BfvParamSet::from(preset).build_arc()));
}
}
// Historical completed requests keep their original threshold-only v5 identity.
if request_config_id == bfv_crypto_config_id(&params, LEGACY_BFV_CONFIG_VERSION) {
return Ok(params);
}
}
if param_set != 0 && param_set != 1 {
let versions = historical_statement_versions(param_set);
if versions.is_empty() {
return Err(eyre!(
"unsupported BFV parameter set {param_set} or configuration ID"
));
Expand All @@ -434,7 +446,10 @@ fn request_bfv_params(
.ok_or_else(|| eyre!("historical BFV parameter set {param_set} is not registered"))?;
decode_bfv_params(params)
.map_err(|error| eyre!("invalid historical BFV parameters: {error}"))?;
if request_config_id != bfv_crypto_config_id(params, "interfold-bfv-v1") {
if !versions
.iter()
.any(|version| request_config_id == bfv_crypto_config_id(params, version))
{
return Err(eyre!(
"local circuit configuration does not match request-time config for parameter set {param_set}"
));
Expand Down Expand Up @@ -467,7 +482,7 @@ async fn store_committee_public_key<S: DataStore, R: ProviderType>(
request_crypto_config_id,
None,
) {
Err(_) if e3.paramSet == 0 || e3.paramSet == 1 => {
Err(_) if !historical_statement_versions(e3.paramSet).is_empty() => {
let historical_params = contract.get_param_set_registry(e3.paramSet).await?;
request_bfv_params(
e3.paramSet,
Expand Down Expand Up @@ -1239,22 +1254,92 @@ mod public_key_chunk_tests {
assert!(request_bfv_params(0, 0, id, Some(&extended)).is_err());
assert!(request_bfv_params(0, 0, extended_id, Some(&params)).is_err());

let slot = BfvProfile::SsInsecure64.param_set();
let current =
encode_bfv_params(&BfvParamSet::from(BfvPreset::InsecureThreshold64).build_arc());
let current_id = BfvProfile::SsInsecure64
.crypto_config_id(CiphernodesCommitteeSize::Minimum)
.unwrap();
assert_eq!(request_bfv_params(0, 0, current_id, None).unwrap(), current);
assert!(request_bfv_params(1, 0, current_id, Some(&current)).is_err());
assert_eq!(
request_bfv_params(slot, 0, current_id, None).unwrap(),
current
);
// A historical slot never resolves a current identity, even over the current bytes.
for historical in [0, 1, 2] {
assert!(request_bfv_params(historical, 0, current_id, Some(&current)).is_err());
}
let disabled = BfvProfile::SyncDecInsecure64
.crypto_config_id(CiphernodesCommitteeSize::Minimum)
.unwrap();
assert!(request_bfv_params(0, 0, disabled, Some(&current))
assert!(request_bfv_params(slot, 0, disabled, Some(&current))
.unwrap_err()
.to_string()
.contains("disabled"));
}

#[test]
fn v019_slots_keep_their_registered_bytes_and_threshold_only_identity() {
// The v0.19 tuples that live registries hold: the insecure one at slot 0 (Sepolia) and
// the secure one at slot 2 (Mainnet and Sepolia).
let insecure = (
U256::from(64u64),
U256::from(17_000_000u64),
vec![
U256::from(0x7fffd81u64),
U256::from(0x7fff801u64),
U256::from(0x7fff781u64),
],
"12617929386".to_string(),
)
.abi_encode();
let secure = (
U256::from(8192u64),
U256::from(17_000_000u64),
vec![
U256::from(0x0800000000db4001u64),
U256::from(0x0800000000d54001u64),
U256::from(0x0800000000cbc001u64),
],
"17723039943798878305460955570711717478400".to_string(),
)
.abi_encode();
for (slot, params, hash, id) in [
(
0,
&insecure,
"0xaff065a7d6e3d8c830dfcd37c2af8a25b3dcc6cd74ba458894075e8264f1ab1e",
"0x92c8b373eb72cc3600a5a818cf8599db16e618ee3ece2dadd14f8608c1a43ac9",
),
(
2,
&secure,
"0x1b2620f6a5919d5ee19a51f34026369816f479efd0404b56585ad37d31c52317",
"0xa174862efd4487031d423ca96516807775ade0191c714e513aab93d0cc289baa",
),
] {
assert_eq!(keccak256(params).to_string(), hash);
let config_id: B256 = id.parse().unwrap();
assert_eq!(
bfv_crypto_config_id(params, LEGACY_BFV_CONFIG_VERSION),
config_id
);
assert_eq!(
&request_bfv_params(slot, 0, config_id, Some(params)).unwrap(),
params
);
assert!(request_bfv_params(slot, 0, config_id, None).is_err());
// Slot 1 only ever carried v1 identities.
assert!(request_bfv_params(1, 0, config_id, Some(params)).is_err());
// No current slot resolves a threshold-only identity.
for current in BfvProfile::ALL.map(BfvProfile::param_set) {
assert!(request_bfv_params(current, 0, config_id, Some(params)).is_err());
}
}
// Slot 2 never held a v1 tuple.
let v1_id = bfv_crypto_config_id(&secure, "interfold-bfv-v1");
assert!(request_bfv_params(2, 0, v1_id, Some(&secure)).is_err());
}

#[test]
fn mainnet_slot_one_matches_the_historical_configuration_id() {
let params = (
Expand Down Expand Up @@ -1304,22 +1389,25 @@ mod public_key_chunk_tests {

#[test]
fn current_secure_slot_requires_the_current_configuration_id() {
let slot = BfvProfile::SsSecure8192.param_set();
let params =
encode_bfv_params(&BfvParamSet::from(BfvPreset::SecureThreshold8192).build_arc());
for committee in CiphernodesCommitteeSize::ALL {
let id = BfvProfile::SsSecure8192
.crypto_config_id(committee)
.unwrap();
assert_eq!(
request_bfv_params(2, committee as u8, id, None).unwrap(),
request_bfv_params(slot, committee as u8, id, None).unwrap(),
params
);
assert!(request_bfv_params(2, (committee as u8 + 1) % 3, id, None).is_err());
assert!(request_bfv_params(3, committee as u8, id, None).is_err());
assert!(request_bfv_params(slot, (committee as u8 + 1) % 3, id, None).is_err());
assert!(request_bfv_params(5, committee as u8, id, None).is_err());
assert!(request_bfv_params(2, committee as u8, id, Some(&params)).is_err());
}
// A threshold-only identity is historical: no current slot admits one.
let legacy_id = bfv_crypto_config_id(&params, LEGACY_BFV_CONFIG_VERSION);
assert_eq!(request_bfv_params(2, 0, legacy_id, None).unwrap(), params);
assert!(request_bfv_params(2, 0, keccak256(b"interfold-bfv-v1"), None).is_err());
assert!(request_bfv_params(slot, 0, legacy_id, Some(&params)).is_err());
assert!(request_bfv_params(slot, 0, keccak256(b"interfold-bfv-v1"), None).is_err());
}

#[tokio::test]
Expand Down Expand Up @@ -1450,7 +1538,7 @@ mod public_key_chunk_tests {
.setRequestCrypto(
U256::from(7),
bfv_encryption_scheme_id(),
2,
BfvProfile::SsSecure8192.param_set(),
BfvProfile::SsSecure8192.crypto_config_id(CiphernodesCommitteeSize::Minimum)?,
)
.send()
Expand Down
9 changes: 7 additions & 2 deletions crates/evm/indexer/tests/fixtures/fake_interfold.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ contract FakeInterfold {
mapping(uint256 => bytes32) private encryptionSchemeOverrides;
mapping(uint256 => uint8) private paramSetOverrides;

function setParamSet(uint8 index, bytes calldata data) external {
paramSetRegistry[index] = data;
}

function setCryptoConfigId(uint256 e3Id, bytes32 configId) external {
cryptoConfigOverrides[e3Id] = configId;
}
Expand All @@ -38,9 +42,10 @@ contract FakeInterfold {
cryptoConfigOverrides[e3Id] = configId;
}

// Without an override, an E3 is a current request: parameter set 4 under the ss-insecure-64/minimum identity.
function e3CryptoConfigIds(uint256 e3Id) external view returns (bytes32) {
if (cryptoConfigOverrides[e3Id] != bytes32(0)) return cryptoConfigOverrides[e3Id];
return 0x5d12098e317ec9918b099e134da06000a91d7c2bff731cc37c630b8b671a724b;
return 0xd76c4f521394e6cbe1df28036cc5dd5590c6630a403525b5c2b052f9b91507f4;
}

// Emit InputPublished event with passed test data
Expand Down Expand Up @@ -89,7 +94,7 @@ contract FakeInterfold {
e3Program: 0x7F3E4df648B8Cb96C1D343be976b91B97CaD5c21,
decryptionVerifier: 0x4B0D8c2E5f7a6c832f8b16d3aB0e7F5d9E9B24b1,
pkVerifier: 0x5C1e9D3f8A7B6c932F9C27e4Ab1F8F6e0F0C35c2,
paramSet: paramSetOverrides[_e3Id],
paramSet: cryptoConfigOverrides[_e3Id] == bytes32(0) ? 4 : paramSetOverrides[_e3Id],
customParams: abi.encode("custom_params"),
committeePublicKey: bytes32(keccak256("committee_public_key")),
ciphertextOutput: bytes32(keccak256("encrypted_data")),
Expand Down
Loading
Loading