2
2
use std:: convert:: TryInto ;
3
3
use std:: fmt:: Formatter ;
4
4
5
+ use elliptic_curve:: ops:: MulByGenerator ;
5
6
use ergo_chain_types:: EcPoint ;
6
7
use ergotree_ir:: serialization:: SigmaSerializable ;
7
8
use ergotree_ir:: sigma_protocol:: dlog_group;
@@ -13,6 +14,7 @@ use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
13
14
extern crate derive_more;
14
15
use derive_more:: From ;
15
16
use k256:: elliptic_curve:: PrimeField ;
17
+ use k256:: ProjectivePoint ;
16
18
use num_bigint:: BigUint ;
17
19
use num_traits:: ToPrimitive ;
18
20
@@ -22,10 +24,12 @@ use super::wscalar::Wscalar;
22
24
/// Secret key of discrete logarithm signature protocol
23
25
#[ cfg_attr( feature = "json" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
24
26
#[ cfg_attr( feature = "json" , serde( transparent) ) ]
25
- #[ derive( PartialEq , Eq , Clone , derive_more :: From ) ]
27
+ #[ derive( PartialEq , Eq , Clone ) ]
26
28
pub struct DlogProverInput {
27
29
/// secret key value
28
30
pub w : Wscalar ,
31
+ #[ serde( skip) ]
32
+ pk : EcPoint ,
29
33
}
30
34
31
35
impl std:: fmt:: Debug for DlogProverInput {
@@ -35,22 +39,35 @@ impl std::fmt::Debug for DlogProverInput {
35
39
}
36
40
}
37
41
42
+ impl From < Wscalar > for DlogProverInput {
43
+ fn from ( scalar : Wscalar ) -> Self {
44
+ DlogProverInput :: new ( scalar)
45
+ }
46
+ }
47
+
38
48
impl DlogProverInput {
39
49
/// Scalar(secret key) size in bytes
40
50
pub const SIZE_BYTES : usize = 32 ;
41
51
52
+ /// Create new DlogProverInput
53
+ pub fn new ( w : Wscalar ) -> DlogProverInput {
54
+ Self {
55
+ pk : EcPoint :: from ( ProjectivePoint :: mul_by_generator ( w. as_scalar_ref ( ) ) ) ,
56
+ w,
57
+ }
58
+ }
42
59
/// generates random secret in the range [0, n), where n is DLog group order.
43
60
pub fn random ( ) -> DlogProverInput {
44
- DlogProverInput {
45
- w : dlog_group:: random_scalar_in_group_range ( crypto_utils:: secure_rng ( ) ) . into ( ) ,
46
- }
61
+ DlogProverInput :: new (
62
+ dlog_group:: random_scalar_in_group_range ( crypto_utils:: secure_rng ( ) ) . into ( ) ,
63
+ )
47
64
}
48
65
49
66
/// Attempts to parse the given byte array as an SEC-1-encoded scalar(secret key).
50
67
/// Returns None if the byte array does not contain a big-endian integer in the range [0, modulus).
51
68
pub fn from_bytes ( bytes : & [ u8 ; DlogProverInput :: SIZE_BYTES ] ) -> Option < DlogProverInput > {
52
69
k256:: Scalar :: from_repr ( ( * bytes) . into ( ) )
53
- . map ( |s| DlogProverInput :: from ( Wscalar :: from ( s) ) )
70
+ . map ( |s| DlogProverInput :: new ( Wscalar :: from ( s) ) )
54
71
. into ( )
55
72
}
56
73
@@ -87,12 +104,7 @@ impl DlogProverInput {
87
104
88
105
/// public key of discrete logarithm signature protocol
89
106
pub fn public_image ( & self ) -> ProveDlog {
90
- // test it, see https://github.com/ergoplatform/sigma-rust/issues/38
91
- let g = ergo_chain_types:: ec_point:: generator ( ) ;
92
- ProveDlog :: new ( ergo_chain_types:: ec_point:: exponentiate (
93
- & g,
94
- self . w . as_scalar_ref ( ) ,
95
- ) )
107
+ ProveDlog :: new ( self . pk )
96
108
}
97
109
98
110
/// Return true if the secret is 0
0 commit comments