Skip to content

Commit ab7f6d8

Browse files
authored
Merge pull request #36 from stevenroose/whitelist
Add whitelist support
2 parents 0dba4c0 + 9144faa commit ab7f6d8

File tree

5 files changed

+571
-1
lines changed

5 files changed

+571
-1
lines changed

secp256k1-zkp-sys/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn main() {
4343
.define("ENABLE_MODULE_GENERATOR", Some("1"))
4444
.define("ENABLE_MODULE_RANGEPROOF", Some("1"))
4545
.define("ENABLE_MODULE_ECDSA_ADAPTOR", Some("1"))
46+
.define("ENABLE_MODULE_WHITELIST", Some("1"))
4647
.define("ECMULT_GEN_PREC_BITS", Some("4"))
4748
// TODO these three should be changed to use libgmp, at least until secp PR 290 is merged
4849
.define("USE_NUM_NONE", Some("1"))

secp256k1-zkp-sys/src/zkp.rs

+90-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use core::{fmt, hash};
2-
use {types::*, Context, PublicKey, Signature};
2+
use {types::*, Context, NonceFn, PublicKey, Signature};
33

44
/// Rangeproof maximum length
55
pub const RANGEPROOF_MAX_LENGTH: size_t = 5134;
66
pub const ECDSA_ADAPTOR_SIGNATURE_LENGTH: size_t = 162;
77

8+
/// The maximum number of whitelist keys.
9+
pub const WHITELIST_MAX_N_KEYS: size_t = 255;
10+
811
extern "C" {
912
#[cfg_attr(
1013
not(feature = "external-symbols"),
@@ -334,6 +337,59 @@ extern "C" {
334337
adaptor_sig162: *const EcdsaAdaptorSignature,
335338
enckey: *const PublicKey,
336339
) -> c_int;
340+
341+
#[cfg_attr(
342+
not(feature = "external-symbols"),
343+
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_signature_parse"
344+
)]
345+
pub fn secp256k1_whitelist_signature_parse(
346+
cx: *const Context,
347+
sig: *mut WhitelistSignature,
348+
input: *const c_uchar,
349+
input_len: size_t,
350+
) -> c_int;
351+
352+
#[cfg_attr(
353+
not(feature = "external-symbols"),
354+
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_signature_serialize"
355+
)]
356+
pub fn secp256k1_whitelist_signature_serialize(
357+
ctx: *const Context,
358+
output: *mut c_uchar,
359+
outputlen: *mut size_t,
360+
sig: *const WhitelistSignature,
361+
) -> c_int;
362+
363+
#[cfg_attr(
364+
not(feature = "external-symbols"),
365+
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_sign"
366+
)]
367+
pub fn secp256k1_whitelist_sign(
368+
ctx: *const Context,
369+
sig: *mut WhitelistSignature,
370+
online_keys: *const PublicKey,
371+
offline_keys: *const PublicKey,
372+
n_keys: size_t,
373+
sub_pubkey: *const PublicKey,
374+
online_seckey: *const c_uchar,
375+
summed_seckey: *const c_uchar,
376+
index: size_t,
377+
noncefp: NonceFn,
378+
noncedata: *mut c_void,
379+
) -> c_int;
380+
381+
#[cfg_attr(
382+
not(feature = "external-symbols"),
383+
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_verify"
384+
)]
385+
pub fn secp256k1_whitelist_verify(
386+
ctx: *const Context,
387+
sig: *const WhitelistSignature,
388+
online_keys: *const PublicKey,
389+
offline_keys: *const PublicKey,
390+
n_keys: size_t,
391+
sub_pubkey: *const PublicKey,
392+
) -> c_int;
337393
}
338394

339395
#[repr(C)]
@@ -476,6 +532,39 @@ impl hash::Hash for PedersenCommitment {
476532
}
477533
}
478534

535+
/// A ring signature for the "whitelist" scheme.
536+
#[repr(C)]
537+
#[derive(Clone)]
538+
pub struct WhitelistSignature {
539+
/// The number of keys.
540+
pub n_keys: size_t,
541+
/// The signature in the form of e0 + n_keys s values.
542+
pub data: [u8; 32 * (1 + WHITELIST_MAX_N_KEYS)],
543+
}
544+
545+
impl hash::Hash for WhitelistSignature {
546+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
547+
self.n_keys.hash(state);
548+
self.data[..].hash(state);
549+
}
550+
}
551+
552+
impl PartialEq for WhitelistSignature {
553+
fn eq(&self, other: &Self) -> bool {
554+
self.n_keys == other.n_keys && self.data[..] == other.data[..]
555+
}
556+
}
557+
impl Eq for WhitelistSignature {}
558+
559+
impl Default for WhitelistSignature {
560+
fn default() -> WhitelistSignature {
561+
WhitelistSignature {
562+
n_keys: 0,
563+
data: [0; 32 * (1 + WHITELIST_MAX_N_KEYS)],
564+
}
565+
}
566+
}
567+
479568
/// Same as secp256k1_nonce_function_hardened with the exception of using the
480569
/// compressed 33-byte encoding for the pubkey argument.
481570
pub type EcdsaAdaptorNonceFn = Option<

src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ pub enum Error {
110110
CannotRecoverAdaptorSecret,
111111
/// Given adaptor signature is not valid for the provided combination of public key, encryption key and message
112112
CannotVerifyAdaptorSignature,
113+
/// Given bytes don't represent a valid whitelist signature
114+
InvalidWhitelistSignature,
115+
/// Invalid PAK list
116+
InvalidPakList,
117+
/// Couldn't create whitelist signature with the given data.
118+
CannotCreateWhitelistSignature,
119+
/// The given whitelist signature doesn't correctly prove inclusion in the whitelist.
120+
InvalidWhitelistProof,
113121
}
114122

115123
// Passthrough Debug to Display, since errors should be user-visible
@@ -129,6 +137,14 @@ impl fmt::Display for Error {
129137
Error::Upstream(inner) => return write!(f, "{}", inner),
130138
Error::InvalidTweakLength => "Tweak must of size 32",
131139
Error::TweakOutOfBounds => "Tweak must be less than secp curve order",
140+
Error::InvalidWhitelistSignature => "malformed whitelist signature",
141+
Error::InvalidPakList => "invalid PAK list",
142+
Error::CannotCreateWhitelistSignature => {
143+
"cannot create whitelist signature with the given data"
144+
}
145+
Error::InvalidWhitelistProof => {
146+
"given whitelist signature doesn't correctly prove inclusion in the whitelist"
147+
}
132148
};
133149

134150
f.write_str(str)

src/zkp/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod rangeproof;
77
#[cfg(feature = "std")]
88
mod surjection_proof;
99
mod tag;
10+
mod whitelist;
1011

1112
pub use self::ecdsa_adaptor::*;
1213
pub use self::generator::*;
@@ -17,3 +18,4 @@ pub use self::rangeproof::*;
1718
#[cfg(feature = "std")]
1819
pub use self::surjection_proof::*;
1920
pub use self::tag::*;
21+
pub use self::whitelist::*;

0 commit comments

Comments
 (0)