An algebraic characterization of injectivity in phase retrieval