Re: Why is this happening to my software? An illegal instruction error.

2024-06-18 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jun 18, 2024 at 11:07:47PM +, Murilo via Digitalmars-d-learn wrote:
> I've created a software which performs the Fermat's Primality Test,
> however if I input a very big number it causes an error saying
> "Illegal instruction (core dumped)". Does anyone know why?
> 
> I've used GDB and here is the message:
> Program received signal SIGILL, Illegal instruction.
> 0x555e40b0 in
> _D3std8internal4math11biguintcore7BigUint3powFNaNbNfNkMSQCcQCbQBvQBtQBjmZQs
> ()
[...]

There are a few possible causes:

1) There's a pointer bug in your program that corrupted some memory,
causing a function pointer to point to something that isn't a function,
and when the CPU tried to execute instructions from there it triggered a
fault.

2) The optimized assembly code in std.bigint may contain an instruction
that's incompatible with your CPU, so a fault is triggered when the CPU
tried to execute it.

3) Your program may have been incorrectly linked, and the ABI
incompatibility could have triggered this fault.

4) You may be using std.bigint for numbers far bigger than the range
it's designed for.  While this is unlikely to be the cause of this error
(I've tested std.bigint with numbers up to 100,000+ digits without any
crashes), using an unusually large number may cause the code to do
something unexpected that may trigger the crash.  The chances are pretty
low that this is actually the case, though.


Among the above causes, I'd say (1) is the most likely, (2) is the 2nd
most likely if (1) isn't the cause.


T

-- 
Truth, Sir, is a cow which will give [skeptics] no more milk, and so they are 
gone to milk the bull. -- Sam. Johnson


Why is this happening to my software? An illegal instruction error.

2024-06-18 Thread Murilo via Digitalmars-d-learn
I've created a software which performs the Fermat's Primality 
Test, however if I input a very big number it causes an error 
saying "Illegal instruction (core dumped)". Does anyone know why?


I've used GDB and here is the message:
Program received signal SIGILL, Illegal instruction.
0x555e40b0 in 
_D3std8internal4math11biguintcore7BigUint3powFNaNbNfNkMSQCcQCbQBvQBtQBjmZQs ()


Here is the link to the source code: 
https://github.com/MuriloMir/Fermat-primality-test


Re: aligned struct field weirdness

2024-06-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On Tuesday, 18 June 2024 at 09:10:28 UTC, realhet wrote:
I tried to narrow the problem, but I wasn't able, but I guess 
I've found another solution: There is a part in my framework 
which works with LDC2 1.28, but that's 'illegal' with later 
versions.


Oh yeah. That is many versions ago! Released in 2021.

That's why I'm stuck in the past, but now it adds more 
motivation to catch up with the latest version.


Another motivation -- even if you found this was a bug and it is 
still in the latest compiler, the fix will go into the *latest 
compiler*, not the one that works with your code!


That part declares an enum, whose members have UDA's pointing 
to the enum's members.
With 2 step parsing it is valid, but later compiler versions 
don't like this.
It's a quite big state machine graph, I have to refactor this 
to eliminate this self-referencing to catch up with the latest 
compiler.


Hm... a possibility is to change the UDAs to strings, and then 
post-process the strings into the actual members using 
`__traits(getMember, State, name)`


So e.g.:

```d
enum State {
   @"state2" state1,
   @"state1" state2
}
```

-Steve


Re: SHA256 Signature

2024-06-18 Thread Vahid via Digitalmars-d-learn

On Tuesday, 18 June 2024 at 17:18:18 UTC, Erdem wrote:

On Tuesday, 18 June 2024 at 06:49:24 UTC, Vahid wrote:


How can I create a SHA256 signature hash in D?


Does this do what you want?


```d
import std.digest.md;
import std.digest.sha;
import std.stdio;

void main()
{
auto key = makeDigest!SHA256();
key.put(cast(ubyte[])"çorba");
auto result = key.finish();
writeln(result.toHexString());
}
```


Thank you for your reply.

I have found that the issue is related to the 'Secret' variable. 
In fact, I possess a private key that needs to sign it for the 
JWT. How can I accomplish this? It appears that RSA encryption is 
required in this scenario.


Re: SHA256 Signature

2024-06-18 Thread Erdem via Digitalmars-d-learn

On Tuesday, 18 June 2024 at 06:49:24 UTC, Vahid wrote:


How can I create a SHA256 signature hash in D?


Does this do what you want?


```d
import std.digest.md;
import std.digest.sha;
import std.stdio;

void main()
{
auto key = makeDigest!SHA256();
key.put(cast(ubyte[])"çorba");
auto result = key.finish();
writeln(result.toHexString());
}
```



Re: aligned struct field weirdness

2024-06-18 Thread realhet via Digitalmars-d-learn
On Tuesday, 18 June 2024 at 02:26:00 UTC, Steven Schveighoffer 
wrote:
All the code you posted here looks fine to me. It compiles and 
runs fine on run.dlang.io (even with the `version(none)` 
changed to `version(all)`, or using `scoped!B`).


Thank You for checking.

Also to add to the weirdness, this happens only with release 
build.


I tried to narrow the problem, but I wasn't able, but I guess 
I've found another solution: There is a part in my framework 
which works with LDC2 1.28, but that's 'illegal' with later 
versions.
That's why I'm stuck in the past, but now it adds more motivation 
to catch up with the latest version.


That part declares an enum, whose members have UDA's pointing to 
the enum's members.
With 2 step parsing it is valid, but later compiler versions 
don't like this.
It's a quite big state machine graph, I have to refactor this to 
eliminate this self-referencing to catch up with the latest 
compiler.


So now I have fixes that seem to be working, and soon with the 
latest compiler I will check it again, and hopefully the fixes 
aren't needed anymore.




SHA256 Signature

2024-06-18 Thread Vahid via Digitalmars-d-learn

Hi,

How can I create a SHA256 signature hash in D? Something similar 
to the PHP code below:


```php
openssl_sign($body, $signature, $secret, OPENSSL_ALGO_SHA256);
```

I've tried the code below without success:

```d
auto signature = 
body.representation.hmac!SHA256(secret.representation).toHexString!(LetterCase.lower).to!string;

```