|
8 | 8 |
|
9 | 9 | namespace Algorand.Unity.Crypto |
10 | 10 | { |
11 | | - public unsafe static class Ed25519 |
| 11 | + public static class Ed25519 |
12 | 12 | { |
13 | | - public struct KeyPair : INativeDisposable |
| 13 | + public readonly struct KeyPair : INativeDisposable |
14 | 14 | { |
15 | 15 | public readonly SecretKeyHandle SecretKey; |
16 | 16 | public readonly PublicKey PublicKey; |
@@ -72,7 +72,7 @@ public static implicit operator SecretKeyHandle(SecureMemoryHandle secureMemoryH |
72 | 72 | return new SecretKeyHandle() { handle = secureMemoryHandle }; |
73 | 73 | } |
74 | 74 |
|
75 | | - public Signature Sign<TMessage>(TMessage message) |
| 75 | + public unsafe Signature Sign<TMessage>(TMessage message) |
76 | 76 | where TMessage : IByteArray |
77 | 77 | { |
78 | 78 | var signature = new Signature(); |
@@ -138,18 +138,22 @@ public KeyPair ToKeyPair() |
138 | 138 | { |
139 | 139 | var pk = new PublicKey(); |
140 | 140 | var sk = SecretKeyHandle.Create(); |
141 | | - fixed (Seed* seedPtr = &this) |
| 141 | + unsafe |
142 | 142 | { |
143 | | - int error = crypto_sign_ed25519_seed_keypair( |
144 | | - &pk, |
145 | | - sk.Ptr, |
146 | | - seedPtr |
147 | | - ); |
148 | | - if (error > 0) |
| 143 | + fixed (Seed* seedPtr = &this) |
149 | 144 | { |
150 | | - throw new System.Exception($"error code {error} when converting to KeyPair"); |
| 145 | + int error = crypto_sign_ed25519_seed_keypair( |
| 146 | + &pk, |
| 147 | + sk.Ptr, |
| 148 | + seedPtr |
| 149 | + ); |
| 150 | + if (error > 0) |
| 151 | + { |
| 152 | + throw new System.Exception($"error code {error} when converting to KeyPair"); |
| 153 | + } |
151 | 154 | } |
152 | 155 | } |
| 156 | + |
153 | 157 | return new KeyPair(sk, pk); |
154 | 158 | } |
155 | 159 | } |
@@ -233,14 +237,17 @@ public byte this[int index] |
233 | 237 | public bool Verify<TMessage>(TMessage message, PublicKey pk) |
234 | 238 | where TMessage : IByteArray |
235 | 239 | { |
236 | | - fixed (Signature* s = &this) |
| 240 | + unsafe |
237 | 241 | { |
238 | | - var error = crypto_sign_ed25519_verify_detached( |
239 | | - s, |
240 | | - message.GetUnsafePtr(), |
241 | | - (UIntPtr)message.Length, |
242 | | - &pk); |
243 | | - return error == 0; |
| 242 | + fixed (Signature* s = &this) |
| 243 | + { |
| 244 | + var error = crypto_sign_ed25519_verify_detached( |
| 245 | + s, |
| 246 | + message.GetUnsafePtr(), |
| 247 | + (UIntPtr)message.Length, |
| 248 | + &pk); |
| 249 | + return error == 0; |
| 250 | + } |
244 | 251 | } |
245 | 252 | } |
246 | 253 |
|
|
0 commit comments