@@ -133,45 +133,45 @@ public IntPtr GetProcAddress(IntPtr dllHandle, string name)
133133 #region Function mapping
134134
135135 private delegate int CreateSeedDelegate (
136- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) ] byte [ ] outputSeed
136+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) , Out ] byte [ ] outputSeed
137137 ) ;
138138
139139 private delegate void CreateKeypairDelegate (
140- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) ] byte [ ] outputPublicKey ,
141- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) ] byte [ ] outputPrivateKey ,
142- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputSeed
140+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) , Out ] byte [ ] outputPublicKey ,
141+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) , Out ] byte [ ] outputPrivateKey ,
142+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputSeed
143143 ) ;
144144
145145 private delegate void SignDelegate (
146- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) ] byte [ ] outputSignature ,
147- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputMessage ,
148- [ MarshalAs ( UnmanagedType . U8 ) ] ulong inputMessageLength ,
149- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputPublicKey ,
150- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputPrivateKey
146+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) , Out ] byte [ ] outputSignature ,
147+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputMessage ,
148+ [ MarshalAs ( UnmanagedType . U8 ) , In ] ulong inputMessageLength ,
149+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputPublicKey ,
150+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputPrivateKey
151151 ) ;
152152
153153 private delegate int VerifyDelegate (
154- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputSignature ,
155- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputMessage ,
156- [ MarshalAs ( UnmanagedType . U8 ) ] ulong inputMessageLength ,
157- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputPublicKey
154+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputSignature ,
155+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputMessage ,
156+ [ MarshalAs ( UnmanagedType . U8 ) , In ] ulong inputMessageLength ,
157+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputPublicKey
158158 ) ;
159159
160160 private delegate void AddScalarDelegate (
161- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) ] byte [ ] publicKey ,
162- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) ] byte [ ] privateKey ,
163- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputScalar
161+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) , In , Out ] byte [ ] publicKey ,
162+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) , In , Out ] byte [ ] privateKey ,
163+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputScalar
164164 ) ;
165165
166166 private delegate void KeyExchangeDelegate (
167- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) ] byte [ ] outputSharedSecret ,
168- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputPublicKey ,
169- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputPrivateKey
167+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 32 ) , Out ] byte [ ] outputSharedSecret ,
168+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputPublicKey ,
169+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputPrivateKey
170170 ) ;
171171
172172 private delegate void Ref10KeyConversionDelegate (
173- [ MarshalAs ( UnmanagedType . LPArray ) ] byte [ ] inputPrivateKey ,
174- [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) ] byte [ ] outputPrivateKey
173+ [ MarshalAs ( UnmanagedType . LPArray ) , In ] byte [ ] inputPrivateKey ,
174+ [ MarshalAs ( UnmanagedType . LPArray , SizeConst = 64 ) , Out ] byte [ ] outputPrivateKey
175175 ) ;
176176
177177 #endregion
@@ -340,9 +340,10 @@ public byte[] CreateSeed()
340340 /// Creates a new key pair from the given seed.
341341 /// </summary>
342342 /// <param name="seed">32B seed value to use for key generation.</param>
343- /// <returns>A byte[] array tuple (publicKey, privateKey).</returns>
343+ /// <param name="outputPublicKey">Output byte array into which the 32B public key will be written.</param>
344+ /// <param name="outputPrivateKey">Output byte array into which the 64B private key will be written.</param>
344345 /// <exception cref="ArgumentException">Thrown when the <paramref name="seed"/> argument is either <c>null</c> or too small (needs to be at least 32B).</exception>
345- public ( byte [ ] , byte [ ] ) CreateKeypair ( byte [ ] seed )
346+ public void CreateKeypair ( byte [ ] seed , out byte [ ] outputPublicKey , out byte [ ] outputPrivateKey )
346347 {
347348 if ( seed is null || seed . Length < 32 )
348349 {
@@ -353,7 +354,9 @@ public byte[] CreateSeed()
353354 byte [ ] privateKey = new byte [ 64 ] ;
354355
355356 createKeypairDelegate ( publicKey , privateKey , seed ) ;
356- return ( publicKey , privateKey ) ;
357+
358+ outputPublicKey = publicKey ;
359+ outputPrivateKey = privateKey ;
357360 }
358361
359362 /// <summary>
@@ -543,32 +546,37 @@ static string BytesToHexString(byte[] bytes)
543546
544547 Console . WriteLine ( "\n --- orlp-ed25519 ---\n " ) ;
545548
546- const string messageString = "Rise and shine, Dr. Freeman... rise and... shiiine !" ;
549+ const string messageString = "Rise and shine, Dr. Freeman... rise and... shine !" ;
547550
548551 using var ed25519 = new OrlpEd25519Context ( ) ;
549552
550553 byte [ ] seed = ed25519 . CreateSeed ( ) ;
551554
552- ( byte [ ] , byte [ ] ) keypair = ed25519 . CreateKeypair ( seed ) ;
555+ Console . WriteLine ( $ "Generated seed: { BytesToHexString ( seed ) } \n ") ;
556+
557+ ed25519 . CreateKeypair ( seed , out byte [ ] publicKey , out byte [ ] privateKey ) ;
553558
559+ Console . WriteLine ( $ "Generated public key: { BytesToHexString ( publicKey ) } \n ") ;
560+ Console . WriteLine ( $ "Generated private key: { BytesToHexString ( privateKey ) } \n ") ;
561+
554562 byte [ ] message = Encoding . UTF8 . GetBytes ( messageString ) ;
555563
556- byte [ ] signature = ed25519 . Sign ( message , keypair . Item1 , keypair . Item2 ) ;
564+ Console . WriteLine ( $ "Message: { messageString } \n ") ;
565+
566+ byte [ ] signature = ed25519 . Sign ( message , publicKey , privateKey ) ;
557567
558- bool valid = ed25519 . Verify ( signature , message , keypair . Item1 ) ;
568+ Console . WriteLine ( $ "Signature: { BytesToHexString ( signature ) } \n ") ;
569+
570+ bool valid = ed25519 . Verify ( signature , message , publicKey ) ;
559571
572+ Console . WriteLine ( $ "Valid: { valid } \n ") ;
573+
560574 byte [ ] seed2 = ed25519 . CreateSeed ( ) ;
575+
576+ ed25519 . AddScalar ( publicKey , privateKey , seed2 ) ;
561577
562- ed25519 . AddScalar ( keypair . Item1 , keypair . Item2 , seed2 ) ;
563-
564- Console . WriteLine ( $ "Generated seed: { BytesToHexString ( seed ) } \n ") ;
565- Console . WriteLine ( $ "Generated public key: { BytesToHexString ( keypair . Item1 ) } \n ") ;
566- Console . WriteLine ( $ "Generated private key: { BytesToHexString ( keypair . Item2 ) } \n ") ;
567- Console . WriteLine ( $ "Message: { messageString } \n ") ;
568- Console . WriteLine ( $ "Signature: { BytesToHexString ( signature ) } \n ") ;
569- Console . WriteLine ( $ "Valid: { valid } \n ") ;
570- Console . WriteLine ( $ "Public key after adding scalar \" { BytesToHexString ( seed2 ) } \" : { BytesToHexString ( keypair . Item1 ) } \n ") ;
571- Console . WriteLine ( $ "Private key after adding scalar \" { BytesToHexString ( seed2 ) } \" : { BytesToHexString ( keypair . Item2 ) } \n ") ;
578+ Console . WriteLine ( $ "Public key after adding scalar \" { BytesToHexString ( seed2 ) } \" : { BytesToHexString ( publicKey ) } \n ") ;
579+ Console . WriteLine ( $ "Private key after adding scalar \" { BytesToHexString ( seed2 ) } \" : { BytesToHexString ( privateKey ) } \n ") ;
572580 }
573581 }
574582}
0 commit comments