Browse Source

Add verify functions for NRs

Vecna 1 year ago
parent
commit
50ce57765d
1 changed files with 25 additions and 3 deletions
  1. 25 3
      src/negative_report.rs

+ 25 - 3
src/negative_report.rs

@@ -42,7 +42,7 @@ impl NegativeReport {
 
     pub fn from_bridgeline(bridge_id: [u8; 20], bridgeline: BridgeLine, country: String) -> Self {
         let bridge_pok =
-            ProofOfBridgeKnowledge::HashOfBridgeLine(HashOfBridgeLine::new(bridgeline));
+            ProofOfBridgeKnowledge::HashOfBridgeLine(HashOfBridgeLine::new(&bridgeline));
         NegativeReport::new(bridge_id, bridge_pok, country)
     }
 
@@ -80,6 +80,28 @@ impl NegativeReport {
             Err(_) => Err(NegativeReportError::FailedToDeserialize),
         }
     }
+
+    /// Verify report if proof of bridge knowledge is bridge line hash
+    pub fn verify_with_hash_of_bridge_line(self, bl: &BridgeLine) -> bool {
+        match self.bridge_pok {
+            ProofOfBridgeKnowledge::HashOfBridgeLine(pok) => {
+                let hash = HashOfBridgeLine::new(bl);
+                hash == pok
+            },
+            _ => false,
+        }
+    }
+
+    /// Verify report if proof of bridge knowledge is bucket hash
+    pub fn verify_with_hash_of_bucket(self, bucket: &Scalar) -> bool {
+        match self.bridge_pok {
+            ProofOfBridgeKnowledge::HashOfBucket(pok) => {
+                let hash = HashOfBucket::new(bucket);
+                hash == pok
+            },
+            _ => false,
+        }
+    }
 }
 
 /// (De)serializable negative report object which must be consumed by the
@@ -125,7 +147,7 @@ pub struct HashOfBridgeLine {
 }
 
 impl HashOfBridgeLine {
-    pub fn new(bl: BridgeLine) -> Self {
+    pub fn new(bl: &BridgeLine) -> Self {
         let mut hasher = Sha3_256::new();
         hasher.update(bincode::serialize(&bl).unwrap());
         let hash: [u8; 32] = hasher.finalize().into();
@@ -140,7 +162,7 @@ pub struct HashOfBucket {
 }
 
 impl HashOfBucket {
-    pub fn new(bucket: Scalar) -> Self {
+    pub fn new(bucket: &Scalar) -> Self {
         let mut hasher = Sha3_256::new();
         hasher.update(bucket.to_bytes());
         let hash: [u8; 32] = hasher.finalize().into();