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

(WIP) Discrete logarithm #110

Closed
wants to merge 6 commits into from
Closed

(WIP) Discrete logarithm #110

wants to merge 6 commits into from

Conversation

Taneb
Copy link

@Taneb Taneb commented Jun 10, 2018

This is the work I started at ZuriHac. It implements Pollard's rho algorithm to compute the discrete logarithm. This has the deficiency that there are some solutions it cannot find, such as 2^x == 7 (mod 9), which I would like to resolve before this PR gets merged.

Taneb added 4 commits June 9, 2018 14:33
This is a first implementation of a discreteLogarithm function.
It uses a naive algorithm that is really slow on large moduli.

In subsequent commits I will add a benchmark and use a more sophisticated algorithm.
Currently this takes 1.22 seconds on my laptop! Lots of room for improvement
Benchmark gives a result of 7ms, a speed-up of about 175 times!
This test currently fails and needs to be resolved.
@Bodigrim
Copy link
Owner

@Taneb just a gentle reminder that you intended to resolve remaining issues. Or we can merge as is and restrict inputs of discrete logarithm to primitive roots only.

@Taneb
Copy link
Author

Taneb commented Jun 22, 2018

Sorry, I've been busier than I expected! 😅 Going to do some work on this tonight, though

@Bodigrim
Copy link
Owner

I think we can introduce a newtype

newtype PrimitiveRoot n = PrimitiveRoot (Mod n)

and amend the signature of Math.NumberTheory.Moduli.PrimitiveRoot to

isPrimitiveRoot
  :: KnownNat n => Mod n -> Maybe (PrimitiveRoot n)

Consequently, discreteLogarithm should allow only PrimitiveRoot as its second argument,

discreteLogarithm :: (KnownNat m, Integral b) => Mod m -> PrimitiveRoot m -> Maybe b

This eliminates the current issue with logarithms by non-primitive base (Pollard's algorithm supposes that the base is primitive) without compromising performance or correctness.

@Taneb
Copy link
Author

Taneb commented Jul 1, 2018

Oooh, that's a good idea. I'll implement that this evening

Taneb added 2 commits July 1, 2018 20:19
…' to use this.

I'm not sure about the name for primitiveRootGetMod.

Another thing I thought of is it might be nice to have a type for members of the
multiplicative group modulo m, that is, the subset of Mod m coprime to m.
-- | 'PrimitiveRoot n' is a type which is only inhabited by primitive roots of
-- n.
newtype PrimitiveRoot n = PrimitiveRoot (Mod n)
deriving (Eq, Ord, Show)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us define newtype PrimitiveRoot n = PrimitiveRoot { unprimitiveRoot :: Mod n } and export the type and the accessor, but not the constructor.

@Bodigrim
Copy link
Owner

@Taneb any chance to finish it soon?

This was referenced Aug 12, 2018
@b-mehta b-mehta mentioned this pull request Aug 22, 2018
7 tasks
@Bodigrim
Copy link
Owner

Bodigrim commented Sep 12, 2018

Closing, superseded by #130. Anyway, thanks for you efforts.

@Bodigrim Bodigrim closed this Sep 12, 2018
@Taneb
Copy link
Author

Taneb commented Sep 12, 2018

Sorry I neglected this issue! I'm glad it's been solved by someone, I had... less time and headspace for this than I'd have liked

@Bodigrim
Copy link
Owner

It was a pleasure to work with you at ZuriHac.

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

Successfully merging this pull request may close these issues.

2 participants