Browse Source

add test for poseidon hash writer; rename methods

fix/bbjj-err
Ilya 1 year ago
parent
commit
85b692c937
3 changed files with 363 additions and 5 deletions
  1. +3
    -2
      babyjub/babyjub_wrapper_test.go
  2. +2
    -2
      poseidon/poseidon_wrapper.go
  3. +358
    -1
      poseidon/poseidon_wrapper_test.go

+ 3
- 2
babyjub/babyjub_wrapper_test.go

@ -39,7 +39,7 @@ func TestBjjWrappedPrivateKey(t *testing.T) {
pub, ok := pk.Public().(*BjjWrappedPublicKey)
require.True(t, ok)
decomrpessSig, err := decomrpessSig(sig)
decomrpessSig, err := DecompressSig(sig)
require.NoError(t, err)
digestBI := big.NewInt(0).SetBytes(digest)
@ -64,7 +64,8 @@ func TestBjjWrappedPublicKeyEqual(t *testing.T) {
require.False(t, x1.Equal(x2))
}
func decomrpessSig(commpresedSig []byte) (*Signature, error) {
// DecompressSig decompresses a compressed signature.
func DecompressSig(commpresedSig []byte) (*Signature, error) {
poseidonComSig := &SignatureComp{}
if err := poseidonComSig.UnmarshalText(commpresedSig); err != nil {
return nil, err

+ 2
- 2
poseidon/poseidon_wrapper.go

@ -11,9 +11,9 @@ type digest struct {
frameSize int
}
// NewPoseidon returns the Poseidon hash of the input bytes.
// Sum returns the Poseidon hash of the input bytes.
// use frame size of 16 inputs by default
func NewPoseidon(b []byte) []byte {
func Sum(b []byte) []byte {
h, _ := New(16)
h.Write(b)
return h.Sum(nil)

+ 358
- 1
poseidon/poseidon_wrapper_test.go

@ -117,10 +117,367 @@ func TestPoseidonNewPoseidon(t *testing.T) {
inputBytes, err := hex.DecodeString(vector.bytes)
require.NoError(t, err)
res := NewPoseidon(inputBytes)
res := Sum(inputBytes)
require.NotEmpty(t, res)
require.Equal(t, vector.expectedHash, hex.EncodeToString(res))
})
}
}
func TestPoseidonWithChunkWriter(t *testing.T) {
type testVector struct {
bytesChunks []string
expectedHash string
}
var testVectors = []testVector{
{
bytesChunks: []string{"de", "ad"},
expectedHash: "0244ec1a137a24c92404de9f9c39907be151026a4eb7f9cfea60a5740e8a73b7",
},
{
bytesChunks: []string{"ce0e8e502600b14bb0a2c9689f", "7d93d10e9f5451f18f030ec3bb6c5001ea60"},
expectedHash: "03ace829d65dae7903fc9b22e904750ca0509b9a187cbeb004a4270e8322a2f4",
},
{
bytesChunks: []string{"34665289b71a2cb8", "bf4c289ae6d17d84", "5457c48bfc18623c", "a39e141b2e40c5d3"},
expectedHash: "144aa8e107a9fd8f8106016a8ff0fd1c265cdf31a40a8ef9ed44dfb4592cbbff",
},
{
bytesChunks: []string{
"60806040523480156200001157600080fd5b506040516200210338",
"0380620021038339810160408190526200003491620002e1565b62",
"00003f3362000125565b60005b620000506001602062000329565b",
"811015620000eb57602181602081106200006e576200006e620003",
"43565b01546021826020811062000086576200008662000343565b",
"015460408051602081019390935282015260600160405160208183",
"0303815290604052805190602001206021826001620000c1919062",
"000359565b60208110620000d457620000d462000343565b015580",
"620000e28162000374565b91505062000042565b50604680546001",
"600160a01b0319166001600160a01b038316179055620001116200",
"0175565b6043556200011e62000250565b50620003b5565b600080",
"546001600160a01b038381166001600160a01b0319831681178455",
"604051919092169283917f8be0079c531659141344cd1fd0a4f284",
"19497f9722a3daafe3b4186f6b6457e09190a35050565b60415460",
"00908190815b602081101562000248578160011660011415620001",
"e05760018160208110620001ac57620001ac62000343565b015460",
"408051602081019290925281018490526060016040516020818303",
"0381529060405280519060200120925062000224565b8260218260",
"208110620001f757620001f762000343565b015460408051602081",
"019390935282015260600160405160208183030381529060405280",
"51906020012092505b6200023160028362000392565b9150806200",
"023f8162000374565b9150506200017f565b509092915050565b60",
"458054906000620002628362000374565b90915550506043546042",
"546040805160208101939093528201526060016040805180830360",
"1f1901815282825280516020918201206045546000908152604483",
"5283902055604354604254908452908301527f61014378f82a0d80",
"9aefaf87a8ac9505b89c321808287a6e7810f29304c1fce3910160",
"405180910390a1565b600060208284031215620002f457600080fd",
"5b81516001600160a01b03811681146200030c57600080fd5b9392",
"505050565b634e487b7160e01b600052601160045260246000fd5b",
"6000828210156200033e576200033e62000313565b500390565b63",
"4e487b7160e01b600052603260045260246000fd5b600082198211",
"156200036f576200036f62000313565b500190565b600060001982",
"14156200038b576200038b62000313565b5060010190565b600082",
"620003b057634e487b7160e01b600052601260045260246000fd5b",
"500490565b611d3e80620003c56000396000f3fe60806040526004",
"361061010e5760003560e01c806355f6bc57116100a55780638da5",
"cb5b11610074578063a71d644411610059578063a71d6444146102",
"fb578063ed6be5c91461032b578063f2fde38b1461035557600080",
"fd5b80638da5cb5b146102b0578063a5392cf6146102db57600080",
"fd5b806355f6bc57146101f95780635ec6a8df1461021957806371",
"5018a61461026b5780637d8f04691461028057600080fd5b806331",
"9cf735116100e1578063319cf7351461017d5780633381fe901461",
"01935780633ae05047146101c05780633ed691ef146101d5576000",
"80fd5b806301fd904414610113578063029f27931461013c578063",
"0e21fbd7146101525780632dfdf0b514610167575b600080fd5b34",
"801561011f57600080fd5b5061012960425481565b604051908152",
"6020015b60405180910390f35b34801561014857600080fd5b5061",
"012960455481565b61016561016036600461170e565b610375565b",
"005b34801561017357600080fd5b5061012960415481565b348015",
"61018957600080fd5b5061012960435481565b34801561019f5760",
"0080fd5b506101296101ae36600461175f565b6044602052600090",
"8152604090205481565b3480156101cc57600080fd5b5061012961",
"0574565b3480156101e157600080fd5b5060455460009081526044",
"6020526040902054610129565b34801561020557600080fd5b5061",
"016561021436600461175f565b610641565b348015610225576000",
"80fd5b506046546102469073ffffffffffffffffffffffffffffff",
"ffffffffff1681565b60405173ffffffffffffffffffffffffffff",
"ffffffffffff9091168152602001610133565b3480156102775760",
"0080fd5b506101656106f8565b34801561028c57600080fd5b5061",
"02a061029b36600461186d565b610785565b604051901515815260",
"2001610133565b3480156102bc57600080fd5b5060005473ffffff",
"ffffffffffffffffffffffffffffffffff16610246565b34801561",
"02e757600080fd5b506101656102f6366004611915565b61093156",
"5b34801561030757600080fd5b506102a061031636600461175f56",
"5b60476020526000908152604090205460ff1681565b3480156103",
"3757600080fd5b50610340600081565b60405163ffffffff909116",
"8152602001610133565b34801561036157600080fd5b5061016561",
"03703660046119d2565b610e4d565b73ffffffffffffffffffffff",
"ffffffffffffffffff841661042957823414610424576040517f08",
"c379a0000000000000000000000000000000000000000000000000",
"00000000815260206004820152603060248201527f427269646765",
"3a3a6465706f7369743a20414d4f554e545f444f45535f4e4f5460",
"448201527f5f4d415443485f4d53475f56414c5545000000000000",
"0000000000000000000060648201526084015b60405180910390fd",
"5b61044b565b61044b73ffffffffffffffffffffffffffffffffff",
"ffffff8516333086610f7a565b63ffffffff82166104de57604051",
"7f08c379a000000000000000000000000000000000000000000000",
"000000000000815260206004820152602c60248201527f42726964",
"67653a3a6465706f7369743a2044455354494e4154494f4e5f4341",
"4e60448201527f545f42455f4d41494e4e45540000000000000000",
"000000000000000000000000606482015260840161041b565b6041",
"546040805173ffffffffffffffffffffffffffffffffffffffff87",
"811682526020820187905263ffffffff8681168385015290851660",
"608301529092166080830152517f0a37f8bae6de7e960aeedce458",
"75d5a75681918316c4bd81f4691152910f8e329181900360a00190",
"a161055b848460008585611056565b610563610574565b60435561",
"056e61125e565b50505050565b6041546000908190815b60208110",
"156106395781600116600114156105d957600181602081106105a6",
"576105a66119ef565b015460408051602081019290925281018490",
"526060016040516020818303038152906040528051906020012092",
"5061061a565b82602182602081106105ed576105ed6119ef565b01",
"546040805160208101939093528201526060016040516020818303",
"038152906040528051906020012092505b610625600283611a4d56",
"5b91508061063181611a88565b91505061057e565b509092915050",
"565b60465473ffffffffffffffffffffffffffffffffffffffff16",
"33146106e8576040517f08c379a000000000000000000000000000",
"000000000000000000000000000000815260206004820152602960",
"248201527f4272696467653a3a757064617465526f6c6c75704578",
"6974526f6f743a204f4e60448201527f4c595f524f4c4c55500000",
"000000000000000000000000000000000000000000606482015260",
"840161041b565b60428190556106f561125e565b50565b60005473",
"ffffffffffffffffffffffffffffffffffffffff16331461077957",
"6040517f08c379a000000000000000000000000000000000000000",
"000000000000000000815260206004820181905260248201527f4f",
"776e61626c653a2063616c6c6572206973206e6f7420746865206f",
"776e6572604482015260640161041b565b610783600061130b565b",
"565b6040517fffffffff0000000000000000000000000000000000",
"000000000000000000000060e088811b821660208401527fffffff",
"ffffffffffffffffffffffffffffffffff00000000000000000000",
"000060608c811b82166024860152603885018c90529189901b9092",
"16605884015286901b16605c820152600090819060700160408051",
"7fffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ffffffffffe08184030181529190528051602090910120905067ff",
"ffffffffffffff841660005b602081101561092057816001166001",
"14156108b357868181518110610873576108736119ef565b602002",
"602001015183604051602001610896929190918252602082015260",
"400190565b60405160208183030381529060405280519060200120",
"9250610901565b828782815181106108c6576108c66119ef565b60",
"200260200101516040516020016108e89291909182526020820152",
"60400190565b604051602081830303815290604052805190602001",
"2092505b61090c600283611a4d565b91508061091881611a88565b",
"91505061084c565b505090911498975050505050505050565b67ff",
"ffffffffffffff841660009081526047602052604090205460ff16",
"156109db576040517f08c379a00000000000000000000000000000",
"0000000000000000000000000000815260206004820152602a6024",
"8201527f4272696467653a3a77697468647261773a20414c524541",
"44595f434c41494d4560448201527f445f57495448445241570000",
"000000000000000000000000000000000000000060648201526084",
"0161041b565b63ffffffff871615610a6f576040517f08c379a000",
"000000000000000000000000000000000000000000000000000000",
"815260206004820152603160248201527f4272696467653a3a7769",
"7468647261773a2044455354494e4154494f4e5f4e456044820152",
"7f54574f524b5f4e4f545f4d41494e4e4554000000000000000000",
"000000000000606482015260840161041b565b63ffffffff881615",
"610b03576040517f08c379a0000000000000000000000000000000",
"00000000000000000000000000815260206004820152602c602482",
"01527f4272696467653a3a77697468647261773a204f524947494e",
"5f4e4554574f524b60448201527f5f4e4f545f4d41494e4e455400",
"000000000000000000000000000000000000006064820152608401",
"61041b565b60008381526044602090815260409182902054825191",
"820185905291810183905260600160405160208183030381529060",
"40528051906020012014610bca576040517f08c379a00000000000",
"000000000000000000000000000000000000000000000081526020",
"6004820152603160248201527f4272696467653a3a776974686472",
"61773a20474c4f42414c5f455849545f524f60448201527f4f545f",
"444f45535f4e4f545f4d4154434800000000000000000000000000",
"0000606482015260840161041b565b610bda8a8a8a8a8a8a8a8861",
"0785565b610c40576040517f08c379a00000000000000000000000",
"000000000000000000000000000000000081526020600482015260",
"1d60248201527f4272696467653a3a77697468647261773a20534d",
"545f494e56414c4944000000604482015260640161041b565b67ff",
"ffffffffffffff8416600090815260476020526040902080547fff",
"ffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ffffff0016600117905573ffffffffffffffffffffffffffffffff",
"ffffffff8a16610dad576040805160008082526020820190925273",
"ffffffffffffffffffffffffffffffffffffffff8816908b906040",
"51610cd49190611aed565b60006040518083038185875af1925050",
"503d8060008114610d11576040519150601f19603f3d0116820160",
"40523d82523d6000602084013e610d16565b606091505b50509050",
"80610da7576040517f08c379a00000000000000000000000000000",
"000000000000000000000000000081526020600482015260256024",
"8201527f4272696467653a3a77697468647261773a204554485f54",
"52414e534645525f4660448201527f41494c454400000000000000",
"000000000000000000000000000000000000000060648201526084",
"0161041b565b50610dce565b610dce73ffffffffffffffffffffff",
"ffffffffffffffffff8b16878b611380565b6040805167ffffffff",
"ffffffff8616815263ffffffff8a16602082015273ffffffffffff",
"ffffffffffffffffffffffffffff8c811682840152606082018c90",
"528816608082015290517f8932892d010aea7e4fdefb3764910523",
"c321e06bb52577dc2439501196bf72559181900360a00190a15050",
"5050505050505050565b60005473ffffffffffffffffffffffffff",
"ffffffffffffff163314610ece576040517f08c379a00000000000",
"000000000000000000000000000000000000000000000081526020",
"6004820181905260248201527f4f776e61626c653a2063616c6c65",
"72206973206e6f7420746865206f776e6572604482015260640161",
"041b565b73ffffffffffffffffffffffffffffffffffffffff8116",
"610f71576040517f08c379a0000000000000000000000000000000",
"000000000000000000000000008152602060048201526026602482",
"01527f4f776e61626c653a206e6577206f776e6572206973207468",
"65207a65726f206160448201527f64647265737300000000000000",
"000000000000000000000000000000000000006064820152608401",
"61041b565b6106f58161130b565b60405173ffffffffffffffffff",
"ffffffffffffffffffffff80851660248301528316604482015260",
"64810182905261056e9085907f23b872dd00000000000000000000",
"000000000000000000000000000000000000906084015b60408051",
"7fffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ffffffffffe08184030181529190526020810180517bffffffffff",
"ffffffffffffffffffffffffffffffffffffffffffffff167fffff",
"ffff00000000000000000000000000000000000000000000000000",
"000000909316929092179091526113db565b6040517fffffffff00",
"000000000000000000000000000000000000000000000000000000",
"60e085811b821660208401527fffffffffffffffffffffffffffff",
"ffffffffffff000000000000000000000000606089811b82166024",
"860152603885018990529186901b909216605884015283901b1660",
"5c8201526000906070016040516020818303038152906040528051",
"906020012090506001602060026110fd9190611c2b565b61110791",
"90611c37565b60415410611197576040517f08c379a00000000000",
"000000000000000000000000000000000000000000000081526020",
"6004820152602a60248201527f4465706f736974436f6e74726163",
"743a5f6465706f7369743a204d45524b4c4560448201527f5f5452",
"45455f46554c4c0000000000000000000000000000000000000000",
"0000606482015260840161041b565b6001604160008282546111aa",
"9190611c4e565b909155505060415460005b602081101561124b57",
"81600116600114156111e95782600182602081106111de576111de",
"6119ef565b015550611257915050565b600181602081106111fc57",
"6111fc6119ef565b01546040805160208101929092528101849052",
"606001604051602081830303815290604052805190602001209250",
"6002826112379190611a4d565b91508061124381611a88565b9150",
"506111b5565b50611254611c66565b50505b5050505050565b6045",
"805490600061126e83611a88565b90915550506043546042546040",
"80516020810193909352820152606001604080518083037fffffff",
"ffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ffe001815282825280516020918201206045546000908152604483",
"5283902055604354604254908452908301527f61014378f82a0d80",
"9aefaf87a8ac9505b89c321808287a6e7810f29304c1fce3910160",
"405180910390a1565b6000805473ffffffffffffffffffffffffff",
"ffffffffffffff8381167fffffffffffffffffffffffff00000000",
"000000000000000000000000000000008316811784556040519190",
"92169283917f8be0079c531659141344cd1fd0a4f28419497f9722",
"a3daafe3b4186f6b6457e09190a35050565b60405173ffffffffff",
"ffffffffffffffffffffffffffffff831660248201526044810182",
"90526113d69084907fa9059cbb0000000000000000000000000000",
"000000000000000000000000000090606401610fd4565b50505056",
"5b600061143d826040518060400160405280602081526020017f53",
"61666545524332303a206c6f772d6c6576656c2063616c6c206661",
"696c65648152508573ffffffffffffffffffffffffffffffffffff",
"ffff166114e79092919063ffffffff16565b8051909150156113d6",
"578080602001905181019061145b9190611c95565b6113d6576040",
"517f08c379a0000000000000000000000000000000000000000000",
"00000000000000815260206004820152602a60248201527f536166",
"6545524332303a204552433230206f7065726174696f6e20646964",
"206e60448201527f6f742073756363656564000000000000000000",
"00000000000000000000000000606482015260840161041b565b60",
"606114f68484600085611500565b90505b9392505050565b606082",
"471015611592576040517f08c379a0000000000000000000000000",
"000000000000000000000000000000008152602060048201526026",
"60248201527f416464726573733a20696e73756666696369656e74",
"2062616c616e636520666f60448201527f722063616c6c00000000",
"000000000000000000000000000000000000000000006064820152",
"60840161041b565b843b6115fa576040517f08c379a00000000000",
"000000000000000000000000000000000000000000000081526020",
"6004820152601d60248201527f416464726573733a2063616c6c20",
"746f206e6f6e2d636f6e7472616374000000604482015260640161",
"041b565b6000808673ffffffffffffffffffffffffffffffffffff",
"ffff1685876040516116239190611aed565b600060405180830381",
"85875af1925050503d8060008114611660576040519150601f1960",
"3f3d011682016040523d82523d6000602084013e611665565b6060",
"91505b5091509150611675828286611680565b9796505050505050",
"50565b6060831561168f5750816114f9565b82511561169f578251",
"8084602001fd5b816040517f08c379a00000000000000000000000",
"0000000000000000000000000000000000815260040161041b9190",
"611cb7565b73ffffffffffffffffffffffffffffffffffffffff81",
"1681146106f557600080fd5b803563ffffffff8116811461170957",
"600080fd5b919050565b6000806000806080858703121561172457",
"600080fd5b843561172f816116d3565b9350602085013592506117",
"44604086016116f5565b91506060850135611754816116d3565b93",
"9692955090935050565b60006020828403121561177157600080fd",
"5b5035919050565b7f4e487b710000000000000000000000000000",
"0000000000000000000000000000600052604160045260246000fd",
"5b600082601f8301126117b857600080fd5b8135602067ffffffff",
"ffffffff808311156117d5576117d5611778565b8260051b604051",
"7fffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ffffffffffe0603f83011681018181108482111715611818576118",
"18611778565b604052938452858101830193838101925087851115",
"61183657600080fd5b83870191505b848210156116755781358352",
"918301919083019061183c565b803567ffffffffffffffff811681",
"1461170957600080fd5b600080600080600080600080610100898b",
"03121561188a57600080fd5b8835611895816116d3565b97506020",
"89013596506118aa60408a016116f5565b95506118b860608a0161",
"16f5565b945060808901356118c8816116d3565b935060a0890135",
"67ffffffffffffffff8111156118e457600080fd5b6118f08b828c",
"016117a7565b9350506118ff60c08a01611855565b915060e08901",
"3590509295985092959890939650565b6000806000806000806000",
"806000806101408b8d03121561193557600080fd5b8a3561194081",
"6116d3565b995060208b0135985061195560408c016116f5565b97",
"5061196360608c016116f5565b965060808b0135611973816116d3",
"565b955060a08b013567ffffffffffffffff81111561198f576000",
"80fd5b61199b8d828e016117a7565b9550506119aa60c08c016118",
"55565b935060e08b013592506101008b013591506101208b013590",
"509295989b9194979a5092959850565b6000602082840312156119",
"e457600080fd5b81356114f9816116d3565b7f4e487b7100000000",
"000000000000000000000000000000000000000000000000600052",
"603260045260246000fd5b7f4e487b710000000000000000000000",
"000000000000000000000000000000000060005260116004526024",
"6000fd5b600082611a83577f4e487b710000000000000000000000",
"000000000000000000000000000000000060005260126004526024",
"6000fd5b500490565b60007fffffffffffffffffffffffffffffff",
"ffffffffffffffffffffffffffffffffff821415611aba57611aba",
"611a1e565b5060010190565b60005b83811015611adc5781810151",
"83820152602001611ac4565b8381111561056e5750506000910152",
"565b60008251611aff818460208701611ac1565b91909101929150",
"50565b600181815b80851115611b6257817fffffffffffffffffff",
"ffffffffffffffffffffffffffffffffffffffffffffff04821115",
"611b4857611b48611a1e565b80851615611b5557918102915b9384",
"1c9390800290611b0e565b509250929050565b600082611b795750",
"6001611c25565b81611b8657506000611c25565b8160018114611b",
"9c5760028114611ba657611bc2565b6001915050611c25565b60ff",
"841115611bb757611bb7611a1e565b50506001821b611c25565b50",
"60208310610133831016604e8410600b8410161715611be5575081",
"810a611c25565b611bef8383611b09565b807fffffffffffffffff",
"ffffffffffffffffffffffffffffffffffffffffffffffff048211",
"15611c2157611c21611a1e565b0290505b92915050565b60006114",
"f98383611b6a565b600082821015611c4957611c49611a1e565b50",
"0390565b60008219821115611c6157611c61611a1e565b50019056",
"5b7f4e487b71000000000000000000000000000000000000000000",
"00000000000000600052600160045260246000fd5b600060208284",
"031215611ca757600080fd5b815180151581146114f957600080fd",
"5b6020815260008251806020840152611cd6816040850160208701",
"611ac1565b601f017fffffffffffffffffffffffffffffffffffff",
"ffffffffffffffffffffffffffe016919091016040019291505056",
"fea264697066735822122086e50005a89e4c2272c1cba11ff802c1",
"213e2b08c8a20c742826166345c5ee3264736f6c63430008090033",
},
expectedHash: "13a3be23e0f86283cb0ba23739fb1020aa7c85ae498f8d543e37cadcd5ec408b",
},
}
for i, vector := range testVectors {
t.Run(fmt.Sprintf("test vector %d", i), func(t *testing.T) {
hasher, err := New(16)
require.NoError(t, err)
for _, chunk := range vector.bytesChunks {
inputBytes, err := hex.DecodeString(chunk)
require.NoError(t, err)
hasher.Write(inputBytes)
}
res := hasher.Sum(nil)
require.Equal(t, vector.expectedHash, hex.EncodeToString(res))
})
}
}

Loading…
Cancel
Save