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

feat: preferable to have chacha take in bytes instead of bits. #58

Closed
0xJepsen opened this issue Nov 22, 2024 · 1 comment · Fixed by #74
Closed

feat: preferable to have chacha take in bytes instead of bits. #58

0xJepsen opened this issue Nov 22, 2024 · 1 comment · Fixed by #74
Labels
feature ✨ New feature or core functionality priority low ☘️

Comments

@0xJepsen
Copy link
Contributor

0xJepsen commented Nov 22, 2024

          I think it would be preferable to have this take in bytes instead of bits. But maybe its not a big deal at all.

Originally posted by @Autoparallel in #51 (comment)

Like mentioned it might not be a big deal. I think when we generate witvalues for this i would be happy to be involved. If it seems difficult then we can work on this.

@0xJepsen
Copy link
Contributor Author

Added a helper for this here

// // from little endian to 32 bit words
// // example:
// 0, 1, 0, 1, 0, 0, 0, 0, => 80
// 0, 1, 0, 1, 0, 1, 0, 0, => 84
// 0, 1, 0, 1, 0, 1, 0, 0, => 84
// 0, 1, 0, 0, 1, 0, 0, 0, => 72
// shoud be encoded as
// 72, 84, 84, 80
template fromLittleEndianToWords32() {
signal input data[32];
signal output words[4];
component Bits2Num[4];
for(var i = 3; i >= 0; i--) {
Bits2Num[i] = Bits2Num(8);
for(var j = 7; j >= 0; j--) {
Bits2Num[i].in[7-j] <== data[i*8 + j];
}
words[3-i] <== Bits2Num[i].out;
}
}
template fromWords32ToLittleEndian() {
signal input words[4];
signal output data[32];
component Num2Bits[4];
for(var i = 3; i >= 0; i--) {
Num2Bits[i] = Num2Bits(8);
Num2Bits[i].in <== words[3-i];
for(var j = 7; j >= 0; j--) {
data[i*8 + j] <== Num2Bits[i].out[7-j];
}
}
}

and tests here

describe("fromLittleEndianToWords32", () => {
let circuit: WitnessTester<["data"], ["words"]>;
it("fromLittleEndianToWords32", async () => {
circuit = await circomkit.WitnessTester(`fromLittleEndianToWords32`, {
file: "utils/array",
template: "fromLittleEndianToWords32",
});
console.log("#constraints:", await circuit.getConstraintCount());
let input = [
0, 1, 0, 1, 0, 0, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0
];
await circuit.expectPass({data: input}, {words: [72, 84, 84, 80]})
});
});
describe("fromWords32ToLittleEndian", () => {
let circuit: WitnessTester<["words"], ["data"]>;
it("fromWords32ToLittleEndian", async () => {
circuit = await circomkit.WitnessTester(`fromWords32ToLittleEndian`, {
file: "utils/array",
template: "fromWords32ToLittleEndian",
});
console.log("#constraints:", await circuit.getConstraintCount());
let input = [72, 84, 84, 80];
await circuit.expectPass({words: input}, {data: [
0, 1, 0, 1, 0, 0, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0
]})
});
});

However i was experiencing an problem when making the switch. Probably not too much more work on top of this. The main complexity is that the 32 bit words are little endian encoded mean the byte order is reversed before changing to bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ✨ New feature or core functionality priority low ☘️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant