alan-turing-institute / trustchain

Trustworthy decentralised PKI
https://alan-turing-institute.github.io/trustchain/
Apache License 2.0
9 stars 4 forks source link

Add a filter for TimestampCommitment and BlockHashCommitment #104

Closed thobson88 closed 1 year ago

thobson88 commented 1 year ago

The TrivialCommitment trait includes (optional) support for filtering of the candidate data.

In the case of a TimestampCommitment, the candidate data is a Bitcoin block header. Currently the expected data (a Unix timestamp) is sought inside the whole header, not just the timestamp field. This introduces a possible attack vector. An attacker could produce a header with a different timestamp but which contains, in a different field, the 32 bits matching a different timestamp. Without filtering of the candidate data, this bogus header would be accepted as valid (in the sense that the commitment would verify successfully).

The same applies to the BlockHashCommitment (where we should filter on the Merkle root field).

thobson88 commented 1 year ago

For this:

thobson88 commented 1 year ago

Ok, this seems to work but the default implementation of the timestamp() method inside the TimestampCommitment trait is brittle:

/// A Commitment whose expected data is a Unix time.
pub trait TimestampCommitment : Commitment {

    /// Gets the timestamp as a Unix time.
    fn timestamp(&self) -> Timestamp {
        self.expected_data()
            .as_u64()
            .unwrap()
            .try_into()
            .expect("Construction guarantees u32.")
    }
}

It would be ok if we could enforce the condition that the expected_data must have type Timestamp, but I can't see a way to do that.

Also, I haven't yet run the implementation tests.

On the plus side, the implementation of verifiable_timestamp in IONVerifier is now a bit simpler, and the filter() is implemented.

sgreenbury commented 1 year ago

Nice one, this is much better and fixes the issue!

I've made a couple of modifications:

I think this is good to go in #106 but some potential options for future:

thobson88 commented 1 year ago

Done in #108