Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equal PARI integers have different hashes #11611

Closed
jdemeyer opened this issue Jul 19, 2011 · 17 comments
Closed

Equal PARI integers have different hashes #11611

jdemeyer opened this issue Jul 19, 2011 · 17 comments

Comments

@jdemeyer
Copy link

In the code below, a5 and b5 are PARI integers equal to 5 but they have different hashes:

sage: pariK=pari("nfinit(x^2-x-1)"); b5 = pari(5); b5.debug(); a5 = pari(5); a5.debug(); print a5.__hash__(), b5.__hash__()
[&=00000000049437b0] INT(lg=3):0200000000000003 (+,lgefint=3):4000000000000003 0000000000000005
[&=000000000493cfe0] INT(lg=3):0200000000000003 (+,lgefint=3):7000000000000003 0000000000000005
-1729382256910270461 -8646911284551352317

This bug was discovered for ideals having different hashes.

The problem is with the initialization of integers in new_gen_from_mpz_t(). The attached patch fixes various instances of bad PARI object initialization.

Component: number fields

Keywords: pari cgetg integer ideal hnf

Author: Jeroen Demeyer

Reviewer: William Stein

Merged: sage-4.7.2.alpha1

Issue created by migration from https://trac.sagemath.org/ticket/11611

@jdemeyer
Copy link
Author

Changed keywords from none to pari ideal hnf

@jdemeyer
Copy link
Author

jdemeyer commented Aug 2, 2011

comment:2

I have not been able to reproduce this error through the string interface:

sage: K.<a> = NumberField(x^2 - x - 1)
sage: I = K.ideal(2 * a - 1)
sage: pari("K=%s; idealhnf(K, idealfactor(K, %s)[1,1])"%(pari(K),pari(I))).debug()
[&=0000000004998178] MAT(lg=3):2600000000000003 0000000004998160 0000000004998130
  mat(1,1) = [&=0000000004998148] INT(lg=3):0200000000000003 (+,lgefint=3):4000000000000003 0000000000000005
  mat(1,2) = [&=0000000004998118] INT(lg=3):0200000000000003 (+,lgefint=3):4000000000000003 0000000000000002
  mat(2,1) = gen_0
  mat(2,2) = [&=0000000004998100] INT(lg=3):0200000000000003 (+,lgefint=3):4000000000000003 0000000000000001

@jdemeyer jdemeyer assigned jdemeyer and unassigned loefflerd Aug 2, 2011
@jdemeyer
Copy link
Author

jdemeyer commented Aug 2, 2011

comment:3

The problem lies with the conversion between PARI and Sage:

sage: K.<a> = NumberField(x^2 - x - 1)
sage: pari_ideal = pari("[5, [2, 1]~, 2, 1, [2, 1]~]")
sage: pari(K.ideal(pari_ideal)).debug()
[&=00000000049f97b8] MAT(lg=3):2600000000000003 00000000049f97a0 00000000049f9770
  mat(1,1) = [&=00000000049f9788] INT(lg=3):0200000000000003 (+,lgefint=3):6600000000000003 0000000000000005
  mat(1,2) = [&=00000000049f9758] INT(lg=3):0200000000000003 (+,lgefint=3):4000000000000003 0000000000000002
  mat(2,1) = gen_0
  mat(2,2) = [&=00000000049f9740] INT(lg=3):0200000000000003 (+,lgefint=3):4000000000000003 0000000000000001

@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

Changed keywords from pari ideal hnf to pari integer ideal hnf

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

comment:4

The issue is already visible on the level of PARI INTs, see the new ticket description.

@jdemeyer jdemeyer changed the title Equal ideals have different hashes Equal PARI integers have different hashes Aug 3, 2011
@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

Changed keywords from pari integer ideal hnf to pari cgetg integer ideal hnf

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

Attachment: 11611.patch.gz

@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

Author: Jeroen Demeyer

@williamstein
Copy link
Contributor

comment:8

I read the patch, which is mostly about rewriting Cython code against the PARI library in a way that looks much cleaner and makes sense. I don't quite understand precisely what it is about the code here that fixes the bug though.

All tests pass, and the new code looks and reads well.

@tornaria
Copy link
Contributor

tornaria commented Aug 3, 2011

comment:9

As William, I read the ticket description and the patch and I don't understand what is really the cause of the issue and why the patch fixes it.

I'd encourage Jeroen to add a comment explaining this bit and, if possible, to split the patch in two parts: a "fix the issue" part and a "cleanup" part (in the order that is most convenient).


I'm also wondering if the hash of a pari integer should match the hash of the corresponding sage or python integers. That is carefully considered in the sage integers/rationals, etc. The principle is that two values that are equivalent (for the purposes of equality comparision) should have the same hash to avoid nastiness in using the values as dictionary keys. The same would be true for some other pari types.

@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

comment:10

The code which is purely about the bug fix is in lines 9005--9007 of the new sage/libs/pari/gen.pyx. The problem is that z[1] was never properly initialized and might contain some random garbage which was on the PARI stack before.

I also rewrote some code which was similar in spirit to the buggy code, even though I could not obviously produce bugs from that code. Then I also did some general cleanup, like getting rid of __set_lvalue__().

@jdemeyer
Copy link
Author

jdemeyer commented Aug 3, 2011

comment:11

Replying to @tornaria:

I'm also wondering if the hash of a pari integer should match the hash of the corresponding sage or python integers. That is carefully considered in the sage integers/rationals, etc. The principle is that two values that are equivalent (for the purposes of equality comparision) should have the same hash to avoid nastiness in using the values as dictionary keys. The same would be true for some other pari types.

This looks quite hard. You would really have to rewrite the PARI hash function for this to match the Sage hash functions or the other way around. Also, consider that PARI objects are rarely directly visible in Sage. They usually live in private members of classes, so are unlikely to end up as dictionary keys. And even if they do, it would be even rarer to mix PARI and Sage types.

@jdemeyer
Copy link
Author

jdemeyer commented Aug 8, 2011

Merged: sage-4.7.2.alpha1

@jdemeyer
Copy link
Author

jdemeyer commented Aug 8, 2011

Reviewer: William Stein

@jdemeyer jdemeyer closed this as completed Aug 8, 2011
@jdemeyer
Copy link
Author

comment:13

See #11854 for a follow-up (it turns out this ticket does not fully fix the problem).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants