Skip to content

Commit

Permalink
Update Poseidon implementation to return only a single BigIntger. #119
Browse files Browse the repository at this point in the history
  • Loading branch information
kPatch committed Nov 15, 2024
1 parent bde3caa commit cc68d01
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static BigInteger[] Hash(
int nOuts = 1
)
{
var bigIntInputs = inputs.Select(i => i switch
BigInteger[] bigIntInputs = inputs.Select(i => i switch
{
BigInteger bi => bi,
int n => new BigInteger(n),
Expand Down Expand Up @@ -103,5 +103,70 @@ public static BigInteger[] Hash(

throw new ArgumentException($"Invalid number of outputs requested {nOuts}, max {state.Length}");
}

public static BigInteger[] Hash(
BigInteger[] inputs,
//object opt,
Dictionary<string, object> opt,
int nOuts = 1
)
{
//var bigIntInputs = inputs.Select(i => i switch
//{
// BigInteger bi => bi,
// int n => new BigInteger(n),
// string s => BigInteger.Parse(s),
// _ => throw new ArgumentException($"Invalid input type: {i.GetType()}")
//}).ToArray();
BigInteger[] bigIntInputs = inputs;

if (bigIntInputs.Length <= 0)
throw new ArgumentException("Not enough inputs");
if (bigIntInputs.Length > N_ROUNDS_P.Length)
throw new ArgumentException("Too many inputs");

int t = bigIntInputs.Length + 1;
int nRoundsF = N_ROUNDS_F;
int nRoundsP = N_ROUNDS_P[t - 2];

// IRVIN: Access C array
//var cArray = (List<BigInteger>)opt["C"];
var cArray = (BigInteger[])opt["C"];

// IRVIN: Access M array (list of lists)
//var mArray = (List<List<BigInteger>>)opt["M"];
var mArray = (BigInteger[][])opt["M"];

if (mArray.Length != t)
throw new ArgumentException($"Incorrect M length, expected {t} got {mArray.Length}");

var state = new BigInteger[t];
state[0] = 0;
Array.Copy(bigIntInputs, 0, state, 1, bigIntInputs.Length);

for (int x = 0; x < nRoundsF + nRoundsP; x++)
{
for (int y = 0; y < state.Length; y++)
{
//state[y] = (state[y] + new BigInteger(Convert.FromBase64String(cArray[x * t + y]))) % F;
state[y] = (state[y] + cArray[x * t + y]) % F; // IRVIN: Assuming cArray is BigInteger already
if (x < nRoundsF / 2 || x >= nRoundsF / 2 + nRoundsP)
state[y] = Pow5(state[y]);
else if (y == 0)
state[y] = Pow5(state[y]);
}
//state = Mix(state, mArray.Select(row =>
// row.Select(s => new BigInteger(Convert.FromBase64String(s))).ToArray()
//).ToArray());
state = Mix(state, mArray);
}

if (nOuts == 1)
return new[] { state[0] };
if (nOuts <= state.Length)
return state.Take(nOuts).ToArray();

throw new ArgumentException($"Invalid number of outputs requested {nOuts}, max {state.Length}");
}
}
}
20 changes: 11 additions & 9 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
using OpenDive.Crypto.PoseidonLite;
using OpenDive.Crypto.PoseidonLite.Constants;

public class Poseidon1
public static class Poseidon1
{
private Dictionary<string, object> c1;
private Dictionary<string, object> c;

public Poseidon1()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c1 = new Dictionary<string, object> { ["C"] = C1.C, ["M"] = C1.M };
c = BigIntUnstringifier.UnstringifyBigInts(c1);
Dictionary<string, object> c1 = new Dictionary<string, object> { ["C"] = C1.C, ["M"] = C1.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c1);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c1 = new Dictionary<string, object> { ["C"] = C1.C, ["M"] = C1.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c1);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}
}
24 changes: 10 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

public class Poseidon10
{
private Dictionary<string, object> c10;
private Dictionary<string, object> c;

public Poseidon10()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c10 = new Dictionary<string, object> { ["C"] = C10.C, ["M"] = C10.M };
c = BigIntUnstringifier.UnstringifyBigInts(c10);
Dictionary<string, object> c10 = new Dictionary<string, object> { ["C"] = C10.C, ["M"] = C10.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c10);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c10 = new Dictionary<string, object> { ["C"] = C10.C, ["M"] = C10.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c10);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
24 changes: 10 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

public class Poseidon11
{
private Dictionary<string, object> c11;
private Dictionary<string, object> c;

public Poseidon11()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c11 = new Dictionary<string, object> { ["C"] = C11.C, ["M"] = C11.M };
c = BigIntUnstringifier.UnstringifyBigInts(c11);
Dictionary<string, object> c11 = new Dictionary<string, object> { ["C"] = C11.C, ["M"] = C11.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c11);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c11 = new Dictionary<string, object> { ["C"] = C11.C, ["M"] = C11.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c11);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
24 changes: 10 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

public class Poseidon12
{
private Dictionary<string, object> c12;
private Dictionary<string, object> c;

public Poseidon12()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c12 = new Dictionary<string, object> { ["C"] = C12.C, ["M"] = C12.M };
c = BigIntUnstringifier.UnstringifyBigInts(c12);
Dictionary<string, object> c12 = new Dictionary<string, object> { ["C"] = C12.C, ["M"] = C12.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c12);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c12 = new Dictionary<string, object> { ["C"] = C12.C, ["M"] = C12.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c12);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
22 changes: 8 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@

public class Poseidon13
{
private Dictionary<string, object> c13;
private Dictionary<string, object> c;

public Poseidon13()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c13 = new Dictionary<string, object> { ["C"] = C13.C, ["M"] = C13.M };
c = BigIntUnstringifier.UnstringifyBigInts(c13);
Dictionary<string, object> c13 = new Dictionary<string, object> { ["C"] = C13.C, ["M"] = C13.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c13);
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c13 = new Dictionary<string, object> { ["C"] = C13.C, ["M"] = C13.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c13);
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
23 changes: 9 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@

public class Poseidon14
{
private Dictionary<string, object> c14;
private Dictionary<string, object> c;

public Poseidon14()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c14 = new Dictionary<string, object> { ["C"] = C14.C, ["M"] = C14.M };
c = BigIntUnstringifier.UnstringifyBigInts(c14);
Dictionary<string, object> c14 = new Dictionary<string, object> { ["C"] = C14.C, ["M"] = C14.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c14);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c14 = new Dictionary<string, object> { ["C"] = C14.C, ["M"] = C14.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c14);
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
24 changes: 10 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon15.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

public class Poseidon15
{
private Dictionary<string, object> c15;
private Dictionary<string, object> c;

public Poseidon15()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c15 = new Dictionary<string, object> { ["C"] = C15.C, ["M"] = C15.M };
c = BigIntUnstringifier.UnstringifyBigInts(c15);
Dictionary<string, object> c15 = new Dictionary<string, object> { ["C"] = C15.C, ["M"] = C15.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c15);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c15 = new Dictionary<string, object> { ["C"] = C15.C, ["M"] = C15.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c15);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
24 changes: 10 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

public class Poseidon16
{
private Dictionary<string, object> c16;
private Dictionary<string, object> c;

public Poseidon16()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c16 = new Dictionary<string, object> { ["C"] = C16.C, ["M"] = C16.M };
c = BigIntUnstringifier.UnstringifyBigInts(c16);
Dictionary<string, object> c16 = new Dictionary<string, object> { ["C"] = C16.C, ["M"] = C16.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c16);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c16 = new Dictionary<string, object> { ["C"] = C16.C, ["M"] = C16.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c16);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
24 changes: 10 additions & 14 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PoseidonLite/Poseidon2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

public class Poseidon2
{
private Dictionary<string, object> c2;
private Dictionary<string, object> c;

public Poseidon2()
public static BigInteger Hash(object[] inputs, int nOuts = 1)
{
c2 = new Dictionary<string, object> { ["C"] = C2.C, ["M"] = C2.M };
c = BigIntUnstringifier.UnstringifyBigInts(c2);
Dictionary<string, object> c2 = new Dictionary<string, object> { ["C"] = C2.C, ["M"] = C2.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c2);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

public BigInteger[] Hash(object[] inputs, int nOuts = 1)
public static BigInteger Hash(BigInteger[] inputs, int nOuts = 1)
{
return Poseidon.Hash(inputs, this.c, nOuts);
Dictionary<string, object> c2 = new Dictionary<string, object> { ["C"] = C2.C, ["M"] = C2.M };
Dictionary<string, object> c = BigIntUnstringifier.UnstringifyBigInts(c2);
// IRVIN: Explicitly constraint it to return the first value of the hash
return Poseidon.Hash(inputs, c, nOuts)[0];
}

//public BigInteger[] Hash(string[] inputs, int nOuts)
//{
// //PoseidonHash.Hash();
// throw new NotSupportedException();
//}
}
Loading

0 comments on commit cc68d01

Please sign in to comment.