How can an undergraduate be recognized for work done on a paper, with a fellow PhD student and a professor? I am an undergraduate student, and recently I was introduced a PhD student in my university's math department.

In a brief interaction with him, he mentioned that he was doing work in Differential Topology, an area which I happen to be self-studying at the moment. I asked him about his research problem, and mentioned what I was self-studying, and he offered me an opportunity to work with his PhD advisor (a former professor of mine), and himself, and stated that they were looking to publish a paper in mid 2018, and I could possibly be listed as a co-author.

He has yet to run the idea past my former professor, but he has stated that my former professor would be all on board with the idea. My former professor likely does not know or remember me, we only had one brief interaction during a class on Discrete Mathematics (coincidentally the interaction was about Differential Topology and some other Differential Geometric stuff, because he saw me reading a book on it before class commenced).

However, my university has no official undergraduate research program, so everything (i.e. any research conducted) will likely be done on an informal basis. My main question is this, how can I protect the work that I do on a project like this? What measures can I take to ensure that whatever work I do is credited, if the paper containing my work is published? 

Firstly it should be noted, that regardless of whether or not my work will be recognized on the paper (if any contributions to the paper are made by my part) I still intend to take this opportunity and work with the PhD student and my former professor, mainly as it will be a good learning experience for me on how to properly do research, the possibility of being listed as a co-author is just an added bonus.

Also I'm well aware, that it's probable that I may not make any contribution at all, however I am asking this question in the event that I do contribute some work towards the paper. 

If it's of any importance, I have a good relationship, and am somewhat well known by other faculty members (Topologists) in my universities' math department, as they have previously allowed me attend lectures for their courses which are only reserved for students in years above me.
The best thing you can do to ensure your work is recognized and credited is to have honest, open communications with the other people you’re working with. So long as everybody’s clear on who is doing what, and everybody is acting ethically, you shouldn’t run into problems.