Skip to content

Commit 046327e

Browse files
committed
chore: code organization changes
1 parent d4135d1 commit 046327e

9 files changed

Lines changed: 128 additions & 91 deletions

File tree

Cargo.lock

Lines changed: 19 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ rustdoc.all = "warn"
4848
clippy.too_long_first_doc_paragraph = "warn"
4949

5050
[workspace.dependencies]
51+
mosaic-adaptor-sigs = { path = "crates/adaptor-sigs" }
5152
mosaic-cac-proto-types = { path = "crates/cac/proto-types" }
5253
mosaic-cac-protocol = { path = "crates/cac/protocol" }
5354
mosaic-cac-types = { path = "crates/cac/types" }
@@ -68,13 +69,20 @@ ark-ec = "0.5.0"
6869
ark-ff = "0.5.0"
6970
ark-secp256k1 = "0.5.0"
7071
async-trait = "0.1"
72+
bitcoin = { version = "0.32.7", features = ["rand", "rand-std"] }
73+
bitcoin-script = { git = "https://github.com/BitVM/rust-bitcoin-script" }
74+
bitcoin-scriptexec = { git = "https://github.com/BitVM/rust-bitcoin-scriptexec" }
7175
bytes = "1.10"
76+
once_cell = "1.21.3"
7277
futures = "0.3.31"
7378
jsonrpsee = { version = "0.26.0", features = ["macros"] }
7479
jsonrpsee-types = "*" # constrained by jsonrpsee dep
80+
k256 = "0.13.4"
7581
rand = "0.8"
82+
rand_chacha = "0.3.0"
7683
rand_core = "0.6"
7784
serde = { version = "1.0", features = ["derive"] }
85+
sha2 = "0.10"
7886
strata-codec = { git = "https://github.com/alpenlabs/strata-common.git" }
7987
thiserror = "2.0"
8088
tokio = { version = "1.48.0", features = ["rt", "sync", "macros", "time"] }

crates/adaptor-sigs/Cargo.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "adaptor-sigs"
2+
name = "mosaic-adaptor-sigs"
33
version.workspace = true
44
edition.workspace = true
55
authors.workspace = true
@@ -14,17 +14,18 @@ keywords.workspace = true
1414
ark-ec.workspace = true
1515
ark-ff.workspace = true
1616
ark-secp256k1.workspace = true
17-
once_cell = "1.21.3"
17+
mosaic-common.workspace = true
18+
once_cell.workspace = true
1819
rand.workspace = true
19-
sha2 = "0.10"
20+
sha2.workspace = true
2021
thiserror.workspace = true
2122

2223
[dev-dependencies]
23-
rand_chacha = "0.3.0"
24-
k256 = "0.13.4"
25-
bitcoin-script = { git = "https://github.com/BitVM/rust-bitcoin-script" }
26-
bitcoin-scriptexec = { git = "https://github.com/BitVM/rust-bitcoin-scriptexec" }
27-
bitcoin = { version = "0.32.7", features = ["rand", "rand-std"] }
24+
rand_chacha.workspace = true
25+
k256.workspace = true
26+
bitcoin.workspace = true
27+
bitcoin-script.workspace = true
28+
bitcoin-scriptexec.workspace = true
2829

2930
[lints]
3031
workspace = true

crates/adaptor-sigs/src/adaptor.rs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rand::{CryptoRng, Rng};
2020
use sha2::{Digest, Sha256};
2121

2222
use crate::{error::Error, fixed_base::gen_mul};
23-
/* ------------------------------ Utilities --------------------------------- */
2423

24+
/// Helpers to serialize and deserialize field as per BIP340
2525
fn serialize_field<F: PrimeField>(x: &F) -> [u8; 32] {
2626
// `Fq` modulus is 256 bits, so its big-endian encoding always fits in 32 bytes.
2727
x.into_bigint()
@@ -46,19 +46,6 @@ fn deserialize_field<F: PrimeField>(bytes: [u8; 32]) -> Result<F, Error> {
4646
))
4747
}
4848

49-
/* --------------------------------- Types ---------------------------------- */
50-
51-
/// Adaptor for the VSSS
52-
#[derive(Debug, Clone, PartialEq, Eq)]
53-
pub struct Adaptor {
54-
/// s' = ±r' + e * x (the evaluator’s partial Schnorr s)
55-
pub tweaked_s: ark_secp256k1::Fr,
56-
/// R' = r'*G
57-
pub R_dash_commit: ark_secp256k1::Projective,
58-
/// S = share*G
59-
pub share_commitment: ark_secp256k1::Projective,
60-
}
61-
6249
/// Signature for the VSSS
6350
#[derive(Debug, Clone, PartialEq, Eq)]
6451
pub struct Signature {
@@ -93,28 +80,17 @@ impl Signature {
9380
}
9481
}
9582

96-
/* ----------------------------- Challenge helper --------------------------- */
97-
98-
/// e = H(BIP0340/challenge, R.x, P.x, wire_index, sighash)
99-
fn challenge_e(
100-
R: ark_secp256k1::Affine,
101-
P: ark_secp256k1::Affine,
102-
sighash: &[u8],
103-
) -> ark_secp256k1::Fr {
104-
// BIP340 tag
105-
let tag_hash = Sha256::digest(b"BIP0340/challenge");
106-
let mut h = Sha256::new();
107-
h.update(tag_hash);
108-
h.update(tag_hash);
109-
h.update(serialize_field(&R.x));
110-
h.update(serialize_field(&P.x));
111-
h.update(sighash);
112-
let digest = h.finalize();
113-
ark_secp256k1::Fr::from_be_bytes_mod_order(&digest)
83+
/// Adaptor for the VSSS
84+
#[derive(Debug, Clone, PartialEq, Eq)]
85+
pub struct Adaptor {
86+
/// s' = ±r' + e * x (the evaluator’s partial Schnorr s)
87+
pub tweaked_s: ark_secp256k1::Fr,
88+
/// R' = r'*G
89+
pub R_dash_commit: ark_secp256k1::Projective,
90+
/// S = share*G
91+
pub share_commitment: ark_secp256k1::Projective,
11492
}
11593

116-
/* --------------------------------- Methods -------------------------------- */
117-
11894
impl Adaptor {
11995
/// Generates an adaptor from the evaluator’s master secret key `x`, a commitment
12096
/// to the garbler’s share (`S = share·G`), and the `(wire_index, sighash)` transcript data.
@@ -161,7 +137,7 @@ impl Adaptor {
161137
));
162138
}
163139

164-
let e = challenge_e(expected_R, evaluator_master_pk, sighash);
140+
let e = Self::challenge_e(expected_R, evaluator_master_pk, sighash);
165141

166142
if expected_R.y.into_bigint().is_odd() {
167143
// negate to make commitment of completed nonce (i.e. r_dash + share) even
@@ -207,7 +183,7 @@ impl Adaptor {
207183
));
208184
}
209185

210-
let e = challenge_e(expected_R, evaluator_master_pk_affine, sighash);
186+
let e = Self::challenge_e(expected_R, evaluator_master_pk_affine, sighash);
211187

212188
// LHS: s'·G - e.P
213189
let lhs = gen_mul(&self.tweaked_s) - evaluator_master_pk * e;
@@ -262,6 +238,24 @@ impl Adaptor {
262238
let diff = signature.s - self.tweaked_s;
263239
if is_odd { -diff } else { diff }
264240
}
241+
242+
/// e = H(BIP0340/challenge, R.x, P.x, wire_index, sighash)
243+
fn challenge_e(
244+
R: ark_secp256k1::Affine,
245+
P: ark_secp256k1::Affine,
246+
sighash: &[u8],
247+
) -> ark_secp256k1::Fr {
248+
// BIP340 tag
249+
let tag_hash = Sha256::digest(b"BIP0340/challenge");
250+
let mut h = Sha256::new();
251+
h.update(tag_hash);
252+
h.update(tag_hash);
253+
h.update(serialize_field(&R.x));
254+
h.update(serialize_field(&P.x));
255+
h.update(sighash);
256+
let digest = h.finalize();
257+
ark_secp256k1::Fr::from_be_bytes_mod_order(&digest)
258+
}
265259
}
266260

267261
/* ---------------------------------- Tests ---------------------------------- */
@@ -463,7 +457,7 @@ mod tests {
463457
// Recompute e like verify() does
464458
let R = fx.adaptor.expected_R().into_affine();
465459
let P = fx.P.into_affine();
466-
let e = super::challenge_e(R, P, &fx.sighash);
460+
let e = Adaptor::challenge_e(R, P, &fx.sighash);
467461

468462
let lhs = gen_mul(&fx.adaptor.tweaked_s); // s'·G
469463
let tweaked_R = if R.y.into_bigint().is_odd() {

crates/adaptor-sigs/src/fixed_base.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
33
use ark_ec::{PrimeGroup, scalar_mul::BatchMulPreprocessing};
44
use ark_secp256k1::{Fr as Scalar, Projective as Point};
5+
use mosaic_common::constants::{N_INPUT_WIRES, N_VERIFICAITON_CIRCUITS as N_COEFFICIENTS};
56
use once_cell::sync::Lazy;
67

7-
use crate::{N_COEFFICIENTS, N_INPUT_WIRES};
8-
9-
/// Heuristic, TODO optimize this
8+
/// TODO: @nakkstar123, @AaronFeickert: add comments on how this heuristic was designed
109
const APPROX_MULS: usize = N_INPUT_WIRES * N_COEFFICIENTS * 3;
1110

1211
/// Single global precomputation for G.

crates/adaptor-sigs/src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
//! Mosaic adaptor signatures library.
22
3-
pub mod adaptor;
4-
pub mod error;
5-
pub(crate) mod fixed_base;
3+
mod adaptor;
4+
mod error;
5+
mod fixed_base;
66

7-
/// Number of coefficients in each polynomial.
8-
pub const N_COEFFICIENTS: usize = 174;
9-
/// Number of circuits (evaluation points).
10-
pub const N_SHARES: usize = 181;
11-
/// Number of input wires to the circuit.
12-
pub const N_INPUT_WIRES: usize = 1273;
7+
pub use adaptor::{Adaptor, Signature};
8+
pub use error::Error;

fuzz/Cargo.lock

Lines changed: 52 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2024"
88
cargo-fuzz = true
99

1010
[dependencies]
11-
adaptor-sigs = { path = "../crates/adaptor-sigs" }
11+
mosaic-adaptor-sigs = { path = "../crates/adaptor-sigs" }
1212
libfuzzer-sys = "0.4"
1313
k256 = "0.13.4"
1414

fuzz/fuzz_targets/adaptor_signature_from_bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_main]
22

33
use libfuzzer_sys::fuzz_target;
4-
use adaptor_sigs::adaptor::Signature;
4+
use mosaic_adaptor_sigs::Signature;
55

66
fuzz_target!(|data: [u8; 64]| {
77
// fuzzed code goes here

0 commit comments

Comments
 (0)