@@ -3,6 +3,7 @@ use core::convert::TryInto;
33use core:: fmt:: Formatter ;
44
55use alloc:: vec:: Vec ;
6+ use elliptic_curve:: ops:: MulByGenerator ;
67use ergo_chain_types:: EcPoint ;
78use ergotree_ir:: serialization:: SigmaSerializable ;
89use ergotree_ir:: sigma_protocol:: sigma_boolean:: ProveDhTuple ;
@@ -13,18 +14,20 @@ use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
1314extern crate derive_more;
1415use derive_more:: From ;
1516use k256:: elliptic_curve:: PrimeField ;
17+ use k256:: ProjectivePoint ;
1618use num_bigint:: BigUint ;
1719use num_traits:: ToPrimitive ;
1820
1921use super :: wscalar:: Wscalar ;
2022
2123/// Secret key of discrete logarithm signature protocol
2224#[ cfg_attr( feature = "json" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
23- #[ cfg_attr( feature = "json" , serde( transparent ) ) ]
24- #[ derive( PartialEq , Eq , Clone , derive_more :: From ) ]
25+ #[ cfg_attr( feature = "json" , serde( from = "Wscalar" , into = "Wscalar" ) ) ]
26+ #[ derive( PartialEq , Eq , Clone ) ]
2527pub struct DlogProverInput {
2628 /// secret key value
2729 pub w : Wscalar ,
30+ pk : EcPoint ,
2831}
2932
3033impl core:: fmt:: Debug for DlogProverInput {
@@ -34,26 +37,46 @@ impl core::fmt::Debug for DlogProverInput {
3437 }
3538}
3639
40+ impl From < Wscalar > for DlogProverInput {
41+ fn from ( scalar : Wscalar ) -> Self {
42+ DlogProverInput :: new ( scalar)
43+ }
44+ }
45+
46+ impl From < DlogProverInput > for Wscalar {
47+ fn from ( prover_input : DlogProverInput ) -> Self {
48+ prover_input. w
49+ }
50+ }
51+
3752impl DlogProverInput {
3853 /// Scalar(secret key) size in bytes
3954 pub const SIZE_BYTES : usize = 32 ;
4055
56+ /// Create new DlogProverInput
57+ pub fn new ( w : Wscalar ) -> DlogProverInput {
58+ Self {
59+ pk : EcPoint :: from ( ProjectivePoint :: mul_by_generator ( w. as_scalar_ref ( ) ) ) ,
60+ w,
61+ }
62+ }
4163 /// generates random secret in the range [0, n), where n is DLog group order.
4264 #[ cfg( feature = "std" ) ]
4365 pub fn random ( ) -> DlogProverInput {
44- DlogProverInput {
45- w : ergotree_ir:: sigma_protocol:: dlog_group:: random_scalar_in_group_range (
46- super :: crypto_utils:: secure_rng ( ) ,
47- )
48- . into ( ) ,
49- }
66+ use ergotree_ir:: sigma_protocol:: dlog_group;
67+
68+ use crate :: sigma_protocol:: crypto_utils;
69+
70+ DlogProverInput :: new (
71+ dlog_group:: random_scalar_in_group_range ( crypto_utils:: secure_rng ( ) ) . into ( ) ,
72+ )
5073 }
5174
5275 /// Attempts to parse the given byte array as an SEC-1-encoded scalar(secret key).
5376 /// Returns None if the byte array does not contain a big-endian integer in the range [0, modulus).
5477 pub fn from_bytes ( bytes : & [ u8 ; DlogProverInput :: SIZE_BYTES ] ) -> Option < DlogProverInput > {
5578 k256:: Scalar :: from_repr ( ( * bytes) . into ( ) )
56- . map ( |s| DlogProverInput :: from ( Wscalar :: from ( s) ) )
79+ . map ( |s| DlogProverInput :: new ( Wscalar :: from ( s) ) )
5780 . into ( )
5881 }
5982
@@ -90,12 +113,7 @@ impl DlogProverInput {
90113
91114 /// public key of discrete logarithm signature protocol
92115 pub fn public_image ( & self ) -> ProveDlog {
93- // test it, see https://github.com/ergoplatform/sigma-rust/issues/38
94- let g = ergo_chain_types:: ec_point:: generator ( ) ;
95- ProveDlog :: new ( ergo_chain_types:: ec_point:: exponentiate (
96- & g,
97- self . w . as_scalar_ref ( ) ,
98- ) )
116+ ProveDlog :: new ( self . pk )
99117 }
100118
101119 /// Return true if the secret is 0
0 commit comments