ergotree_interpreter/
sigma_protocol.rs1#![deny(clippy::unwrap_used)]
4
5pub mod private_input;
6pub mod prover;
7pub mod verifier;
8
9pub(crate) mod challenge;
10mod crypto_utils;
11pub mod dht_protocol;
12pub mod dlog_protocol;
13mod fiat_shamir;
14mod gf2_192;
15pub mod proof_tree;
16pub mod sig_serializer;
17pub mod unchecked_tree;
18pub mod unproven_tree;
19pub mod wscalar;
20
21use std::array::TryFromSliceError;
22use std::convert::TryFrom;
23use std::convert::TryInto;
24
25use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
26
27use dlog_protocol::FirstDlogProverMessage;
28use unchecked_tree::UncheckedTree;
29use unproven_tree::{UnprovenLeaf, UnprovenSchnorr};
30
31use self::challenge::Challenge;
32use self::dht_protocol::FirstDhTupleProverMessage;
33use self::unchecked_tree::UncheckedSchnorr;
34
35use derive_more::From;
36use derive_more::TryInto;
37
38pub(crate) trait ProverMessage {
40 fn bytes(&self) -> Vec<u8>;
42}
43
44#[derive(PartialEq, Eq, Debug, Clone, From, TryInto)]
46#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]
47#[cfg_attr(feature = "json", serde(tag = "type"))]
48#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
49pub enum FirstProverMessage {
50 #[cfg_attr(feature = "json", serde(rename = "dlog"))]
52 FirstDlogProverMessage(FirstDlogProverMessage),
53 #[cfg_attr(feature = "json", serde(rename = "dht"))]
55 FirstDhtProverMessage(FirstDhTupleProverMessage),
56}
57
58impl ProverMessage for FirstProverMessage {
59 fn bytes(&self) -> Vec<u8> {
60 match self {
61 FirstProverMessage::FirstDlogProverMessage(fdpm) => fdpm.bytes(),
62 FirstProverMessage::FirstDhtProverMessage(fdhtpm) => fdhtpm.bytes(),
63 }
64 }
65}
66
67pub(crate) const GROUP_SIZE_BITS: usize = 256;
69pub(crate) const GROUP_SIZE: usize = GROUP_SIZE_BITS / 8;
71
72#[derive(PartialEq, Eq, Debug, Clone)]
74pub(crate) struct GroupSizedBytes(pub(crate) Box<[u8; GROUP_SIZE]>);
75
76impl From<&[u8; GROUP_SIZE]> for GroupSizedBytes {
77 fn from(b: &[u8; GROUP_SIZE]) -> Self {
78 GroupSizedBytes(Box::new(*b))
79 }
80}
81
82impl TryFrom<Vec<u8>> for GroupSizedBytes {
83 type Error = TryFromSliceError;
84
85 fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
86 let bytes: [u8; GROUP_SIZE] = value.as_slice().try_into()?;
87 Ok(GroupSizedBytes(bytes.into()))
88 }
89}
90
91impl TryFrom<&[u8]> for GroupSizedBytes {
92 type Error = TryFromSliceError;
93
94 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
95 let bytes: [u8; GROUP_SIZE] = value.try_into()?;
96 Ok(GroupSizedBytes(bytes.into()))
97 }
98}
99
100pub const SOUNDNESS_BITS: usize = 192;
105pub const SOUNDNESS_BYTES: usize = SOUNDNESS_BITS / 8;
107
108#[cfg(test)]
109#[cfg(feature = "arbitrary")]
110mod tests {
111 use super::*;
112
113 #[allow(clippy::assertions_on_constants)]
114 #[test]
115 fn ensure_soundness_bits() {
116 assert!(SOUNDNESS_BITS < GROUP_SIZE_BITS);
118 assert!(SOUNDNESS_BYTES * 8 <= 512);
120 assert!(SOUNDNESS_BYTES % 8 == 0);
121 }
122}